function exceptionPadrao(cod, status) {
	alert(cod + ": " + status);
}

function ajaxPadrao(formulario, metodo, url, layer) {
	var objAjax = new AjaxObject(metodo, url);
	objAjax.setLoadingFunction(loading);
	objAjax.addAllRequestObjects(document.forms[formulario]);
	objAjax.setCallBackFunction(callBackPadrao, new Array(layer));
	objAjax.load();
}


function ajaxPadraoForm(formulario, metodo, url, callBack) {
		objAjax = new AjaxObject(metodo, url);
		for (x = 0; x < document.forms[formulario].elements.length; x++) {
			objAjax.addRequestObject(document.forms[formulario].elements[x]);
		}
		objAjax.setCallBackFunction(callBack, new Array());
		objAjax.loadAjax();
}

// Função padrão de Callback
		
function callBackAlert(response) {
	eval(response);
}

/*
* Class to build assynchronous request to webservers.
*
* You can, optionally, pass request values or request form input objects
* 
* 
* @param method Request method (GET or POST)
* @param url url requested on LoadAjax() function
* @param xmlResponse enables xml format to response
* @see loadAjax()
*/ 
// class AjaxObject {

	/*
	* AjaxObject Constructor
	* 
	* @param method Request method (GET or POST)
	* @param url url requested on LoadAjax() function
	* @param xmlResponse enables xml format to response
	* @see loadAjax()
	*/ 
	function AjaxObject(method, url) {
		// setting attributes
		this.method					= method;
		this.url					= url + ((url.indexOf('?') > 0) ? "&" : "?") + "requestTime=" + new Date().getTime();
		this.requestObjects			= new Array();
		this.requestValues			= new Array();
		this.responseFormat			= (AjaxObject.arguments[2]) ? "xml" : "text";

		// setting methods
		this.addRequestObject		= addRequestObject;
		this.addRequestValue		= addRequestValue;
		this.setCallBackFunction	= setCallBackFunction;
		this.setLoadingFunction		= setLoadingFunction;
		this.buildRequestString		= buildRequestString;
		this.loadAjax				= loadAjax;
	}
	
	/*
	* AjaxObject's method that adds an input object to pass in the request
	* 
	* @param requestObject
	*/ 
	function addRequestObject(requestObject) {
		this.requestObjects[this.requestObjects.length] = requestObject;
	}
	
	/*
	* AjaxObject's method that adds a key-value pair to pass in the request as a parameter
	* 
	* @param requestObject
	*/ 
	function addRequestValue(requestKey, requestValue) {
		this.requestValues[this.requestValues.length] = new Array(requestKey, requestValue);
	}

	/*
	* AjaxObject's method that sets the Javascript's function
	* assigned to be onreadstatechange event listener
	* 
	* @param callBackFunction
	* @param arrayArguments Some arguments used by the method
	*/ 
	function setCallBackFunction(callBackFunction, arrayArguments) {
		this.callBackFunction	= callBackFunction;
		this.callBackArguments	= arrayArguments;
	}
	
	/*
	* AjaxObject's method that sets the Javascript's function
	* called within the loadAjax() to change loading status
	* 
	* @param loadingFunction
	* @param arrayArguments Some arguments used by the method
	*/ 
	function setLoadingFunction(loadingFunction) {
		this.loadingFunction	= loadingFunction;
	}
	
	/*
	* AjaxObject's method that process requestObjects and requestValues
	* arrays and builds the return string used on GET's URL or POST's request parameters
	* 
	* @return the new URL string
	*/ 
	function buildRequestString() {
		returnString = (this.method.toUpperCase() == 'GET') ? this.url : '';
		for (x = 0; x < this.requestValues.length; x++) {
			returnString += '&' + this.requestValues[x][0] + '=' + encodeURIComponent(this.requestValues[x][1]);
		}
		for (x = 0; x < this.requestObjects.length; x++) {
			try {
				returnString += '&' + this.requestObjects[x].name + '=' + encodeURIComponent(this.requestObjects[x].value);
			} catch (e) {
				alert('The ' + x + 'º passed object is not a valid form input object!');
			}
		}
		return returnString;
	}

	/*
	* AjaxObject's method that loads the new thread of XMLHttpRequest
	*/ 
	function loadAjax() {
		xmlHttpRequest = new XmlHttpRequestObject(this);
		
		if (this.method.toUpperCase() == "POST") {
			xmlHttpRequest.thread.open("POST", this.url, true);
			xmlHttpRequest.thread.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			requestString = this.buildRequestString();
			xmlHttpRequest.thread.setRequestHeader('Content-Length',returnString.length);
			xmlHttpRequest.thread.send(requestString);
		} else if (this.method.toUpperCase() == "GET") {
			this.url = this.buildRequestString();
			xmlHttpRequest.thread.open("GET", this.url, true);
			xmlHttpRequest.thread.send(null);
		}
	
		
		xmlHttpRequest.thread.onreadystatechange = function() {
			if (xmlHttpRequest.thread.readyState == 0) { state = 'uninitialized' }
			if (xmlHttpRequest.thread.readyState == 1) { state = 'loading' }
			if (xmlHttpRequest.thread.readyState == 2) { state = 'loaded' }
			if (xmlHttpRequest.thread.readyState == 3) { state = 'interactive' }
			if (xmlHttpRequest.thread.readyState == 4) { state = 'complete' }
			if (xmlHttpRequest.ajaxObject.loadingFunction) xmlHttpRequest.ajaxObject.loadingFunction(state);
			
			if (xmlHttpRequest.thread.readyState == 4) {
				if (xmlHttpRequest.thread.status == 200) {
					params = "";
					for (x = 0; x < xmlHttpRequest.ajaxObject.callBackArguments.length; x++) {
						params += ", xmlHttpRequest.ajaxObject.callBackArguments[" + x + "]";
					}
					if (xmlHttpRequest.ajaxObject.callBackFunction) {
						eval("xmlHttpRequest.ajaxObject.callBackFunction( unescape(xmlHttpRequest.thread.responseText)" + params + " )");
					}
				} else {
					if (xmlHttpRequest.ajaxObject.exceptionFunction) {
						eval("xmlHttpRequest.ajaxObject.exceptionFunction( unescape(xmlHttpRequest.thread.status))");
					}
				}
			}
			
		}
	}

// }

// class XmlHttpRequestObject {

	/*
	* XmlHttpRequestObject Constructor
	* 
	* @param callBackFunction function loaded when response status code is 200 - OK
	* @param callBackArguments array of arguments passed to the callback function
	* @param loadingFunction function called on every change of request state
	*/ 
	function XmlHttpRequestObject(ajaxObject) {
		try { this.thread = new XMLHttpRequest(); }
		catch(e) { try {this.thread = new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {} }
		
		this.ajaxObject	= ajaxObject;
	}

// }
