<!--

//////////////////////////////////////////////////////////////////////////////////////////
// Formata um número em formato moeda.
//////////////////////////////////////////////////////////////////////////////////////////
function fncFormatarMoeda(ad_numero) {
var ls_numero;
var li_posDecimal;
var ls_parteDecimal;
var ls_parteInteira;
var ls_parteInteiraNova;
var li_qtdeSepMilhar;
var i;

	ls_numero = "";
	if (ad_numero != "") {
		ad_numero = Math.round(100 * ad_numero)/100;
		ls_numero = ad_numero.toString().replace(".", ",");
		li_posDecimal = ls_numero.indexOf(",");
		if (li_posDecimal>0) {
			ls_parteDecimal = ls_numero.substr(li_posDecimal+1, ls_numero.length);
			ls_parteInteira = ls_numero.substr(0, li_posDecimal);
			//complementa a parte decimal com zeros, se necessário
			while (ls_parteDecimal.length<2) {
				ls_parteDecimal = ls_parteDecimal + "0";
			}
		} else {
			ls_parteDecimal = "00";
			ls_parteInteira = ls_numero;
		}

		//agora insere os separadores de milhar na parte inteira
		li_qtdeSepMilhar = parseInt((ls_parteInteira.length-1)/3)
		i = 1;
		ls_parteInteiraNova = "";
		while (i <= li_qtdeSepMilhar) {
			ls_parteInteiraNova = "." + ls_parteInteira.substr((ls_parteInteira.length-i*3), 3) + ls_parteInteiraNova;
			i++;
		}
		if (li_qtdeSepMilhar == 0) {
			ls_parteInteiraNova = ls_parteInteira;
		} else {
			ls_parteInteiraNova = ls_parteInteira.substr(0, ls_parteInteira.length-li_qtdeSepMilhar*3) + ls_parteInteiraNova;
		}
		
		//monta o resultado final
		ls_numero = ls_parteInteiraNova + "," + ls_parteDecimal;	
	}
	return(ls_numero);
}

////////////////////////////////////////////////////////////
// retira caracteres invalidos da string 
////////////////////////////////////////////////////////////
function fncLimparCaracterInvalido(valor, validos) { 
	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; 
} 

//////////////////////////////////////////////////////////////
//Formata número tipo moeda usando o evento onKeyDown 
//////////////////////////////////////////////////////////////
function fncFormataMoedaOnKeyDown(campo,tammax,teclapres,decimal) { 
	var tecla = teclapres.keyCode; 
	vr = fncLimparCaracterInvalido(campo.value,"0123456789"); 
	tam = vr.length; 
	dec=decimal 

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; } 
	if (tecla == 8 ){ 
		tam = tam - 1 ; } 

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) 
	{ 
		if ( tam <= dec ){ 
			campo.value = vr ; } 
		if ( (tam > dec) && (tam <= 5) ){ 
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; } 
		if ( (tam >= 6) && (tam <= 8) ){ 
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, 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 ) ;} 
	}
}

//////////////////////////////////////////////////////////////////////////////////////////
// Validar campo.
//////////////////////////////////////////////////////////////////////////////////////////
function fncValidarForm(p_form, p_tipo)
{
	eval("p_form="+p_form);
	var mensagem = "";
	var temp = "";
	for(var i=0;i<p_form.length;i++){		
		if(p_form[i].id.indexOf("CT")!=-1){
			var l_control = p_form[i].id.split("_");
			if(l_control.length>1){
				if(p_tipo.indexOf(l_control[3])!=-1){
						Trim(p_form[i]);
						temp += fncValidar(p_form[i],p_form[i].name.substring(4,p_form[i].name.length),l_control[1],l_control[2]);						
						if (temp != "")
						{
							mensagem += temp + "\n";
							temp = "";
						}
					}
				}
			}		
		}
		if(mensagem != "")
		{
			alert(mensagem);
			return false;
		}
		else
			return true;
}

