function validastr(ev){
	var codigos ='44,46,59,47,60,62,58,63,126,94,93,125,180,96,91,123,61,43,45,95,41,92,92,92,92,40,42,38,168,37,36,35,64,33,124,224,232,236,242,249,226,234,238,244,251,228,235,239,246,252,225,233,237,243,250,227,245,192,200,204,210,217,194,202,206,212,219,196,203,207,214,220,193,201,205,211,218,195,213,231,199,32,34';
	var keys = codigos.split(',');
	for(i=0;i<keys.length;i++){
		if(ev.keyCode == parseInt(keys[i])){
			return false;
		}else{
			if(ev.which == parseInt(keys[i])){
				return false;
			}
		}
	}
	return true;
}

// Efetuando Login
function EfetuarLogin() {

	var pLogin = document.formlogin.pLogin.value;
	if (pLogin=="") {
		alert("Favor informar o seu login!");
		document.formlogin.pLogin.focus();
		return false;
	}
	
	var pSenha = document.formlogin.pSenha.value;
	if (pSenha=="") {
		alert("Favor informar a sua senha!");
		document.formlogin.pSenha.focus();
		return false;
	}
	
	document.formlogin.submit();
	
}

function trocaCampo(objAtual, objProx) {
	nCarac = objAtual.maxLength - 1;
	if(objAtual.textLength >= nCarac) {
		objProx.focus();
	}
}

function MM_goToURL() {
	for (var i=0; i< (MM_goToURL.arguments.length - 1); i+=100)
	eval(MM_goToURL.arguments[i]+".location='"+MM_goToURL.arguments[i+1]+"'");
	document.MM_returnValue = false;
} 

// Mascara para data
function Data(objeto){
	if (objeto.value.length == 2 || objeto.value.length == 5 ){
	objeto.value = objeto.value+"/";
	}
}

// Digitar somente números
function soninteiro(evento) {
	if(window.navigator.appName=="Microsoft Internet Explorer"){
		if(evento.keyCode>=48 && evento.keyCode<=57) return true;
		else
		alert("Somente números!");
		return false;
	}else{
		if((evento.charCode>=48 && evento.charCode<=57) || evento.charCode==0) return true;
		else
		alert("Somente números!");
		return false;
	}
}


function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		return false;
	}
	
	month = matchArray[3]; // p@rse date into variables
	day = matchArray[1];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		return false;
	}

	if (day < 1 || day > 31) {
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return false;
		}
	}

	return true; // date is valid
}


