function goAjax(url, metodo, modo, tagRetorno, parametros) {
	//alert(tagRetorno);
	//alert(url+","+metodo+","+modo+","+tagRetorno)
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
		} else {   
			xmlhttp = new XMLHttpRequest();
			xmlhttp.open("POST", url, modo);
			alert(url);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 1) {            
            document.getElementById(tagRetorno).innerHTML = '<img src="../img/loading.gif">';
        }
        if(xmlhttp.readyState == 2) {
			document.getElementById(tagRetorno).innerHTML = '<img src="../img/loading.gif">';
        }
        if(xmlhttp.readyState == 3) {
			document.getElementById(tagRetorno).innerHTML = '<img src="../img/loading.gif">';
        }
        if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200){
                    retorno=xmlhttp.responseText;
					//alert(retorno);
					document.getElementById(tagRetorno).innerHTML=retorno;
					
				}
            }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}

function goAjax2(url, metodo, modo, tagRetorno, objForm) {
	
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
			
		} else {  
			var parametros = mountParameters(objForm);
			xmlhttp.open("POST", url, modo);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 1) {
				if(tagRetorno!=""){
					document.getElementById(tagRetorno).innerHTML = '<img src="imagens/fake_loader.gif">';
				}
	            
	        }
	        if(xmlhttp.readyState == 2) {
				if(tagRetorno!=""){
					document.getElementById(tagRetorno).innerHTML = '<img src="imagens/fake_loader.gif">';
				}
	        }
	        if(xmlhttp.readyState == 3) {
				if(tagRetorno!=""){
					document.getElementById(tagRetorno).innerHTML = '<img src="imagens/fake_loader.gif">';
				}
	        }
	        if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
	                retorno=xmlhttp.responseText;					
					if(tagRetorno!=""){
						document.getElementById(tagRetorno).innerHTML=retorno;
					}else{
						alert(retorno);
					}
				}
				else{
					var nErro = xmlhttp.status;
					alert('Erro '+nErro + ': Falha ao processar o serviço.');
				}
	        }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}

function goAjax3(url, metodo, modo, tagRetorno, callback_function) {
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
		} else {   
			xmlhttp = new XMLHttpRequest();
			xmlhttp.open("POST", url, modo);
			alert(url);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 1) {            
	            document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	        }
	        if(xmlhttp.readyState == 2) {
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	        }
	        if(xmlhttp.readyState == 3) {
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	        }
	        if(xmlhttp.readyState == 4) {
				if(xmlhttp.status == 200){
	                retorno=xmlhttp.responseText;
					document.getElementById(tagRetorno).innerHTML=retorno;
					if(callback_function!=""){
						eval(callback_function+"()");
					}
				}
				else{
					var nErro = xmlhttp.status;
					alert('Erro '+nErro + ': Falha ao processar o serviço.');
				}
	        }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}
function delServico(url, metodo, modo, tagRetorno, parametros) {
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
		} else {   
			xmlhttp = new XMLHttpRequest();
			xmlhttp.open("POST", url, modo);
			alert(url);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
      
        if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200){
                    retorno=xmlhttp.responseText;
					if(retorno=="erro"){
						alert('Erro ao deletar o Registro');
					}else{
						if(retorno=='unidConsumidora'){
							alert('Registro deletado!');
							window.location.reload();
						}else{
						alert('Registro deletado!');
						document.getElementById(tagRetorno).innerHTML = retorno;
						}
						
					}
					    
				}
            }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}
function cadServico(url, metodo, modo, tagRetorno, objForm) {
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
		} else { 
			var parametros = mountParameters(objForm);
			xmlhttp.open("POST", url, modo);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 1) {            
	        document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
	    if(xmlhttp.readyState == 2) {
			document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
	    if(xmlhttp.readyState == 3) {
			document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
        if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200){
                    retorno=xmlhttp.responseText;
					if(retorno=="erro"){
						alert('Erro ao cadastrar o Registro');
						document.getElementById(tagRetorno).innerHTML = "<FONT STYLE='COLOR:RED;'>Cadastro não realizado.</FONT>";
					}else{
						document.getElementById(tagRetorno).innerHTML = retorno;
						alert('Cadastro realizado com sucesso!');
						window.history.back();
						
					}
					    
			}
			else{
				var nErro = xmlhttp.status;
				alert('Erro '+nErro + ': Falha ao processar o serviço.');
			}
        }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}
function viewServico(url, callback_function, return_xml)
{
   var http_request = false;

   if (window.XMLHttpRequest)
   {
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
         http_request.overrideMimeType('text/xml');
      }
   }
   else if (window.ActiveXObject)
   {  // IE
      try
      { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e)
      {
         try
         { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e) {}
      }
   }

   if (!http_request)
   {
   	  alert("Este navegador não tem suporte para solicitar este serviço!");
      return false;
	  history.back(1);
   }

   http_request.onreadystatechange = function()
   { 
   		
      if (http_request.readyState == 4)
      {
         if (http_request.status == 200)
         {
            if (return_xml)
            { eval(callback_function + '(http_request.responseXML)'); }
            else
            { eval(callback_function + '(http_request.responseText)'); }
           
         }
         else
         { alert('Há um problema com a solicitação.(Código: ' + http_request.status + ')'); }
      }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}
