//////////////////////////////////////////////////
// Função para abrir pop-up
//////////////////////////////////////////////////
function abrir(pagina,largura,altura, noscroll, alvo) {
	if(!noscroll){
		scrollbars = 'no';
	} else {
		scrollbars = 'yes';
	}
	config="toolbar=no,location=no,width="+largura+",height="+altura+",status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=no,top=" + ((screen.height - altura) / 2) + ",left=" + ((screen.width - largura) /2);
	pop=window.open(pagina,alvo,config);
}

//////////////////////////////////////////////////
// Função para adicionar aos favoritos
//////////////////////////////////////////////////
function favoritos()
{
	var browser = navigator.appName;
	var bookmarkurl="http://www.portaleducacao.com.br";
	var bookmarktitle="Portal Educação";
	if (browser=="Netscape")
	{
		alert ("Aperte CTRL+D para adicionar o Portal Educação aos seus favoritos")
	}
	else if(browser=="Microsoft Internet Explorer")
	{
		window.external.AddFavorite(bookmarkurl,bookmarktitle)
	}
}

//////////////////////////////////////////////////
// Função para checar ou unchecar todos checkbox
//////////////////////////////////////////////////
function checkaTodos(algumAlterado)
{
	f = validar;
	if (algumAlterado)
	{ 
		if (algumAlterado.checked == false) 
			f.br.checked = false;
	}
	else 
		for (i=0; i<f.length; i++) 
		if (f.elements[i].type == "checkbox")
		if (f.br.checked) f.elements[i].checked = true
		else f.elements[i].checked = false
}

//////////////////////////////////////////////////
// Função para validar Formulário
//////////////////////////////////////////////////
function Valida_Cadastro(theForm)
{
	// Nome
	if (theForm.nome.value == "")
	{
		alert("Preencha o campo \"Nome\".");
		theForm.nome.focus();
		return false;
	}

	// E-mail
	if (theForm.email.value == ""){
		alert("Preencha o campo \"E-mail\".");
		theForm.email.focus();
	return false;
	}
	if (Valida_Email(theForm) == false)
		return false;
	
}

//////////////////////////////////////////////////
// Função que faz uma máscara para cep
//////////////////////////////////////////////////
function Formata_CEP(objeto){
	if (objeto.value.indexOf("-") == -1 && objeto.value.length > 5){ objeto.value = ""; }
	if (objeto.value.length == 5){
		objeto.value += "-";
	}
}
//OnKeyPress="Formata_CEP(this)"


//////////////////////////////////////////////////
// Verifica CPF
//////////////////////////////////////////////////
function Valida_CPF(theForm)
{
var CPF = theForm.cpf.value; 

var POSICAO, I, SOMA, DV, DV_INFORMADO;
var DIGITO = new Array(10);
DV_INFORMADO = CPF.substr(9, 2); 

for (I=0; I<=8; I++) {
 DIGITO[I] = CPF.substr( I, 1);
}

POSICAO = 10;
SOMA = 0;
 for (I=0; I<=8; I++) {
 SOMA = SOMA + DIGITO[I] * POSICAO;
 POSICAO = POSICAO - 1;
 }
DIGITO[9] = SOMA % 11;
 if (DIGITO[9] < 2) {
 DIGITO[9] = 0;
}
 else{
 DIGITO[9] = 11 - DIGITO[9];
}

POSICAO = 11;
SOMA = 0;
 for (I=0; I<=9; I++) {
 SOMA = SOMA + DIGITO[I] * POSICAO;
 POSICAO = POSICAO - 1;
 }
DIGITO[10] = SOMA % 11;
 if (DIGITO[10] < 2) {
 DIGITO[10] = 0;
 }
 else {
 DIGITO[10] = 11 - DIGITO[10];
 }

DV = DIGITO[9] * 10 + DIGITO[10];
 if (DV != DV_INFORMADO) {
 alert('CPF inválido');
 theForm.cpf.value = '';
 theForm.cpf.focus();
 return false;
 }

 for (I=0; I<=9; I++) {

	if (DIGITO[I] == DIGITO[9] && DIGITO[I] == DIGITO[8] && DIGITO[I] == DIGITO[7] && DIGITO[I] == DIGITO[6] && DIGITO[I] == DIGITO[5] && DIGITO[I] == DIGITO[4] && DIGITO[I] == DIGITO[3] && DIGITO[I] == DIGITO[2] && DIGITO[I] == DIGITO[1] && DIGITO[I] == DIGITO[0]) {
	  alert('CPF inválido');
  	  theForm.cpf.value = '';
      theForm.cpf.focus();
      return false;
    }
 }

}

//////////////////////////////////////////////////
// Função para validar e-mail
//////////////////////////////////////////////////
function Valida_Email(theForm)
{
	var email = theForm.email.value;
    var BadChars = "*|,\":<>[]{}`\'';()&$#% ";
    var GoodChars = "@.";
    var posarroba = email.indexOf ('@',0);
    if (email.length < 6){
        alert("E-mail inválido!");
        theForm.email.value = "";
        theForm.email.focus();
        return false;
    }
    for (var i = 0; i < email.length; i++){
        if (BadChars.indexOf(email.charAt(i)) != -1){
            alert("E-mail inválido!");
            theForm.email.value = "";
            theForm.email.focus();
            return false;
        }
    }
    for (var i = 0; i < GoodChars.length; i++){
        if (email.indexOf(GoodChars.charAt(i)) == -1){
            alert("E-mail inválido!");
            theForm.email.value = "";
            theForm.email.focus();
            return false;
        }
        if (email.indexOf(GoodChars.charAt(i),0) == 0){
            alert("E-mail inválido!");
            theForm.email.value = "";
            theForm.email.focus();
            return false;
        }
        if (email.lastIndexOf(GoodChars.charAt(i)) > email.length-3){
            alert("E-mail inválido!");
            theForm.email.value = "";
            theForm.email.focus();
            return false;
        }
    }
    if (email.lastIndexOf('@') > email.lastIndexOf('.')){
        alert("E-mail inválido!");
        theForm.email.value = "";
        theForm.email.focus();
        return false;
    }

    if (email.indexOf ('@.',0) != -1 || email.indexOf ('.@',0) != -1){
        alert("E-mail inválido!");
        theForm.email.value = "";
        theForm.email.focus();
        return false;
    }
    if (email.indexOf ('@',posarroba+1) != -1){
        alert("E-mail inválido!");
        theForm.email.value = "";
        theForm.email.focus();
        return false;
    }
}