﻿/// XSL CLASS ///
//© 2008 SeatQuest LLC.

//ELI this class was generated by ELI VAKNIN.
//http://www.htmlgoodies.com/primers/jsp/article.php/3600451
//http://bitstructures.com/2007/11/javascript-method-callbacks

//ELI good closure article.
//http://www.claassen.net/geek/blog/2006/07/delegates-in-javascript.html

//ELI XSLT CLASS
function XSLT(args)
{
    /// <sumary>Represents an XSLT parser</sumary>
    //ELI PRIVATE DECLERATIONS
    //-------------------------
    function get_type()
    {
        if (window.ActiveXObject)
        {
            return "IE";
        } else if (document.implementation && document.implementation.createDocument){
            return "GECKO";
        }
        return null;    
    }
    this.type=get_type();
     
    if(!args){return;}
    this.debug=args.debug;
//    if(args.xml_url && args.xsl_url && (args.id || args.pre_render || args.post_render || args.xml_ready || args.xsl_ready) ){this.download(args);}
}

XSLT.prototype.type=null;
XSLT.prototype.debug=null;
XSLT.prototype.args={debug:null}

//ELI process the XSL
//PARAMS: 
//xml - Ajax.responseXML;
//xsl - string;
//string_params - string parameters to replace in XSL.
// RETURNS: a DOM object of the result XML+XSL document.
XSLT.prototype.merge = function(xml, xsl, string_params)
{
    this.debug.time("Merge");
    var dom = null;
    try {
        //ELI error proofing themerge operation.        
        if (!xml && !xsl) { throw 'XML or XSL not present!'; }

        //ELI parse parameters
        var xsl_s = this.parse_string_params(string_params, xsl);

        var xsl_doc = null;
        if (this.type == "GECKO") {
        
            var xslt = new XSLTProcessor();

            //ELI apply string parsed xsl
            xsl_doc = XMLDOMObject().parseFromString(xsl_s, "text/xml");
            
            //ELI import the style sheet.
            xslt.importStylesheet(xsl_doc);

            //ELI generate result
            dom = xslt.transformToFragment(xml.responseXML, document );
            
        } else if (this.type == "IE") {

            //ELI create the XSL object.
            xsl_doc = XMLDOMObject();
            xsl_doc.loadXML(xsl_s);

            //ELI IE has a weird condition in which it doesnt generate an XMLDOM if the XSL
            // document does not contain surrounding DIV or HTML element.
            // this code checks for it.
            var html = xml.responseXML.transformNode(xsl_doc);
            dom = XMLDOMObject();
            dom.loadXML(html);
            if (html && !dom.xml) { throw { message: "DOM creation failed!" }; }
            //            dom=new ActiveXObject("Msxml2.DOMDocument.3.0");
            //            var ret=xml.responseXML.transformNodeToObject(xsl_doc,dom);
        }
    } catch (err) {
        this.debug.error("XSL->merge", err);
        dom = null;
    }
    this.debug.time("Merge", true);
    return dom;
};

//ELI PARSING FUNCTIONS
//---------------------
//ELI XSL is a string
XSLT.prototype.parse_string_params=function (string_params,xsl)
{
    ///<summary>Parse the string_params in orer to create a dynamic XSL.</summary>
    if(!string_params) return xsl;
    var xsl_s=xsl;
    
    var sp=string_params;
    if(typeof sp=='function'){sp=sp();}
    
    for(s in sp){
        var re = new RegExp(s, 'gi');
        if(sp[s]!=null)
        {
            var s=sp[s];
            if(typeof s=='function'){s=s();}
            xsl_s=xsl_s.replace(re,s);
        }
    }
    return xsl_s;
}
