function apenasNumeros(e){
	navegador = /msie/i.test(navigator.userAgent);
		if (navegador)
			var tecla = event.keyCode;
		else
			var tecla = e.which;
		if(tecla > 47 && tecla < 58 || tecla==0) // numeros de 0 a 9

			return true;
		else{
			if (tecla != 8 ) // backspace
		return false;
		else
			return true;
	}
}



function preparar(){
	var faixa = this.getIdade();
	var cancer = this.getCancer();
	submeter = validarCampos();

	
	if(submeter){
		for(i=0; i < faixa.length; i++){	
		if(faixa.options[i].selected==true){
			document.frm.faixa.value = faixa.options[i].text;
			break;
		} 
	}
		
		for(i=0; i < cancer.length; i++){
			if(cancer.options[i].selected==true){
				document.frm.capitalCancer.value = cancer.options[i].text;
				break;
			} 
		}
		
		
		return true;	
	}
	
}

function calcular(idForm){
	if(preparar()){
		document.frm.action ='contrato.php';
		document.frm.submit();
	}
}

function getIdade(){
	return document.getElementById('fatorFaixaEtaria');	
}

function getCancer(){
	return document.getElementById('capitalMorte');	
	
}

function getNome(){
	return document.getElementById('nome').value
}

function getDDD(){
	return document.getElementById('ddd').value;
}
function getNumero(){
	return document.getElementById('telefone').value
}
function getEmail(){
	return document.getElementById('email').value
}
function getCidade(){
	return document.getElementById('cidade').value
}
function getEstado(){
	estado=	document.getElementById('estado');
	for(i=0; i < estado.length; i++){
		if(estado.options[i].selected==true){
			return estado.options[i].value;
			break;
		} 
	}
}


function getSexo(){
	sexo=	document.getElementById('sexo');
	for(i=0; i < sexo.length; i++){
		if(sexo.options[i].selected==true){
			return sexo.options[i].value;
			break;
		} 
	}
}

function capitalCancerValidate(){
	var cancer = this.getCancer();
	for(i=0; i < cancer.length; i++){
			if(cancer.options[i].selected==true){
				return cancer.options[i].value;
				break;
			} 
		}
}

function validarCampos(){
	idade = this.getIdade();
	nome = this.getNome();
	ddd = this.getDDD();
	numero = this.getNumero();
	email = this.getEmail();
	cidade = this.getCidade();
	estado = this.getEstado();
	var cancer = this.capitalCancerValidate();
	
	if(idade == '' || nome== '' || ddd== '' || numero=='' || email=='' || cidade=='' || estado == 'selecione' || cancer=='selecione' ){
		alert('É preciso preencher todos os campos para poder realizar a simulação.');
		return false;
	}
	return true;
	
}



function getDebConta(){
	return document.getElementById('debCnta');	
}

function getCartaoCredito(){
	return document.getElementById('crtCred');	
}

function getCarne(){
	return document.getElementById('carne');	
}


function getData(){
	return document.getElementById('dataCarne');	
}

function getAgencia(){
	return document.getElementById('agencia');	
}

function getConta(){
	return document.getElementById('conta');	
}

function getBanco(){
	return document.getElementById('banco');	
}

function getNumeroCartao(){
	return document.getElementById('numeroCartao');	
}

function getCodigoSeg(){
	return document.getElementById('codSeg');	
}

function getValidade(){
	return document.getElementById('validade');	
}

function getRadios(status,disable){
	radios = document.getElementsByName('radio3');	
	 for(i=0;i<radios.length;i++){
		 radios[i].checked=status;
		 radios[i].disabled=disable;

	 }

}

function getRadioOutro(status){
	radioOutro = document.getElementById('outros');	
	radioOutro.readOnly=status;
	if(status){
			radioOutro.value="";
	}
	return radioOutro;
}

