﻿//=============================================================================
//This Function will Render the Posted DATA.
//function setDataIslendXSL(PlaceHolder, xmlDataIsland, xslDataIsland)
//{
//    xmlDataIsland.XMLDocument.async = false;
//    xslDataIsland.XMLDocument.async = false;
//    if(xmlDataIsland && xslDataIsland && xslDataIsland.readyState == "complete" && xmlDataIsland.readyState == "complete")
//    {
//         document.getElementById(PlaceHolder).innerHTML = xmlDataIsland.XMLDocument.transformNode(xslDataIsland.XMLDocument);  
//    }  
//}
//=============================================================================
function XMLWriter()
{
    this.XML   = [];
    this.nodes = [];
    this.state = "";
    this.formatXML = function(Str)
    {
        if(Str)
        {
            Str = Str.toString();
            return Str.replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        }
        return "";
    }
    this.beginNode = function(Name)
    {
         if(!Name) 
         {
              return;
         }
         if(this.state == "beg") 
         {
              this.XML.push(">");
         }
         this.state = "beg";
         this.nodes.push(Name);
         this.XML.push("<" + Name);
    }
    this.endNode = function()
    {
         if(this.state == "beg")
         {
              this.XML.push("/>");
              this.nodes.pop();
         }
         else if(this.nodes.length > 0)
         {
              this.XML.push("</" + this.nodes.pop() + ">");
         }
         this.state = "";
    }
    this.attrib = function(Name, Value)
    {
         if(this.state != "beg" || !Name) return;
         this.XML.push(" " + Name + "='" + this.formatXML(Value) + "'");
    }
    this.writeString = function(Value)
    {
         if(this.state == "beg") this.XML.push(">");
         this.XML.push(this.formatXML(Value));
         this.state = "";
    }
    this.writeStringNoFormat = function(Value)
    {
         if(this.state == "beg") this.XML.push(">");
         this.XML.push(Value);
         this.state = "";
    }
    this.node = function(Name, Value)
    {
         if(!Name) return;
         if(this.state == "beg") this.XML.push(">");
         this.XML.push((Value == "" || !Value) ? "<" + Name + "/>" : "<" + Name + ">" + this.formatXML(Value) + "</" + Name + ">");
         this.state = "";
    }
    this.close = function()
    {
         while(this.nodes.length > 0)
         {         
              this.endNode();
         }
         this.state = "closed";
    }
    this.toString = function()
    {
         return this.XML.join("");
    }
}
/**
* remarks...
*
* @param	integer - 	
* @param	integer - 	
*
* @return	string
*
* TODO: ...
*/
//=============================================================================
function XMLXSLWriter()
{
    this.xmlStr            = "";
    this.xmlPath           = "";
    this.xmlDoc            = null;
    
    this.xslPath       = "";
    this.xslDoc        = null;
    this.xslProcessor  = null;
    this.xslParameters = new Array();;
    this.placeHolder   = "XMLDataPlaceHolder";
    //=========================================================================
    /**
    * Load XML file according to file name or URL and set xmlDoc... 
    *
    * @param	None...
    *
    * @return	None...
    *
    * TODO: ...
    */
    //=========================================================================    
    this.loadXMLFile = function()
    {
         //Newer version Netsacape,FireFox, Mozilla...
         if(window.XMLHttpRequest)
         {
         	    var xmlHttpRequest = new XMLHttpRequest();
		    xmlHttpRequest.open("GET", this.xmlPath, false);
		    xmlHttpRequest.send(null);
		    this.xmlDoc = xmlHttpRequest.responseXML;                  
         }                   
         //IE...
         else if(window.ActiveXObject)
         {
              this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");         
              this.xmlDoc.async = false;               
              this.xmlDoc.load(this.xmlPath);                
         }          
         //Mozilla, Firefox, Opera, etc...
         else if(document.implementation && document.implementation.createDocument)
         {
              this.xmlDoc = document.implementation.createDocument("", "", null);        
              this.xmlDoc.async = false;               
              this.xmlDoc.load(this.xmlPath);              
         }    
         else
         {
              alert('Your browser cannot handle this script');
         }
    } 
    //=========================================================================
    /**
    * Load XML well formated XML String and set xmlDoc... 
    *
    * @param	None...
    *
    * @return	None...
    *
    * TODO: ...
    */        
    //=========================================================================   
    this.loadXMLString = function()
    {
         //IE...
         if(window.ActiveXObject)
         {
              this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
              this.xmlDoc.async = false; 
              this.xmlDoc.loadXML(this.xmlStr);             
         }       
         //Mozilla, Firefox, Opera, etc...
         else if(document.implementation && document.implementation.createDocument)
         {
              var parser = new DOMParser();
              //"text/xml" - description of the content...
              this.xmlDoc = parser.parseFromString(this.xmlStr, "text/xml");       
         }
         else
         {
              alert('Your browser cannot handle this script');
         }
    } 
    //=========================================================================
    /**
    * Extract text String value from xmlDoc... 
    *
    * @param	None...
    *
    * @return	None...
    *
    * TODO: ...
    */            
    //=========================================================================       
    this.getText = function()
    {
         //IE...
         if(window.ActiveXObject)
         {
              return this.xmlDoc.documentElement.text;     
         }       
         //Mozilla, Firefox, Opera, etc...
         else if(document.implementation && document.implementation.createDocument)
         {
              var sText = "";
              for(var i = 0; i < this.getDocumentChildNodes().length; i++)
              {
                   if(this.getDocumentChildNodes()[i].hasChildNodes())
                   {
                        sText += getText(this.getDocumentChildNodes()[i]);
                   }
                   else
                   {
                        sText += this.getDocumentChildNodes()[i].nodeValue;
                   }        
              }
              return sText;
         }
         else
         {
              alert('Your browser cannot handle this script');
         }
    } 
    //=========================================================================
    /**
    * Extract XML String value from xmlDoc... 
    *
    * @param	None...
    *
    * @return	None...
    *
    * TODO: ...
    */                    
    //=========================================================================       
    this.getXML = function()
    {
         //IE...
         if(window.ActiveXObject)
         {
              return this.xmlDoc.documentElement.xml;     
         }       
         //Mozilla, Firefox, Opera, etc...
         else if(document.implementation && document.implementation.createDocument)
         {
              return (new XMLSerializer()).serializeToString(this.xmlDoc.documentElement);
         }
         else
         {
              alert('Your browser cannot handle this script');
         }
    }  
    //=========================================================================
    /**
    * Get xmlDoc child nodes... 
    *
    * @param	None...
    *
    * @return	None...
    *
    * TODO: ...
    */                 
    //=========================================================================       
    this.getDocumentChildNodes = function()
    {
         return this.xmlDoc.documentElement.childNodes;
    }   
    //=========================================================================
    /**
    * Get the Value of an specific Attribute... 
    *
    * @param string	 - tagName  - The name of the Node to extract...
    * @param integer - tagIndex - The index of Node in the array...
    * @param string  - attrName - The attribute name...    
    *
    * @return	string - The value of the attribute...
    *
    * How to use: xmlDocTemp.getTagAttributeValue("friendID", 0, "id");
    *
    * TODO: ...
    */                
    //=========================================================================       
    this.getTagAttributeValue = function(tagName, tagIndex, attrName)
    {
         return this.xmlDoc.getElementsByTagName(tagName)[tagIndex].getAttribute(attrName);
    }   
    //=========================================================================
    /**
    * Get the Value of an node...
    *
    * @param string  - nodeName  - The name of the Node to extract...
    * @param integer - nodeIndex - The index of Node in the array...
    * @param string	 - attrName  - The attribute name...    
    *
    * @return	string - The value of the Tag...
    *
    * How to use: xmlDocTemp.getTagValue("friendID", 1, 0);
    *
    * TODO: ...
    */                 
    //=========================================================================       
    this.getTagValue = function(nodeName, nodeIndex, childIndex)
    {
        if(null == this.xmlDoc)
            return "";
         var elem = this.xmlDoc.getElementsByTagName(nodeName);
         if(null == elem )
            return "";
         elem = elem[nodeIndex];
         if(null == elem )
            return "";
         elem = elem.childNodes[childIndex];
         if(null == elem )
            return "";
         return elem.nodeValue;
    } 
    //=========================================================================
    /**
    * Transformation between the xml and xsl...
    *
    * @param  None...
    *
    * @return None...
    *
    * How to use: ...
    *
    * TODO: ...
    */                                                   
    //=========================================================================
    this.bindXMLXSL = function()
    {
	    //FireFox...
	    if(document.implementation && document.implementation.createDocument)
	    {
		    //Load the XML file...
		    if(this.xmlDoc == null) this.loadXMLFile();		    		    
		    		    		    		    
		    //Load XSL file...		    		    		    
		    if(this.xslDoc == null)
		    {
		         var xmlHttpRequest = new XMLHttpRequest();
		         xmlHttpRequest.open("GET", this.xslPath, false);
		         xmlHttpRequest.send(null);
		         var resXSL = xmlHttpRequest.responseXML;
		         //XSL Processor...
		         this.xslProcessor = new XSLTProcessor();
		         this.addParametersToXSL();
		         this.xslProcessor.importStylesheet(resXSL);	    
		    }		 		    		    		    		    		
		    var returnValue = this.xslProcessor.transformToFragment(this.xmlDoc, document);
		    //alert(new XMLSerializer().serializeToString(this.xmlDoc.documentElement));
		    //alert(new XMLSerializer().serializeToString(returnValue));
		    $(this.placeHolder).innerHTML = this.fixCDATA(new XMLSerializer().serializeToString(returnValue));
	    }
	    //IE...
	    else
	    {
		    //Load the xml file...
		    if(this.xmlDoc == null) this.loadXMLFile();
		    
		    //Load the xsl file...
		    if(this.xslDoc == null)
		    {
		         this.xslDoc = new ActiveXObject('MSXML2.FreeThreadedDOMDocument'); 
		         this.xslDoc.async = false; 
		         this.xslDoc.load(this.xslPath); 
		
		         var xslTemplate = new ActiveXObject('MSXML2.XSLTemplate'); 
		         xslTemplate.stylesheet = this.xslDoc; 
              
                   //XSL Processor...
		         this.xslProcessor = xslTemplate.createProcessor(); 
		         this.addParametersToXSL();		    		    
		    }		    
		    this.xslProcessor.input = this.xmlDoc;
		    this.xslProcessor.transform(); 
		
		    var transformationXML = this.xslProcessor.output;
		    $(this.placeHolder).innerHTML = transformationXML;		    
	    }
    }
    //=========================================================================
    /**
    * Fix the transformation string (replace &lt; and &gt; to <> from the string)...
    *
    * @param string - str - The string to fix...
    *
    * @return string - The fixed string...
    *
    * How to use: ...
    *
    * TODO: ...
    */             
    //=========================================================================     
    this.fixCDATA = function(str)
    {
         return str.replace(/&lt;/g,"\x3c").replace(/&gt;/g,"\x3e");
    }  
    //=========================================================================
    /**
    * Add all xslParameters to xslParameters array...
    *
    * @param None...
    *
    * @return None...
    *
    * How to use: ...
    *
    * TODO: ...
    */                 
    //=========================================================================
    this.addParametersToXSL = function()
    {
         for(i = 0; i < this.xslParameters.length; i++)
         {
	         //FireFox...
	         if(document.implementation && document.implementation.createDocument)
	         {                            
                   this.xslProcessor.setParameter(null, this.xslParameters[i].Name, this.xslParameters[i].Value);
              }
              //IE...
              else if(window.ActiveXObject)
              {
                   this.xslProcessor.addParameter(this.xslParameters[i].Name, this.xslParameters[i].Value);
              }              
         }         
    }
    //=========================================================================
    /**
    * Add one XSL parameter to xslParameters array...
    *
    * @param user Object - param - xslParameter to add...
    *
    * @return None...
    *
    * How to use: ...
    *
    * TODO: ...
    */                        
    //=========================================================================
    this.addParameterToArray = function(param)
    {
         this.xslParameters.push(param);
    }    
    //=========================================================================            
}
//=============================================================================
function xslParameter(name, value)
{
    this.Name = name;
    this.Value = value;
}
//=============================================================================