﻿// client javascript functions for ajax
 
var HttpObjects = new Array();
var DestDivs = new Array();

// Read a URL and put the result in the DestDiv. Then run the code in the string RunCodeWhenDone
function DynamicLoadURLToDiv (URL, DestDiv, RunCodeWhenDone)
{
    var Http;
    if(navigator.appName == "Microsoft Internet Explorer")
	    Http = new ActiveXObject("Microsoft.XMLHTTP");
	else
	    Http = new XMLHttpRequest();

    var HttpIndex = HttpObjects.length;
    HttpObjects[HttpIndex] = Http;
    DestDivs[HttpIndex] = DestDiv;


//    eval ("HttpObjects["+HttpIndex+"].onreadystatechange = function() { " +
//        " alert(HttpObjects["+HttpIndex+"].readyState); " +
//    "}    ");
//    
    eval (
        "HttpObjects[" + HttpIndex + "].onreadystatechange = function() { " +
        "   if (HttpObjects[" + HttpIndex + "].readyState == 4) {    " +
        "       var HTML = HttpObjects[" + HttpIndex + "].responseText; " +
        "       DestDivs[" + HttpIndex + "].innerHTML = HTML;  " +
        "       RunInternalScripts (HTML); " + 
        "       " + RunCodeWhenDone +
        "   } " +
        "} "
     );

    Http.open('get', URL);
    //open(URL);
    Http.send(null);
}

//  הנתון והרץ אותן HTML בתוך ה <script> מצא תגיות
function RunInternalScripts(HTML) {
    var f1, f2=0;
    while (1) {
        f1 = HTML.indexOf("<script>", f2);
        if (f1 < 0) break;
        f2 = HTML.indexOf("</script>", f1);
        if (f2 < 0) break;
        try {
        	eval(HTML.slice(f1 + 8, f2 - 1));
        } catch (ex) {
			alert(HTML.slice(f1 + 8, f2 - 1));
        }
    }
}

function LoadXMLDocument(URL, DestObjectStr, RunCodeWhenDone) {
    if (navigator.appName == "Microsoft Internet Explorer") { //ie
        eval("" +
            "var XMLDocument = new ActiveXObject(\"Microsoft.XMLDOM\"); " +
            "XMLDocument.async = false; " +
            "XMLDocument.load(URL); " +
            "" + DestObjectStr + "=XMLDocument; " +
            "" + RunCodeWhenDone
        );

    } else {   // firefox/chrome

        //var XMLDocument = document.implementation.createDocument("", "", null);
        //XMLDocument.async = false;
        //XMLDocument.load(URL);
        //return XMLDocument;

        var Http = new XMLHttpRequest();
        var HttpIndex = HttpObjects.length;
        HttpObjects[HttpIndex] = Http;
        eval(
            "HttpObjects[" + HttpIndex + "].onreadystatechange = function() { " +
            "   if (HttpObjects[" + HttpIndex + "].readyState == 4) {    " +
            "       var XML = HttpObjects[" + HttpIndex + "].responseText; " +
            "       var DP = new DOMParser(); " +
            "       var XMLDocument = DP.parseFromString(XML, \"text/xml\"); " +
            "       " + DestObjectStr + "=XMLDocument; " +
            "       " + RunCodeWhenDone +
            "   } " +
            "} "
         );

        Http.open('get', URL);
        //open(URL);
        Http.send(null);

    }
}

function GetXMLDocumentAsText(XMLDocument) {
    if (navigator.appName == "Microsoft Internet Explorer") { //ie
        return (XMLDocument.xml + "");
    } else {   // firefox/chrome
        return ((new XMLSerializer()).serializeToString(XMLDocument));
    }
}

