/* ************************************************************************
    AJAXMasterFile.js

    REQUIRED FILES
        URLDecode.js
        trappingBadChars.js

************************************************************************ */
//<!--
var httpObject = null;
var xmlDoc = null;
var respText = "";
var jsDebug = false;
var errMsg = new Array("There is an error processing the XML.", "The data exceeds the maximum length for the AJAX service.");

//little feature to turn view jsDebug on/off ("z" turns jsDebug on. All others, off.
document.onkeypress = function(){
    if(window.event) // IE
      {
      jsDebug = (event.keyCode == 122);
      }
    else if(e.which) // Netscape/Firefox/Opera
      {
      jsDebug = (e.which == 122);
      }
}

/* **************************************************************************
        PRIMARY FUNCTIONS
****************************************************************************/
//Get the HTTP Object
function getHTTPObject(){ 
    var output = null;
    try{
        // Firefox, Opera 8.0+, Safari
        output = new XMLHttpRequest();
    }catch(e){
        // Internet Explorer
        try{
            output = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            output = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return output;
}

/**
*  getAJAXContentV2
*
*   @param object objHTTP This is the XMLHttpRequest object
*   @param string URL the full path to the PHP file that builds the HTML to be displayed
*   @param string divID The id of the div that receives the HTML
*
* */ 
function getAJAXContentV2(objHTTP, url, divID){ 
    //if(url.length > 6000){AJAXerror(1); return false;}
    if (objHTTP != null){
        if(jsDebug){alert(url);}
        objHTTP.open("POST", url, true);
        objHTTP.send(null);
        objHTTP.onreadystatechange = function(){
            if(objHTTP.readyState == 4){
                document.getElementById(divID).innerHTML = objHTTP.responseText;
            }
        }
    }
}

/**
    getAJAXContent_XML()
    
    @param object objHTTP the XMLHttpRequest object
    @param str url full path name to the PHP file
    
    This function makes the call to the PHP file, and retrieves the 
        sends the returning stream to loadXML so that it is parsed as
        XML data. It then runs setElementValues, and setInnerHTML.
        
    REQUIRES
        loadXML()
        setElementValues()
        setInnerHTML()
*       a form item that uses ".value" on the page calling this function
* */
function getAJAXContent_XML(objHTTP, url, elementName, xmlTagName_FormID, xmlTagName_FormVal, type){
    //if(url.length > 6000){AJAXerror(1); return false;}
    if (objHTTP != null){
        if(jsDebug){alert(url);}
        objHTTP.open("POST", url, true);
        objHTTP.send(null);
        objHTTP.onreadystatechange = function(){
            if(objHTTP.readyState == 4){ 
                if(loadXML(objHTTP.responseText)){ 
                    switch(type){
                        case "html":
                            setElementInnerHTML(elementName, xmlTagName_FormID, xmlTagName_FormVal);
                            break;
                        case "value":
                            setElementValues(elementName, xmlTagName_FormID, xmlTagName_FormVal);
                            break;
                        default:
                            setElementMixed(elementName, xmlTagName_FormID, xmlTagName_FormVal);
                            break;
                   }
                }else{
                    AJAXerror(0);
                }
            }
        }
    }
}

/**
*  getAJAXContent_Set
*
*   @param object objHTTP This is the XMLHttpRequest object
*   @param string URL the full path to the PHP file that builds the HTML to be displayed
*
*   DESCRIPTION
*       This is a one-way transaction. It needs to send information to the AJAX file, but
            does not require any returning HTML/XML. This is used in instances where a session needs to be changed, 
            or there is any action where nothing needs to be returned.
    
* */ 
function getAJAXContent_OneWay(objHTTP, url){ 
    //if(url.length > 6000){AJAXerror(1); return false;}
    var result = "";
    if (objHTTP != null){
        if(jsDebug){alert(url);}
        objHTTP.open("POST", url, true);
        objHTTP.send(null);
        objHTTP.onreadystatechange = function(){
            if(objHTTP.readyState == 4){
                result = objHTTP.responseText;
                if(result != 0){ AJAXerror(0); }
            }
        }
    }
}

/* **************************************************************************
        SUPPORTING FUNCTIONS
****************************************************************************/
/**
    loadXML()
    
    @param str xmlData fully formated XML stream retrieved from the PHP script
    
* */
function loadXML(xmlData) {
    if(jsDebug){alert(xmlData);}
    if(xmlData.length <= 0){return false;}
    respText = xmlData;//setting respText allows us to ALERT it if we pass in a formID named "output"
    try {                                               //Internet Explorer
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlData);
        return true;
    }catch(e){                                          //Firefox et. all
        try {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(xmlData, "text/xml");
            return true;
        }catch(e){
            try {
                xmlDoc = document.implementation.createDocument("", "", null);
                xmlDoc.load(xmlData);
                return true;
            }catch(e){
                alert("The browser is not XML compatible.")
            }
        }
    }
    return false;
}

/**
    setElementInnerHTML()
    
    This function set the document.elements that are listed in the 
        elementValues node of the XML file returned from the PHP file.
        
    REQUIRES
        xmlDoc to be initialized outside of the function.
        the elements listed in the XML file must exist on the page.
        URLDecode.js - for decoding the HTML sent. 
* */
function setElementInnerHTML(elementName, xmlTagName_FormID, xmlTagName_FormVal){
    var oRV = xmlDoc.getElementsByTagName(elementName);
    if(oRV.length > 0){
        var oV = oRV[0].childNodes;
        var formID = "";
        var value = "";
        for (i = 0; i < oV.length; i++){
            oFormID = oV[i].getElementsByTagName(xmlTagName_FormID)[0];
            oFormVal = oV[i].getElementsByTagName(xmlTagName_FormVal)[0];
            formID = URLDecode(oFormID.firstChild.nodeValue);
            formVal = URLDecode(oFormVal.firstChild.nodeValue);
            if(document.getElementById(formID)){ document.getElementById(formID).innerHTML = formVal; }
            else if(formID == "output"){ alert(respText); }
        }
    }else{
        AJAXerror(0);
    }
}
/**
    setElementValues()
    
    This function set the document.elements that are listed in the 
        elementValues node of the XML file returned from the PHP file.
        
    REQUIRES
        xmlDoc to be initialized outside of the function.
        the elements listed in the XML file must exist on the page.
        URLDecode.js - for decoding the HTML sent. 
* */
function setElementValues(elementName, xmlTagName_FormID, xmlTagName_FormVal){
    var oRV = xmlDoc.getElementsByTagName(elementName);
    if(oRV.length > 0){
        var oV = oRV[0].childNodes;
        var formID = "";
        var value = "";
        for (i = 0; i < oV.length; i++){
            oFormID = oV[i].getElementsByTagName(xmlTagName_FormID)[0];
            oFormVal = oV[i].getElementsByTagName(xmlTagName_FormVal)[0];
            formID = URLDecode(oFormID.firstChild.nodeValue);
            formVal = URLDecode(oFormVal.firstChild.nodeValue);
            if(document.getElementById(formID)){ document.getElementById(formID).value = formVal; }
            else if(formID == "output"){ alert(respText); }
        }
    }else{
        AJAXerror(0);
    }
}
/**
    setElementValues()
    
    This function set the document.elements that are listed in the 
        elementValues node of the XML file returned from the PHP file.
        
    REQUIRES
        xmlDoc to be initialized outside of the function.
        the elements listed in the XML file must exist on the page.
        URLDecode.js - for decoding the HTML sent. 
* */
function setElementMixed(elementName, xmlTagName_FormID, xmlTagName_FormVal){
    var oRV = xmlDoc.getElementsByTagName(elementName);
    if(oRV.length > 0){
        var oV = oRV[0].childNodes;
        var formID = "";
        var value = "";
        for (i = 0; i < oV.length; i++){
            oFormID = oV[i].getElementsByTagName(xmlTagName_FormID)[0];
            oFormVal = oV[i].getElementsByTagName(xmlTagName_FormVal)[0];
            oFormType = oV[i].getElementsByTagName("formType")[0];
            formID = URLDecode(oFormID.firstChild.nodeValue);
            formVal = URLDecode(oFormVal.firstChild.nodeValue);
            formType = URLDecode(oFormType.firstChild.nodeValue);
            //alert("fid = " + formID + ", fv = " + formVal + ", ft = " + formType);
            if(document.getElementById(formID)){ 
                if(formType == "value"){
                    document.getElementById(formID).value = formVal; 
                }else{
                    document.getElementById(formID).innerHTML = formVal;
                }
            }else if(formID == "output"){ 
                alert(respText); 
            }
        }
    }else{
        AJAXerror(0);
    }
}
/** 
 * Error with XML Transfer
 *
 *  This is specific to the accounting system 
 *
 * */
function AJAXerror(errNum){
    document.getElementById("tabArea1").innerHTML = "<div class='highlight_red' style='width: 100%; text-align: center;'>" + errMsg[errNum] + "</div>";
    document.getElementById("tabArea2").innerHTML = "&nbsp;"; 
    document.getElementById("tabArea3").innerHTML = "&nbsp;"; 
}
/**********************************************************************************************************************
**********************************************************************************************************************
************************************************  DEPRICATED    ******************************************************
**********************************************************************************************************************
***********************************************************************************************************************/
/**
    setInnerHTML()

    REQUIRES
        xmlDoc to be initialized outside of the function.
        the elements listed in the XML file must exist on the page.
        URLDecode.js - for decoding the HTML sent. 
        
        DEPRICATED
* */
function setInnerHTML(htmlContentName){
    alert("DEPRICATED");
    var oHC = xmlDoc.getElementsByTagName(htmlContentName);
    if(oRV.length > 0){
        var divID = "";
        var htmlEncoded = "";
        
        divID = oHC[0].childNodes[0].text;
        htmlEncoded = oHC[0].childNodes[1].text;
        if(document.getElementById(divID)){ 
            document.getElementById(divID).innerHTML = URLDecode(htmlEncoded); 
        }
    }else{
        AJAXerror(0);
    }
 }
 
/**
    Suspend Next Function 
    
    @param string divID The dive that is going to be replaced
    @param string command This is the javascript you want to run once the previous script is done.
    
    DEPRICATED
* */
function suspendNextFunction(divID, command){
    alert("DEPRICATED");
    if(document.getElementById(divID).value = "done"){
        return eval(command);
    }else{
        var func = "suspendNextFunction(" + divID + ", " + command + ");"
        setTimeout(func, 1000);
    }
}


/**

*  getAJAXContent
*
*   @depricated 2/6/2009
*
* */ 
function getAJAXContent(url, divID){
    alert("DEPRICATED");
    httpObject = getHTTPObject();
    getAJAXContentV2(httpObject, url, divID);
}
//-->