function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function SimpleDialog(url,target,dialogWidth,dialogHeight) {
	if (!dialogWidth)	{dialogWidth=300;dialogHeight=400;} //valores default
	r=showModalDialog(url,window,"scroll: No; dialogHeight: "+dialogHeight+"px; dialogWidth: "+dialogWidth+"px; edge: Raised; center: Yes; help: No; resizable: No; status: No;unadorned: YES;"); //dialogTop: 452px; dialogLeft: 991px; 
	if (r){
		if (target) {
      target.navigate(target.location);
		} else {
			navigate(location);
		}
  }
	return r;
}

//*****************************************************************************
//* Verifica se os valores digitados são numéricos ou com o caracter hífen
//*****************************************************************************
function SoNumeros(valor){
	char = '1234567890-';
	qtde = valor;
	ret = true;
	i = 0;
	while ((i < qtde.length) && (ret)) {
		x = 0;
		while ((x < char.length) && (ret)) {
			if (qtde.substr(i,1) == char.substr(x,1)) {
				ret = false;
			}
			x++;
		}
		ret = !ret;
		i++;
	}
	return ret;
}

//*****************************************************************************
//* Valida o campo passado
//*****************************************************************************
function ValidaCampo(obj){
	valor=obj.value
	if (!SoNumeros(valor)){
		alert("Por favor, digite apenas números.");
		obj.value='';
		obj.focus();
		return (false);
	} else if (SoNumeros(valor) && valor.length < 9){
		alert("Por favor, entre com um C.E.P. válido.");
		obj.value='';
		obj.focus();
	}
}

//*****************************************************************************
//* Formata o campo cep
//*****************************************************************************
function FormataCEP(obj,teclapres) {
	var tecla = teclapres.keyCode;
	vr = obj.value;	
	vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );
	tam = vr.length;
	if (tecla != 8){ tam = vr.length + 1 ; } else {	tam = tam - 1 ; }
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
			obj.value = vr ; }
		if ( (tam > 4) ){
			obj.value = vr.substr( 0, tam - 4 ) + '-' + vr.substr( tam - 4, tam ) ; }
	}		
}

//*****************************************************************************
//* Formata o campo telefone
//*****************************************************************************
function FormataTelefone(obj,teclapres) {
	var tecla = teclapres.keyCode;
	vr = obj.value;
	vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );	
	tam = vr.length;
	if (tecla != 8){ tam = vr.length + 1 ; } else {	tam = tam - 1 ; }
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
			obj.value = vr ; }
		if ( (tam > 5) ){
			obj.value = vr.substr( 0, tam - 5 ) + '-' + vr.substr( tam - 5, tam ) ; }
	}		
}

//*****************************************************************************
//* Formata o campo ddd
//*****************************************************************************
function FormataDDD(obj) {
  if (obj.value.length>0) {
		return '('+obj.value+')';
  } else {
	  return '';
	}
}

//*****************************************************************************
//* Valida se o campo data é uma data válida
//*****************************************************************************
var dtCh= "/";

function DaysArray(n) {
	for (var i=1; i<=n; i++) {
	  this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
  } 
  return this
}

function ValidarData(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
  if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
  for (var i = 1; i <= 3; i++) {
   if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
  }
  month=parseInt(strMonth)
  day=parseInt(strDay)
  year=parseInt(strYr)

  if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
   alert("Por favor, entre com uma DATA DE NASCIMENTO válida.")
   return false
  }
  return true
}

