/*
*	AjaxZ
*	A javascript "object" for assyncronous requests.
*	Developer : Rafael Zuquim
*
*	Version 1.0: 1/6/2007
*		- 
*/

function clearElement(el) {
 if (el != null) {
   if (el.childNodes) {
     for (var i = 0; i < el.childNodes.length; i++) {
       var childNode = el.childNodes[i];
       el.removeChild(childNode);
     }
   }
 }
}

function replaceText(el, text) {
 if (el != null) {
   clearElement(el);
   var newNode = document.createTextNode(text);
   el.appendChild(newNode);
 }
}

var AjaxZ = {

	/*
	*	Atributes
	*/
	Version :		"1.0 Beta",
	xmlHttpObj:		null,
	actionURL:		null,
	parameters:		new Array(),
	textResponse:	null,
	xmlResponse:	null,
	
	method:			null,
	encoded:		null,
	
	onSetUp:		null,
	onSend:			null,
	onProcess:		null,
	onComplete:		null,

	textReplaceTarget: null,

	/*
	*	construct (key, value):
	*	A function to set some values.
	*/
	construct: 
		function (method, encoded) {
			this.method=method;
			this.encoded=encoded;
	},
  	
	/*
	*	addParameter (key, value):
	*	Adds a new parameter for a posterior post request.
	*/
	addParameter:
		function (key,value) {
			this.parameters[key] = value;

	},

	/*
	*	GetXmlHttpObj():
	*	Sets the xmlHttp Object according with user's browser.
	*/

	getXmlHttpObj: 
		function () { 
			var objXMLHttp=null;
			try{objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");} //IE >= v6
			catch(e){
				try{objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");}// v5.5 >= IE > 6
				catch(e){
					try{objXMLHttp = new XMLHttpRequest();} //Standart browsers
					catch(e){this.xmlHttpObj = null;}//Unsuported...
				}
			}
			this.xmlHttpObj = objXMLHttp;
	},
	
	/*
	*	ajaxRequest(url):
	*	Manages the entire assyncronous request.
	*/	

	ajaxRequest: 
		function (url) {
		this.getXmlHttpObj();
		this.actionURL = url;
		if (this.xmlHttpObj==null) {
			alert (errorMsgs.ajaxSuport);
			return;
		}
		if (this.actionURL == "") {
			alert(errorMsgs.noURL);
			return;
		}

		var self = this;

		this.xmlHttpObj.onreadystatechange = function() {

			if (self.xmlHttpObj.readyState==1) {
				if(self.onSetUp)
					self.onSetUp();
			}
			else if (self.xmlHttpObj.readyState==2) {
				if(self.onSend)
					self.onSend();
			}
			else if (self.xmlHttpObj.readyState==3) {
				if(self.onProcess)
					self.onProcess();
			}
			else if (self.xmlHttpObj.readyState==4 ||  self.xmlHttp.readyState=="complete") { 
				self.textResponse =  self.xmlHttpObj.responseText;
				self.xmlResponse = self.xmlHttpObj.responseXML;
				if (self.textReplaceTarget != null)
				    self.simpleResponseTextReplace();
				if(self.onComplete)
					self.onComplete();
			} 
		};

		if(this.method.toUpperCase() == "POST") {
			this.xmlHttpObj.open("POST",this.actionURL,true);
			this.xmlHttpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xmlHttpObj.setRequestHeader("Content-length", this.parameters.length);
			this.xmlHttpObj.setRequestHeader("Connection", "close");
			this.xmlHttpObj.send(this.mountParameters(this.parameters));
		} 
		else if(this.method.toUpperCase() == "GET") {
			this.xmlHttpObj.open("GET",this.actionURL + "?" + this.mountParameters(this.parameters) ,true);
			this.xmlHttpObj.send(null);
		}

	},
	
	
	/*
	*	mountParameters():
	*	Formats the parameters.
	*/	
	
	mountParameters : 
		function (){
		var strParam = "";
		for(key in this.parameters){
			if(key != "" && this.parameters[key] != ""){
				if(strParam != "")
					strParam += "&";
				if (this.encoded)
					strParam += key + "=" + encodeURIComponent(this.parameters[key]);
				else
					strParam += key + "=" + this.parameters[key];
			}
		}
		return strParam;
	},

	simpleResponseTextReplace:
		function () {
		clearElement(this.textReplaceTarget);
		this.textReplaceTarget.innerHTML = this.textResponse;
	}
}