/* ----------------------------------------------------------------------------------- \\
 * Classe XMLHttp v0.1
 * 
 * Por: Bruno Soares [ BSoares.com.br ]
 * Data: 25/09/2006
 * Descrição: Chamada de Procedimento Remoto [ RPC | "Ajax" ]
 * ----------------------------------------------------------------------------------- \\
*/
// ----------------------------------------------------------------------------------- \\
// Construtores
//
function XMLHttp () {
	//
	// Propriedades
	this.objXMLHTTP = null;
	this.booASC     = true;
	this.strURL     = '';
	this.strQuery   = '';
	this.strMethod  = '';
	//
	// Dados
	this.strNavigator = navigator.userAgent.toUpperCase();
	this.isIE  = ( this.strNavigator.indexOf( 'MSIE'   ) != -1 );
	this.isIE5 = ( this.strNavigator.indexOf( 'MSIE 5' ) != -1 );
}
// ----------------------------------------------------------------------------------- \\
// Métodos de acesso
//
XMLHttp.prototype.setXMLHTTP = function ( objXMLHTTP ) {
	this.objXMLHTTP = objXMLHTTP;
}
XMLHttp.prototype.getXMLHTTP = function () {
	return ( this.objXMLHTTP );
}
//
XMLHttp.prototype.setQuery = function ( strQuery ) {
	this.strQuery = strQuery;
}
XMLHttp.prototype.getQuery = function () {
	return ( this.strQuery );
}
//
XMLHttp.prototype.setMethod = function ( strMethod ) {
	this.strMethod = strMethod.toUpperCase();
}
XMLHttp.prototype.getMethod = function () {
	return ( this.strMethod );
}
//
XMLHttp.prototype.setURL = function ( strURL ) {
	this.strURL = strURL;
}
XMLHttp.prototype.getURL = function () {
	return ( this.strURL );
}
//
XMLHttp.prototype.setASC = function ( booASC ) {
	this.booASC = booASC;
}
XMLHttp.prototype.getASC = function () {
	return ( this.booASC );
}
//
// ----------------------------------------------------------------------------------- \\
/* criaCarregando ()
 * 
 * return void
*/
XMLHttp.prototype.criaCarregando = function () {
	//
	// Carregando
	if ( document.getElementById( "objCarregando" ) == null ) {
		var txtCarregando = document.createTextNode( "CARREGANDO" );
		var objDiv        = document.createElement ( "div" );
		objDiv.appendChild( txtCarregando );
		objDiv.style.position   = this.isIE ? "absolute" : "fixed";
		objDiv.style.padding    = "4px";
		objDiv.style.margin     = "0";
		objDiv.style.top        = "0px";
		objDiv.style.right      = "10px";
		objDiv.style.background = "#900";
		objDiv.style.color      = "#FFF";
		objDiv.style.visibility = "hidden";
		objDiv.style.fontWeight = "bold";
		objDiv.style.border     = "1px dotted #FFF";
		objDiv.setAttribute( "id", "objCarregando" );
		var objBody = document.getElementsByTagName( "body" ).item( 0 );
		if ( objBody != null ) {
			objBody.appendChild( objDiv );
		}
		objDiv = null;
	}
}
//
// ----------------------------------------------------------------------------------- \\
/* getXmlhttpObject ()
 * 
 * @_Function_ : function - funcionalidade a ser chamada nos eventos do XMLHttpRequest
 * 
 * return Object
*/
XMLHttp.prototype.getXmlhttpObject = function ( _Function_ ) {
	var objXmlHttp = null;
	try {
		if ( this.isIE ) {
			var strAssinaturaClass = ( this.isIE5 ) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
			objXmlHttp = new ActiveXObject( strAssinaturaClass );
			objXmlHttp.onreadystatechange = _Function_;
		} else {
			objXmlHttp = new XMLHttpRequest();
			objXmlHttp.onload  = _Function_;
			objXmlHttp.onerror = _Function_;
			if ( objXmlHttp.overrideMimeType ) {
				objXmlHttp.overrideMimeType( "text/xml" );
			} else {
				objXmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
				objXmlHttp.onreadystatechange = _Function_;
			}
		}
	}
	catch ( e ) {
		alert (
			"Erro ao criar 'XML HTTP Request'!\nMensagem: " + e.message
			+ "\nNúmero: " + e.number
		);
	}
	return ( objXmlHttp );
}
//
// ----------------------------------------------------------------------------------- \\
/* run ()
 * 
 * @_Function_ : function - funcionalidade a ser chamada nos eventos do XMLHttpRequest
 * 
 * return void
*/
XMLHttp.prototype.run = function ( _Function_ ) {
	this.objXMLHTTP = this.getXmlhttpObject( _Function_ );
	if ( this.objXMLHTTP != null ) {
		this.objXMLHTTP.open(
			this.strMethod,
			( this.strMethod == "POST" ) ? this.strURL : ( this.strURL + '?' + this.strQuery ),
			this.booASC
		);
		this.objXMLHTTP.setRequestHeader( "Cache-Control", "no-store, no-cache, must-revalidate");
		this.objXMLHTTP.setRequestHeader( "Cache-Control", "post-check=0, pre-check=0");
		this.objXMLHTTP.setRequestHeader( "Pragma", "no-cache");
		if ( this.strMethod == "POST" ) {
			this.objXMLHTTP.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1" );
		}
		this.objXMLHTTP.send( this.strQuery );
	}
}
//
// ----------------------------------------------------------------------------------- \\
/* criaQuery ()
 * 
 * @objForm : Object - Form
 * 
 * return String
*/
XMLHttp.prototype.criaQueryPorFrom = function ( objForm ) {
	var strSaida = '';
	for ( var i = 0; i < objForm.length; i++ ) {
		var item = objForm[ i ];
		switch ( item.type.toLowerCase() ) {
		case "submit":
		case "button":
		case "file":
		case "reset":
		break;
		case "checkbox":
		case "radio":
			if ( item.checked ) {
				strSaida += item.name + '=' + escape( item.value ) + '&';
			}
		break;
		default:
			strSaida += item.name + '=' + escape( item.value ) + '&';
		}
	}
	return ( strSaida.substr( 0, strSaida.length - 1 ) );
}
//
// ----------------------------------------------------------------------------------- \\
/* criaESetQueryPorForm ()
 * 
 * @objForm : Object - Form
 * 
 * return void
*/
XMLHttp.prototype.criaESetQueryPorForm = function ( objForm ) {
	this.setQuery( this.criaQueryPorFrom( objForm ) );
}
//
// ----------------------------------------------------------------------------------- \\
/* getParametro ()
 * 
 * @strValor       : string - Texto para ser tratado
 * @intComprimento : int    - Comprimento maxímo da string a ser retornada
 * 
 * return void
*/
XMLHttp.prototype.getParametro = function ( strValor, intComprimento ) {
	strValor = unescape( strValor );
	strValor = strValor.replace( /\+/g, ' ' );
	strValor = strValor.replace( /\$\-+M+A+I+S+\-\$/g, '+' );
	if ( intComprimento > 0 ) {
		strValor = strValor.substr( 0, intComprimento );
	}
	return ( strValor );
}
//
// ----------------------------------------------------------------------------------- \\
/* getNodeValue ()
 * 
 * @objNo : Object - Nó de um XML
 * 
 * return String
*/
XMLHttp.prototype.getNodeValue = function ( objNo ) {
	if ( objNo.firstChild == null ) {
		return ( '' );
	} else {
		return ( this.getParametro( objNo.firstChild.nodeValue ) );
	}
}
//
// ----------------------------------------------------------------------------------- \\
/* setStatusCarregando ()
 * 
 * @intStatus : int
 * 
 * return void
*/
XMLHttp.prototype.setStatusCarregando = function ( intStatus ) {
	var strVisibility = '';
	switch ( intStatus ) {
		case 0 :
			strVisibility = 'hidden';
		break;
		case 1 :
			strVisibility = 'visible';
		break;
		default:
			strVisibility = 'hidden';
	}
	if ( document.getElementById( "objCarregando" ) == null ) {
		this.criaCarregando();
	}
	document.getElementById( "objCarregando" ).style.visibility = strVisibility;
}
//
// ----------------------------------------------------------------------------------- \\