function validate( id ){
	
 debito = this.getDebConta();
 cartao = this.getCartaoCredito();
 carne  = this.getCarne();
 dataCarne = this.getData();
 agencia = this.getAgencia();
 conta = this.getConta();
 banco = this.getBanco();
 numeroCartao = this.getNumeroCartao();
 codSeg = this.getCodigoSeg();
 validade = this.getValidade();
 radioOutr = this.getRadioOutro(true);
 
 switch(id){
	case debito.id:
		cartao.checked=false;
		carne.checked=false;
		dataCarne.readOnly=true;
		dataCarne.value="";
		debito.checked=true;
		dataCarne.value="";
		dataCarne.readOnly=true;
		numeroCartao.value="";
		numeroCartao.readOnly=true;
		codSeg.value="";
		codSeg.readOnly=true;
		validade.value="";
		validade.readOnly=true;
		agencia.readOnly=false;
		conta.readOnly=false;
		banco.readOnly=false;
		this.getRadios(false,true);
		radioOutr.readOnly=true;
		break;

	case cartao.id:
		debito.checked=false;
		cartao.checked=true;
		carne.checked=false;
		dataCarne.value="";
		dataCarne.readOnly=true;
		agencia.value="";
		agencia.readOnly=true;
		conta.value="";
		conta.readOnly=true;
		banco.value="";
		banco.readOnly=true;
		numeroCartao.readOnly=false;
		codSeg.readOnly=false;
		validade.readOnly=false;
		this.getRadios(false,false);
		radioOutr.readOnly=true;
		break;
	
	case carne.id:
		debito.checked=false;
		cartao.checked=false;
		dataCarne.value="";
		dataCarne.readOnly=false;
		agencia.value="";
		agencia.readOnly=true;
		conta.value="";
		conta.readOnly=true;
		banco.value="";
		banco.readOnly=true;
		numeroCartao.value="";
		numeroCartao.readOnly=true;
		codSeg.value="";
		codSeg.readOnly=true;
		validade.value="";
		validade.readOnly=true;
		this.getRadios(false,true);
		radioOutr.readOnly=true;
		break;
	
 }
 	
}


function mascara (formato,objeto){
campo = eval (objeto);

// cep
	if (formato=='cep'){
		separador = '-';
		conjunto1 = 5;
	if (campo.value.length == conjunto1){
		campo.value = campo.value + separador;}
	}

// cpf
	if (formato=='cpf'){
		separador1 = '.'; 
		separador2 = '-'; 
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
	if (campo.value.length == conjunto1)
	  {
	 	campo.value = campo.value + separador1;
	  }
	if (campo.value.length == conjunto2)
	  {
	  	campo.value = campo.value + separador1;
	  }
	if (campo.value.length == conjunto3)
	  {
	  	campo.value = campo.value + separador2;
	  }
	}


	if (formato=='rg'){
		separador2 = '-'; 
		conjunto2 = 8;
		if (campo.value.length == conjunto2)
		  {
		  	campo.value = campo.value + separador2;
		  }
	}



// nascimento
	if (formato=='nascimento'){
	separador = '/'; 
	conjunto1 = 2;
	conjunto2 = 5;
	if (campo.value.length == conjunto1)
	  {
	  	campo.value = campo.value + separador;
	  }
	if (campo.value.length == conjunto2)
	  {
	  	campo.value = campo.value + separador;
	  }
	}

// telefone
	if (formato=='telefone'){
		separador1 = '(';
		separador2 = ')';
		separador3 = '-';
		conjunto1 = 0;
		conjunto2 = 3;
		conjunto3 = 8;
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador3;
			}
		}
		
	if (formato=='telefoneR'){
		separador3 = '-';
		conjunto1 = 0;
		conjunto2 = 3;
		conjunto3 = 8;
		
			if (campo.value.length == 4){
				campo.value = campo.value + separador3;
			}
		}
}


function getCartaoSelecionado(){
	return document.getElementsByName('radio3');	
}

function setarCartao(){
	cartao = this.getCartaoSelecionado();
	
	for(i=0;i<cartao.length;i++){
		if(cartao[i].checked){
		document.frmContrato.cartaoSel =cartao[i].value;
		}
	}
	
}

function formaPgto(obj){
	document.frmContrato.formaEscolhida.value = obj.value;	
	
}


function getPercent(numero){
	if(numero==1){
		return document.getElementById('percentual').value;
	}else{
		return document.getElementById('percentual'+numero).value;	
	}
}

function validaPercentagem(){
	total = 100;	
	percent1 = this.getPercent(1);
	percent2 = this.getPercent(2);
	percent3 = this.getPercent(3);
	percent4 = this.getPercent(4);
	soma=0.0;
	if(percent2!=''){
		percent1 = parseFloat(percent1);
		percent2 = parseFloat(percent2);
		soma = 	percent1+percent2;	
		if(percent3!=''){
			percent3 = parseFloat(percent3);
			soma += percent3;
			if(percent4!=''){
				percent4=parseFloat(percent4);
				soma+=percent4;
			}
		}
	}else{
		percent1 = parseFloat(percent1);
		soma = percent1;
	}
	if(soma>100.00){
		alert('A soma das partes não pode exeder 100%');
		return false;
	}
	return true;
}


