if (navigator.appName.indexOf('Microsoft') != -1){
 	clientNavigator = "IE";
 }else{
 	clientNavigator = "Other";
}
 
function Id(id) {
  return document.getElementById(id);
} 

function jAbreMenu(nome) {
  if (nome != '') {
    if (Id(nome).style.display == 'none') {
      Id(nome).style.display = '';      
    }else{
      Id(nome).style.display = 'none';
    }
  }
}


function jMostraSenha(nome) {
  oculto = nome+'senhaOculta';
  asterisco = nome+'senhaAsterisco';
  link = nome+'linkSenha';
  if (nome != '') {
    if (Id(oculto).style.display == 'none') {
      Id(oculto).style.display = '';      
      Id(asterisco).style.display = 'none';
      Id(link).innerHTML = 'ocultar senha';
    }else{
      Id(oculto).style.display = 'none';
      Id(asterisco).style.display = '';
      Id(link).innerHTML = 'ver senha';
    }
  }
}


function jEnviarSenha() {
  if (Id('acao_login').value == 'Entrar') {
    Id('acao_login').value = 'Enviar senha';
    Id('msg-senha').style.display = '';
    Id('id-senha').value = '';
  }else{
    Id('acao_login').value = 'Entrar';
    Id('msg-senha').style.display = 'none';
  }
}

function jAbreTab(id,titulo,url,fechar,cor) {
  if (AjaxTabs.TabExists(id)==true) {
    AjaxTabs.FocusTab(id);
    AjaxTabs.SetTabURL(id,url);
  }else{
    AjaxTabs.OpenTab(id,titulo,url,fechar,cor);
  }
}

function jAbreJanela(url) {
	janela=window.open(url,"","resizable=yes,scrollbars=yes,location=yes,menubar=yes,toolbar=yes");
	//interceptacao de erro na abertura da janela
	text = "Grrr! Se a janela não abre, talvez você tenha um bloqueador de pop-ups!";
	if(janela == null) { alert(text); return false; }
}

function jAjustaiframe(iframe,opcao)
{
  if (iframe) {
    if (clientNavigator == "IE") // IE
    {
      if (iframe.height < iframe.contentWindow.document.body.scrollHeight) {
        iframe.height = iframe.contentWindow.document.body.scrollHeight + 70;
        
        if (iframe.height < 360)
          iframe.height = 360;
      }
      //iframe.width = iframe.contentWindow.document.body.scrollWidth;
      if (iframe.contentWindow.document.body.scrollWidth <= 810){
        iframe.style.width = "810px";
      }else{
        iframe.style.width = "100%";
      }
    }
    else
    if ( iframe.contentDocument ) // firefox
    {
      if (iframe.height < iframe.contentDocument.height){
        iframe.height = iframe.contentDocument.height + 60;
        
        if (iframe.height < 360)
          iframe.height = 360;
      }

      if (iframe.parentNode.clientWidth <= 810){
        iframe.width = 810;
      }else{
        iframe.width = "100%";
      }
    }
    
    if (opcao == 'load'){
        window.scrollTo(0,0);
    }
  }
}

function jAtualizaJanela(){
  window.location.reload();
  if (clientNavigator == "IE"){
    window.scrollTo(0,0);
  }
}

vcorantiga  ="#FFFFFF";
vcorselecao ="#F0E3DD";

function destaca_linha_over(linha) {
   vcorantiga   = linha.bgColor;
   linha.bgColor = vcorselecao;
}
function destaca_linha_out(linha) {
   linha.bgColor = vcorantiga;
}

function jAbre_FTP(campo_destino,caminho,retira) {
	top.tb_show('Gerenciador de Arquivos',"FTP/index.php?destino="+campo_destino+"&caminho="+caminho+"&retira="+retira+"TB_iframe=true&width=800&height=450");
  //janela=window.open("FTP/index.php?destino="+campo_destino+"&caminho="+caminho+"&retira="+retira,"","width=800,height=450,scrollbars=yes")
	//interceptacao de erro na abertura da janela
	//text = "Se a janela nao estava abrindo\ntalvez seja porque voce tenha um\nprograma bloqueador de pop-up!\nObservacao » O windows XP service pack 2\nbloqueia pop-ups!";
	//if(janela == null) { alert(text); return; }
	//fim
	//janela.moveTo(100,50);
}

function jAbre_Mapa(campo_destino,localizacao) {
	//janela=window.open("mapa.php?destino="+campo_destino+"&localizacao="+localizacao,"","width=800,height=450,scrollbars=yes")
	
	top.tb_show('Localização Geográfica',"mapa.php?destino="+campo_destino+"&localizacao="+localizacao+"TB_iframe=true&width=720&height=450");

	//interceptacao de erro na abertura da janela
	//text = "Se a janela nao estava abrindo\ntalvez seja porque voce tenha um\nprograma bloqueador de pop-up!\nObservacao » O windows XP service pack 2\nbloqueia pop-ups!";
	//if(janela == null) { alert(text); return; }
	//fim
	//janela.moveTo(100,50);
}

