//Adaptção do JavaScript para o sistema Vida Freedom
//André Gadelha
// Data: 26/10/2006


// Caracteres Reservados
var TipoMascara="/-._,:"
var TipoMascara2="AX9"

function Atualizacao(Valor,Mascara) {

	var sValue = Valor.value
	var TamValor  = sValue.length
	var TamMascara = Mascara.length

	if (Valor.createTextRange) {
		// Código das teclas de movimentação
		var TeclasMovimentacao="8;46;37;38;39;40;33;34;35;36;45;46"

		// Desprezar Teclas de Movimentação
		if (TeclasMovimentacao.indexOf(event.keyCode+";")>-1) return

		// Ajutar Valores
		if (Valor.value!=Valor._value) {
		   newValue = Pesquisa(Valor.value, Mascara)
 			if (newValue!=Valor.value) {
 				Valor.value=newValue
			}
		}
		// Limitar tamanho do campo
	    var sValue = Valor.value
	    TamValor  = sValue.length
		if (TamValor > TamMascara) {
			Valor.value = sValue.substring(0,TamMascara)
			return
		}

		var r1 = Valor.createTextRange()
		if (newValue!=Valor.value) {
			Valor.value=newValue
			var rNew = Valor.createTextRange()
			r1.setEndPoint("StartToEnd",rNew)
			r1.select()
		}

		Valor._value=Valor.value

	}

}

function Pesquisa(Valor, Mascara) {
	var TamValor  = Valor.length
	var TamMascara = Mascara.length
	var sValue = ""

	// Acescentar Caracteres Reservados
	for (var i=0; i < TamValor; i++) {
	    if (TipoMascara.indexOf(Mascara.substring(i,i+1))>-1) {
			if (Valor.substring(i,i+1) != Mascara.substring(i,i+1))
				Valor = Valor.substring(0,i) + Mascara.substring(i,i+1) + Valor.substring(i,TamValor)
		}
	}


	// Limitar tamanho do campo
    if (TamValor >= TamMascara) {
		TamValor = TamMascara
		Valor = Valor.substring(0,TamValor)
	}

	// Remover Caracteres Inválidos
	for (var i=0; i < TamValor; i++) {
		if ((Mascara.substring(i,i+1) == '9') && ((Valor.substring(i,i+1) < '0') || (Valor.substring(i,i+1) > '9'))) {
			Valor = Valor.substring(0,i) + Valor.substring(i+1,TamValor)
			TamValor = TamValor - 1
			i = i - 1
		}
		if ((Mascara.substring(i,i+1) == 'A') && ((Valor.substring(i,i+1) < 'A') || (Valor.substring(i,i+1) > 'z'))) {
			Valor = Valor.substring(0,i) + Valor.substring(i+1,TamValor)
			TamValor = TamValor - 1
			i = i - 1
		}
		if ((Mascara.substring(i,i+1) == 'X') && ((Valor.substring(i,i+1) < '0') || (Valor.substring(i,i+1) > 'z'))) {
			Valor = Valor.substring(0,i) + Valor.substring(i+1,TamValor)
			TamValor = TamValor - 1
			i = i - 1
		}
	}
	TamValor  = Valor.length

	// Remover Caracteres Reservados
	for (var i=0; i < TamValor; i++) {
	    if (TipoMascara.indexOf(Valor.substring(i,i+1))>-1) {
		   Valor = Valor.substring(0,i) + Valor.substring(i+1,TamValor)
		   i = i+1
		}
	}

	// Acescentar Caracteres Reservados
	for (var i=0; i < TamValor; i++) {
	    if (TipoMascara.indexOf(Mascara.substring(i,i+1))>-1) {
			if (Valor.substring(i,i+1) != Mascara.substring(i,i+1))
				Valor = Valor.substring(0,i) + Mascara.substring(i,i+1) + Valor.substring(i,TamValor)
		}
	}

	return Valor
	
} 

