﻿// IsIE() - returns true if the web browser is Internet Explorer or a derivative
function IsIE() {
    var responseValue = false;
    var temp = navigator.appName;
    temp = temp.toLowerCase();
    if (temp == "internet explorer" || temp == "microsoft internet explorer" || temp == "ie" || temp == "msie") {
        responseValue = true;
    }
    return responseValue;
}

// isNull() - returns true if an input value is null
function isNull(inVal) {
    return typeof inVal == 'object' && !inVal;
}

// isUndefined() - returns true if an input value is undefined
function isUndefined(inVal) {
    return typeof inVal == 'undefined';
}

// addEvent() - adds an event function to a form element
function addEvent(elm, eventType, functionPointer, useCapture) {
    var responseValue = false;

    if (elm.addEventListener) {
        elm.addEventListener(eventType, functionPointer, useCapture);
        responseValue = true;
    }
    else if (elm.attachEvent) {
        var result = elm.attachEvent('on' + eventType, functionPointer);
        responseValue = result;
    }
    else {
        elm['on' + eventType] = functionPointer;
    }
    
    return responseValue;
}


// GetXmlHttp() - Returns the correct XMLHttpRequest depending on the current browser.        
function GetXmlHttp() {
    
    var xmlhttp = false;

    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) // code for IE
    {
        try 
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) 
        {
            try 
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) 
            {
                xmlhttp = false;
            }
        }
    }

    return xmlhttp;
}


/*
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and 
the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');
function FunctionToHandleTheResponse(response, d1, d2, d3)
{
var data = response.split(';');
document.getElementById(d1).innerHTML = data[0];
document.getElementById(d2).innerHTML = data[1];
document.getElementById(d3).innerHTML = data[2];
}
</code>
</example>
*/
function PassAjaxResponseToFunction(url, callbackFunction, params) {

    var xmlhttp = new GetXmlHttp();

    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp && xmlhttp.readyState == 4) {	// we got something back...
                if (xmlhttp.status == 200) // and it's good...
                {
                    var response = xmlhttp.responseText;
                    var functionToCall = callbackFunction + '(response';
                    if (params != '') {
                        functionToCall += ',' + params + ')';
                    }
                    else {
                        functionToCall += ');';
                    }
                    eval(functionToCall);
                }
            }
        }
        
        try 
        {
            // dont cache make a unique url
            url += (url.indexOf('?') >= 0 ? '&' : '?') + "nocache=" + new String(Math.random()).substring(2);
            xmlhttp.open("GET", url, true);
            xmlhttp.send(null);
        }
        catch (e) {
        }

    }
}


/*
<summary>
Sets the innerHTML property of obj_id with the response from the passed url./
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponse(url, obj_id, sThemePath) {

    var xmlhttp = new GetXmlHttp();

    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {

        var sAni = '<div style="text-align: center;	vertical-align: middle;"><img src="' + sThemePath + 'images/ajax/smallAjaxLoading.gif" /></div>'
    
        // first, setup the ajax load animation
        if (typeof obj_id == 'object') {
            obj_id.innerHTML = sAni;
        }
        else {
            document.getElementById(obj_id).innerHTML = sAni;
        }

        xmlhttp.onreadystatechange = function () {
            var sBuff;

            if (xmlhttp && xmlhttp.readyState == 4) { // we got something back...
                if (xmlhttp.status == 200) {	// and it's good...
                    sBuff = xmlhttp.responseText;
                }
                else {
                    obj_id.innerHTML = 'Error: ' + xmlhttp.status + '; ' + url + xmlhttp.responseText; ;
                }

                if (typeof obj_id == 'object') {
                    obj_id.innerHTML = sBuff;
                }
                else {
                    document.getElementById(obj_id).innerHTML = sBuff;
                }

                try 
                {

                    if (!isNull(sBuff) && !isUndefined(sBuff)) {
                        // eval any script held within the returned content.
                        var strDiv = sBuff.replace(/[\n\r\t\f]/g, ''); //strip non white characters
                        strDiv = strDiv.replace(/[\n\r\t\f]/g, '');
                        var regexScriptTag = /<\s*script[^>]*>([^<]*)</ig;
                        var ScriptDataArray = regexScriptTag.exec(strDiv);
                        if (!isNull(ScriptDataArray)) {
                            // alert(ScriptDataArray[1]);
                            eval(ScriptDataArray[1]);
                        }
                    }
                }
                catch (e) 
                {
                    //alert('An error occurred while processing the Ajax embedded jscript.');
                }
            }
        }

        try 
        {
            url += (url.indexOf('?') >= 0 ? '&' : '?') + "nocache=" + new String(Math.random()).substring(5);
            xmlhttp.open("GET", url, true);
            xmlhttp.send(null);
        }
        catch (e) {
        }

    }
}