function daysInFebruary (year){
 // February has 29 days in any year evenly divisible by four,
 // EXCEPT for centurial years which are not also divisible by 400.
 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

//*****************************************************************************
//* Validar o e-mail do formulário de cadastro
//*****************************************************************************
function ValidarEmail(obj){
	var objRegExp  = /^[A-Za-z]([\w\.]*)@([A-Za-z0-9\.]*)\.(([A-Za-z]{3}\.[A-Za-z]{2}$)|([A-Za-z]{3}$)|([a-z]{2}$))/i ;
  if (!objRegExp.test(obj.value)){
		alert("Por favor, entre com um endereço de e-MAIL válido.");
    obj.value='';
    obj.focus();
	}
}

//*****************************************************************************
//* Valida os campos obrigatórios do formulário de cadastro 
//*****************************************************************************
function ValidarCadastro(){

	if (document.boletim.nome.value == ""){
		alert("Por favor, entre com um valor para o campo NOME.");
		boletim.nome.focus();
	  return (false);
	} 

	if (document.boletim.senha.value == ""){
		alert("Por favor, entre com um valor para o campo SENHA.");
		boletim.senha.focus();
		return (false);
	} //else {	}

	if ((document.boletim.senha_confirma.value == "")||(document.boletim.senha_confirma.value!=document.boletim.senha.value)){
		alert("o campo CONFIRMAÇÃO DE SENHA deve ser igual ao campo SENHA.");
		boletim.senha_confirma.focus();
		return (false);
	} //else { }


	if (document.boletim.cep.value == ""){
		alert("Por favor, entre com um valor para o campo CEP.");
		boletim.cep.focus();
		return (false);
	}
	
	if (document.boletim.municipio.value == ""){
		alert("Por favor, entre com um valor para o campo MUNICÍPIO.");
		boletim.municipio.focus();
		return (false);
	}

	if (document.boletim.estado[0].selected) {
		alert("Por favor, entre com um valor para o campo ESTADO.");
		boletim.estado.focus();
		return (false);
	}

	if (document.boletim.pais[0].selected) {
		alert("Por favor, entre com um valor para o campo PAÍS.");
		boletim.pais.focus();
		return (false);
	}

	if (document.boletim.diadtnascimento[0].selected || document.boletim.mesdtnascimento[0].selected || document.boletim.anodtnascimento[0].selected) {
		alert("Por favor, entre com um valor para o campo DATA DE NASCIMENTO.");
		boletim.diadtnascimento.focus();
		return (false);
	} else {
		dtformatada=boletim.diadtnascimento.value+"/"+boletim.mesdtnascimento.value+"/"+boletim.anodtnascimento.value;
		if (!ValidarData(dtformatada)) { 
			boletim.diadtnascimento.focus();
			return (false);
		}
	}

	if (document.boletim.sexo[0].selected) {
		alert("Por favor, entre com um valor para o campo SEXO.");
		boletim.sexo.focus();
		return (false);
	}

	if (document.boletim.escolaridade[0].selected) {
		alert("Por favor, entre com um valor para o campo ESCOLARIDADE.");
		boletim.escolaridade.focus();
		return (false);
	}

	if (document.boletim.profissao[0].selected) {
		alert("Por favor, entre com um valor para o campo PROFISSÃO.");
		boletim.profissao.focus();
		return (false);
	}

	if (document.boletim.renda[0].selected) {
		alert("Por favor, entre com um valor para o campo RENDA MENSAL.");
		boletim.renda.focus();
		return (false);
	}

	if (document.boletim.estadocivil[0].selected) {
		alert("Por favor, entre com um valor para o campo ESTADO CIVIL.");
		boletim.estadocivil.focus();
		return (false);
	}

	if (document.boletim.nrfilhos[0].selected) {
		alert("Por favor, entre com um valor para o campo Nº DE FILHOS.");
		boletim.nrfilhos.focus();
		return (false);
	}

	boletim.senha.value=Encode(boletim.senha.value);
  boletim.senha_confirma.value=Encode(boletim.senha_confirma.value);

}

function ValidarRadio(campo) {
	var n=0;
	for (i=0;i<campo.length;i++) {
		if (campo[i].checked){
			n=campo[i].value;
		} 
	}
	if (n>0){
		return(true);
	} else {
	  alert("Por favor, selecione uma opção.");
	}
}


function EsqueciMinhaSenha(email) {
	if (email>'') { 
		navigate('exec.phtml?link=esqueciminhasenha&email='+email);
	} else {
		alert('Para executar esta opção é necessário informar o e-mail utilizado no cadastro.');
	}
}

//*****************************************************************************
//* Retorna a senha criptografada
//*****************************************************************************
function Encode(str) {
	tam=str.length;
	vet=new Array(tam);
	res='';
	for (i=0;i<tam;i++) {
		vet[i]=str.charCodeAt(i);						//código ASCII do caracter
		vet[i]=(vet[i]*2)+10;									//sequência de criptografia
		res=res+String.fromCharCode(vet[i]);	//concatenação da string final
	}
	return res;
}

function Decode(valor) {
	tam=valor.length;
	vet=new Array(tam);
	res='';
	for (i=0;i<tam;i++) {
		vet[i]=valor.charCodeAt(i);						//código ASCII do caracter
		vet[i]=(vet[i]/2)-5;									//sequência de descriptografia
		res=res+String.fromCharCode(vet[i]);	//concatenação da string final
	}
	return res;
}
