// JavaScript Document

///////////////////////////////////////////////////////////////////
//   Objeto para criar campos ocultos ao abrir uma nova janela   //
//     Exemplo de uso                                            //
//          var Objetos = createOculto();                        //
//          Objetos.add('Id_Pedido', 2);                         //
//          Objetos.add('Id_Pedido_Fechado', 5);                 //
//			Objetos.onchange = function() {                      //
//				alert(Objetos.value);                            //
//			}                                                    //
///////////////////////////////////////////////////////////////////
function createOcultoM() {
	var TOculto = function() {
		this.itens = new Array();
		this.name = 'TOculto';
		this.value = '';
	}
	
	TOculto.prototype.add = function(Campo, Valor) {
		this.itens[this.itens.length] = new Object();
		this.itens[this.itens.length-1].Campo = Campo;
		this.itens[this.itens.length-1].Valor = Valor;
	}
	
	TOculto.prototype.count = function() {
		return(this.itens.length);
	}
	
	TOculto.prototype.onchange = '';

	return(new TOculto());
}


// A função abaixo pega a versão mais nova do xmlhttp do IE e verifica se é Firefox. Funciona nos dois.
function createXMLHTTPM() {
  try {
    ajax = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e) {
	  try {
		  ajax = new ActiveXObject("Msxml2.XMLHTTP");
		  alert(ajax);
	  }
  	  catch(ex) {
		  try {
			  ajax = new XMLHttpRequest();
		  }
		  catch(exc) {
			  alert("Esse browser não tem recursos para uso do Ajax");
			  ajax = null;
		  }
	  }
	  return ajax;
  }
  var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
  for (var i=0; i < arrSignatures.length; i++) {
	try {
		var oRequest = new ActiveXObject(arrSignatures[i]);
		return oRequest;
	} 
	catch (oError) { }
  }           
  throw new Error("MSXML não está instalado em seu sistema.");
}

function AjaxM(Path, Objeto, tipo) {
	var oHTTPRequest = createXMLHTTPM();
	var oStringSend = "";
	if (tipo.length == 0)
		tipo = 'get';
	if (tipo.toLowerCase() == 'post') {
		oHTTPRequest.open("post", Path, false);
		oHTTPRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	} else {
		oHTTPRequest.open("get", Path, false);
	}
  	oHTTPRequest.onreadystatechange=function() {
    	if (oHTTPRequest.readyState==4) {
			var resultado = oHTTPRequest.responseText;
            resultado = resultado.replace(/\+/g,' ');
			resultado = unescape(resultado); 
			Objeto.value = resultado;
			if (typeof(Objeto.onchange) == 'function') {
				Objeto.onchange();
			}
      	}
  	}
	if (typeof(Objeto) == 'object') {
		if (typeof(Objeto.name) == 'string') {
			if (Objeto.name == 'TOculto') {
				for(i=0;i<=Objeto.count()-1;i++) {
					//alert(Objeto.itens[i].Campo + ' = '+Objeto.itens[i].Valor);
					if (oStringSend.length == 0) {
						if ( (Objeto.itens[i].Campo != '') && (Objeto.itens[i].Valor != '') )
							if (Objeto.itens[i].Valor.toString().search('&'))
								oStringSend = Objeto.itens[i].Campo + '=' + escape(Objeto.itens[i].Valor);
							else
								oStringSend = Objeto.itens[i].Campo + '=' + Objeto.itens[i].Valor;
					} else {
						if ( (Objeto.itens[i].Campo != '') && (Objeto.itens[i].Valor != '') )
							if (Objeto.itens[i].Valor.toString().search('&'))
								oStringSend += '&' + Objeto.itens[i].Campo + '=' + escape(Objeto.itens[i].Valor);
							else
								oStringSend += '&' + Objeto.itens[i].Campo + '=' + Objeto.itens[i].Valor;
					}
				}
			}
		}
	}
	oHTTPRequest.send(oStringSend);
}
