//////////////////////////////////////
function criaXMLHttpRequest() {
var XMLHTTPREQUEST_IE = new Array(
  "Msxml2.XMLHTTP.7.0",
  "Msxml2.XMLHTTP.6.0",
  "Msxml2.XMLHTTP.5.0",
  "Msxml2.XMLHTTP.4.0",
  "MSXML2.XMLHTTP.3.0",
  "MSXML2.XMLHTTP",
  "Microsoft.XMLHTTP"
);
  var oXMLhttp = null;

  //Criar o objeto HttpRequest para o respectivo navegador.
  if (window.XMLHttpRequest != null)
    oXMLhttp = new window.XMLHttpRequest();
  else if (window.ActiveXObject != null)
  {
    /*Percorre no IE a procura do objeto ActiveX na biblioteca mais
      recente*/
    var bCriado = false;
    for (var ind = 0;
         ind < XMLHTTPREQUEST_IE.length && ! bCriado; ind++)		
    {
      try
      {
        oXMLhttp = new ActiveXObject(XMLHTTPREQUEST_IE[ind]);
        bCriado = true;
      }
      catch (ex)
      {}
    }
  }

  // Tratar erro caso no consiga instanciar o objeto.
  if (oXMLhttp == null)
    alert("Falha no HttpRequest():\n\n"
      + "Objeto XMLHttpRequest nao foi criado.");

  // Retornar o objeto instanciado
  return oXMLhttp;
}

function criaXMLDocument(){
   var XMLDOCUMENT_IE = new Array (
        "Msxml2.DOMDocument.6.0",
        "Msxml2.DOMDocument.5.0",
        "Msxml2.DOMDocument.4.0",
        "Msxml2.DOMDocument.3.0",
        "Msxml2.DOMDocument",
        "Microsoft.XmlDom");

    var oXMLdoc = null;
    
    //Criar o objeto DOMDocument para o respectivo navegador.
    if (window.ActiveXObject != null){
        
        for (var i = 0; i < XMLDOCUMENT_IE.length; i++) {
            try {                
                var oXMLdoc = new ActiveXObject(XMLDOCUMENT_IE[i]);
                return oXMLdoc;
            } catch (oErro) {
                 //No deve fazer nada
            }
         }
         
         //Mostrar mensagem de erro.    
         throw new Error("A pacote MSXML nao esta instalado.");       
    }            
    // Criao do XMLDocument no Mozilla
    else if (document.implementation && 
             document.implementation.createDocument){
        try {
            oXMLdoc = document.implementation.createDocument("","",
                                                             null);
            return oXMLdoc;
        } catch(oErro){
            oXMLdoc = null;
        	}        
         throw new Error("Erro ao criar XMLDocument no Mozilla.");       
    }      
}

	//Definir variveis para identificar o navegador
	var sUserAgent = navigator.userAgent;
	var fAppVersion = parseFloat(navigator.appVersion);
	var ehOpera = sUserAgent.indexOf("Opera") > -1;
	var ehIE = !ehOpera && sUserAgent.indexOf("compatible") > -1
	&& sUserAgent.indexOf("MSIE") > -1;

	//
	xmlhttp = criaXMLHttpRequest();


////////////////////////////////////////////////////////// limpa cache de ajax
function antiCacheRand(aurl){
	var dt = new Date();
	if(aurl.indexOf("?")>=0){ // j tem parametros
		return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime())
	} else { 
		return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());
	}
}

/*--------------------------------------- imprimir
-----------------------------------------------------------------*/
function Imprimir() {
	window.print();  
}

/*--------------------------------------- mascara de campos
-----------------------------------------------------------------*/
//OnKeyPress="FormatarCampo(this, '###.###.###-##')"
function FormatarCampo(src, mask) {
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
		src.value += texto.substring(0,1);
  }
}

/*--------------------------------------- processa pagina
-----------------------------------------------------------------*/
function ProcessaPagina(url, msg_ok, msg_erro) {

    //
	url = antiCacheRand(url);
    xmlhttp.open("GET", url);

    //
    xmlhttp.onreadystatechange = function() {

		//
		if (xmlhttp.readyState == 4)
			if (xmlhttp.status == 200)		
					alert(msg_ok);
		else
			alert(msg_erro);
		
    }

    //
    xmlhttp.send(null)
	
}

/*-------------------------------------------
--------------------------------------------------------*/
function AbrePagina(div, url) {

    //
	//alert(url);
	url = antiCacheRand(url);
    xmlhttp.open("GET", url);

    //
    xmlhttp.onreadystatechange = function() {

		if (xmlhttp.readyState == 1)
  			document.getElementById(div).innerHTML = '<table width="100%"><tr><td width="4%"><img src="imagem/i_carregando.gif" width="32" /></td><td width="96%"><strong>Carregando...</strong></td></tr></table>';

		//
		//alert(xmlhttp.responseText);
	
		if (xmlhttp.readyState == 4)
			if (xmlhttp.status == 200)
				document.getElementById(div).innerHTML = xmlhttp.responseText;
		
    }

    //
    xmlhttp.send(null)
	
}

/*-------------------------------------------
--------------------------------------------------------*/
//'<select name="id_sistema" onChange="MM_jumpMenu(\'parent\',this,0)">';
	function MM_jumpMenu(targ,selObj,restore){ //v3.0
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
	}


/*-------------------------------------------
--------------------------------------------------------*/
function DivVisivel(obj, valor) {
	
	with (document.getElementById(obj)) {	
		if (valor == '1')
			style.display == 'block';
		else
			style.display = 'none';
	}		
	
	
}

/*-------------------------------------------
--------------------------------------------------------*/
function TextareaLimite() {

	intCaracteres = 100 - document.getElementById('ds_servico').value.length;// form1.ds_servico.value.length;
	if (intCaracteres > 0) {
	  document.getElementById('nu_caracteres').value = intCaracteres;
	  return true;
	} else {
	  intMensagem = 100;
	  document.getElementById('nu_caracteres').value = 0;
	  document.getElementById('ds_servico').value = document.getElementById('ds_servico').value.substr(0, intMensagem)
	  return false;
	}

}

/*-------------------------------------------
--------------------------------------------------------*/
function Redireciona(obj) {
	
	//
	window.location.href = obj;
	
}


/*-------------------------------------------
--------------------------------------------------------*/
// javascript: selTudo('form1.textarea');
function selTudo(texto) {
	var tempval=eval("document."+texto)
	tempval.focus()
	tempval.select()
}
	
	
/*-------------------------------------------
--------------------------------------------------------*/
function mo(destino) {
	with (document.getElementById(destino)) {
		if (style.display == 'block') {//Se o item estiver visível...
			style.display = 'none';//Oculta o item.
		} else {
			style.display = 'block';//Se o item for invisível, mostra o ítem.
		}
	}
}

/*----------------------------------------------------------
------------------------------------------------------------*/
function Limpar(valor, validos) {

	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
	aux = validos.indexOf(valor.substring(i, i+1));
	if (aux>=0) {
	result += aux;
	}
	}
	return result;

}

/*--------- Formata número tipo moeda usando o evento onKeyDow
------------------------------------------------------------*/
function Formata(campo,tammax,teclapres,decimal) {

	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }
	
	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
	}
	if ( (tam >= 9) && (tam <= 11) ){
	campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
	campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	} 

}
