// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//Borrowed from storyevertelling.com - DRB 9/7
//either toggles display property from current state (to 'onState' or none) or if 'force' present, sets display to the 'force' value.
//showHideD(things [,force [,onState]])
//things is a string which may contain one or many items each separated with a comma.
//DRB 28.8.7 - added support for multiple 'things' including 'thing' validity checking
//DRB 1.9.7 - added some ideas posted by Mike "mwinter" @ Dynamic Drive Forums - 12-29-2005 (http://www.dynamicdrive.com/forums/showthread.php?t=6846)

function showHideD(things,force,onState) {
    if (document.getElementById) {
        var tehThing;
        if (!onState) { onState = ''; } //by setting onState to '' rather than 'inherit' we let it default to that set by CSS (if any)
        var tehThings = things.split(",");

        for (var thing in tehThings) {
            var thisThing = tehThings[thing].toString();
            if ((tehThing = document.getElementById(thisThing)) && tehThing.style) {
                if (!force || force=='') {
                    var state = tehThing.style.display;

                    if (state=='none') {
                        tehThing.style.display = onState;
                    } else {
                        tehThing.style.display = 'none';
                    }
                } else {
                    tehThing.style.display = force;
                }
            } else { break; } //exit loop if invalid (non-existant) item exists in 'things'
        }
    }
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//toggles 'display' property of an object and modifies 'className' of the calling object
function showhide2(thing,lnk) {
    var tehthing = document.getElementById(thing);
    var tehlnk = document.getElementById(lnk);
    var state = tehthing.style.display;

    if (state=='none') { //Wow!  This is so obviously better than i=0,i++,i-- DRB 6.5.6
        tehlnk.className = 'less';
    } else {
        tehlnk.className = 'more';
    }
    showHideD(thing);
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//toggle enable/disable the 'last' style sheet (should be the one in the head of the calling document)
//optionally set off=true to force state to disabled
function swCSS(off) {
    // http://www.howtocreate.co.uk/tutorials/javascript/domstylesheets
    if (document.styleSheets) {
        var myCSS = document.styleSheets.length-1;

        if (off) {
            document.styleSheets[myCSS].disabled = true;

        } else {
            if (document.styleSheets[myCSS].disabled==false) {
                document.styleSheets[myCSS].disabled = true;
            } else {
                document.styleSheets[myCSS].disabled = false;
            }
        }
    }
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


function chngtxt(here,txta,txtb,txtc) {
    if (!txtc) {txtc="";}
    var content = document.getElementById(here);

    if (content.innerHTML==(txta+txtc)) {
        content.innerHTML = txtb + txtc;
    } else {
        content.innerHTML = txta + txtc;
    }
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//either toggles 'display' property automatically based on current state (and 'ON' state settings) or sets 'display' to the 'force' value.
function showhide(thing,force,onState) {

    if (!onState) { var onState = "inherit"; }
    var tehthing = document.getElementById(thing);

    if (!force || force=='') {
        var state = tehthing.style.display;

        if (state=="none") {
            //IE bug fix - tehthing.style.display = onState;
            tehthing.style.cssText = "display:"+onState+";";
        } else {
            tehthing.style.cssText = "display:none;";
        }
    } else {
        tehthing.style.cssText = "display:"+force+";";
    }
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//  Ajax - http://www-128.ibm.com/developerworks/java/library/wa-ajaxintro1.html

/* Create a new XMLHttpRequest object to talk to the Web server */
var ajax = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
    ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
        ajax = false;
    }
}
@end @*/

if (!ajax && typeof XMLHttpRequest != 'undefined') {
    ajax = new XMLHttpRequest();
}

    //--------------------------------------------------////--------------------------------------------------//

/* doajax("someurl" [, 'sometarget'] [, setstatus]);
 - set sometarget to the ID of the HTML object you would like results put into (innerHTML)
 - setstatus=true will show "Loading...[25%]" status box during ajax.readyState's 1-3
 - doajax2 will always return a string named 'tehString' containing the results (see following note)
    - The catch with using 'tehString' is that you need to know when it exists; with ajax the JS that called this function will carry on while this function works away in the background. Below is an example of how you can check to see if 'tehString' is available:

        var url = "foobar.php";
        doajax(url);
            wait4ajax = setInterval('ajaxDone()',100);
        }

        function ajaxDone() {
            if (tehString) {
                clearInterval(wait4ajax);
                alert("All done!  Here is tehString: " + tehString);
            }
        }
*/

function doajax(url,settarget,setstatus) {

    tehtarget = settarget; // JS doesn't have "Global" like PHP
    statuson = setstatus;

    ajax.open("GET", url, true);
    ajax.onreadystatechange = doajax2; //not sure why but I can't do doajax2(settarget) which would remove need for tehtarget
//     ajax.onreadystatechange = ajaxTesting; //TESTING
    ajax.send(null);

}


function doajax2() {

    if (statuson && tehtarget) {
        switch (ajax.readyState) {
            case 1:
                document.getElementById(tehtarget).innerHTML = "<div id='ajaxstatus'>Loading...[25%]</div>";
                break;
            case 2:
                document.getElementById(tehtarget).innerHTML = "<div id='ajaxstatus'>Loading...[50%]</div>";
                break;
            case 3:
                document.getElementById(tehtarget).innerHTML = "<div id='ajaxstatus'>Loading...[75%]</div>";
                break;
        }
    }

    if (ajax.readyState == 4) {
        if (ajax.status == 200) {
            /*return results as string (perhaps to be parsed into array)*/
            tehString = ajax.responseText;
            /*return results as string using innerHTML to put into 'tehtarget'*/
            if (tehtarget) { document.getElementById(tehtarget).innerHTML = ajax.responseText; }
        } else {
            alert("Please contact the IT Department.  Status is " + ajax.status);
        }
    }

    //alert("ready state is " + ajax.readyState);

}

    //--------------------------------------------------////--------------------------------------------------//

//for testing
function ajaxTesting() {
    // Output the current ready state
    alert("ajaxTesting() called with ready state of " + ajax.readyState +
           " and a response text of '" + ajax.responseText + "'");
}

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //

// cookie code from http://www.quirksmode.org/js/cookies.html

// cookies created in PHP; this is for deleteing cookies
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function eraseCookie(name) {
    createCookie(name,"",-1);
}

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //
