function Bloqueia_Caracteres(evnt){
 //Função permite digitação de números
 	if (clientNavigator == "IE"){
 		if (evnt.keyCode < 48 || evnt.keyCode > 57){
 			return false
 		}
 	}else{
 		if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0){
 			return false
 		}
 	}
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
    var keyCode = (isNN) ? e.which : e.keyCode; 
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
        if(arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
return true;
}

function Bloqueia_Car_Espciais(evnt){
 	if (clientNavigator == "IE"){
  		if ((evnt.keyCode < 40 || evnt.keyCode > 122) || (evnt.keyCode == 94 || evnt.keyCode == 96)){
		  if (evnt.keyCode == 32 || evnt.keyCode == 33 || evnt.keyCode == 13)
		    return true
		  else 
 			return false
 		}
 	}
}

function vai (url){
  location.replace(url);
}

function msgRetorno (msg,url){
  alert(msg);		
  location.replace(url);
}


function validarCadastro() {
	if ($("#nome").val().length == 0) {
		alert(VldEndALERTcmpnome);
        $("#nome").focus();
        return false;
	}
       
	var cpf_cob = $("#cpf_b1").val() + $("#cpf_b2").val() + $("#cpf_b3").val() + $("#cpf_b4").val();

	if (cpf_cob.length == 0) {
		alert(VldEndALERTcmpcpf);
		$("#cpf_b1").focus();
		return false;
	}

	s = limpa_string(cpf_cob);
	if (s.length == 11) {
		if (valida_CPF(cpf_cob) == false ) {
			alert(VldEndALERTcmpcpfinc);
			$("#cpf_b1").focus();
			return false;
		}
	}else{
		alert(VldEndALERTcmpcpfinc);
		$("#cpf_b1").focus();
		return false;
	}

	$("#cpf").val(cpf_cob);

	if ($("#rg").val().length == 0) {
		alert(VldEndALERTcmprg);
		$("#rg").focus();
		return false;
	}

	if ($("#tipo_pessoa2").get(0).checked == true) {
		if ($("#razao_social").val().length == 0) {
			alert(VldEndALERTcmprazaosocial);
			$("#razao_social").focus();
			return false;
		}
		
		var cnpj_cob = $("#cnpj_b1").val() + $("#cnpj_b2").val() + $("#cnpj_b3").val() +$("#cnpj_b4").val() + $("#cnpj_b5").val();
	
		if (cnpj_cob.length == 0) {
			alert(VldEndALERTcmpcnpj);
			$("#cnpj_b1").focus();
			return false;
		}
		 
		s = limpa_string(cnpj_cob);
		if (s.length == 14) {
			if (valida_CNPJ(cnpj_cob) == false ) {
				alert(VldEndALERTcmpcnpjinc);
				$("#cnpj_b1").focus();
				return false;
			}
		}else{
			alert(VldEndALERTcmpcnpjinc);
			$("#cnpj_b1").focus();
			return false;
		}
		$("#cnpj").val(cnpj_cob);
	}

    if ($("#ddd").val().length == 0) {
		alert(VldEndALERTcmpddd);
		$("#ddd").focus();
		return false;
	}

	if ($("#telefone").val().length == 0) {
		alert(VldEndALERTcmptel);
		$("#telefone").focus();
		return false;
	}

	if ($("#email").val().length == 0) {
      alert(CkLoginALERTcmpemail);
      document.getElementById("email").focus();
      return false;
   }

   if ($("#email").val().indexOf('@', 0) == -1 || $("#email").val().indexOf('.', 0) == -1) {
      alert(CkLoginALERTcmpemailinc); 
      document.getElementById("email").focus();
      return false;
   }

   if ($("#senha").val().length == 0) {	
		alert(VldPwdALERTcamposenha);
		$("#senha").focus();
		return false;
	}
	if ($("#senha").val().length > 0) {	
		if ($("#senha").val().length != $("#conf_senha").val().length) {	
			alert(VldPwdALERTsenhadif);
			$("#senha").focus();
			return false;
		}
	}


	if ($("#endereco").val().length == 0) {
		alert(VldEndALERTcmpend);
		$("#endereco").focus();
		return false;
	}
     
	if ($("#numero").val().length == 0) {
		alert(VldEndALERTcmpnum);
		$("#numero").focus();
		return false;
	}

	if ($("#bairro").val().length == 0) {
		alert(VldEndALERTcmpbairro);
		$("#bairro").focus();
		return false;
	}

	if ($("#cidade").val().length == 0) {
		alert(VldEndALERTcmpcidade);
		$("#cidade").focus();
		return false;
	}

    $("#data_nasc").val($("#data_nasc_b3").val() + "-" + $("#data_nasc_b1").val() + "-" + $("#data_nasc_b2").val());

		
	// Caso as informações estiverem corretas a função ira retorna true 
	return true;

}