// Validação do Formulário
function ValidaForm() {

	var f_num_cpf = document.frmCadastro.f_num_cpf.value;
	if (document.frmCadastro.f_num_cpf.value=="") {
		alert("Informe o seu CPF!");
		document.frmCadastro.f_num_cpf.focus;
		return false;
	
	}
	if(!validaCPF(f_num_cpf)){
		alert("CPF inválido!");
		document.frmCadastro.f_num_cpf.focus;
		return false;	
	}

	var f_sexo = document.frmCadastro.f_sexo.value;
	if (f_sexo=="") {

		alert("Informe o sexo!");
		document.frmCadastro.f_sexo.focus;
		return false;
	
	}
	
	var f_dth_dia_nascimento = document.frmCadastro.f_dth_dia_nascimento.value;
	var f_dth_mes_nascimento = document.frmCadastro.f_dth_mes_nascimento.value;
	var f_dth_ano_nascimento = document.frmCadastro.f_dth_ano_nascimento.value;	
	var data_nascimento = f_dth_dia_nascimento+"-"+f_dth_mes_nascimento+"-"+f_dth_ano_nascimento;
	var data_nascimento_br = f_dth_dia_nascimento+"/"+f_dth_mes_nascimento+"/"+f_dth_ano_nascimento;	
		
	if (!isDate(data_nascimento)) {
	
		alert("Favor informar uma data de nascimento valida!");
		document.frmCadastro.f_dia_dth_nascimento.focus;
		return false;
	
	} else {
		if (!ValidaDataNasc(data_nascimento_br)) {
			alert("Atenção! Não é permitido a venda para menores de 18 anos.");
			return false;
		}
	}
	
	Trim(document.frmCadastro.f_nom_pessoa,document.frmCadastro.f_nom_pessoa.value);
	var f_nom_pessoa = document.frmCadastro.f_nom_pessoa.value;
	if (f_nom_pessoa=="") {
	
		alert("Favor informar o nome do responsável!");
		document.frmCadastro.f_nom_pessoa.focus;
		return false;
	
	}

	var f_tel_celular = document.frmCadastro.f_tel_celular.value;
	if (f_tel_celular!="") {
		
		var f_ddd_celular = document.frmCadastro.f_ddd_celular.value;
		if (f_ddd_celular=="") {
		
			alert("Favor informar o código de área do celular!");
			document.frmCadastro.f_ddd_celular.focus;
			return false;	
		}
		f_tel_celular.replace("-","");
	}

	var f_tel_residencial = document.frmCadastro.f_tel_residencial.value;
	if (f_tel_residencial!="") {

		var f_ddd_residencial = document.frmCadastro.f_ddd_residencial.value;
		if (f_ddd_residencial=="") {
		
			alert("Favor informar o código de área do telefone residencial!");
			document.frmCadastro.f_ddd_residencial.focus;
			return false;
		
		}
		f_tel_residencial.replace("-","");
	}

	var f_tel_comercial = document.frmCadastro.f_tel_comercial.value;
	if (f_tel_comercial!="") {
		
		var f_ddd_comercial = document.frmCadastro.f_ddd_comercial.value;
		if (f_ddd_comercial=="") {
		
			alert("Favor informar o código de área do comercial!");
			document.frmCadastro.f_ddd_comercial.focus();
			return false;
		
		}
		f_tel_comercial.replace("-","");
	}
	
	if((f_tel_celular=='') && (f_tel_residencial=='') && (f_tel_comercial=='')){
		alert('Atenção! Ao menos um dos telefones deve ser preenchido.');
	 	document.frmCadastro.f_ddd_celular.focus();
	 	return false;
	}
	
	var f_num_cep = document.frmCadastro.f_num_cep.value;
	f_num_cep.replace("-","");
	if (f_num_cep=="") {
	
		alert("Favor informar o seu CEP!");
		document.frmCadastro.f_num_cep.focus();
		return false;
	
	}
	
	var f_tipo_logradouro = document.frmCadastro.f_tipo_logradouro.value;
	if (f_tipo_logradouro=="") {
	
		alert("Favor informar o tipo do seu logradouro");
		document.frmCadastro.f_tipo_logradouro.focus();
		return false;
	
	}
	
	Trim(document.frmCadastro.f_logradouro,document.frmCadastro.f_logradouro.value);
	var f_logradouro = document.frmCadastro.f_logradouro.value;
	if (f_logradouro=="") {
	
		alert("Favor informar o seu logradouro!");
		document.frmCadastro.f_logradouro.focus();
		return false;
	
	}
	
	Trim(document.frmCadastro.f_numero,document.frmCadastro.f_numero.value);
	var f_numero = document.frmCadastro.f_numero.value;
	if (f_numero=="") {
	
		alert("Favor informar o número do logradouro!");
		document.frmCadastro.f_numero.focus();
		return false;
	
	}
	Trim(document.frmCadastro.f_complemento,document.frmCadastro.f_complemento.value);
	
	Trim(document.frmCadastro.f_bairro,document.frmCadastro.f_bairro.value);
	var f_bairro = document.frmCadastro.f_bairro.value;
	if (f_bairro=="") {
	
		alert("Favor informar o seu bairro!");
		document.frmCadastro.f_bairro.focus();
		return false;
	
	}
	
	Trim(document.frmCadastro.f_municipio,document.frmCadastro.f_municipio.value);
	var f_municipio = document.frmCadastro.f_municipio.value;
	if (f_municipio=="") {
	
		alert("Favor informar o município!");
		document.frmCadastro.f_municipio.focus();
		return false;
	
	}
	var f_uf = document.frmCadastro.f_uf.value;
	if (f_uf=="") {
	
		alert("Favor selecionar o estado!");
		document.frmCadastro.f_uf.focus();
		return false;
	
	}
	
	Trim(document.frmCadastro.f_email,document.frmCadastro.f_email.value);
	var f_email = document.frmCadastro.f_email.value;
	if (f_email=="") {
	
		alert("Favor informar o seu e-mail de cadastro!");
		document.frmCadastro.f_email.focus();
		return false;
	
	}
	if(!checkEmail(f_email)){
		alert("e-mail inválido!");
		document.frmCadastro.f_email.focus();
		return false;	
	}
	
	Trim(document.frmCadastro.f_login,document.frmCadastro.f_login.value);
	var f_login = document.frmCadastro.f_login.value;
	if(f_login.length < 6){
		 alert("O campo Login deve ter no mínimo 6 caracteres!");
                 document.frmCadastro.f_login.focus();
	 	 return false;
	}

	if (f_login=="") {
	
		alert("Favor informar o seu login");
		document.frmCadastro.f_login.focus();
		return false;
	
	}
	if(!checkLogin(f_login)){
		alert("Escolha outro login, este login é inválido.");
		document.frmCadastro.f_login.focus();
		return false;
	
	}
	var validaLogin = document.frmCadastro.validaLogin.value;
	if(validaLogin == '0') {
		var f_senha = document.frmCadastro.f_senha.value;
		if (f_senha=="") {
		
			alert("Favor informar a sua senha!");
			document.frmCadastro.f_senha.focus();
			return false;
		
		}
		
		var f_confirma_senha = document.frmCadastro.f_confirma_senha.value;
		if (f_confirma_senha=="") {
		
			alert("Favor confirmar a sua senha!");
			document.frmCadastro.f_confirma_senha.focus();
			return false;
		
		}
		
		if(f_senha.length < 6 || f_senha.length > 32) {
			alert("Informe de 6 a 32 caracteres no campo senha!");
			document.frmCadastro.f_senha.focus();
			return false;
		}		
		
		if (f_confirma_senha!=f_senha) {
			
			alert("As senhas não são iguais! Favor redigitar a senha!");
			document.frmCadastro.f_confirma_senha.focus();
			return false;
		
		}
	}	
	var f_Contrato = document.frmCadastro.f_Contrato.checked;
	if(!f_Contrato) {
		alert("É necessário confirmar o contrato de prestação de servico!");
		document.frmCadastro.f_Contrato.focus();
		return false;
	}

	document.frmCadastro.submit();
	
}


