﻿// Wrapper for the XMLHttpRequest object
var xmlObjs = new Array();
var errMsgEnabled = false;

function XMLHttpReqObj(url){
    
    this.XMLHttpReq = false; 
    this.oURL = url;
    this.openMethod = "POST";
    this.loaded = false;
    
    this.onLoadFunc = "";
    xmlObjs.push(this);
      
}


XMLHttpReqObj.prototype.loadXML = loadXML;
XMLHttpReqObj.prototype.onLoad = onLoad;
XMLHttpReqObj.prototype.processReqChange = processReqChange;

function loadXML(url,method,sendXML) {

    var send = "";

    //if method given as parameter
    if(method != null){
	    
	    this.openMethod = method;
	    
	}
	if(sendXML != null){
	    send = sendXML;
	}


    //if called for refresh uses stored URL
    if(url == null){
        url = this.oURL;
    }
    //set stored URL to current 
	this.oURL = url;

	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			this.XMLHttpReq = new XMLHttpRequest();
        } catch(e) {
			this.XMLHttpReq = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	this.XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		this.XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(this.XMLHttpReq) {
		this.XMLHttpReq.onreadystatechange = processReqChange;
	    this.loaded = false;
		this.XMLHttpReq.open(this.openMethod, url, true);
		this.XMLHttpReq.send(send);
	}
	
	
}


function onLoad(func){

    this.onLoadFunc = func;

}


function processReqChange() {

   
    
    for(var i = 0; i < xmlObjs.length; i++){
    
        var curObj = xmlObjs[i].XMLHttpReq;
    
        // only if req shows "loaded"
        if (curObj.readyState == 4 && !xmlObjs[i].loaded) {
            // only if "OK"
            if (curObj.status == 200 ) {
                // ...processing statements go here...
                 xmlObjs[i].loaded = true;
                //alert("loaded!" + curObj.responseXML.documentElement.xml);
                eval(xmlObjs[i].onLoadFunc);
               
            } else {
				if(errMsgEnabled){
					alert("There was a problem retrieving the XML data:\n" +
					   curObj.statusText);
				   }
            }
         }

    }
}

function XSLTransform(xml, xsl){

    if(window.XMLHttpRequest) {
    	try {
    	
    	   var processor = new XSLTProcessor();
            // Finally import the .xsl
            processor.importStylesheet(xsl);
    	    fragment = processor.transformToFragment(xml,document);	
			return(fragment.firstChild.innerHTML);
        } catch(e) {
			if(errMsgEnabled){
				alert("XSL failure:" + e);
			}
		}
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	return(xml.transformNode(xsl));
      	} catch(e) {
			if(errMsgEnabled){
				alert("XSL failure");	
			}
		}
    }

}