// colocar no evento onKeyUp passando o objeto como parametro
function formata_data(val)
{
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		// charAt -> retorna o caractere posicionado no índice especificado
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0){
		   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
		   // ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
		   // instStr.search(expReg);
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}else if(i==1){
			   
			   if(lchar.search(expr) != 0){
				  // substring(indice1,indice2)
				  // indice1, indice2 -> será usado para delimitar a string
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if(pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}

function getValue(nome){
   var obj = eval("document.forms[0]."+nome+".value");
   return obj;
}
function setValue(nome, valor){
   obj = eval("document.forms[0]."+nome);
   obj.value = valor;
}

/////////////Valores em dinheiro
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;
}


function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string"){
		if(er.test(mail)){ return true; }
	}else if(typeof(mail) == "object"){
		if(er.test(mail.val())){ 
			return true; 
		}
   }else{
        return false;
   }
}

function addFlash(movie, align, bgcolor, width, height){  
    var swf = 
      '<object '
    + 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
    + 'codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0" '
	+ 'width="'+ width +'" '
	+ 'height="'+ height +'"> '
    + '<param name="movie"  value="' + movie + '"/> '
    + '<param name="quality" value="high"/> '
	+ '<param name="scale" value="noscale"/> '
	+ '<param name="salign" value="' + align + '"/> '
	+ '<param name="bgcolor" value="' + bgcolor + '"/> '
	+ '<param name="menu" value="false"/> '
    + '<embed src="' + movie + '" '
    + 'bgcolor="' + bgcolor + '" '
	+ 'menu="false" '
	+ 'quality="high" '
	+ 'scale="noscale" '
	+ 'salign="' + align + '" '
	+ 'width="'+ width +'" '
	+ 'height="'+ height +'" '
	+ 'type="application/x-shockwave-flash" '
	+ 'other="foo" '
	+ 'pluginspage="https://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"/> '
	+ '</object>'
	;
	document.write(swf);
}


function imprimir(){
/*  window.onbeforeprint = function() { 
    document.getElementById('lMenus').style.display="none";
  }
  window.onafterprint = function() { 
    document.getElementById('lMenus').style.display="block";
  } */
  document.getElementById('lMenus').style.display="none";  
  window.parent.focus();
  window.parent.print();
}

function Excluir(caminho,desc_nome,id,tabela,campo_tabela,op,urlRetorno,pasta,foto)
  {
    if(window.confirm("Confirma a exclusao de "+desc_nome))
      {
       location.href = caminho+"?id="+id+"&tabela="+tabela+"&campo_tabela="+campo_tabela+"&urlRetorno="+urlRetorno+"&op="+op+"&pasta="+pasta+"&foto="+foto;;
      } 
  } // Fim da Função 


function msgOpcao(msg,caminho,desc_nome,id,tabela,campo_tabela,op,urlRetorno){
    if(window.confirm(msg+desc_nome))
       location.href = caminho+"?id="+id+"&tabela="+tabela+"&campo_tabela="+campo_tabela+"&urlRetorno="+urlRetorno+"&op="+op;
} // Fim da Função 


function janelaCenter(url, nome, parms, width, height) {
 	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;
	if (parms != "") {
		str += ", " + parms;
	}
	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;

	} else {
		var xc = 0, xy = 0;
	}
	str += ",left=" + xc + ",screenX=" + xc;
	str += ",top=" + yc + ",screenY=" + yc;
	return window.open(url, nome, str);
}

function format_valor(valor){
  if (valor!='') 
	{
	  var valor   = valor.replace(".","");
	  var valor   = valor.replace(",",".");						   			  	
	  return valor;
	} else {
	  return 0;
	}
}									  

// Retorno o valor selecionado do Radio Button 
function valRadio(nomeCampo){ 
  var i;
  for (i=0; i < nomeCampo.length; i++){ 
    if (nomeCampo[i].checked) 
      return nomeCampo[i].value; 
  } 	
}