function Bloqueia_Caracteres(evnt, decimal) {
 //Função permite digitação de números, virgula e sinal
  if (decimal == ''){
		decimal = 0;
	}
  if (clientNavigator == "IE"){
 		if (evnt.keyCode < 48 || evnt.keyCode > 57){
			if (decimal) {
				if (evnt.keyCode != 44 && evnt.keyCode != 45){
   			   return false;
			  }else{
				   return true;
				}
			}else{
			  return false;	
			}
 		}else{
			return true;
		}
 	}else{
 		if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0){
			if (decimal) {
				if (evnt.charCode != 44 && evnt.charCode != 45){
   			   return false;
			  }else{
				   return true;
				}
			}else{
			  return false;	
			}
 		}else{
			return true;
		}
 	}
}
 
function Ajusta_Mascara(input, event, mascara){
  
  //var keyCode = (window.Event) ? event.which : event.keyCode;
  
  // Enter, backspace, delete e setas direcionais
 //if ((keyCode == 13) || (keyCode == 0) || (keyCode == 8)) {
 // return true;
 //}
 
  // Previne teclas especiais como delete, tab, enter
  
  if (clientNavigator == "IE"){
    var rcode = event.keyCode
  }else{
    if (event.keyCode == 0)
      var rcode = event.charCode
    else
      var rcode = event.keyCode;  
 	}
  
  // Enter - 13, Tab - 9, backspace, delete - 46 e setas direcionais (37,38,39,40)
  if ((rcode == 13) ||(rcode == 9) ||(rcode == 0) || (rcode == 46) || (rcode == 8) || (rcode >= 37 && rcode <= 40)) {
   return true;
  }
 	
 	//if (keyCode <= 29){
  //  return true;
  //}
  
  var posicao = input.value.length;
  var atual = mascara.charAt(posicao);
  
  
 	while ((atual != '#') && (atual != 'A') && (atual != '9') && (atual != '')) {
 		/*if(clientNavigator == "IE"){
      input.value += atual;
 		}else{
      if(event.keyCode == 0){
 				input.value += atual;
 			}
 			else 
 			  break;
 		}*/
 		input.value += atual;
 		posicao = input.value.length;
    atual = mascara.charAt(posicao);
 	}
  
  if ((atual == '#') && ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))) {
    return true;  
  }
  else
  if ((atual == 'A') && ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))) {
    return true;
  }
  else
  if ((atual == '9') && (keyCode >= 48 && keyCode <= 57)) {
    return true;
  }
  else
    return false;
    
  //Chama a função Bloqueia_Caracteres para só permitir a digitação de números
 	//return Bloqueia_Caracteres(evnt);
}

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;
      }

 }//fim de for

return result;

}//fim de function

//Utilize esta função no evento onkeydown do input
//Ex.: <input type="text" name="VALOR" size="15" maxlength="15" onkeydown="javascript:return(formataValor(this,event));" onblur="javascript:formataNumerico(this);" style="text-align: right">
function formataValor(campo,event) {
 
 var tammax = campo.maxLength;
 if (tammax == -1) tammax = campo.size;
 if (tammax == -1) tammax = 15;
 //var rcode = (window.Event) ? event.keyCode : event.which;
 
 if (clientNavigator == "IE"){
    var rcode = event.keyCode
  }else{
    if (event.keyCode == 0)
      var rcode = event.charCode
    else
      var rcode = event.keyCode;  
 	}
  
 // Enter - 13, Tab - 9, backspace, delete - 46 e setas direcionais (37,38,39,40)
 if ((rcode == 13) ||(rcode == 9) ||(rcode == 0) || (rcode == 46) || (rcode == 8) || (rcode >= 37 && rcode <= 40)) {
  return true;
 }
 
 var vr = campo.value;
 
 vr = Limpar(vr,"0123456789");
 
 tam = vr.length;

 if (tam < tammax && rcode != 8){ tam = vr.length + 1 ; }

 if (rcode == 8 ){ tam = tam - 1 ; }

 if ((rcode == 8) || ( rcode >= 48 && rcode <= 57 ) || (rcode >= 96 && rcode <= 105)){
  if ( tam <= 2 ){
    campo.value = vr ; }
   if ( (tam > 2) && (tam <= 5) ){
    campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
   if ( (tam >= 6) && (tam <= 8) ){
    campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, 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 - 2, 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 - 2, 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 ) ;}
 } else
 {
   return false; 
 }
}