function ValidaValor(Valor) {

    if (document.body==null) return true
    if (document.body.createTextRange==null) return true

    // Convert key code to character
    var key = String.fromCharCode(event.keyCode)
	var dotRelationship
	var allowDigit
    var checkDot = false
    var allowDigit = false

    // Check if number of digits following decimal point is cached
    if (Valor._digits==null) {
      // Examine default value (initial value attribute)
 	  var strNum = Valor.defaultValue
      // Find the decimal point
      var idx = strNum.indexOf(".")
      // Calculate number of decimal digits
      if (idx>-1) 
        Valor._digits = strNum.length - idx - 1
      else
        Valor._digits = -1
    }

    // Make sure a digit or a decimal point
    if (!isNaN(key) || key==".") {
      // Get selection (either insertion point or text selection)
      var selectionRange = document.selection.createRange()
      // Store a copy for use in comparisons
      var tempSel = selectionRange.duplicate()

      // Variable for the input range 
      var inputRange = Valor.createTextRange()
 
      // Get index of the dot
      var dotPos = Valor.value.indexOf(".")
     
      // If decimal point, check if new character valid
      if (dotPos>-1) {
        // Locate existing decimal point 
        inputRange.findText(".")
        // When user types decimal, it is only accepted if
        // replacing existing decimal point
        if ((key==".") && (Valor._digits>-1)) {
          if (tempSel.findText("."))
          // Test if existing decimal in selection
          checkDot = selectionRange.inRange(tempSel)
        } else {
          // Determine whether the selection is before or after the decimal point
          dotRelationship = inputRange.compareEndPoints("EndToStart",selectionRange)
          // Test if new input is an allowable digit
          allowDigit = ((dotRelationship<=0 && (Valor.value.length - dotPos -1 < Valor._digits || selectionRange.text.length>0)) || (dotRelationship==1 && key!="."))
        }
      } else {
        // If inserting a decimal, test number of following digits
        if (key==".") 
          tempSel.moveEnd("textEdit",1)          
        allowDigit = (key=="." && (tempSel.text.length - 1 < Valor._digits)) || !isNaN(key)
      }
    }
    // Return whether the key is allowed

    var selectionRange = document.selection.createRange()
	selectionRange.moveEnd("textEdit",2)          

    return (checkDot || allowDigit)
  }

function AjustaValor(Valor) {

	var strValor = Valor.value
	var strNum = strValor.length

    if (strNum >-1 && strNum<1)
    {
       Valor.value = "0.00"
       return 
    }

    var PontoDecimal = strValor.indexOf(".") + 1

    if (PontoDecimal!=0) 
       Digitos = strNum - PontoDecimal 
    else
       Digitos = 0
        
    if (Digitos>-1 && Digitos<1)
       if (PontoDecimal!=0)
          Valor.value = Valor.value + "00"
       else
          Valor.value = Valor.value + ".00"
    else
       if (Digitos>0 && Digitos<2)
          Valor.value = Valor.value + "0"
  }
  
function update(Valor, Mascara, Tipo) {

	var ValorNovo   = ""
	var ValorAntigo = ""

	if (Valor.value!=null)  {ValorNovo   = Valor.value}
	if (Valor._value!=null) {ValorAntigo = Valor._value}

	var TamValorNovo   = ValorNovo.length
	var TamValorAntigo = ValorAntigo.length
	var TamMascara     = TamMascara     = Mascara.length

	// Encontrar Digito Novo
	for (var i=0; i < TamValorNovo; i++) {

		if (i <= TamValorAntigo)  {
			if (ValorNovo.substring(i,i+1) != ValorAntigo.substring(i,i+1)) {
			   break;
			}

		}
	}
	
}
//Valida data de nascimento
function validaDataNascimento(stData){

    dia = (stData.substring(0,2));
    mes = (stData.substring(3,5)); 
    ano = (stData.substring(6,10)); 

    situacao = ""; 
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
        situacao = "falsa"; 
    } 

    // verifica se o mes e valido 
    if (mes < 01 || mes > 12 ) { 
        situacao = "falsa"; 
    } 

    // verifica se eh ano bissexto 
    if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
        situacao = "falsa"; 
    }

    if (situacao == "falsa") { 
        alert("Data inválida!");
		document.form1.tdtnascimento.value = "" ;
		document.form1.tdtnascimento.focus();
    }
	
}

function validaDataEmissao(stData){

    dia = (stData.substring(0,2));
    mes = (stData.substring(3,5)); 
    ano = (stData.substring(6,10)); 

    situacao = ""; 
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
        situacao = "falsa"; 
    } 

    // verifica se o mes e valido 
    if (mes < 01 || mes > 12 ) { 
        situacao = "falsa"; 
    } 

    // verifica se eh ano bissexto 
    if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
        situacao = "falsa"; 
    }

    if (situacao == "falsa") { 
        alert("Data inválida!");
		document.form1.tdtemissao.value = "" ;
		document.form1.tdtemissao.focus();
    }
	
}