function checkRadioButton(nomeCampo){
  var i;
  for (i=0; i < nomeCampo.length; i++){ 
    if (nomeCampo[i].checked) 
      return 1; 
  } 	
  return 0;
}

function valida_camposArray(campo,nome){
  var d = document;
  for (var i = 0;i<campo.length; i++){
  	 if (d.getElementById(campo[i]).value == ""){
    	alert ("Campo "+nome[i]+" esta em branco!");
	 	d.getElementById(campo[i]).focus();
	 	return false;
     }
	 if (campo[i] == "email" || campo[i] == "emailamigo"){
	 	if (!checkEmail2(d.getElementById(campo[i]).value)){
    		alert ("Campo "+nome[i]+" esta incorreto!");
	 		d.getElementById(campo[i]).focus();	 
			return false;
	 	}
	 }
  }
  return true;
}

// Fechar Janela 
function fechaJanela(){
  top.window.close();
}

function msgRetornoOpener(msg,url){
  alert(msg);		
  fechaJanela(); // Fecha a Janela Atual
  opener.location.href = url; // Carrega a janela que abriu o POPUP 
}
function voltar(){
   history.back();
}


// Verifica se a srting eh um numero 
function isNum(str){
	var VBlnIsNum;
	VIntTam = str.length;
	VBlnIsNum = true;
	if (VIntTam == 0){
		return false;
	} else {
		for (i=0; i < VIntTam; i++){
			if (str.substring(i,i+1) < '0' || str.substring(i,i+1) >'9'){
				VBlnIsNum = false;
			}
		}
		return VBlnIsNum;	
	}
}


// Codificar Strings

function url_encode(str) {  
        var hex_chars = "0123456789ABCDEF";  
        var noEncode = /^([a-zA-Z0-9\_\-\.])$/;  
        var n, strCode, hex1, hex2, strEncode = "";  

        for(n = 0; n < str.length; n++) {  
            if (noEncode.test(str.charAt(n))) {  
                strEncode += str.charAt(n);  
            } else {  
                strCode = str.charCodeAt(n);  
                hex1 = hex_chars.charAt(Math.floor(strCode / 16));  
                hex2 = hex_chars.charAt(strCode % 16);  
                strEncode += "%" + (hex1 + hex2);  
            }  
        }  
	return strEncode;  
}  

// Descodificar String 

function url_decode(str) {  
	var n, strCode, strDecode = "";  

	for (n = 0; n < str.length; n++) {  
		if (str.charAt(n) == "%") {  
			strCode = str.charAt(n + 1) + str.charAt(n + 2);  
			strDecode += String.fromCharCode(parseInt(strCode, 16));  
			n += 2;  
		} else {  
			strDecode += str.charAt(n);  
		}  
	}  

	return strDecode;  
} 


// Valida CPF 
function valida_CPF(s){
	var i;
	s = limpa_string(s);

	var vlrInvalido	= new Array('11111111111','22222222222','33333333333','44444444444',
								    '55555555555','66666666666','77777777777','88888888888',
								    '99999999999','00000000000');
	for (x =0; x < vlrInvalido.length; x++) {
		if (s == vlrInvalido[x])
			return false;
	}
	
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++){
		d1 += c.charAt(i)*(10-i);
	}
        if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 9; i++)	{
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	return (dv.charAt(1) != d1 ? false : true);
}

// Valida CNPJ 
function valida_CNPJ(s){
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++){
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++){
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	
	return (dv.charAt(1) != d1 ? false : true);
}

// Limpa caracteres
function limpa_string(S){
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++){
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0){temp=temp+digito}
	}
	return temp
}
function validaCampos(frm){
   var i = 0;
   var obj;
   var f = frm;
   //Conta os imputs
   for (i=0; i<f.elements.length; i++) {
     //conta quais os imputs que não foram selecionados....
	 obj = f.elements[i];
     if(obj.id != 1){
	    if(f.elements[i].value == "") {
           alert('O Campo ' + obj.id + ' não foi preenchido!');
		   f.elements[i].focus();
           return false;
 		}    
		if ((f.elements[i].name == "email" || f.elements[i].name == "emailamigo") && f.elements[i].value != ""){
           if (checkEmail(f.elements[i].value)){
		     alert(obj.id+" esta digitado de forma incorreto!")
		     return false;
		   }		 
 		}    		
	 }
  }
  f.submit();
}
function checkEmail(email){
   var d = email;
   if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(d.value)) || !d.value) 
     return false;
   else 
     return true;
}