function editServico(url, metodo, modo, tagRetorno, objForm) {
	if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { 
            try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
	   
		if(metodo == "GET") {
			xmlhttp.open("GET", url, modo);
		} else {   
			var parametros = mountParameters(objForm);
			xmlhttp.open("POST", url, modo);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			xmlhttp.setRequestHeader("Pragma", "no-cache");
        }    
        xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 1) {            
	        document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
	    if(xmlhttp.readyState == 2) {
			document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
	    if(xmlhttp.readyState == 3) {
			document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fake_loader.gif">';
	    }
        if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200){
                    retorno=xmlhttp.responseText;
					if(retorno=="erro"){
						alert('Erro ao atualizar o Registro');
						document.getElementById(tagRetorno).innerHTML = "<FONT STYLE='COLOR:RED;'>Atualização não realizada.</FONT>";
					}else{
						document.getElementById(tagRetorno).innerHTML = retorno;
						alert('Atualização realizada com sucesso!');
						window.history.back();
						
					}
					    
			}
			else{
				var nErro = xmlhttp.status;
				alert('Erro '+nErro + ': Falha ao processar o serviço.');
			}
        }
		}
		if(metodo == "GET") {
                xmlhttp.send(null);    
			} else {        
                xmlhttp.send(parametros);
        }
}
function validarContrato(url, tagRetorno, verificador)
{
   var http_request = false;

   if (window.XMLHttpRequest)
   {
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
         http_request.overrideMimeType('text/xml');
      }
   }
   else if (window.ActiveXObject)
   {  // IE
      try
      { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e)
      {
         try
         { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e) {}
      }
   }

   if (!http_request)
   {
   	  alert("Este navegador não tem suporte para solicitar este serviço!");
      return false;
	  history.back(1);
   }

   http_request.onreadystatechange = function()
   { 
   		
      if (http_request.readyState == 4)
      {
         if (http_request.status == 200)
         {
			var retorno = http_request.responseText;
			if(retorno=="01"){
				//alert('O contrato não existe ou é inválido!');
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fail.png" alt="Contrato inválido" title="Contrato inválido">';
				document.getElementById(verificador).value = '1';
			}else{
				if(retorno=="00"){
					document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/ok.png">';
					document.getElementById(verificador).value = '0';
				}else{
					alert('Erro ao verificar o contrato.');
				}
			
			}
            
           
         }
         else
         { alert('Há um problema com a solicitação.(Código: ' + http_request.status + ')'); }
      }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}
function validarContratoMedidor(url, tagRetorno, verificador)
{
   var http_request = false;

   if (window.XMLHttpRequest)
   {
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
         http_request.overrideMimeType('text/xml');
      }
   }
   else if (window.ActiveXObject)
   {  // IE
      try
      { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e)
      {
         try
         { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e) {}
      }
   }

   if (!http_request)
   {
   	  alert("Este navegador não tem suporte para solicitar este serviço!");
      return false;
	  history.back(1);
   }

   http_request.onreadystatechange = function()
   { 
   		
      if (http_request.readyState == 4)
      {
         if (http_request.status == 200)
         {
			var retorno = http_request.responseText;
			if(retorno=="01"){
				//alert('O contrato não existe ou é inválido!');
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fail.png" alt="Contrato inválido" title="Contrato inválido">';
				document.getElementById(verificador).value = '1';
			}
			if(retorno=="02"){
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/fail.png"alt="Medidor não associado ao contrato" title="Medidor não associado ao contrato">';
				document.getElementById(verificador).value = '0';
			}
			if(retorno=="00"){
				document.getElementById(tagRetorno).innerHTML = '<img src="./sistemas/sac/imagens/ok.png">';
				document.getElementById(verificador).value = '0';
			}
			
         }
         else
         { alert('Há um problema com a solicitação.(Código: ' + http_request.status + ')'); }
      }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}
function verificaContratoJaCadastrado(url)
{
   var http_request = false;

   if (window.XMLHttpRequest)
   {
      // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType)
      {
         http_request.overrideMimeType('text/xml');
      }
   }
   else if (window.ActiveXObject)
   {  // IE
      try
      { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e)
      {
         try
         { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
         catch (e) {}
      }
   }

   if (!http_request)
   {
   	  alert("Este navegador não tem suporte para solicitar este serviço!");
      return false;
	  history.back(1);
   }

   http_request.onreadystatechange = function()
   { 
   		
      if (http_request.readyState == 4)
      {
         if (http_request.status == 200)
         {
			var retorno = http_request.responseText;
			if(retorno=="01"){
				alert('Já existe cadastro de avaliação para o CONTRATO fornecido.');
				return false;
			}
			if(retorno=="02"){
				alert('Erro ao verificar cadastro de contrato.');
				return false;
			}			
			if(retorno=="00"){
				isNull2("formUnidConsumidora","cadAvalUnidConsumidora");// verificação do formulário se contem valores nulos
				
			}
			
         }
         else
         { alert('Há um problema com a solicitação.(Código: ' + http_request.status + ')'); }
      }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}
function ParseTag(xml, tag)
{
  var xtag = "<" + tag + ">(.+?)</" + tag + ">"
  var re = new RegExp(xtag);
  var m = re.exec(xml);

  if (m == null) { s = ""; }
  else
  { s = m[1]; }
  return (s);
}

function mountParameters(objForm){

	var strReturn = "";

	if(objForm){

		var y = objForm.length-1;
		for(x = 0; x < objForm.length; x++){
			if(objForm[x].value && !objForm[x].disabled && objForm[x].style.display != "none" && objForm[x].type != "button" && objForm[x].type != "reset" && objForm[x].type != "submit"){
				if(strReturn){
					strReturn = strReturn + "&";
				}
				strReturn = strReturn + objForm[x].name + "=" + escape(objForm[x].value);
			}
		}

	}

	return strReturn;
}