﻿// JScript File
function XMLHTTPRequestObject()
{
    //XmlHttp request object initilize 
    var XmlHttp;
    //function name 
    var funName;
    
    //call to callPage function
    this.callPage= callPage;
    //call to GetInnerText function
    this.GetInnerText = GetInnerText;
    //call to xmlToHtml function
    this.xmlToHtml = xmlToHtml;    
    //
    this.showDataWithXSLT = showDataWithXSLT;
    //create object of XmlHttp
    try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
	
	//call this function with parameter(request Url and function name)
	function callPage(requestUrl,fun)
	{
	    if(XmlHttp)
	    {
	        // assign function name to class varible
	        funName=fun;
		    //Setting the event handler for the response
		    XmlHttp.onreadystatechange = callback;
    		
    		//call to page whitch is pass into requsturl
		    XmlHttp.open("GET", requestUrl,  true);
    		
		    //Sends the request to server
		    XmlHttp.send(null);		
    		
	    }
	}
	
	// handle the callback event
	function callback()
	{
	        var moz ;
	        var ie ;
	        
	        // create mozila object
	        try
	        {
	            moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	        }
	        catch(ex){}
	        // create IE object
            try
            {
                ie = (typeof window.ActiveXObject != 'undefined');
            }
            catch(ex){}
	        // To make sure valid response is received from the server, 200 means response received is OK
	        try
	        {
		        if(XmlHttp.status == 200)
		        {
    		         
		            // check the complete response from the server
	    	        if(XmlHttp.readyState==4)
	    	        {
    	    	        
			            if (document.implementation.createDocument)
			            {	
			                // call function which is provide by callPage function
				            funName(XmlHttp.responseXML);
			            } 
			            else if(moz)
			            {	
			                // call function which is provide by callPage function		
				            funName(XmlHttp.responseXML);
            				
			            } 
			            else if(ie)
			            { 
			                //create xml document object
				            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				            xmlDoc.async = false;
				            //load xml file into object
				            xmlDoc.loadXML(XmlHttp.responseText);
				            // call function which is provide by callPage function		
				            funName(xmlDoc);
			            }
		                else
		                {
			               alert('browser does not support this script.');
		                }
		             }
        			
		        }
		        else
		        {
		    	    //alert("There was a problem retrieving data from the server." );
		        }
		    }
            catch(ex){}
	}
	//Returns the node text value 
    function GetInnerText(node)
    {
	    if(node==null)	
	    {
		    return "";
	    }
	    else
	    {
		    return (node.textContent || node.innerText || node.text) ;
	    }
    }
    
    //return the html code
    function xmlToHtml (ex)
    {
          var newstr;    
          newstr="<";
          //replace &lt into <
          ex = ex.replace(/&lt;/g, newstr);                  
          newstr=">";
          //replace &gt into >
          ex =ex.replace(/&gt;/g, newstr);
          newstr=" ";
          //replace &amp;nbsp; into ' '
          ex =ex.replace(/&amp;nbsp;/g, newstr);
          newstr="&";
          //replace &amp into &
          ex =ex.replace(/&amp;/g, newstr);
          return ex;
    }
    
	function showDataWithXSLT(node,xsl,divObj,paras)
    {
        
		if (node!=null)
		{
		        
		        	
   				if (window.ActiveXObject)
                  {
                        var xslt = new ActiveXObject("Msxml2.XSLTemplate");
                        var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
                        var xslProc;
                        xslDoc.async = false;
                        xslDoc.resolveExternals = false;
                        
                        xslDoc.load(xsl);
                        xslt.stylesheet = xslDoc;
                        var xmlDoc = node;
                        
                        xmlDoc.async = false;
                        xmlDoc.resolveExternals = false;
                        xslProc = xslt.createProcessor();
                        
                        xslProc.input = xmlDoc;
                        //add para here
                        
                        try
                        {
                            
                            for (cnt = 0 ; cnt < paras.length  ; cnt++)
                            {
                                xslProc.addParameter(paras[cnt][0], paras[cnt][1]);
                            }
                            
                        }
                        catch(ex){}
                        xslProc.transform();
                        var ex = xslProc.output;
                        if (ex.indexOf("?>") > -1)
                        {
                            ex = ex.substr(ex.indexOf("?>") + 2);
                        }
                        
                        divObj.innerHTML =ex;
                      
                  }
      // code for Mozilla, Firefox, Opera, etc.
                  else
                  {
                        var xslStylesheet;
                        var xsltProcessor = new XSLTProcessor();
                        var myDOM;
                        var xmlDoc;
                        var myXMLHTTPRequest = new XMLHttpRequest();
                        
                        myXMLHTTPRequest.open("GET", xsl, false);
                        
                        myXMLHTTPRequest.send(null);
                        xslStylesheet = myXMLHTTPRequest.responseXML;
                        xsltProcessor.importStylesheet(xslStylesheet);                        
				        xmlDoc = node;				        
                        // set the parameter using the parameter passed to the outputgroup function
                        try
                        {
                            for (cnt = 0 ; cnt < paras.length  ; cnt++)
                            {                               
                                xsltProcessor.setParameter(null,paras[cnt][0], paras[cnt][1]);                            
                            }                            
                         }
                         catch(ex){}                           
                        var fragment = xsltProcessor.transformToFragment(xmlDoc,document);
                        myDOM = fragment;                        
                        divObj.innerHTML = "";
                        
                        divObj.appendChild(fragment);
                        
                        ex = divObj.innerHTML;                        
                  }
                  divObj.innerHTML="";
                  ex = xmlToHtml(ex);                  
                  divObj.innerHTML=ex;
		}
		else
		{
		}
		
    }
	
}

