//---------------------------------------------------------------------------
// Objetivo...........: Encapsular funções de requisição XMLHTTP e
//						manipulação de XML
// Autor..............: Alan A. Oliveira
// Data...............: 12/04/2007
// Comentários........: 
// Alterado por.......: 
// Data...............: 
// Motivo da Alteração: 
//--------------------------------------------------------------------------
// Componentes usados.: Microsoft.XMLHTTP, Msxml2.XMLHTTP, Microsoft.XMLDOM
//						e DOMParser
// Entradas...........: 
// Retorno............: 
//---------------------------------------------------------------------------

var gobjXMLHTTP;	//Objeto Microsoft.xmlhttp
var gobjXMLDOM;		//Objeto XMLDOM

//--------------------------------------------------------------------------------
// Objetivo	 : Instanciar objeto XMLHTTP de acordo com o navegador
// Premissas : 
// Entradas	 : 
// Retorno	 : Objeto XMLHTTP utilizado pelo navegador em uso
//--------------------------------------------------------------------------------
function InstanciarXMLHTTP(){

	if (window.XMLHttpRequest){//Mozzila
		return (new XMLHttpRequest());
	}
	else if (window.ActiveXObject){//Internet Explorer
		try {
			return (new ActiveXObject("Msxml2.XMLHTTP"));
		}
		catch (objErro){
			try {
				return (new ActiveXObject("Microsoft.XMLHTTP"));
			}
			catch (objErro){
				alert("Computador sem suporte para XMLHTTP.\n" + objErro.message)
			}
		}
	}
}

//--------------------------------------------------------------------------------
// Objetivo  : Estabeler conexao XMLHTTP
// Premissas : 
// Entradas  : strURL - URL do servidor que receberá a solicitação
//			 : strMetodoEnvio - Forma de envio dos parâmetros ("get" ou "post")
//			 : intCodigoTipoRetorno - Código do tipo de retorno do servidor
//									  1 > Retorna literal
//									  2 > Retorna objeto XML
//			 : blnAssincrono - Comportamento após da solicitação ao servidor
//							   "true" > Continua execução do script
//							   "false" > Aguarda resposta do servidor para continuar execução do script
//			 : strFuncaoGerenciadora - Nome da função que receberá os dados retornados pelo servidor
// Retorno   : Literal / Objeto XML
//--------------------------------------------------------------------------------
function CarregarXMLHTTP(strURL, strMetodoEnvio, intCodigoTipoRetorno, blnAssincrono, strFuncaoGerenciadora, arrParametro){

	try {
		gobjXMLHTTP = new InstanciarXMLHTTP();		//Objeto Microsoft.xmlhttp

		if (gobjXMLHTTP){
			with (gobjXMLHTTP){
				if (strFuncaoGerenciadora.length > 0) gobjXMLHTTP.onreadystatechange = new Function("if (gobjXMLHTTP.readyState == 4){" + strFuncaoGerenciadora + "(gobjXMLHTTP." + ((intCodigoTipoRetorno == 1)?"responseText":"responseXML") + ")}");
				open(strMetodoEnvio, strURL, blnAssincrono);
				
				if (strMetodoEnvio.toUpperCase() == "POST"){
					setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
					setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
					setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
					setRequestHeader("Pragma", "no-cache");

					if (typeof(arrParametro)=="object"){
						send(MontarParametro(arrParametro));
					}
					else {
						send(arrParametro);
					}
				}
				else {
					send(null);
				}
				if (strFuncaoGerenciadora.length == 0) return ((intCodigoTipoRetorno == 1)?gobjXMLHTTP.responseText:gobjXMLHTTP.responseXML)
			}
		}
	}
	catch(objEx) {
	    alert(objEx.message)
	}
}
/*function CarregarXMLHTTP(strURL, strMetodoEnvio, intCodigoTipoRetorno, blnAssincrono, strFuncaoGerenciadora){

	gobjXMLHTTP = new InstanciarXMLHTTP();		//Objeto Microsoft.xmlhttp

	if (gobjXMLHTTP){
		with (gobjXMLHTTP){
			if (strFuncaoGerenciadora.length > 0) gobjXMLHTTP.onreadystatechange = new Function("if (gobjXMLHTTP.readyState == 4){" + strFuncaoGerenciadora + "(gobjXMLHTTP." + ((intCodigoTipoRetorno == 1)?"responseText":"responseXML") + ")}");
			open(strMetodoEnvio, strURL, blnAssincrono);
			send(null);
			if (strFuncaoGerenciadora.length == 0) return ((intCodigoTipoRetorno == 1)?gobjXMLHTTP.responseText:gobjXMLHTTP.responseXML)
		}
	}
}*/
//--------------------------------------------------------------------------------
// Objetivo	 : Montar Parâmetro form
// Premissas : 
// Entradas	 : objForm - objeto form 
// Retorno	 : 
//--------------------------------------------------------------------------------
function MontarParametro(objForm)
{
    var arrParametro = new String('')
    var intIaux  
    
    for(intIaux=0;intIaux<objForm.length;intIaux++)
    {
        if (objForm[intIaux].type=="text")
        arrParametro += objForm[intIaux].name + "="+ objForm[intIaux].value + "&"
    }
    
    return arrParametro 
}
//--------------------------------------------------------------------------------
// Objetivo  : Estabeler conexao XMLHTTP
// Premissas : 
// Entradas  : XML quando intCodigoTipoXML = 1 ou localização do XML quando intCodigoTipoXML = 2
//			 : intCodigoTipoXML - Tipo de XML que será carregado
//							   1 > XML literal (memória)
//							   2 > XML físico
//			 : blnAssincrono - Comportamento após da solicitação de carregamento do XML
//							   "true" > Continua execução do script
//							   "false" > Aguarda carregamento para continuar execução do script
//			 : strFuncaoGerenciadora - Nome da função que receberá o objeto XML carregado
//			 : blnXPath - Habilitar suporte a linguagem XPath
// Retorno   : Objeto XML
//--------------------------------------------------------------------------------
function CarregarXML(strXML, intCodigoTipoXML, blnAssincrono, strFuncaoGerenciadora, blnXPath){
	if (window.ActiveXObject){//Internet Explorer
		objXMLDOM = new ActiveXObject("Microsoft.XMLDOM");

		if (blnXPath) objXMLDOM.setProperty ("SelectionLanguage", "XPath");

		objXMLDOM.async = blnAssincrono;

		if (intCodigoTipoXML == 1) objXMLDOM.loadXML(strXML)
		else if (intCodigoTipoXML == 2) objXMLDOM.load(strXML)

		if (strFuncaoGerenciadora.length == 0) return (objXMLDOM)
		else eval(strFuncaoGerenciadora + "(objXMLDOM)");
	}
	else {//Mozzila
		if (intCodigoTipoXML == 1){alert("1 CarregarXML")
			objXMLDOM = new DOMParser();//Manipulador XML Mozzila
			objXMLDOM.parseFromString(strXML, "text/xml");
			//objXMLDOM.onload = new Function(strFuncaoGerenciadora + "(objXMLDOM)");
		}
		if (intCodigoTipoXML == 2){
			objXMLDOM = document.implementation.createDocument("","",null);
			objXMLDOM.load(strXML);

			if (strFuncaoGerenciadora.length > 0){
				objXMLDOM.onload = new Function(strFuncaoGerenciadora + "(objXMLDOM)");
			}
			else {
				objXMLDOM.onload = new Function("return (objXMLDOM)")
			}
		}
	}
}