//valida data de nascimento do benefíciário
function validaDataNcBeneciciario(stData){

    dia = (stData.substring(0,2));
    mes = (stData.substring(3,5)); 
    ano = (stData.substring(6,10)); 

    situacao = ""; 
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
        situacao = "falsa"; 
    } 

    // verifica se o mes e valido 
    if (mes < 01 || mes > 12 ) { 
        situacao = "falsa"; 
    } 

    // verifica se eh ano bissexto 
    if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
        situacao = "falsa"; 
    }

    if (situacao == "falsa") { 
        alert("Data inválida!");
		document.formulario.tnasc_beneficiario.value = "" ;
		document.formulario.tnasc_beneficiario.focus();
    }
	
}

//valida se campo é numérico
function numerico() {
	if( !(event.keyCode >= 48 && event.keyCode <= 57 ) )
	return false;
}

//Validação dos dados do formulário
    function validaDados(){

        //Valida o campo "Nome"
    	if (document.form1.tnome.value == ""){
    		alert('Por favor, informe o \"Nome\" do proponente.');
    		document.form1.tnome.focus();
    		return false;
    	}

		//Valida o campo "CPF"
    	if (document.form1.tcpf.value == ""){
    		alert('Por favor, informe o \"CPF\".');
    		document.form1.tcpf.focus();
    		return false;
    	}

		//Verifica se o CPF e válido
		if(ValidaCpf(document.form1.tcpf.value) ==  false){
    		alert('Por favor, informe um \"CPF\" válido.');
    		document.form1.tcpf.focus();
    		return false;
		}
		
		//Valida o campo "data de nascimento"
    	if (document.form1.tdtnascimento.value == "" || document.form1.tdtnascimento.length < 10){
    		alert('Por favor, informe o \"CPF\".');
    		document.form1.tcpf.focus();
    		return false;
    	}
		
		//Valida o campo "cbx sexo"
    	if (document.form1.cbxsexo.value == 0){
    		alert('Por favor, informe o \"Sexo\" do proponente.');
    		document.form1.cbxsexo.focus();
    		return false;
		}
		
		//Valida o campo "etado civil"
    	if (document.form1.cbxestadocivil.value == 0){
    		alert('Por favor, informe o \"Estado Civil\" do proponente.');
    		document.form1.cbxestadocivil.focus();
    		return false;
		}
		
		//Valida o campo "rg"
    	if (document.form1.trg.value == ""){
    		alert('Por favor, informe o \"RG\" do proponente.');
    		document.form1.trg.focus();
    		return false;
		}
		//Valida o campo "Ogão Emissor"
    	if (document.form1.cbxOEmissor.value == 44){
    		alert('Por favor, informe o \"Orgão Emissor\" do Rg do proponente.');
    		document.form1.cbxOEmissor.focus();
    		return false;
		}
		
		//Valida o campo "cbx sexo"
    	if (document.form1.cbxsexo.value == 0){
    		alert('Por favor, informe o \"Orgão Emissor\" do Rg.');
    		document.form1.ccbxsexo.focus();
    		return false;
		}
		
		//Valida o campo "Profissão"
    	if (document.form1.cbxprofissao.value == 0){
    		alert('Por favor, informe a \"Profissão\" do proponente.');
    		document.form1.cbxprofissao.focus();
    		return false;
		}
		
		//Valida o campo "Endereço"
    	if (document.form1.tendereco.value == ""){
    		alert('Por favor, informe o \"Endereço\" do proponente.');
    		document.form1.tendereco.focus();
    		return false;
		}
		
		//Valida o campo "Nº do endereço"
    	if (document.form1.tnumero.value == ""){
    		alert('Por favor, informe o \"Número\" do Endereço do Proponente.');
    		document.form1.tnumero.focus();
    		return false;
		}
		
		//Valida o campo "Bairro"
    	if (document.form1.tbairro.value == ""){
    		alert('Por favor, informe o \"Bairro\" do Endereço do Proponente.');
    		document.form1.tbairro.focus();
    		return false;
		}
		
		//Valida o campo "Cidade"
    	if (document.form1.tcidade.value == ""){
    		alert('Por favor, informe a \"Cidade\" do Endereço do Proponente.');
    		document.form1.tcidade.focus();
    		return false;
		}
		
		//Valida o campo "UF"
    	if (document.form1.cbxuf.value == 0){
    		alert('Por favor, informe a \"UF\" do Endereço do Proponente.');
    		document.form1.cbxuf.focus();
    		return false;
		}
		
		//Valida o campo "CEP"
    	if (document.form1.tcep.value == ""){
    		alert('Por favor, informe o \"CEP\" do Endereço do Proponente.');
    		document.form1.tcep.focus();
    		return false;
		}
		//Valida o campo "dddFone"
    	if (document.form1.tdddresidencial.value == ""){
    		alert('Por favor, informe o \"DDD\" do Telefone.');
    		document.form1.tdddresidencial.focus();
    		return false;
		}
		//Valida o campo "TelefoneFone"
    	if (document.form1.tfoneresidencial.value == ""){
    		alert('Por favor, informe o \"Telefone\".');
    		document.form1.tfoneresidencial.focus();
    		return false;
		}
		
	    //valida frequencia de pagamento
		if (document.form1.radio[0].checked == false && document.form1.radio[1].checked == false && document.form1.radio[2].checked == false && document.form1.radio[3].checked == false && document.form1.radio[4].checked == false){
		    alert('Por favor, informe uma \"Frequencia de Pagamento\".');
    	    return false;
	    }
		
		//valida forma de pagamento
		if (document.form1.radio2[0].checked == false && document.form1.radio2[1].checked == false){
		    alert('Por favor, informe uma \"Forma de Pagamento(Boleto ou Débito em conta)\".');
    	    return false;
	    }

		//valida bancos caso a opção débito em conta seja escolhida
		if (document.form1.radio2[1].checked == true){
			if (document.form1.radio3[0].checked == false && document.form1.radio3[1].checked == false){
		    alert('Por favor, informe um dos\"Bancos\" disponíveis.');
    	    return false;
			}

		}		
		
		//valida dados da conta bancária
		if (document.form1.radio2[1].checked == true){
			if (document.form1.tagencia.value == ""){
		    alert('Por favor, informe o número da\"Agência\" bancária do proponente.');
    	    return false;
			}
		}
		if (document.form1.radio2[1].checked == true){
			if (document.form1.tconta.value == ""){
		    alert('Por favor, informe o número da\"Conta Corrente\" do Proponente.');
    	    return false;
			}
		}
		if (document.form1.radio2[1].checked == true){
			if (document.form1.tdia.value == ""){
		    alert('Por favor, informe o\"Dia\" do mês para Débito em conta.');
    	    return false;
			}
		}
		//valida se o usuário concorda com as condições gerais do seguro.
		if (document.form1.radioCG[0].checked == false && document.form1.radioCG[1].checked == false ){
			alert('Por favor, informe se aceita as condições gerais do seguro.');
			document.form1.termo.focus();
    	    return false;
			}
		if (document.form1.radioCG[1].checked == true){
			alert('Caso ainda tenha dúvidas, entre em contato com a nossa central de atendimento: 0800 644 6767.');
			document.form1.termo.focus();
    	    return false;
			}
	}
