/**
Classe ajax
@author Caio Meriguetti Costa
@version 0.9
@revisao e adaptacao Edivaldo Merlo Stens
*/

function ajax(){
	this.ORequest = this.getRequestObject();
}
/**
verifica qual o browser do cliente
@return Object
*/
ajax.prototype.getRequestObject = function(){
	try{
		// if Mozilla, Safari etc
		if (window.XMLHttpRequest){
			return new XMLHttpRequest();
			// if IE
		} else if (window.ActiveXObject){
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e1){
				try{
					return new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e2){
					alert("(IE) - " +e2.message);
					return null;
				}
			}
		} else{
			return null;
		}
	}
	catch (e){
		alert("(Mozilla) - " +e.message);
		return null;
	}
}
/**
Handlers
*/
ajax.prototype.onLoad;
ajax.prototype.onError;
ajax.prototype.onLoading;
ajax.prototype.onLoaded;
ajax.prototype.onInteractive;
/**
Pega as propriedades em um array
*/
ajax.prototype.getProps = function(){
	var str = "";
	for(var i in this){
		var tipo = typeof(this[i]);
		if(tipo == "string" || tipo == "number"){
			str += i+"="+this[i]+"&";
		};
	}
	return str;
}
/**
Carrega
@param arquivo String
@param metodo Strings
* 0 - nao inicializado (uninitalized)
* 1 - carregando (loading)
* 2 - carregado (loaded)
* 3 - interativo (interactive)
* 4 - completo (complete)
*/
ajax.prototype.load = function(arquivo, parameters, metodo, mimeType){
	var container = this;

	// Enable privileges
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	} catch (e) {
		//alert("Permission UniversalBrowserRead denied.");
	}

	this.ORequest.onreadystatechange = function(){
		var req = container.ORequest;
		//alert(arquivo+' : '+req.readyState+' : '+req.status);
		switch(req.readyState){
			case 0:
			//nao inicializado
			container.onLoading(req);
			break;
			case 1:
			//carregando
			container.onLoading(req);
			break;
			case 2:
			//carregado
			//container.onLoaded(req);
			container.onLoading(req);
			break;
			case 3:
			//interativo
			//container.onInteractive(req);
			container.onLoading(req);
			break;
			case 4:
			//concluído
			if(req.status == 200 || req.status == 0){
				//ok
				container.onLoad(req);
			} else{
				//ocorreu um erro
				container.onError(req.status,req.statusText);
			}
			break;
			default:
			container.onError(req.status,req.statusText);
			break;
		}
	}
	//arquivo += "?"+this.getProps();
	//alert(arquivo.length);
	this.ORequest.open(metodo, arquivo, true);
	this.ORequest.setRequestHeader('Content-Type',mimeType);
	if (this.ORequest.overrideMimeType) {
		this.ORequest.overrideMimeType(mimeType);
	}

	this.ORequest.setRequestHeader('encoding','iso-8859-1');
	if (metodo == "POST") {
		this.ORequest.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
		this.ORequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	}
	this.ORequest.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	this.ORequest.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	this.ORequest.setRequestHeader("Pragma", "no-cache");

	if(parameters){
		this.ORequest.setRequestHeader("Content-length", parameters.length);
	}
	this.ORequest.setRequestHeader("Connection", "close");
	this.ORequest.send(parameters);
}

/**
Classe showLoading
@author Edivaldo Merlo Stens
@version 1.0
@date 15 dez 2006
*/

function showLoading(objWhere){
	this.start(objWhere);
}
/**
Inicia o loading
@return  void
*/
showLoading.prototype.start = function(objWhere){
	this.objWhere = objWhere;
	//this.objWhere.innerHTML+='<div id=\'carregando\' style=\'display:block;\'></div>';
	this.objLoad = document.getElementById('carregando');

	this.objLoad.style.width=(document.body.clientWidth+10)+'px';
	this.objLoad.style.height=(document.body.clientHeight+10)+'px';
	this.objLoad.style.display='block';
	// se o selectOff ja estava, deixa como esta no hide()
	this.selectOff = (document.body.className=='selectOff');
	document.body.className='selectOff';
}

/**
Inicia o loading
@return  void
*/
showLoading.prototype.showCancel = function(req){
	var aobjLoad = this.objLoad;
	function cancel(){
		req.abort();
		aobjLoad.style.display='none';
	}
	try{
		this.objLoad.innerHTML = '<input type="button" value="cancelar" id="bt_cancel">';
		document.getElementById('bt_cancel').onclick=cancel;
	} catch(e){}
}
/**
Cancela o loading
@return void
*/
showLoading.prototype.hide = function(){
	this.objLoad.style.display='none';
	(!this.selectOff?document.body.className='selectOn':null);

}

