﻿
var responseHash = {};

function loadXMLDoc(dname, async, xtype, ajaxHandler, useCache)
{
    if (!async)
    {  
        var xhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
        
        xhttp.open("GET",dname,false);
        xhttp.send("");
        
        return xhttp.responseXML;
    }
    else
    {    
        if (xtype == 'xml')
        {
            $.ajax( 
            { 
                url : dname,
                success: ajaxHandler.XMLResponseHandler,
                context: ajaxHandler,
                cache : useCache
            });
        }
        else if (xtype== 'xsl')
        {
            $.ajax( 
            { 
                url : dname,
                success: ajaxHandler.XSLTResponseHandler,
                context: ajaxHandler,
                cache : useCache
            });
        }
    }
} 

function AjaxHandler()
{
    
}

AjaxHandler.prototype.jQueryInfo = null;
AjaxHandler.prototype.xml = null;
AjaxHandler.prototype.xsl = null;
AjaxHandler.prototype.xhttp = null;
AjaxHandler.prototype.callback = null;

AjaxHandler.prototype.XMLResponseHandler = function(xmlData, status, request)
{
    this.xml = xmlData;   
    this.Write();
}
AjaxHandler.prototype.XSLTResponseHandler = function(xslData, status, request)
{
    this.xsl = xslData;
    this.Write();
}
AjaxHandler.prototype.Write = function()
{
    if (this.xml == null || this.xsl == null) 
        return;
        
    this.Output(this.jQueryInfo, this.xml, this.xsl);
    
    if (this.callback != undefined && this.callback != null)
        this.callback.call(this);
}

AjaxHandler.prototype.Output = function(jQLookup, xml, xsl)
{
    var element = $(jQLookup)[0];
    
    if (window.ActiveXObject)
    {
        ex=xml.transformNode(xsl);
        element.innerHTML=ex;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        
        fireFoxAppendChildFix(resultDocument, element);
    }
}

function displayResult(jQLookup, xmlURI, xslURI, async, callback, cache)
{
    var element = $(jQLookup)[0];

    if (xmlURI == undefined || xmlURI == null)
        xmlURI = element.attributes["datauri"].value;

    if (xslURI == undefined || xmlURI == null)
        xslURI = element.attributes["displayuri"].value;
        
    if (async == undefined || async == null)
        async = element.attributes["ajax"] ? element.attributes["ajax"].value == "true" : false;
        
    
    if (cache == undefined || cache == null)
        cache = element.attributes["CSJSCache"] ? element.attributes["CSJSCache"].value == "true" : true;
    
    var ajaxHandler = new AjaxHandler();
    
    ajaxHandler.jQueryInfo = jQLookup;
      
    xml=loadXMLDoc(xmlURI, async, 'xml', ajaxHandler, cache);
    xsl=loadXMLDoc(xslURI, async, 'xsl', ajaxHandler, cache);

    if (async) 
    {   
        ajaxHandler.callback = callback;
        return;
    }
    
    ajaxHandler.Output(jQLookup, xml, xsl);
}

function fireFoxAppendChildFix(myDOM, element)
{
        element.innerHTML = "";
        element.appendChild(myDOM);
        
        // Firefox seems to have an issue with just appendChild not
        // displaying the styles of the html.
        // the workaround is to render the html 
        // then resassign to the innerHTML to try 
        // and "rebind" the elements.
        var elementHTML = element.innerHTML;
        
        element.innerHTML = "";
        
        element.innerHTML = elementHTML.replace(/&amp;\#160;/gi, '&nbsp;');
}