function fncValidar(objeto,nome,tipo,obrigatorio){
	var msg = "";	
	if (objeto.value.length > 0){
		tipo = tipo.toUpperCase();
		eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
		switch (tipo){
			case "RADIO":				
				if(nome == "Tipo de Residencia")
				{
					if((document.form1.CT41_RADIO_TRUE_B.checked == false) && (document.form1.CT42_RADIO_TRUE_B.checked == false)){
						msg += "Campo " + nome + ": preenchimento inválido.";
						eval("document.form1.CT41_RADIO_TRUE_B.style.backgroundColor = '#FFF686'");
						eval("document.form1.CT42_RADIO_TRUE_B.style.backgroundColor = '#FFF686'");
					}
					else{
						eval("document.form1.CT41_RADIO_TRUE_B.style.backgroundColor = '#ffffff'");
						eval("document.form1.CT42_RADIO_TRUE_B.style.backgroundColor = '#ffffff'");
					}
				}
				break;
			case "CNPJ":
				if (fncCNPJ(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.";									
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "CPF":
				if (fncCPF(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "CNPJCPF":				
				if (fncCNPJ(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{
					if (fncCPF(objeto.value)){
						eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
					}
					else{						
						msg += "Campo " + nome + ": preenchimento inválido.";
						eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
					}
				}
				break;
			case "DATA":
				if (fncData(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.\nPor favor, utilize o formato 'DD/MM/AAAA' válido.";	
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "EMAIL":
				if (fncEmail(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");					
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "MOEDA":
				if (fncMoeda(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "NUMERO":
				if (fncNumero(objeto.value)){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{					
					msg += "Campo " + nome + ": preenchimento inválido.\nPor favor, utilize somente números.\n";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				break;
			case "NUMESPECIAL":
				if(!contaTamanho(objeto)){
					msg += "Campo " + nome + ": preenchimento inválido. Preencha o campo com " + objeto.maxLength + " números.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				else{
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				if (fncNumero(objeto.value) && (contaTamanho(objeto))){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{
					if(!fncNumero(objeto.value)){
						msg += "Campo " + nome + ": preenchimento inválido.";
						eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
					}
				}
				break;			
			case "MES":
				if(!contaTamanho(objeto) || (objeto.value < 1) || (objeto.value > 12)){
					msg += "Campo " + nome + ": preenchimento inválido. Digite o mês no formato 'MM'.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				else{
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				if (fncNumero(objeto.value) && (contaTamanho(objeto) && (objeto.value < 13) && (objeto.value > 0))){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{
					if(!fncNumero(objeto.value)){
						msg += "Campo " + nome + ": preenchimento inválido.";
						eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
					}
				}
				break;
			case "ANO":
				if(!contaTamanho(objeto) || (objeto.value < 1900) ){
					msg += "Campo " + nome + ": preenchimento inválido. Digite o ano no formato 'AAAA'.";
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
				}
				else{
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				if (fncNumero(objeto.value) && (contaTamanho(objeto) && (objeto.value > 1899))){
					eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");
				}
				else{
					if(!fncNumero(objeto.value)){
						msg += "Campo " + nome + ": preenchimento inválido.";
						eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
					}
				}
				break;
		}
  }  
  else{
		if(obrigatorio.toUpperCase()=="TRUE"){			
			msg += "Campo " + nome + ": preenchimento obrigatório.";
			eval("document.form1." + objeto.id + ".style.backgroundColor = '#FFF686'");
		}
		else{
			eval("document.form1." + objeto.id + ".style.backgroundColor = '#ffffff'");			
		}
	}
	
	return msg;		
	
}//end fncValidar

//conta o tamanho do campo para validacao de campos numericos
function contaTamanho(f)
{
	if(f.value.length==f.maxLength){
		return true;
	}
	else
		return false;
}//end contaTamanho

//função responsável pela validação da Moeda
function fncMoeda(moeda){
var i = 0;
var prtMoeda1 = 0;

	prtMoeda1 = moeda.split(",");
	//window.alert(prtMoeda1.length);
	if (prtMoeda1.length == 2) {
		if (prtMoeda1[1].length > 0){
			if (!(fncNumero(prtMoeda1[1]))){
				return false;
			}
		}
		else{
			return false;
		}
	}
	else{ 
		if (prtMoeda1.length > 2) {
				return false;
		}
	}
	
	var prtMoeda2 = 0;
	prtMoeda2 = prtMoeda1[0].split(".");
	//window.alert(prtMoeda2.length);
	if ((prtMoeda1.length == 1) && (prtMoeda2.length == 2) && (prtMoeda2[0].length == 0)){
		
	}
	else{
		if ((prtMoeda2.length > 1) && ((prtMoeda2[0].length < 1) || (prtMoeda2[0].length > 3))){
			return false;
		}
	}
	//window.alert(fncNumero(prtMoeda2[0]));
	if (!(fncNumero(prtMoeda2[0]))){
		return false;
	}
	
	for (i=1;i<prtMoeda2.length;i++){
		//window.alert(prtMoeda2.length);
		if (prtMoeda2[i].length == 3){
			if (!(fncNumero(prtMoeda2[i]))){
				return false;
			}
		}
		else{
			if ((prtMoeda1.length == 1) && (prtMoeda2.length == 2)){
				if (prtMoeda2[i].length > 0){
					if (!(fncNumero(prtMoeda2[i]))){
						return false;
					} 
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}
	}

return true;
}//end fncMoeda

//função responsável pela validação do Número
function fncNumero(numero){   
var i = 0;

  for (i=0;i<numero.length;i++){
		if (!((numero.charAt(i) >= "0") && (numero.charAt(i) <= "9"))){
			return false;
		}
  }
  return true;
}//end fncNumero  

//função responsável pela validação dos Números
function fncPressKeyIsNumeric(objeto,evt){
  var numero = ""; 
  var tecla = (evt.which) ? evt.which : evt.keyCode;
  var seta = "37,38,39,40";
  if (seta.indexOf(tecla) == -1){
		for (i=0;i<objeto.value.length;i++){
			if (fncNumero(objeto.value.substring(i,i+1))){
				numero = numero + objeto.value.substring(i,i+1)
			}
		}
	objeto.value = numero;
	}
}//end fncPressKeyIsNumeric

//função responsável pela validação da Data
function fncData(data) { 
var prtData = data.split("/");

	if (prtData.length != 3){
		return false;
	}
	
	var dia = prtData[0]; 
	var mes = prtData[1];
	var ano = prtData[2];
	
	//verifica se só foi digitado caracter númerico - dia
	if ((dia.length == 1) || (dia.length == 2)){
		if (!(fncNumero(dia))){
			return false;
		}
	}
	else{
		return false;
	}
	
	//verifica se só foi digitado caracter númerico - mes
	if ((mes.length == 1) || (mes.length == 2)){
		if (!(fncNumero(mes))){
			return false;
		}
	}
	else{
		return false;
	}
	
	//verifica se só foi digitado caracter númerico - ano
	if ((ano.length == 4) && (ano < 2007) && (ano > 1900)){
		if (!(fncNumero(ano))){
			return false;
		}
	}
	else{
		return false;
	}
	  
  // verifica o dia valido para cada mes 
  if ((dia < 1)||(dia < 1 || dia > 30) && (mes == 4 || mes == 6 || mes == 9 || mes == 11 ) || dia > 31) { 
		return false;
  } 

  // verifica se o mes e valido 
  if ((mes < 1) || (mes > 12)) { 
		return false;
  } 

  // verifica se e ano bissexto 
  if (mes == 2 && ( dia < 1 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
		return false;
  } 
  
  if((ano < 1900) || (ano > 2007)){
	  return false;
  }  
	 
return true;
}//end fncData 

//função responsável pela validação do CPF
function fncCPF (cpf) {
if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") return false;

soma = 0;
for (i=0; i < 9; i ++)
	soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11) resto = 0;
	if (resto != parseInt(cpf.charAt(9))) return false;

soma = 0;
for (i = 0; i < 10; i ++)
	soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11) resto = 0;
	if (resto != parseInt(cpf.charAt(10))) return false;

return true;
}//end fncCPF

//função responsável pela validação do CNPJ
function fncCNPJ (cnpj) {
if (cnpj.length != 14 || cnpj == "00000000000" || cnpj == "11111111111" || cnpj == "22222222222" || cnpj == "33333333333" || cnpj == "44444444444" || cnpj == "55555555555" || cnpj == "66666666666" || cnpj == "77777777777" || cnpj == "88888888888" || cnpj == "99999999999") return false;

soma = 0;
	soma = parseInt(cnpj.charAt(0)) * 5 + parseInt(cnpj.charAt(1)) * 4 + parseInt(cnpj.charAt(2)) * 3 + parseInt(cnpj.charAt(3)) * 2 + parseInt(cnpj.charAt(4)) * 9 + parseInt(cnpj.charAt(5)) * 8 + parseInt(cnpj.charAt(6)) * 7 + parseInt(cnpj.charAt(7)) * 6 + parseInt(cnpj.charAt(8)) * 5 + parseInt(cnpj.charAt(9)) * 4 + parseInt(cnpj.charAt(10)) * 3 + parseInt(cnpj.charAt(11)) * 2;
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11) resto = 0;
	if (resto != parseInt(cnpj.charAt(12))) return false;

soma = 0;
	soma = parseInt(cnpj.charAt(0)) * 6 + parseInt(cnpj.charAt(1)) * 5 + parseInt(cnpj.charAt(2)) * 4 + parseInt(cnpj.charAt(3)) * 3 + parseInt(cnpj.charAt(4)) * 2 + parseInt(cnpj.charAt(5)) * 9 + parseInt(cnpj.charAt(6)) * 8 + parseInt(cnpj.charAt(7)) * 7 + parseInt(cnpj.charAt(8)) * 6 + parseInt(cnpj.charAt(9)) * 5 + parseInt(cnpj.charAt(10)) * 4 + parseInt(cnpj.charAt(11)) * 3 + parseInt(cnpj.charAt(12)) * 2;
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11) resto = 0;
	if (resto != parseInt(cnpj.charAt(13))) return false;

return true;
}//end fncCNPJ

//função responsável pela validação do E-mail
function fncEmail(email) {
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return true
	}
return (false)
}//end fncEmail

//função responsável pela máscara da Data
function fncMskData(objeto,evt){ 
  var data = ""; 
  var tecla = (evt.which) ? evt.which : evt.keyCode;
  var seta = "37,38,39,40";
  if (seta.indexOf(tecla) == -1){
		for (i=0;i<objeto.value.length;i++){
			if (fncNumero(objeto.value.substring(i,i+1))){
				data = data + objeto.value.substring(i,i+1)
				if (data.length == 2){
				    data = data + '/'; 
				}
				if  (data.length == 5){ 
				   data = data + '/'; 
				}
			}
		}
	objeto.value = data;
	}
}//end fncMskData 

//função responsável pela retirada de espaços do campo
function Trim(nomeCampo)
{
	var str = new String();	
	str = nomeCampo.value;
	
	while (str.charAt(0) == " ")
	str = str.substr(1,str.length -1);
	
	while (str.charAt(str.length-1) == " ")
	str = str.substr(0,str.length-1);	
	
	nomeCampo.value = str;
}//end Trim

//funcao que retira valores de campos para melhor interpretacao no email
function mudaValorCampo(verdade)
{
	if (document.form1.CT000.checked == true)
	{
		window.document.form1.HiddenCT02.disabled = verdade;
	}
	else
	{
		document.form1.HiddenCT02.value = "1 + " + document.form1.CT003.value + " de " + document.form1.CT004.value;									
	}	
	
	window.document.form1.CT003.disabled = verdade;
	window.document.form1.CT004.disabled = verdade;
	
}//end mudaValorCampo
//-->