function FormataValor(fld, milSep, decSep, e) { 
	if (fld.value.length == 1 && fld.value == '0'){
			return true;
	}
	if ( fld != null ) {
		var sep = 0; 
		var key = ''; 
		var i = j = 0; 
		var len = len2 = 0; 
		var strCheck = '0123456789'; 
		var aux = aux2 = ''; 
		
		len = fld.value.length; 
		for(i = 0; i < len; i++) 
			if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break; 
		aux = ''; 
		for(; i < len; i++) 
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i); 
		aux += key; 
		len = aux.length; 
		if (len == 0) fld.value = ''; 
		if (len == 1) fld.value = '0'+ decSep + '0' + aux; 
		if (len == 2) fld.value = '0'+ decSep + aux; 
		if (len > 2) { 
			aux2 = ''; 
			for (j = 0, i = len - 3; i >= 0; i--) { 
				if (j == 3) { 
					aux2 += milSep; 
					j = 0; 
				} 
				aux2 += aux.charAt(i); 
				j++; 
			} 
			fld.value = ''; 
			len2 = aux2.length; 
			for (i = len2 - 1; i >= 0; i--) 
				fld.value += aux2.charAt(i); 
			fld.value += decSep + aux.substr(len - 2, len); 
		} 
		return false; 
	} else 
		return false;
} 



function enviarProposta(){
	if(validarSubmit()){
		document.frmContrato.action='relat/generate.php';
		document.frmContrato.submit();
	} else {
		return null;
		
	}

}

function getDataNascimento(){
	return document.frmContrato.value;
	
}



function validarSubmit(){
	oForm = document.frmContrato;
	
	// if(validarPercentualSubmit()){
	
		idade = oForm.idade.value;
		nome = oForm.nome.value;
		dtaNasc = oForm.dtaNasc.value;
		natural = oForm.natural.value;
		nacionalidade = oForm.nacionalidade.value;
		estCivil = oForm.estCivil.value;
		/*sexo =  this.getSexo();
		cpf = oForm.cpf.value;
		rg = oForm.rg.value;
		dtaEmissao = oForm.dtaEmissao.value;
		rendaMensal = oForm.rendaMensal.value;
		atividadeProfissional = oForm.atividadeProfissional.value;*/
		endereco = oForm.endereco.value;
		numero = oForm.numero.value;
		complemento = oForm.complemento.value;
		bairro = oForm.bairro.value;
		cidade = oForm.cidade.value;
		telefone = oForm.telefoneR.value
		
		
		if(idade =='' || nome =='' || cidade=="" || telefone==""){
			alert('É preciso preencher todos os campos do Proponente para enviar a prosposta.');
			return false;
		}
	/*	
		if(idade =='' || nome =='' || dtaNasc =='' || natural =='' || nacionalidade =='' || estCivil =='' || sexo =='' || cpf =='' || rg =='' || 
		   dtaEmissao =='' || rendaMensal =='' || rendaMensal =='' || atividadeProfissional =='' || endereco =='' || numero =='' || complemento =='' || bairro =='' || cidade){
			alert('É preciso preencher todos os campos do Proponente para enviar a prosposta.');
			return false;
		}*/
	//}
	return true;	
}


function validarPercentualSubmit(){
	percent1 = this.getPercent(1);
	percent2 = this.getPercent(2);
	percent3 = this.getPercent(3);
	percent4 = this.getPercent(4);
	soma=0.0;
	
	if(percent2!=''){
		percent1 = parseFloat(percent1);
		percent2 = parseFloat(percent2);
		soma = 	percent1+percent2;	
		if(percent3!=''){
			percent3 = parseFloat(percent3);
			soma += percent3;
			if(percent4!=''){
				percent4=parseFloat(percent4);
				soma+=percent4;
			}
		}
	}else{
		percent1 = parseFloat(percent1);
		soma = percent1;
	}
	if(soma > 100){
		alert('A soma das partes não pode exeder 100%');
		return false;
	}	
	return true;
}