﻿// JScript File

function getInnerXmlText(xmlNode)
{
    var text = null;
    if (!xmlNode)
    {
        text = null;
    }
    else if ( xmlNode.textContent )
    {
        text = xmlNode.textContent;
    }
    else
    {
        text = xmlNode.text;
    }
    return text;
}

function createXMLHttp() {
    if (typeof XMLHttpRequest != "undefined" ) {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        var aVersions = [ 
            "MSXML2.XMLHttp.5.0", 
            "MSXML2.XMLHttp.4.0",
            "MSXML2.XMLHttp.3.0",
            "MSXML2.XMLHttp", 
            "Microsoft.XMLHttp"
            ];
    
        for (var i = 0; i < aVersions.length; ++i) {
            try {
                var oXmlHttp = new ActiveXObject(aVersions[i]);
                return oXmlHttp;
            }
            catch (oError) {
                // do nothing, try next.
            }
        }
    }
    
    throw new Error("XMLHttp object could not be created.");
}

function buildHttpPostBody(raw_param_array)
{
    var tempArray = [];
    
    for (var the_name in raw_param_array )
    {
        var the_value = raw_param_array[the_name];
        if (typeof(the_value) != 'function')
        {
            tempArray[tempArray.length] = encodeURIComponent(the_name) + "=" + encodeURIComponent(the_value);
        }
    }
    
    return tempArray.join("&");
}

function processAjaxRequest(action, raw_param_array, result_element_or_callback, show_full_error) {
    var post_body = buildHttpPostBody(raw_param_array);
    var lXmlRequest = createXMLHttp();
    lXmlRequest.open("post", action, true);
    lXmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    lXmlRequest.onreadystatechange = function() {
        if (lXmlRequest.readyState == 4) {
            var lAttributeType = typeof(result_element_or_callback);
            lAttributeType = lAttributeType.toUpperCase();
            
            if (lAttributeType == "FUNCTION")
            {
                // call this function
                result_element_or_callback(lXmlRequest.status, lXmlRequest.responseText, lXmlRequest.responseXML);
            }
            else if (lAttributeType == "STRING")
            {
                // find the element with this ID. change its innerHTML
                if (lXmlRequest.status == 200) {
                    document.getElementById(result_element_or_callback).innerHTML = lXmlRequest.responseText;
                }
                else {
                    document.getElementById(result_element_or_callback).innerHTML = "An error occured: " + lXmlRequest.statusText + "<BR>" +lXmlRequest.responseText ;
                }
            }
        }
    }
    lXmlRequest.send(post_body);	
}
