// JavaScript Document
// Asynchronous Ajax Request
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;


function AsyncAjaxRequest() {

	this.url = "";
	this.parameters = "";
	this.request = null;

	this.xml = "";
	this.xsl = "";
	this.dest = null;
	
	this.parent = null;

	this.reset = function() {
		this.url = "";
		this.parameters = "";
		this.request = null;	
		this.xml = "";
		this.xsl = "";
		this.dest = null;
		this.parent = null;
	}

	this.getRequest = function(url, callback) {
		if(url != null) {
			this.url = url;
		}
		
		if (window.XMLHttpRequest) {
			//Codigo para outros browsers
			this.request = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) {
			//Codigo para IE
			this.request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		this.request.onreadystatechange = callback;
		this.request.open('GET', this.url, true);
		this.request.send(null);
	}


	this.postRequest = function(url, callback) {
		if(url != null) {
			this.url = url;
		}
		if (window.XMLHttpRequest) {
			//Codigo para outros browsers
			this.request = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) {
			//Codigo para IE
			this.request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		this.request.onreadystatechange = callback;
		this.request.open('post', this.url, true);
		this.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.request.setRequestHeader("Content-length", this.parameters.length);
		this.request.send(this.parameters);
	}

	this.addParameter = function(name, value) {
		this.parameters = (this.parameters==null) ? name + "=" + escape( value ) : this.parameters + "&" + name + "=" + escape( value );
	}




	this.bindXSL = function(xml, xsl) {
		//Utiliza os atributos da classe caso nao sejam passados
		this.xml = xml;
		this.xsl = xsl;
		if(window.ActiveXObject) {			
			//Get a XML into a XSL style and fill the target
			this.srcTree = new ActiveXObject("Msxml2.DOMDocument");
			this.srcTree.async = false;
			this.srcTree.loadXML(xml);
			//Load the XSL		
			var xsltTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
			xsltTree.async = false;
			xsltTree.load(xsl);
			//Create a template made of your XSL file. Much needed for the creation of your processor.
			var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate");
			//Setting the actual XSL document in the template
			xslTemplate.stylesheet = xsltTree;
			
			//Compile your stylesheet into a processor
			this.xslProc = xslTemplate.createProcessor();
			this.xslProc.input = this.srcTree;  // The xml input here is actually the xml you want to transform
		} else {
			this.xslProcFF = new XSLTProcessor();
			p = new XMLHttpRequest();
			p.open("GET", xsl, false);
			p.send(null);
			//alert(p.responseXML);
			xslRef = p.responseXML;
			this.xslProcFF.importStylesheet(xslRef);
		}
	}
	
	this.addXSLParameter = function(param, value) {
		if(window.ActiveXObject) {
			this.xslProc.addParameter(param, value);
		} else {
			this.xslProcFF.setParameter(null, param, value);
		}
	}
	
	this.applyXSL = function(target, a) {
		this.dest = target;
		if(window.ActiveXObject) {
			this.xslProc.input = this.srcTree;
			this.xslProc.transform();
			var c= this.xslProc.output;
			//alert("\&\#xA\;");
			var re = new RegExp("&#xA;","gim");
			c = c.replace(re, "");
			c = c.substr(c.indexOf("?>")+2);
			//if(a!=null) { alert(c); }
			//c = c.substr(c.indexOf(		
			target.innerHTML = c;//this.xslProc.output;
		} else {
			//alert(this.xml);
			this.xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + this.xml;
			objSrcTree = document.implementation.createDocument(this.xml, "aa", null)
			//objSrcTree.loadXML();
			//xDoc.createTextNode(this.xml);
			//alert(xDoc.nodeList);
			/*
			p = new XMLHttpRequest();
			p.open("GET", SERVICESPATH + "shome.aspx?action=avisos", false);
			p.send(null);
			xslRef = p.responseXML;
			alert(xslRef);*/
			//alert(objSrcTree.documentElement);
			
			var fragment = this.xslProcFF.transformToFragment(objSrcTree.documentElement, document);
			//alert(fragment);
			target.innerHTML = "";
			target.appendChild(fragment);
		}
	}
	
	this.getFormatedXSL = function() {
		this.xslProc.input = this.srcTree;
		this.xslProc.transform();				
		return this.xslProc.output;
	}

}