// Validação do Formulário
function ValidaFormUpdate() {

	var f_num_cpf = document.frmCadastro.f_num_cpf.value;
	f_num_cpf.replace("-", "");
	if (f_num_cpf=="") {
	
		alert("Informe o seu CPF!");
		document.frmCadastro.f_num_cpf.focus();
		return false;
	
	}
	if(!validaCPF(f_num_cpf)){
		alert("CPF inválido!");
		document.frmCadastro.f_num_cpf.focus();
		return false;
	}

	var f_sexo = document.frmCadastro.f_sexo.value;
	if (f_sexo=="") {
	
		alert("Informe o sexo!");
		document.frmCadastro.f_sexo.focus();
		return false;
	
	}
	
	var f_dth_dia_nascimento = document.frmCadastro.f_dth_dia_nascimento.value;
	var f_dth_mes_nascimento = document.frmCadastro.f_dth_mes_nascimento.value;
	var f_dth_ano_nascimento = document.frmCadastro.f_dth_ano_nascimento.value;	
	var data_nascimento = f_dth_dia_nascimento+"-"+f_dth_mes_nascimento+"-"+f_dth_ano_nascimento;
	
		
	if (!isDate(data_nascimento)) {
	
		alert("Favor informar uma data de nascimento valida!");
		document.frmCadastro.frmCadastro.f_dia_dth_nascimento.focus();
		return false;
	
	}

	var f_nom_pessoa = document.frmCadastro.f_nom_pessoa.value;
	if (f_nom_pessoa=="") {
	
		alert("Favor informar o nome do responsável!");
		document.frmCadastro.f_nom_pessoa.focus();
		return false;
	
	}

	var f_tel_celular = document.frmCadastro.f_tel_celular.value;
	if (f_tel_celular!="") {
		
		var f_ddd_celular = document.frmCadastro.f_ddd_celular.value;
		if (f_ddd_celular=="") {
		
			alert("Favor informar o código de área do celular!");
			document.frmCadastro.f_ddd_celular.focus();
			return false;
		
		}
		f_tel_celular.replace("-", "");
	}

	var f_tel_residencial = document.frmCadastro.f_tel_residencial.value;
	if (f_tel_residencial!="") {
	
		var f_ddd_residencial = document.frmCadastro.f_ddd_residencial.value;
		if (f_ddd_residencial=="") {
		
			alert("Favor informar o código de área do telefone residencial!");
			document.frmCadastro.f_ddd_residencial.focus();
			return false;
		
		}
		f_tel_residencial.replace("-", "");	
	}
	
	var f_tel_comercial = document.frmCadastro.f_tel_comercial.value;
	if (f_tel_comercial!="") {
		var f_ddd_comercial = document.frmCadastro.f_ddd_comercial.value;
		if (f_ddd_comercial=="") {
		
			alert("Favor informar o código de área do comercial!");
			document.frmCadastro.f_ddd_comercial.focus();
			return false;
		
		}
	
		f_tel_comercial.replace("-", "");	
	}
	
	var f_num_cep = document.frmCadastro.f_num_cep.value;
	f_num_cep.replace("-", "");	
	if (f_num_cep=="") {
	
		alert("Favor informar o seu CEP!");
		document.frmCadastro.f_num_cep.focus();
		return false;
	
	}
	
	var f_tipo_logradouro = document.frmCadastro.f_tipo_logradouro.value;
	if (f_tipo_logradouro=="") {
	
		alert("Favor informar o tipo do seu logradouro");
		document.frmCadastro.f_tipo_logradouro.focus();
		return false;
	
	}
	
	var f_logradouro = document.frmCadastro.f_logradouro.value;
	if (f_logradouro=="") {
	
		alert("Favor informar o seu logradouro!");
		document.frmCadastro.f_logradouro.focus();
		return false;
	
	}
	
	var f_numero = document.frmCadastro.f_numero.value;
	if (f_numero=="") {
	
		alert("Favor informar o numero do logradouro!");
		document.frmCadastro.f_numero.focus();
		return false;
	
	}
	
	var f_bairro = document.frmCadastro.f_bairro.value;
	if (f_bairro=="") {
	
		alert("Favor informar o seu bairro!");
		document.frmCadastro.f_bairro.focus();
		return false;
	
	}
	
	var f_municipio = document.frmCadastro.f_municipio.value;
	if (f_municipio=="") {
	
		alert("Favor informar o município!");
		document.frmCadastro.f_municipio.focus();
		return false;
	
	}
	var f_uf = document.frmCadastro.f_uf.value;
	if (f_uf=="") {
	
		alert("Favor selecionar o estado!");
		document.frmCadastro.f_uf.focus();
		return false;
	
	}
	
	var f_email = document.frmCadastro.f_email.value;
	if (f_email=="") {
	
		alert("Favor informar o seu e-mail de cadastro!");
		document.frmCadastro.f_email.focus();
		return false;
	
	}
	if(!checkEmail(f_email)){
		alert("e-mail inválido!");
		document.frmCadastro.f_email.focus();
		return false;
	}
	 
	document.frmCadastro.submit();
	
}



