﻿function ajaxPost(strParams,strServiceURL,strCallbackFunction)
{
    var xmlHttp;
    var xmlHttpTimeout = 5000;
    try
    {
        /* Firefox, Opera 8.0+, Safari */
        xmlHttp=new XMLHttpRequest();
        /* override DOM methods for these pain in the arse browsers */
        XPathForFireFox();
    }
    catch (e)
    {
        /* Internet Explorer */
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                /*alert("Your browser does not support AJAX!");*/
                return "";
            }
        }
    }

    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4&&xmlHttp.status==200)
        {
            window.clearTimeout(timeoutAjax);
            /* determine what function will handle the output */
            if(strCallbackFunction!=null)
            {
                var fn = strCallbackFunction;
                window[fn](xmlHttp.responseText);
            }
        } 
    }

    xmlHttp.open("POST",strServiceURL,true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   if (!(navigator.appVersion.indexOf("MSIE") > 0 && (navigator.appVersion.indexOf("Windows NT 5.0") > 0 || navigator.appVersion.indexOf("Windows NT 4") > 0)))
   { 
        xmlHttp.setRequestHeader("Content-Length", strParams.length);
        xmlHttp.setRequestHeader("Connection", "close");
    }
   
    var timeoutAjax = window.setTimeout(function(){if (callInProgress(xmlHttp) ){xmlHttp.abort();}},xmlHttpTimeout);
    xmlHttp.send(strParams);
}

/* this enables 'selectSingleNode' & 'selectNodes' XPath methods for AJAX for FireFox */
function XPathForFireFox()
{
    if (!window.ActiveXObject)
    {

        /* this solution came from a lot of debugging, finally the 'getting started' article on http://developer.mozilla.org/en/docs/AJAX helped. */
       XMLDocument.prototype.selectSingleNode = function(sXPath)
        {
            return this.getElementsByTagName(Right(sXPath,Len(sXPath) - sXPath.lastIndexOf("/") - 1)).item(0).firstChild.data;
        }
        
        /* the following protoypes haven't been tested, because they're not needed yet */
	    Element.prototype.selectNodes = function(sXPath)
	    {
		    var oEvaluator = new XPathEvaluator();
		    var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
		    var aNodes = new Array();
		    if (oResult != null)
		    {
			    var oElement = oResult.iterateNext();
			    while(oElement)
			    {
				    aNodes.push(oElement);
				    oElement = oResult.iterateNext();
			    }
		    }
		    return aNodes;
	    }
	    
	    Element.prototype.selectSingleNode = function(sXPath)
	    {
		    var oEvaluator = new XPathEvaluator();
		      /* FIRST_ORDERED_NODE_TYPE returns the first match to the xpath. */
		    var oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
		    if (oResult != null)
		    {
			    return oResult.singleNodeValue;
		    }
		    else
		    {
			    return null;
		    }              
	    }

    }
}

function callInProgress(xmlhttp)
{
    switch (xmlhttp.readyState)
    {
        case 1, 2, 3:
            return true;
            break;
        default: /* Case 4 and 0 */
            return false;
            break;
    }
}


