/**
 * @author Rodrigo
*/

/**
 * Constructor of class AJAX
 * 
 * @param {string} dirRoot
 */
function AJAX(dirRoot){
	this.urlRequest = "";
	Ajax = false;
	this.dirRoot = dirRoot; // directory base of your AJAX aplication
}

/**
 * Creates AJAX Object for any browser
 * @return
 */
function createAjax(){
	if(window.XMLHttpRequest){
		Ajax = new XMLHttpRequest();	
	}else if(window.ActiveXObject){
		try{
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){}
		}
	}
}

/**
 * Sends a request for a script PHP
 * 
 * @param {string} urlRequest
 * @param {string} method
 * @return
 */
function sendDatasForPHP(urlRequest,method){
	this.createAjax();
	if(!Ajax){
		alert("Nao foi possivel criar o objeto AJAX");
		return false;
	}
	pleaseWait();
	Ajax.onreadystatechange = getDatasReturnedOfPHP;
	this.urlRequest = this.dirRoot + urlRequest;
	//method = "'" + method + "'";
	Ajax.open(method ,this.urlRequest,true);
	Ajax.send(null);
}

/**
 * Change the standard "getDatasReturnedOfPHP" function for your needle function
 * @param {Function} nameOfFunction
 */
function changeGetDatasReturnedOfPHP(nameOfFunction){
	putDatasOfPHP = nameOfFunction;
}

/**
 * Displays a DIV with message for wait
 */
function pleaseWait(){
	if(!document.getElementById("pleaseWait")){
		var myDiv = document.createElement("div");
		
		var width = 100;
		var height = 100;
		
		myDiv.setAttribute("id","pleaseWait");
		myDiv.style.position = "absolute";
		myDiv.style.left = '0px';
		myDiv.style.top = '0px';
		myDiv.style.border = '1px double #CCCCCC';
		myDiv.style.width = width + 'px';
		myDiv.style.height = height + 'px';
		
		if(getAgent() == "Netscape"){
			docWidth = window.innerWidth;
			docHeight = window.innerHeight;
		}else{
			docWidth = window.screen.availWidth;
			docHeight = window.screen.availHeight;
		}
		
		docWidth = parseInt((docWidth - width) / 2);
		docHeight = parseInt((docHeight - height) / 2);
		myDiv.style.left = String(docWidth + "px");
		myDiv.style.top = String(docHeight + "px");
		
		myDiv.style.background = 'white';
		myDiv.style.zIndex = 9999;
		myDiv.innerHTML = '<p align="center">Por favor aguarde...</p><p align="center"><img src="/images/waiting.gif" /></p>';
		document.getElementsByTagName("body")[0].appendChild(myDiv);
	}
}

function closePleaseWait(){
	var myDiv = document.getElementById("pleaseWait");
	
	document.body.removeChild(myDiv);
	
}

/**
 * This is empty function that receives your customize "getDatasOfPHP" function for work within AJAX
 */
//function putDatasOfPHP(){}

/**
 * This function do work of "onreadystatechange" it overrides him
 */
function getDatasReturnedOfPHP(){
	var teste = document;
	if(Ajax.readyState == 4){//request was loaded
		if(Ajax.status == 200){//the page was loaded successful
		
			/**
			 * Executes a final function that must be show result
			 */
			closePleaseWait();
			eval(putDatasOfPHP + '()');
			
		}else{
			alert('Erro no retorno do Servidor: ' + Ajax.statusText);
		}
	}else{
		pleaseWait();
	}
}

/**
 * 
 * @param {Array} params
 * @return {string}
 */
function assemblesParamsInString(params){
	var allParams = "";
	var first = true;
	for (i in params)	//sprinting array
	{
		if(!first)
			allParams += "&";
		else
			first = false;
			
		allParams += i + "=" +params[i];
	}
	return allParams;
}

AJAX.prototype.createAjax = createAjax;
AJAX.prototype.sendDatasForPHP = sendDatasForPHP;
AJAX.prototype.pleaseWait = pleaseWait;
AJAX.prototype.changeGetDatasReturnedOfPHP = changeGetDatasReturnedOfPHP;
AJAX.prototype.getDatasReturnedOfPHP = getDatasReturnedOfPHP;
AJAX.prototype.assemblesParamsInString = assemblesParamsInString;