function ValidaSenhaUpdate() {
	
	
	var f_senha_atual = document.frmSenha.f_senha.value;
	if (f_senha_atual=="") {
	
		alert("Favor informar a sua senha atual!");
		document.frmSenha.f_senha_atual.focus();
		return false;
	
	}
	


	var f_senha = document.frmSenha.f_senha.value;
	if (f_senha=="") {
	
		alert("Favor informar a sua senha!");
		document.frmSenha.f_senha.focus();
		return false;
	
	}
	

	var f_confirma_senha = document.frmSenha.f_confirma_senha.value;
	if (f_confirma_senha=="") {
	
		alert("Favor confirmar a sua senha!");
		document.frmSenha.f_confirma_senha.focus();
		return false;
	
	}
	
	if(f_senha.length < 6 || f_senha.length > 32) {
		alert("Informe de 6 a 32 caracteres no campo senha!");
		document.frmSenha.f_senha.focus();
		return false;
	}
	
	if (f_confirma_senha!=f_senha) {
		
		alert("As senhas não são iguais! Favor redigitar a senha!");
		document.frmSenha.f_confirma_senha.focus();
		return false;
	
	}	
	
	document.frmSenha.submit();

	
	
}

function ValidaCredito() {

	var pacotes = document.frmPacote.pacotes.value;
	if (pacotes=="") {
	
		alert("Favor selecionar o um pacote!");
		document.frmPacote.pacotes.focus();
		return false;
	
	}
	
	document.frmPacote.submit();
	
}

