/* InfoNet - Daniel Baer - daniel@danielbaer.com */

var k=0, m=0;

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //

/*
Open a window, print it, then close it
http://forum.weborum.com/index.php?showtopic=2869

index.html:
    <a href="#" onclick="window.open('yourpage.html','printMe');return false;">Click me!</a>
yourpage.html:
    <body onload="javascript:window.print();window.close();">
*/

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//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;
            }
        }
    }
}


// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = //


//Borrowed from storyevertelling - 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'||state=='') {
                        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 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
        tehthing.style.display = 'inherit';
        tehlnk.className = 'less';
    } else {
        tehthing.style.display = 'none';
        tehlnk.className = 'more';
    }
}

//======================================================//
//DRB 6/8 added var lvl

function tree(branch,leaf,lvl,mod) {
    var treebranch = document.getElementById(branch);
    var treeleaf = document.getElementById(leaf);
    var state = treebranch.style.display;

    if (!mod) { var mod = ""; } else { var mod = "_" + mod; }
    if (!lvl) { var lvl = ""; } else { var lvl = "_lvl" + lvl; }

    if (state=='none') {
        treebranch.style.display = 'inherit';
        treeleaf.className = 'tree' + lvl + '_less' + mod;
    } else {
        treebranch.style.display = 'none';
        treeleaf.className = 'tree' + lvl + '_more' + mod;
    }
}

//======================================================//

//switches 'display' property value of two objects (A=off,B=on becomes A=on,B=off or visa versa)
function showhide3(thing1,thing2) {
    var tehthing1 = document.getElementById(thing1);
    var tehthing2 = document.getElementById(thing2);
    var state = tehthing1.style.display;

    if (state=='none') {
        tehthing1.style.display = 'inherit';
        tehthing2.style.display = 'none';
    } else {
        tehthing1.style.display = 'none';
        tehthing2.style.display = 'inherit';
    }
}

//======================================================//

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;
    }
}

//======================================================//
//grabbed this from SitePoint.com code

function toppage() {
    self.scrollTo(0,0)
}

function cancelbubble(evt) {
  evt = (evt) ? evt : ((event) ? event : null);
  if (evt) {evt.cancelBubble = true;}
}

//======================================================//
//basic img roll

function imgroll(imgnum,newimg) {
        document.getElementById(imgnum).src = newimg;
}

//======================================================//
//img roll w/ auto toggle (used with index.php - not as of 20.2.7)

function imgroll(imgname) {
        i01="images/" + imgname + ".png";
        i02="images/" + imgname + "_off.png";

  if (k==0) {
                document.getElementById(imgname).src = i01;
                k++;
        } else {
                document.getElementById(imgname).src = i02;
            k--;
        }
}

//======================================================//
//  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);
}
//======================================================//