// transform an XML string with XSL stylesheet
// and return the result as string
function MergeXMLWithXSL(XML, XSL) {
    // XML - XML string
    // XSL - XSL string or XML object
    var XMLDocument, XSLDocument;

    if (navigator.appName == "Microsoft Internet Explorer") { //ie
        if (typeof (XML) == "object") {
            XMLDocument = XML;
        } else {
            XMLDocument = new ActiveXObject("Microsoft.XMLDOM");
            XMLDocument.async = false;
            XMLDocument.loadXML(XML);
        }
        if (typeof (XSL) == "object") {
            XSLDocument = XSL;
        } else {
            XSLDocument = new ActiveXObject("Microsoft.XMLDOM");
            XSLDocument.async = false;
            XSLDocument.loadXML(XSL);
        }
        var Result = XMLDocument.transformNode(XSLDocument);
        return Result;

    } else {   // firefox/chrome

        if (XML.toString().indexOf("object") >= 0) {
            XMLDocument = XML;
        } else {
            if (XML.indexOf("<?xml") < 0) XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + XML;
            XMLDocument = (new DOMParser()).parseFromString(XML, "text/xml")
        }
        if (XSL.toString().indexOf("object") >= 0) {
            XSLDocument = XSL;
        } else {
            if (XSL.indexOf("<?xml") < 0) XSL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + XSL;
            while (XSL.indexOf("<tbody>") >= 0) XSL = XSL.replace("<tbody>", "");
            while (XSL.indexOf("</tbody>") >= 0) XSL = XSL.replace("</tbody>", "");
            XSLDocument = (new DOMParser()).parseFromString(XSL, "text/xml");
        }

        var Processor = new XSLTProcessor();
        try {
            Processor.importStylesheet(XSLDocument);
        } catch (ex) {
            alert(ex);
        }

        var rs = Processor.transformToFragment(XMLDocument, document);

        //alert((new XMLSerializer()).serializeToString(XMLDocument));
        //alert((new XMLSerializer()).serializeToString(XSLDocument));
        //alert((new XMLSerializer()).serializeToString(rs));
        //return;

        return ((new XMLSerializer()).serializeToString(rs));
    }
}


function GetXMLDocumentFromString(XML) 
{
    if (navigator.appName == "Microsoft Internet Explorer") {
        XMLDocument = new ActiveXObject("Microsoft.XMLDOM");
        XMLDocument.loadXML(XML);
        return XMLDocument;
    } else {
        return (new DOMParser()).parseFromString(r, "text/xml");
    }
}

function GetStringFromXMLDocument(XMLDocument) {
    if (navigator.appName == "Microsoft Internet Explorer") {
        return XMLDocument.xml;
    } else {
        return (new XMLSerializer()).serializeToString(XMLDocument);
    }
}


function SelectSingleNode(xmlDoc, elementPath) {
    if (window.ActiveXObject) {
        return xmlDoc.selectSingleNode(elementPath);
    }
    else {
        var xpe = new XPathEvaluator();
        var nsResolver = xpe.createNSResolver(xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
        var results = xpe.evaluate(elementPath, xmlDoc, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        return results.singleNodeValue;
    }
}


// check for XPath implementation 
if( document.implementation.hasFeature("XPath", "3.0") ) 
{ 
    // prototying the XMLDocument 
 
    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;
        } 
    } 

    // prototying the Element 
    Element.prototype.selectSingleNode = function(cXPathString) 
    {
        if(this.ownerDocument.selectSingleNode) 
        { 
            return this.ownerDocument.selectSingleNode(cXPathString, this);
        } else {
            throw "For XML Elements Only";
        } 
    } 

    // prototying the XMLDocument 
    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;
    }

    Document.prototype.selectNodes = XMLDocument.prototype.selectNodes;

    // prototying the Element 
    Element.prototype.selectNodes = function(cXPathString) 
    { 
        if(this.ownerDocument.selectNodes) 
        { 
            return this.ownerDocument.selectNodes(cXPathString, this);
        } else {
            throw "For XML Elements Only"; 
        }
    }

}