function ValidaFormPag() {
	

	var formpag = document.frmPag.formpag.value;
	if (formpag=="") {
	
		alert("Favor selecionar a forma de pagamento");
		document.frmPag.formpag.focus();
		return false;
	
	}
	document.frmPag.submit();
}

function ValidaSigame() {
	
	var tipo = document.frmSigame.tipoRedir.value;
	var codPais = document.frmSigame.codPais.value;
	var codArea = document.frmSigame.codArea.value;
	var telefone = document.frmSigame.telefone.value;
	
	if (tipo=="") {
	
		alert("Favor selecionar o tipo do siga-me desejado");
		document.frmSigame.tipoRedir.focus();
		return false;
	
	}
	
	
	if (codPais=="" || codArea=="" || telefone=="") {
	
		alert("Insira o telefone para redirecionamento no formato codigo do pais-codigo de area- telefone");
		return false;
	
	}
	document.frmSigame.submit();
}


function ValidaSigame1(tipo) {
	
	document.frmSigame.submit();
}

function AbreFormPag(formpag, url) {
	var result = window.open('http://orders.ct.tesatelecom.com/processOrder.do?return='+url+'&orderid='+formpag+'&ts=2','_blank','menubar=0,resizable=1,scrollbars=1,width=1200,height=600');
	if(result) {
		self.location = '/financeiro/';
	}
}

function checkEmail(emailStr) { 	var checkTLD=1; var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; 	var emailPat=/^(.+)@(.+)$/; 	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 	var validChars="\[^\\s" + specialChars + "\]"; 	var quotedUser="(\"[^\"]*\")"; 	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 	var atom=validChars + '+'; 	var word="(" + atom + "|" + quotedUser + ")"; 	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 	var matchArray=emailStr.match(emailPat);  	if (matchArray==null) { 		return false; 	} 	var user=matchArray[1]; 	var domain=matchArray[2];  	for (i=0; i<user.length; i++) { 		if (user.charCodeAt(i)>127) { 			return false; 		} 	} 	for (i=0; i<domain.length; i++) { 		if (domain.charCodeAt(i)>127) { 			return false; 		}	 	}  	if (user.match(userPat)==null) { 		return false; 	}  	var IPArray=domain.match(ipDomainPat); 	if (IPArray!=null) {  		for (var i=1;i<=4;i++) { 			if (IPArray[i]>255) { 				return false; 			} 		} 		return true; 	}  	var atomPat=new RegExp("^" + atom + "$"); 	var domArr=domain.split("."); 	var len=domArr.length; 	for (i=0;i<len;i++) { 		if (domArr[i].search(atomPat)==-1) { 			return false; 		} 	}  	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) { 		return false; 	}  	if (len<2) { 		return false; 	} 	return true; }

function checkLogin(pVal) {

	var exp1=/^([a-zA-Z])/;
	var exp2=/.*@.*/;
	
	var iniciaComLetra = exp1.test(pVal);
	var contemArroba = exp2.test(pVal);

	if (pVal.length < 64 && iniciaComLetra && !contemArroba)
	{
		return true;
	}
	return false;
}


function ValidaConvite(){

	var qtde  = document.frmConvite.theValue.value;

	if(qtde > 0) {
		var nome = document.frmConvite.c_nome[0].value;
		var email = document.frmConvite.c_email[0].value;
	} else {
		var nome = document.frmConvite.c_nome.value;
		var email = document.frmConvite.c_email.value;	
	}

	if(!checkEmail(email)){
		alert("Insira um e-mail válido");
		return false;	
	}
	if (nome=="") {
		alert("Insira o seu nome");
		return false;
	}
	document.frmConvite.submit();
}

function ValidaEsqueciSenha(){

	var login = document.frmEsqueciSenha.login.value;

	if(login==""){
		alert("Insira um login");
		return false;	
	}

	document.frmEsqueciSenha.submit();
}

function ValidaNovaSenha(){

	var nova = document.frmaltera.nova.value;
	var novaconf = document.frmaltera.confirmnova.value;

	if(nova != novaconf){
		alert("Nova senha e confirmação tem de ser iguais.");
		return false;	
	}

	document.frmaltera.submit();
}




