﻿/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}


/* Prevent Firefox store of Viewstate */
function setAutoCompleteOff(id) {
    var elem = document.getElementById(id);
    if (elem) {
        elem.setAttribute('autocomplete', 'off');
    }
}

setAutoCompleteOff('__VIEWSTATE');
setAutoCompleteOff('__EVENTTARGET');
setAutoCompleteOff('__EVENTARGUMENT');
setAutoCompleteOff('__EVENTVALIDATION');

/****************/

function ePath() {
    this.switchToSync = function() {
        var __xmlHttpRequest = window.XMLHttpRequest;
        window.XMLHttpRequest = XMLHttpRequest = function() {
            var _xmlHttp = null;
            if (!__xmlHttpRequest) {
                try {
                    _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (ex) { }
            }
            else {
                _xmlHttp = new __xmlHttpRequest();
            }

            if (!_xmlHttp) return null;

            this.abort = function() { return _xmlHttp.abort(); };
            this.getAllResponseHeaders = function() { return _xmlHttp.getAllResponseHeaders(); };
            this.getResponseHeader = function(header) { return _xmlHttp.getResponseHeader(header); };
            this.open = function(method, url, async, user, password) {
                return _xmlHttp.open(method, url, false, user, password);
            };
            this.send = function(body) {
                _xmlHttp.send(body);
                this.readyState = _xmlHttp.readyState;
                this.responseBody = _xmlHttp.responseBody;
                this.responseStream = _xmlHttp.responseStream;
                this.responseText = _xmlHttp.responseText;
                this.responseXML = _xmlHttp.responseXML;
                this.status = _xmlHttp.status;
                this.statusText = _xmlHttp.statusText;
                this.onreadystatechange();
            };
            this.setRequestHeader = function(name, value) { return _xmlHttp.setRequestHeader(name, value); };
        };
    };
}


function XmlNodes(nodes) {
    var _nodesSnapshot;
    var _viewNodes;
    var _length;

    if (window.document.evaluate) {
        _nodesSnapshot = nodes;
        _length = _nodesSnapshot.snapshotLength;
    }
    else {
        _viewNodes = nodes;
        _length = _viewNodes.length;
    }

    this.items = items;
    this.length = _length;

    function items(index) {
        function XmlNode(node) {

            var _text;
            var _value;
            var _snapshotItem;
            var _viewNode;

            if (window.document.evaluate) {
                _snapshotItem = node;
                _text = _snapshotItem.textContent;
                _value = _snapshotItem.stringValue;
            }
            else {
                _viewNode = node;
                _text = _viewNode.text;
                _value = _viewNode.value;
            }

            this.text = _text;
            this.value = _value;
            this.XPath = XPath;
            this.GetDocumentObject = GetDocumentObject;

            function GetDocumentObject() {
                if (_snapshotItem)
                    return _snapshotItem;
                else
                    return _viewNode
            }

            function XPath(xpathExpression) {
                if (window.document.evaluate) {

                    var xmlDoc = _snapshotItem;
                    var nsResolver = xmlDoc.ownerDocument.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
                    var nodesSnapshot = xmlDoc.ownerDocument.evaluate(xpathExpression, xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

                    if (nodesSnapshot.snapshotLength == 0)
                        return null;
                    var nodes = new XmlNodes(nodesSnapshot);
                    return nodes;

                }
                else if (window.ActiveXObject) {
                    objDoc = _viewNode;
                    objDoc.ownerDocument.setProperty("SelectionLanguage", "XPath");
                    var viewNodes = objDoc.selectNodes(xpathExpression);

                    if (viewNodes.length == 0)
                        return null;
                    var nodes = new XmlNodes(viewNodes);
                    return nodes;
                }
            }
        }

        if (_nodesSnapshot) {
            return new XmlNode(_nodesSnapshot.snapshotItem(index));
        }
        else if (_viewNodes) {
            return new XmlNode(_viewNodes(index));
        }
    }

}

function ePathAJAX() {
    var req;
    var responseXML;
    var xsl;
    var xml;
    this.GetRequest = GetRequest;
    this.GetResponceXML = GetResponceXML;
    this.XPath = XPath;
    this.loadXMLRequest = loadXMLRequest;
    this.applyXSLTransform = applyXSLTransform;
    this.runXSLTransform = runXSLTransform;

    function GetRequest() {
        return req;
    }

    function GetResponceXML() {
        if (req.responseXML && req.responseXML.childNodes.length > 0)
            return req.responseXML;
        else
            return null;
    }

    function XPath(xpathExpression) {
        if (window.document.evaluate) {
            if (req.responseXML) {
                var xmlDoc = req.responseXML;
                //alert( GetResponceXML().childNodes.length );
                var nsResolver = xmlDoc.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
                var nodesSnapshot = xmlDoc.evaluate(xpathExpression, xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

                if (nodesSnapshot.snapshotLength == 0)
                    return null;

                var nodes = new XmlNodes(nodesSnapshot);
                return nodes;
            }
        }
        else if (window.ActiveXObject) {
            if (req.responseXML.xml && req.responseXML.xml != "") {
                objDoc = req.responseXML;
                objDoc.setProperty("SelectionLanguage", "XPath");
                var viewNodes = objDoc.documentElement.selectNodes(xpathExpression);

                if (viewNodes.length == 0)
                    return null;

                var nodes = new XmlNodes(viewNodes);
                return nodes;
            }
        }
    }

    function loadXMLRequest(XMLURL, dataSend, async, onreadystatechange) {

        if (window.XMLHttpRequest) {
            // Mozilla
            req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {
                    req = false;
                }
            }
        }
        else {
            // Browser unknown
            alert("Browser unknown");
            return;
        }
        if (req) {
            if (onreadystatechange) req.onreadystatechange = onreadystatechange;
            req.open("POST", XMLURL, async);
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.send(dataSend);
            responseXML = req.responseXML;
            return req;
        }
    }

    function applyXSLTransform(XMLString, XSLURL) {

        if (document.implementation && document.implementation.createDocument) {
            // Mozilla
            var xsltProcessor = new XSLTProcessor();
            // load the xslt file
            var myXMLHTTPRequest = new XMLHttpRequest();
            myXMLHTTPRequest.open("GET", XSLURL, false);
            myXMLHTTPRequest.send(null);
            // get the XML document
            xslStylesheet = myXMLHTTPRequest.responseXML;
            xsltProcessor.importStylesheet(xslStylesheet);

            var xmlSource = XMLString; //req.responceXML;		
            //transform
            var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);


            tempDiv = document.createElement('div');
            tempDiv.innerHTML = "";
            tempDiv.appendChild(resultDocument);
            return tempDiv.innerHTML;
        }
        else if (window.ActiveXObject) {
            // IE

            if (XMLString.documentElement) {
                // Load XML
                xml = new ActiveXObject("MSXML2.DOMDocument");
                xml.async = false;
                xml = XMLString.documentElement.selectSingleNode("/")

                // Load XSL
                xsl = new ActiveXObject("MSXML2.DOMDocument");
                xsl.async = false;
                xsl.load(XSLURL)

                // Transform
                //	parentElement.innerHTML = xml.transformNode(xsl);
                return xml.transformNode(xsl);
            }
            else {
                return "";
            }
        } else {
            // Browser unknown
            alert("Browser unknown");
        }
    }


    function runXSLTransform(XMLURL, XSLURL, parentElement) {
        if (document.implementation && document.implementation.createDocument) {
            // Mozilla

            var xsltProcessor = new XSLTProcessor();

            // load the xslt file
            var myXMLHTTPRequest = new XMLHttpRequest();
            myXMLHTTPRequest.open("POST", XSLURL, false);
            myXMLHTTPRequest.send(null);

            // get the XML document
            xslStylesheet = myXMLHTTPRequest.responseXML;
            xsltProcessor.importStylesheet(xslStylesheet);

            // load the xml file
            myXMLHTTPRequest = new XMLHttpRequest();
            myXMLHTTPRequest.open("POST", XMLURL, false);
            myXMLHTTPRequest.send(null);

            var xmlSource = myXMLHTTPRequest.responseXML;

            //transform
            var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);

            parentElement.innerHTML = "";
            parentElement.appendChild(resultDocument);
        }
        else if (window.ActiveXObject) {
            // IE

            // Load XML
            xml = new ActiveXObject("MSXML2.DOMDocument");
            xml.async = false;
            xml.load(XMLURL);

            // Load XSL
            xsl = new ActiveXObject("MSXML2.DOMDocument");
            xsl.async = false;
            xsl.load(XSLURL);


            // Transform
            parentElement.innerHTML = xml.transformNode(xsl);
        } else {
            // Browser unknown
            alert("Browser unknown");
        }

    }
}