function aplicaMascara(campo) {

 var texto = campo;
 
 var tam = campo.length;

 var i = campo.indexOf(",");
 
 var is = campo.indexOf("-");
  
 //Se texto apresenta sinal de negativo remover ele (será inserido no final).	
 if (is != -1)
	texto = texto.replace( "-", "" );	

	
 //Se vazio
 if (tam == 0) {
	return texto;
 } 
 
 //Se for zero
 if (texto==0) {
   texto = "0";
 }
 
 //Se texto for apenas uma virgula 
 if (texto==",")
	texto="0";

  
 //verifica se tem: 
 // 1 - Não têm casas decimais.
 if (i==-1) {
 
   texto = Limpar(texto,"0123456789");
   
   tam = texto.length;
    
   //acrescenta decimais
   texto = texto + '00';
   tam = tam + 2;
 
   if ( tam <= 2 ){
    texto = texto ; }
   if ( (tam > 2) && (tam <= 5) ){
    texto = texto.substr( 0, tam - 2 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 6) && (tam <= 8) ){
    texto = texto.substr( 0, tam - 5 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 9) && (tam <= 11) ){
    texto = texto.substr( 0, tam - 8 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 12) && (tam <= 14) ){
    texto = texto.substr( 0, tam - 11 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
   if ( (tam >= 15) && (tam <= 17) ){
    texto = texto.substr( 0, tam - 14 ) + '.' + texto.substr( tam - 14, 3 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ;}
    
 }
 // 2 - Com casa decimal
 else {
 
 // 2.1 - Trata com duas casas decimais ex: 0,5 -> 0,50
 if ((tam - i) == 1) {
	texto = texto + '00';
 }
 
 if ((tam - i) == 2) {
	texto = texto + '0';
 }
 
 // 2.2 - Trata sem o zero - Casa decimal ex: ,5 -> 0,5
 if ( (i == 0) && (tam > 1) ) {
  texto = '0'+ texto;
 }
  
 // 2.3 - Tira casas decimais com mais de 3
 if ((tam - i) > 3) {
    texto = texto.substr(0, i) + texto.substr(i, i + 2);
 }
 
 // 2.3 - Aplica máscara para casa decimal
 texto = Limpar(texto,"0123456789");
 
 tam = texto.length;
 
 if ( tam <= 2 ){
    texto = texto ; }
 if ( (tam > 2) && (tam <= 5) ){
    texto = texto.substr( 0, tam - 2 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 6) && (tam <= 8) ){
    texto = texto.substr( 0, tam - 5 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 9) && (tam <= 11) ){
    texto = texto.substr( 0, tam - 8 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 12) && (tam <= 14) ){
    texto = texto.substr( 0, tam - 11 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ; }
 if ( (tam >= 15) && (tam <= 17) ){
    texto = texto.substr( 0, tam - 14 ) + '.' + texto.substr( tam - 14, 3 ) + '.' + texto.substr( tam - 11, 3 ) + '.' + texto.substr( tam - 8, 3 ) + '.' + texto.substr( tam - 5, 3 ) + ',' + texto.substr( tam - 2, tam ) ;}
 }
 
 //Se apresenta sinal de negativo aplica o sinal novamente
 if (is != -1)
	texto = '-'+ texto;	

 return texto;
}

//Utilize esta função no evento onblur do input
//Ex.: <input type="text" name="VALOR" size="15" maxlength="15" onkeydown="javascript:return(formataValor(this,event));" onblur="javascript:formataNumerico(this);" style="text-align: right">
function formataNumerico(campo) {

var texto = campo.value;

for (var i=0; i < texto.length; i++) {
	texto = texto.replace( " ", "" );
	texto = texto.replace( ".", "" );
}

texto = aplicaMascara(texto);

campo.value = texto;

}

function jCampoBusca(campo,script) {

function buscaAjax(v,m){
  if (v){
    var valor = m.children('#'+campo+'_busca').val();
    if (valor!=null && valor!=""){ 
      $("#"+campo).html("<option>Carregando...</option>");
      $.get(script,
            {busca: valor},
            function(msg) {
               $("#"+campo).html(msg);
            }
      );    
    }
  }
}

parent.$.prompt('Buscar:<br /> <input type="text" id="'+campo+'_busca" value="" />',{
      callback: buscaAjax,
      buttons: { OK: true, Cancelar: false }
}).find('#'+campo+'_busca').focus();

}

function jCampoBusca2(tabela,campo) {

function buscaAjax(v,m){
  if (v){
    var valor = m.children('#'+campo+'_busca').val();
    if (valor!=null && valor!=""){ 
      $("#"+campo).html("<option>Carregando...</option>");
      $.get("hotbuscar.php",
            {tabela: tabela, campo: campo, busca: valor},
            function(msg) {
               $("#"+campo).html(msg);
            }
      );    
    }
  }
}

parent.$.prompt('Buscar:<br /> <input type="text" id="'+campo+'_busca" value="" />',{
      callback: buscaAjax,
      buttons: { OK: true, Cancelar: false }
}).find('#'+campo+'_busca').focus();

}