function validaCPF(cpf) {
		cpf = cpf.replace(".","");
		cpf = cpf.replace(".","");
		cpf = cpf.replace("-","");
		cpf = cpf.replace("/","");
			 erro = new String;
			 if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n";
			 var nonNumbers = /\D/;
			 if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
			 if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
					 return false;
		   }
		   var a = [];
		   var b = new Number;
		   var c = 11;
		   for (i=0; i<11; i++){
				   a[i] = cpf.charAt(i);
				   if (i < 9) b += (a[i] * --c);
		   }
		   if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		  b = 0;
		   c = 11;
		   for (y=0; y<10; y++) b += (a[y] * c--);
		   if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		   if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
				//   erro +="Digito verificador com problema!";
				return false;
		   }

		   return true;
}

function formataNumero(num, obj, ev){
	if(ev.keyCode != 8 && ev.keyCode != 46 && ev.keyCode != 37 && ev.keyCode != 39){
		if(obj.value.length == num){
			obj.value = obj.value+"-";
		}
	}
}

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

function cpf(v){
    v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
                                             //de novo (para o segundo bloco de números)
    v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
    return v
}

function cep(v){
    v=v.replace(/\D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse é tão fácil que não merece explicações
    return v
}

function telefone(v){
    v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
//    v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    v=v.replace(/^(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function addInput() {
   var ni = document.getElementById('moreinput');
   var numi = document.getElementById('theValue');
   if(numi.value < 3) {
	   var num = (document.getElementById("theValue").value -1)+ 2;
	   numi.value = num;
	   var newdiv = document.createElement('div');
	   newdiv.innerHTML = "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' class='texto'><tr><td width='101' height='25' class='texto'><img src='/appl/view/imagens/nome.gif' ></td><td height='35'><input name='c_nome[]' type='text' class='texto' id='c_nome' value='' size='45' /></td></tr><tr><td width='101' height='25' class='texto'><img src='/appl/view/imagens/email.gif' ></td><td height='35'><input name='c_email[]' type='text' class='texto' id='c_email' value='' size='45' /></td></tr></table>";
	   //newdiv.innerHTML = "<input id='input" + num + "' name='minas" + num + "' type='text' size='20'> <a href=\"javascript:removeElement(\'"+divIdName+"\')\"\">Remover</a>"
	   ni.appendChild(newdiv);
	}
}

function removeElement(divNum) {
   var d = document.getElementById('moreinput');
   var olddiv = document.getElementById(divNum);
   d.removeChild(olddiv);
}

var xml;
function BuscaCep(valor){
	f = document.frmCadastro;
	valor = limpa_string(valor);
	if ((valor != "") && (valor.length == 8)){
		xml = null;
		if (window.XMLHttpRequest)
		{
			xml = new XMLHttpRequest();
			xml.onreadystatechange = Monta;
			xml.open("GET", "http://www.falafacil.com.br/appl/view/ajax/buscacep.php?cep="+valor+"&tipo=1", true);
			//xml.open("GET", "http://voip/appl/view/ajax/buscacep.php?cep="+valor+"&tipo=1", true);
			xml.send(null);
		}
		else
			if (window.ActiveXObject)
			{
				xml = new ActiveXObject("Microsoft.XMLHTTP");
				xml.onreadystatechange = Monta;
				xml.open("GET", "http://www.falafacil.com.br/appl/view/ajax/buscacep.php?cep="+valor+"&tipo=1", true);
				//xml.open("GET", "http://voip/appl/view/ajax/buscacep.php?cep="+valor+"&tipo=1", true);
				xml.send();
			}
		}
		else
		{
			f.f_logradouro.value = "";
			f.f_bairro.value = "";
			f.f_municipio.value = "";
			f.f_uf.value = "";
			
			f.f_logradouro.readOnly = false;
			f.f_bairro.readOnly = false;
			f.f_municipio.readOnly = false;
			f.f_uf.readOnly = false;
		}
}
function Monta(){
	f = document.frmCadastro;
	var exibe = document.getElementById('procurandocep');

	if (xml.readyState == 1)
		exibe.style.display = 'block';
		exibe.innerHTML = "<img src='/appl/view/imagens/refresh.gif' /><font color='red' size='-2'> Procurando Cep. Aguarde...</font>";

		if (xml.readyState == 4)
		{
			exibe.innerHTML = "";
			exibe.style.display = 'none';

			if (xml.status == 200)
			{
				response = xml.responseText;
				response = unescape(response);
				response = response.replace(/\+/g," ");
				result = response.split("|");
				
				tam_endereco = f.f_logradouro.maxLength;
				tam_bairro = f.f_bairro.maxLength;
				tam_municipio = f.f_municipio.maxLength;

				if (result[0] == '0')
				{
					//f.f_tipo_logradouro.value = result[1].substring(0,1);
					
					var logra = result[1].substr(result[1].indexOf(" ")+1 );
					
					if(logra.substr(0,logra.indexOf(","))!=""){
						f.f_logradouro.value = logra.substr(0,logra.indexOf(","));
					}else{
						f.f_logradouro.value = logra;					
					}

					f.f_bairro.value = result[2].substring(0, tam_bairro);
					f.f_municipio.value = result[3].substring(0, tam_municipio);
					f.f_uf.value = result[4];
					var pref = result[1].substr(result[1].indexOf(",")+1 );
					//f.f_prefixo.value = pref;
					
					//f.f_prefixo.readOnly = true;
					/*f.f_logradouro.readOnly = true;
					f.f_bairro.readOnly = true;
					f.f_municipio.readOnly = true;
					f.f_uf.readOnly = true;*/
					if(f.f_logradouro.value != '') f.f_logradouro.readOnly = true;
          if(f.f_bairro.value != '') f.f_bairro.readOnly = true;
          if(f.f_municipio.value != '') f.f_municipio.readOnly = true;
          if(f.f_uf.value != '') f.f_uf.readOnly = true;

				}
				else
				{
					f.f_tipo_logradouro.value ="";
					f.f_logradouro.value = "";
					f.f_bairro.value = "";
					f.f_municipio.value = "";
					f.f_uf.value = "";
					
					f.f_logradouro.readOnly = false;
					f.f_bairro.readOnly = false;
					f.f_municipio.readOnly = false;
					f.f_uf.readOnly = false;
					/*if (confirm('Cep não encontrado. Deseja ir para o site dos correios?') == 1)
					{
					window.open('http://www.correios.com.br/servicos/cep/cep_loc_log.cfm');
					}*/
				}
			}
			else
			{
			//f.f_tipo_logradouro.value="";
			//f.f_logradouro.value = "";
			//f.f_bairro.value = "";
			//f.f_municipio.value = "";
			//f.f_uf.value = "";
			}

		}
}

function limpa_string(S){
	// Deixa so' os digitos do 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 ValidaDataNasc(value){
	var vData = value;
	var dt_hj = new Date();
	var datahoje = zero(dt_hj.getDate()) + '/' + zero(dt_hj.getMonth()) + '/' + dt_hj.getFullYear();
	var idade = comparaDatas(datahoje,vData) - 6544;
	if(idade < 0){
	return false;
	}else{
	return true;
	}
}
function comparaDatas(p_data1,p_data2){
	/*
	// ### comparaDatas(DATA_FINAL,DATA_INICIAL)
	// ### Objetivo: calcular a diferença em dias das duas datas
	*/
	
	mDay = p_data1.substr(0,2);
	mMonth = p_data1.substr(3,2);
	mYear = p_data1.substr(6,4)
	datDate1 = mMonth+'/'+mDay+'/'+mYear;
	
	mDay = p_data2.substr(0,2);
	mMonth = p_data2.substr(3,2);
	mYear = p_data2.substr(6,4)
	datDate2 = mMonth+'/'+mDay+'/'+mYear;
	
	datDate1= Date.parse(datDate1);
	datDate2= Date.parse(datDate2);
	
	v_dateDiff = Math.round((datDate1-datDate2)/(24*60*60*1000));
	return v_dateDiff;
}

function zero(pValor){
	if(String(pValor).length<2){
	return String('0' + pValor);
	}else{
	return pValor;
	}
}

var xmlLogin;
function ValidaLogin(valor){
	f = document.formlogin;
	//valor = limpa_string(valor);
	if ((valor != "") && (checkEmail(valor))){
		xmlLogin = null;
		if (window.XMLHttpRequest)
		{
			xmlLogin = new XMLHttpRequest();
			xmlLogin.onreadystatechange = MontaLogin;
			xmlLogin.open("GET", "http://www.falafacil.com.br/appl/view/ajax/validalogin.php?loginEmail="+valor, true);
			//xmlLogin.open("GET", "http://voip:8081/appl/view/ajax/validalogin.php?loginEmail="+valor, true);
			xmlLogin.send(null);
		}
		else
			if (window.ActiveXObject)
			{
				xmlLogin = new ActiveXObject("Microsoft.XMLHTTP");
				xmlLogin.onreadystatechange = MontaLogin;
				xmlLogin.open("GET", "http://www.falafacil.com.br/appl/view/ajax/validalogin.php?loginEmail="+valor, true);
				//xmlLogin.open("GET", "http://voip:8081/appl/view/ajax/validalogin.php?loginEmail="+valor, true);
				xmlLogin.send();
			}
		}
		else
		{
			//alert('O endereço de e-mail não é válido!');
		}
}

function MontaLogin(){
	f = document.formlogin;
	var exibe = document.getElementById('validandologin');
	var loading = document.getElementById('loading');
	
	if (xmlLogin.readyState == 1)
		loading.style.display = 'block';
		loading.innerHTML = "<img src='/appl/view/imagens/refresh.gif' /><font color='red' size='-2'> Validando login. Aguarde...</font>";

		if (xmlLogin.readyState == 4)
		{
			loading.innerHTML = "";
			loading.style.display = 'none';

			if (xmlLogin.status == 200)
			{
				result = xmlLogin.responseText;
				f.validaLogin.value = result;
				//response = unescape(response);
				//result = response.replace(/\+/g," ");
				//result = response.split("|");
				if (result == '0')
				{
					exibe.style.display = 'none';
				}
				else
				{
					exibe.style.display = 'block';
				}
			}
			//else
			//{
			//f.f_tipo_logradouro.value="";
			//f.f_logradouro.value = "";
			//f.f_bairro.value = "";
			//f.f_municipio.value = "";
			//f.f_uf.value = "";
			//}

		}
}

function ValidaFormLogin(form) {

	ValidaLogin(form.pLogin.value);
	
	if(form.validaLogin.value == '1') {
		if(form.pSenha.value == "") {
			alert('Atenção! O campo senha deve ser preenchido!');
			return false;
		}
		/*if(form.pSenha.value.length < 6){
			alert('Atenção! O campo senha deve ter no mínimo 6 caracteres.');
			form.pSenha.focus();
			return false;
		}*/
		form.action = '/comunicacao/?view=insert&acao=recuperadados';
	} //else {
		if(form.pLogin.value == "" || !checkEmail(form.pLogin.value)) {
			alert('Atenção! o campo e-mail deve ser válido!');
			return false;
		}
	//}
	form.submit();
	return true;
	
}

function LTrim(s){
	var i = 0;
	var j = 0;
	for (i = 0; i <= s.length - 1; i++)
		if (s.substring(i, i + 1) != ' ')
			{
				j = i;
				break;
			}
	return s.substring(j, s.length);
}
	
function RTrim(s){
	var j = 0;
	for (var i = s.length-1; i > -1; i--)
		if (s.substring(i, i + 1) != ' ')
			{
				j = i;
				break;
			}
	return s.substring(0, j + 1);
}

function Trim(obj, s){
	obj.value = LTrim(RTrim(s));
}

var reDigits = /^\d+$/;

function doDigits(campo){
	var pStr = campo.value;
	if (!reDigits.test(pStr)) {
		if (pStr != null && pStr != "") {
			document.getElementById(campo.id).value = "";
			alert(pStr + " n\u00E3o cont\u00E9m apenas n\u00FAmeros.");
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

function autoTab(element, nextElement) {
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}

function abre(){
	var foto = window.open("http://www.directtalk.com.br/server/directtalkr.dll/user?S=I&id_s=45910751B8580835854C","janela","width=350,height=360,resizable=no,toolbar=0,location=0,directories=0,status=no,menubar=0");
	var f = document.forms[0];
	function vai(){ f.submit();};
	f.action="http://www.directtalk.com.br/server/directtalkr.dll/user?S=I&id_s=45910751B8580835854C";
	setTimeout('vai();',10);
}

function func(event, funcao)
{
    if(window.event) { //IE
       tecla = event.keyCode;
    } else if(event.which) { // Netscape/Firefox/Opera
       tecla = event.which;
    } 
	if(tecla == 13) { //código do enter
		//executa a ação aqui
		setTimeout(funcao, 0);
	}
} 

/*
Analitics
*/
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try {
var pageTracker = _gat._getTracker("UA-1012760-8");
pageTracker._trackPageview();
} catch(err) {}
