// JavaScript Document
var ScreenX = screen.width;
var ScreenY = screen.height;
var CEP_CLICADO = false;
var defaultImg  = "";

function Banner_160x425() {
	banner = '<iframe src="config/banners/160x425.htm" border=0 marginwidth=0 marginheight=0 leftmargin=0 topmargin=0 frameborder=0 scrolling="no" width="160" height="425"></iframe>';
	if( ScreenX > 800 ) {
		document.getElementById("Banner_160x425").innerHTML = banner;
	}
	return;
}

function CEP_Click() {
	CEP_CLICADO = true;
}

function CEP_Habilita() {
	var f  = document.CADASTRO;
	f.endereco_cad.disabled = false;
	f.bairro_cad.disabled = false;
	f.cidade_cad.disabled = false;
	f.uf_cad.disabled = false;
	return;
}

function Cadastro_Processa() {
	var f = document.CADASTRO;
	if( !Cadastro_Check( f.nome_cad, "nome_cad", "Informe o seu nome completo!", false ) ) return;
	if( !Cadastro_Check( f.apelido_cad, "apelido_cad", "Escolha um apelido!", false ) ) return;

	if( f.senha_atual ) {
		if( !f.senha_atual.value ) {
			Cadastro_Focus("senha_atual","Informe a senha atual do seu cadastro!", true);
			return;
		}
		if( f.senha_cad.value && f.senha_cad.value != f.senha2_cad.value ) {
			Cadastro_Focus("senha_cad","Informe a nova senha corretamente!", true);
			return;
		}
	} else {
		if( !f.senha_cad.value || f.senha_cad.value != f.senha2_cad.value ) {
			Cadastro_Focus("senha_cad","Informe a sua senha corretamente!", true);
			return;
		}
	}
	if( !Cadastro_Check( f.cpfcnpj_cad, "cpfcnpj_cad", "Você precisa informar seu CPF!", false ) ) return;
	if( !ValidaCPF( f.cpfcnpj_cad.value ) ) {
		Cadastro_Focus("cpfcnpj_cad","CPF inválido! Informe sue CPF corretamente", true);
		return;
	}
	if( !Cadastro_Check( f.dtnasc_cad, "dtnasc_cad", "Informe sua data de nascimento!", false ) ) return;
	if( !f.sexo_cad[0].checked && !f.sexo_cad[1].checked ) {
		Cadastro_Focus("sexo_cad","Informe o seu sexo.", false);
		return;
	}
	if( f.instrucao_cad.selectedIndex == 0 ) {
		Cadastro_Focus("instrucao_cad","Informe o seu grau de instrução.", false);
		return;
	}
	if( f.profissao_cad.selectedIndex == 0 ) {
		Cadastro_Focus("profissao_cad","Informe a sua ocupação atual.", false);
		return;
	}
	if( !Cadastro_Check( f.cep_cad, "cep_cad", "Informe seu CEP corretamente!", false ) ) return;
	if( !Cadastro_Check( f.endereco_cad, "endereco_cad", "Informe o seu endereço corretamente!", false ) ) return;
	if( !Cadastro_Check( f.numero_cad, "numero_cad", "Informe o número de sua residência!", false ) ) return;
	if( !Cadastro_Check( f.bairro_cad, "nome_cad", "Informe nome do seu bairro!", false ) ) return;
	if( !Cadastro_Check( f.cidade_cad, "cidade_cad", "Informe o nome da sua cidade!", false ) ) return;
	if( f.uf_cad.selectedIndex == 0 ) {
		Cadastro_Focus("uf_cad","Selecione seu Estado.", false);
		return;
	}
	if( !Cadastro_Check( f.foneres_cad, "foneres_cad", "Necessário informar o telefone residencial!", false ) ) return;
	if( f.horario_cad.selectedIndex == 0 ) {
		Cadastro_Focus("horario_cad","Informe o melhor horário para contato.", false);
		return;
	}
	if( !f.concordo_cad.checked ) {
		Cadastro_Focus("concordo_cad","Você precisa concordar com a nossa Política\nde Privacidade antes de prosseguir.",false);
		return;
	}
	CEP_Habilita();
	f.submit();
}

function Cadastro_Check( campo, fld, msg, sel) {
	if( !campo.value ) {
		Cadastro_Focus( fld, msg, sel );
		return( false );
	}
	return( true );
}
function Cadastro_Focus( fld, msg, sel ) {
	alert( msg );
	document.getElementById(fld).focus();
	if( sel ) document.getElementById(fld).select();
}

function Cadastro_SetDefaultImage( img ) {
	defaultImg = img;
	return;
}

function Cadastro_EditaImagem( vl ) {
	var f = document.CADASTRO;
	if( vl == 2 ) {
		document.getElementById("imagem_cad").disabled = false;
	} else {
		document.getElementById("imagem_cad").disabled = true;
	}
	return;
}

function Cadastro_ShowImagem( vl ) {
	var browserName=navigator.appName; 
	if ( browserName != "Netscape" ) { 
		if( ValidaImagem(vl) ) {
			document.getElementById("imagem").src = vl;
		} else {
			if( vl.length > 0 ) {
				alert( "Formato inválido de imagem" )
				document.getElementById("imagem_cad").focus();
				document.getElementById("imagem_cad").select();
				document.getElementById("imagem_cad").value = "";
			}
			document.getElementById("imagem").src = "images/__foto_cadastro_form.gif";
		}
	}
}

function Cadastro_ShowImagemEdita() {
	var browserName=navigator.appName; 
	if ( browserName != "Netscape" ) { 
		document.getElementById("imagem").src = defaultImg;
	}
	return;
}

function ValidaImagem(img) {
	if( img.length == 0 ) return( false );
	var pieces = img.split( "." );
	if( pieces[(pieces.length - 1)].toLowerCase() == 'jpg' ) {
		return( true );							   
	} else {
		return( false );
	}
}

function ValidaEmail(email) {
	parte1 = email.indexOf("@");
	parte2 = email.indexOf(".");
	parte3 = email.length;
	if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
		return false;
	}
	return( true );
}

function ValidaCPF(c){
	var i; 
	s = c.replace(/\./g, "");
	s = s.replace(/\-/g, "");
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) 
	{ 
		d1 += c.charAt(i)*(10-i); 
	} 
	if (d1 == 0){ 
		return false; 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1) 
	{ 
		return false; 
	} 
	
	d1 *= 2; 
	for (i = 0; i < 9; i++) 
	{ 
		d1 += c.charAt(i)*(11-i); 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(1) != d1) 
	{ 
		return false; 
	} 
	return( true );
}

function openWindow(theURL, Name, popW, popH, properties) { // V 1.0
	var winleft = (screen.width - popW) / 2;
	var winUp = (screen.height - popH) / 2;
	winProp = 'width=' + popW + ',height=' + popH + ',left=' + winleft + ',top=' + winUp + ',' + properties
	Win = window.open(theURL, Name, winProp)
	if (parseInt(navigator.appVersion) >= 4) {
		Win.window.focus();
	}
	return;
}

function topradio( id ) {
	openWindow( "player.php?estacao=" + id, "TopRadio", 478, 220, "toolbar=no,menubar=no,status=yes,statusbar=yes,scroll=no,scrollbars=no");
	return;
}

function topradioCidade() {
	openWindow( "http://www.folhadoestado.com.br/radio.php", "RadioCidade", 512, 119, "toolbar=no,menubar=no,status=yes,statusbar=yes,scroll=no,scrollbars=no");
	return;
}