﻿var _asyncAJAXRequest;

function handleReturn() {
	if (_asyncAJAXRequest.readyState == 4) {
		_asyncAJAXRequest.onreadystatechange = dummy;
		if (_asyncAJAXRequest.status == 200) {
			_asyncAJAXRequest.doHandleReturn(_asyncAJAXRequest);
			//location.reload();
			//window.open('<%=Utility.Generic.JSEscape(url.URLLocation)%>');
		}
		else {
			_asyncAJAXRequest.doHandleFailure(_asyncAJAXRequest);
		}
	}
	return false;
}

function dummy() {
    return false;
}

function getXMLHttpRequest() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    }
    else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (ex) {
            return null;
        }
    }
}

function createXMLDOMObject(sourceXMLData) {
	var oXMLDOMObject;

	if (window.ActiveXObject) {
		oXMLDOMObject = new ActiveXObject("Microsoft.XMLDOM");
		oXMLDOMObject.async = false;
		oXMLDOMObject.loadXML(sourceXMLData);
	}
	else {
		oXMLDOMObject = (new DOMParser()).parseFromString(sourceXMLData, "text/xml");
	}

	return oXMLDOMObject;
}

function sendAJAXRequest(url, data) {
	try {
	    var ar = getXMLHttpRequest(); //window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		ar.open('GET', url, false);
		ar.send(data);
		return ar.responseXML;
	}
	catch (e) {
	}
	return null;
}

function sendAJAXRequestAsync(url, data, callBack, failCallBack, handleData) {
    try {
        _asyncAJAXRequest = getXMLHttpRequest(); //window._asyncAJAXRequest ? window._asyncAJAXRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());
        _asyncAJAXRequest.open('POST', url, true);
        _asyncAJAXRequest.doHandleReturn = (callBack != null ? callBack : dummy);
        _asyncAJAXRequest.doHandleFailure = (failCallBack != null ? failCallBack : dummy);
        _asyncAJAXRequest.handleData = handleData;
        _asyncAJAXRequest.onreadystatechange = handleReturn;
        _asyncAJAXRequest.send(data);
    }
    catch (e) {
        alert(e.description);
    }
    return true;
}

// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if (document.implementation && document.implementation.hasFeature("XPath", "3.0")) {
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
        if (!xNode) { xNode = this; }

        var oNSResolver = this.createNSResolver(this.documentElement)
        var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
        var aResult = [];
        for (var i = 0; i < aItems.snapshotLength; i++) {
            aResult[i] = aItems.snapshotItem(i);
        }

        return aResult;
    }
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
        if (!xNode) { xNode = this; }

        var xItems = this.selectNodes(cXPathString, xNode);
        if (xItems.length > 0) {
            return xItems[0];
        }
        else {
            return null;
        }
    }

    XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
    });



    Element.prototype.selectNodes = function(cXPathString) {
        if (this.ownerDocument.selectNodes) {
            return this.ownerDocument.selectNodes(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }

    Element.prototype.selectSingleNode = function(cXPathString) {
        if (this.ownerDocument.selectSingleNode) {
            return this.ownerDocument.selectSingleNode(cXPathString, this);
        }
        else { throw "For XML Elements Only"; }
    }

    Element.prototype.__defineGetter__("xml", function () {
        if (this.ownerDocument.xml) {
			return (new XMLSerializer()).serializeToString(this);
		}
		else { throw "For XML Elements Only"; }
	});

}



