//////////////////////////////////////////////////////////////////////////
//SOLICITACAO DO COMPONENTE DO BROWSER (AJAX)
//////////////////////////////////////////////////////////////////////////
function openXmlHttp() {
	var Ajax;
	try {
		Ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers mais populares, como: Firefox, Safari, dentre outros.
	}catch(ee) {
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
		}catch(e) {
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
			}catch(e) {
				Ajax = false;
				}
		}
	}
	return Ajax;
}
//////////////////////////////////////////////////////////////////////////
//XHTMLHTTP ASSÍNCRONO
//////////////////////////////////////////////////////////////////////////
function loadAjax(pObjeto, pPagina, pTextoCarregando, pValores) {
	if (document.getElementsByName('btSalvardados'))
		document.getElementsByName('btSalvardados').disabled=true;

	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
			var exibeResultado = document.getElementById(pObjeto); // div que exibirá o resultado.
			var Ajax = openXmlHttp(); // Inicia o Ajax.
			Ajax.open("POST", pPagina, true); // fazendo a requisição
			Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			Ajax.setRequestHeader("Content-length", pValores.length);
			Ajax.setRequestHeader("Connection", "close");

			Ajax.onreadystatechange = function() {
				if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
						exibeResultado.innerHTML = pTextoCarregando;
				}
				if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
					if(Ajax.status == 200) {	
						var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa variável
						exibeResultado.innerHTML = resultado;
						document.getElementById("txtMensagem").value='';
						document.getElementById("btEnviar").disabled=false;
					}
				}
			}
			
			Ajax.send(pValores);
	}
}
//////////////////////////////////////////////////////////////////////////
//XHTMLHTTP SÍNCRONO
//////////////////////////////////////////////////////////////////////////
function syncCall(pPagina, pValores) {
  document.getElementById("divLoad").style.display='block';
  var objHttp = null;

  if ( window.XMLHttpRequest )
  { objHttp = new XMLHttpRequest(); }

  else if ( window.ActiveXObject )
  { objHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); }

  if ( objHttp == null )
  { alert( "Seu navegador não possui suporte à AJAX" ); }

  objHttp.open("POST", pPagina, false); // fazendo a requisição
  objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  objHttp.setRequestHeader("Content-length", pValores.length);
  objHttp.setRequestHeader("Connection", "close");
  objHttp.send(pValores);

  if ( objHttp.readyState == 4 || objHttp.readyState == "complete" ) {
    if ( objHttp.statusText == 200 || objHttp.statusText == "OK" ) {
		document.getElementById("divLoad").style.display='none';
		if (objHttp.responseText.substring(0, 2) == "OK")
			return objHttp.responseText;
		else {
			alert(objHttp.responseText);
			return objHttp.responseText;
		}
    } else {
      alert( "Erro na resposta do servidor" );
      return false;
    }
  }
}
//////////////////////////////////////////////////////////////////////////
//CONCATENA PARAMETROS
//////////////////////////////////////////////////////////////////////////
function createParams(pForm) {
	var strParametros = '';
	for (var i=0; i < document.forms[pForm].elements.length; i++) {
		//SE O OBJETO FOR UM CHECKBOX APENAS CONSIDERAR OS CHECKADOS
		if (document.forms[pForm].elements[i].type=='checkbox') {
			if (document.forms[pForm].elements[i].checked==true)
				strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
			}
		else if (document.forms[pForm].elements[i].type=='radio') {
			if (document.forms[pForm].elements[i].checked==true)
				strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
			}			
		else
			strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
	}
	strParametros = strParametros.substring(1, strParametros.length);
	return strParametros;
}