//verifica se o dia do mês informado pelo usuário é válido
function diasmes() {
	
	if (document.form1.tdia.value > 31 || document.form1.tdia.value < 1 ){
	alert("Dia inválido");
	document.form1.tdia.focus();
	return document.form1.tdia.value="";
	}
}

/*
function ValidaCpf(num)
{
var i
var j
var soma
var aux
var digito
cpf = num


  if (num.length != 11){
	alert('Número do CPF Inválido !')
	 return false;}

  if (num == "00000000000" || num == "11111111111" || num == "22222222222" || num == "33333333333" || num == "44444444444"
  		|| num == "55555555555" || num == "66666666666" || num == "77777777777" || num == "88888888888"
		|| num == "99999999999"){
	 alert('Número do CPF Inválido !')	
	 return false;}

  aux = num.substr(0, num.length-2);
  for (j=1; j<=2; j++)
  {
	soma = 0;
	for (i=0; i<=aux.length-1; i++)
	{
		soma = soma + (aux.substr(aux.length - 1 - i, 1) * (i + 2) * 10);
	}
	aux = aux + "01234567890".substr((soma % 11), 1);
  }

  if (aux != num){
	alert('Número de CPF Inválido')
	return false;}
  if (aux == num){
  	return true;}
}			

*/

















