function formatar(campo, mask, evt) {
	if(document.all) { // Internet Explorer
		key = evt.keyCode;
	} else{ // Nestcape
		key = evt.which;
	}
	if (key == 8 || (key == 0 && campo.value.length == mask.length)) {
		return true;
	}

	string = campo.value;  
	i = string.length;

	if (i < mask.length) {
		if (mask.charAt(i) == '?') {
			return (key > 47 && key < 58);
		} else {
			if (mask.charAt(i) == '!') {  return true;  }
			for (c = i; c < mask.length; c++) {
				if (mask.charAt(c) != '?' && mask.charAt(c) != '!')
					campo.value = campo.value + mask.charAt(c);
				else if (mask.charAt(c) == '!'){
					return true;
				} else {
					return (key > 47 && key < 58);
				}
			}
		}
	} else return false;
}

function numeros(campo,evt){
	if(document.all) { // Internet Explorer
		key = evt.keyCode;
	} else{ // Nestcape
		key = evt.which;
	}

	if ((key == 8 || key == 0) && (campo.value.length > 0)) {
		return true;
	}

	if (!(key > 47 && key < 58)){
		campo.value = campo.value.substring(0,campo.value.length); 
		return false;
	} else {
		return true;
	}
}

function avancar(campo, mask, proximo_id) {
	if (campo.value.length == mask.length) {
		document.getElementById(proximo_id).focus();
		return true;
	} else {
		return false;
	}
}

function toupper(campo) {
	campo.value = campo.value.toUpperCase();
	return true;
}

function validarCadastroAlunos(form) {
	estadoVazio = true;
	for( i=1; i < form.estado.length; i++ ){
		if( form.estado[i].selected ){
			estadoVazio = false;
			break;
		}
	}
	if (estadoVazio) {
		alert('Campo Estado obrigatÃ³rio!');
		document.getElementById('estado').focus();
		return false;
	}
	if (document.getElementById('cidade').value == '') {
		alert('Campo Cidade obrigatÃ³rio!');
		document.getElementById('cidade').focus();
		return false;
	}
	estadoCivilVazio = true;
	for( i=1; i < form.estadocivil.length; i++ ){
		if( form.estadocivil[i].selected ){
			estadoCivilVazio = false;
			break;
		}
	}
	if (estadoCivilVazio) {
		alert('Campo Estado Civil obrigatÃ³rio!');
		document.getElementById('estadocivil').focus();
		return false;
	}
	if (document.getElementById('nome').value == '') {
		alert('Campo Nome obrigatÃ³rio!');
		document.getElementById('nome').focus();
		return false;
	}
	if (document.getElementById('dia').value == '') {
		alert('Campo Data de nascimento obrigatÃ³rio!');
		document.getElementById('dia').focus();
		return false;
	}
	if (document.getElementById('mes').value == '') {
		alert('Campo Data de nascimento obrigatÃ³rio!');
		document.getElementById('mes').focus();
		return false;
	}
	if (document.getElementById('ano').value == '') {
		alert('Campo Data de nascimento obrigatÃ³rio!');
		document.getElementById('ano').focus();
		return false;
	}
	if (document.getElementById('naturalidade').value == '') {
		alert('Campo Naturalidade obrigatÃ³rio!');
		document.getElementById('naturalidade').focus();
		return false;
	}
	if (document.getElementById('nacionalidade').value == '') {
		alert('Campo Nacionalidade obrigatÃ³rio!');
		document.getElementById('nacionalidade').focus();
		return false;
	}
	if (document.getElementById('nmae').value == '') {
		alert('Campo Nome da MÃ£e obrigatÃ³rio!');
		document.getElementById('nmae').focus();
		return false;
	}
	if (document.getElementById('preferencia').value == '') {
		alert('Campo Ponto de ReferÃªncia obrigatÃ³rio!');
		document.getElementById('preferencia').focus();
		return false;
	}
	sexoVazio = true;
	for( i=1; i < form.sexo.length; i++ ){
		if( form.sexo[i].selected ){
			sexoVazio = false;
			break;
		}
	}
	if (sexoVazio) {
		alert('Campo Sexo obrigatÃ³rio!');
		document.getElementById('sexo').focus();
		return false;
	}
	if (document.getElementById('rg').value == '') {
		alert('Campo RG obrigatÃ³rio!');
		document.getElementById('rg').focus();
		return false;
	}
	if (document.getElementById('cpf').value == '') {
		alert('Campo CPF obrigatÃ³rio!');
		document.getElementById('cpf').focus();
		return false;
	}
	if (document.getElementById('titulo').value == '') {
		alert('Campo TÃ­tulo obrigatÃ³rio!');
		document.getElementById('titulo').focus();
		return false;
	}
	escolaridadeVazia = true;
	for( i=0; i < form.escolaridade.length; i++ ){
		if( form.escolaridade[i].checked ){
			escolaridadeVazia = false;
			break;
		}
	}
	if (escolaridadeVazia) {
		alert('Campo Escolaridade obrigatÃ³rio!');
		document.getElementById('escolaridade').focus();
		return false;
	}
	canhotoVazio = true;
	for( i=0; i < form.canhoto.length; i++ ){
		if( form.canhoto[i].checked ){
			canhotoVazio = false;
			break;
		}
	}
	if (canhotoVazio) {
		alert('Campo Canhoto obrigatÃ³rio!');
		document.getElementById('canhoto').focus();
		return false;
	}
	deficienciaVazia = true;
	for( i=0; i < form.deficiencia.length; i++ ){
		if( form.deficiencia[i].checked ){
			deficienciaVazia = false;
			break;
		}
	}
	if (deficienciaVazia) {
		alert('Campo DeficiÃªncia obrigatÃ³rio!');
		document.getElementById('deficiencia').focus();
		return false;
	}
	cursoVazio = true;
	for( i=1; i < form.curso.length; i++ ){
		if( form.curso[51].selected ){
			cursoVazio = true;
			break;
		}
		if( form.curso[i].selected ){
			cursoVazio = false;
			break;
		}
	}
	if (cursoVazio) {
		alert('Campo Curso obrigatÃ³rio!');
		document.getElementById('curso').focus();
		return false;
	}
	try{
		if (document.getElementById('login').value == '') {
			alert('Campo Login obrigatÃ³rio!');
			document.getElementById('login').focus();
			return false;
		}
	} catch(e){}
	try{
		if (document.getElementById('senha').value == '') {
			alert('Campo Senha obrigatÃ³rio!');
			document.getElementById('senha').focus();
			return false;
		}
	} catch(e){}
	try{
		if (document.getElementById('confirmar').value == '') {
			alert('Campo Confirmar Senha obrigatÃ³rio!');
			document.getElementById('confirmar').focus();
			return false;
		}
	} catch(e){}
	try{
		if (document.getElementById('senha').value != document.getElementById('confirmar').value) {
			alert('Campo Senha e Confirmar Senha nÃ£o conferem!');
			document.getElementById('confirmar').focus();
			return false;
		}
	} catch(e){}
	if (document.getElementById('email').value != '') {
		if (!validarEmail(document.getElementById('email').value)) {
			alert('Campo E-mail invÃ¡lido!');
			document.getElementById('email').focus();
			return false;
		}
		return true;
	}
	return true;	
}

function validarEmail(mail){
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string"){
		if(er.test(mail)){ return true; }
	}else if(typeof(mail) == "object"){
		if(er.test(mail.value)){ 
			return true; 
		}
	}else{
		return false;
	}
}

function imprimir(){
	if (!window.print){
		alert("Use o Netscape  ou Internet Explorer \n nas versões 4.0 ou superior!");
		return;
	}
	window.print();
}

function abrirPopupAluno(form, largura, altura) {
	if (validarCadastroAlunos(form)) {
		estado = 0;
		for( i=1; i < window.document.getElementById('estado').length; i++ ){
			if( window.document.getElementById('estado')[i].selected ){
				estado = i;
				break;
			}
		}
		cidade = window.document.getElementById('cidade').value;
		civil = 0;
		for( i=1; i < window.document.getElementById('estadocivil').length; i++ ){
			if( window.document.getElementById('estadocivil')[i].selected ){
				civil = i;
				break;
			}
		}
		nome = window.document.getElementById('nome').value;
		dia = window.document.getElementById('dia').value;
		mes = window.document.getElementById('mes').value;
		ano = window.document.getElementById('ano').value;
		naturalidade = window.document.getElementById('naturalidade').value;
		nacionalidade = window.document.getElementById('nacionalidade').value;
		nmae = window.document.getElementById('nmae').value;
		npai = window.document.getElementById('npai').value;
		endereco = window.document.getElementById('endereco').value;
		numero = window.document.getElementById('numero').value;
		bairro = window.document.getElementById('bairro').value;
		cep = window.document.getElementById('cep').value;
		complemento = window.document.getElementById('complemento').value;
		preferencia = window.document.getElementById('preferencia').value;
		sexo = 0;
		for( i=1; i < window.document.getElementById('sexo').length; i++ ){
			if( window.document.getElementById('sexo')[i].selected ){
				sexo = i;
				break;
			}
		}
		email = window.document.getElementById('email').value;
		rg = window.document.getElementById('rg').value;
		cpf = window.document.getElementById('cpf').value;
		titulo = window.document.getElementById('titulo').value;
		zona = window.document.getElementById('zona').value;
		secao = window.document.getElementById('secao').value;
		ddd = window.document.getElementById('ddd').value;
		telefone = window.document.getElementById('telefone').value;
		dddc = window.document.getElementById('dddc').value;
		celular = window.document.getElementById('celular').value;
		dddo = window.document.getElementById('dddo').value;
		coutro = window.document.getElementById('coutro').value;
		escolaridade = 0;
		for( i=0; i < form.escolaridade.length; i++ ){
			if( form.escolaridade[i].checked ){
				escolaridade = i;
				break;
			}
		}	
		canhoto = 0;
		for( i=1; i < form.canhoto.length; i++ ){
			if( form.canhoto[i].checked ){
				canhoto = i;
				break;
			}
		}
		deficiencia = 0;
		for( i=1; i < form.deficiencia.length; i++ ){
			if( form.deficiencia[i].checked ){
				deficiencia = i;
				break;
			}
		}
		curso = 0;
		for( i=1; i < window.document.getElementById('curso').length; i++ ){
			if( window.document.getElementById('curso')[i].selected ){
				curso = i;
				break;
			}
		}
		login = window.document.getElementById('login').value;
		senha = window.document.getElementById('senha').value;
		confirmar = window.document.getElementById('confirmar').value;

		//---------------------------------
		//Cria popup
		//---------------------------------
		//pega a resolução do visitante
		w = screen.width;
		h = screen.height;

		//divide a resolução por 2, obtendo o centro do monitor
		meio_w = w/2;
		meio_h = h/2;

		//diminui o valor da metade da resolução pelo tamanho da janela, fazendo com q ela fique centralizada
		altura2 = altura/2;
		largura2 = largura/2;
		meio1 = meio_h-altura2;
		meio2 = meio_w-largura2;

		//abre a nova janela, já com a sua devida posição
		args = 'nome='+nome+'&estado='+estado+'&cidade='+cidade+'&civil='+civil+'&nome='+nome+'&dia='+dia+'&mes='+mes+'&ano='+ano
		+'&naturalidade='+naturalidade+'&nacionalidade='+nacionalidade+'&nmae='+nmae+'&npai='+npai+'&endereco='+endereco+'&numero='+numero
		+'&bairro='+bairro+'&cep='+cep+'&complemento='+complemento+'&preferencia='+preferencia+'&sexo='+sexo+'&email='+email+'&rg='+rg
		+'&cpf='+cpf+'&titulo='+titulo+'&zona='+zona+'&secao='+secao+'&ddd='+ddd+'&telefone='+telefone+'&dddc='+dddc+'&celular='+celular
		+'&dddo='+dddo+'&coutro='+coutro+'&escolaridade='+escolaridade+'&canhoto='+canhoto+'&deficiencia='+deficiencia+'&curso='+curso
		+'&login='+login+'&senha='+senha+'&confirmar='+confirmar;
		
		var popup = window.open('visualiza_cadastro_aluno.php?'+args, 'popup','toolbar=no, scrollbars=yes, location=no, directories=no, status=no, menubar=no, resizable=yes, titlebar=no, width='+largura+', height='+altura+', copyhistory=no, marginheight=2, marginwidth=2, top='+meio1+', left='+meio2);
		popup.focus();
		popup.opener = window.self;

	}
	
	return false;
}

function fecharAluno(opener){
	window.opener = opener;
	window.close("#");
	opener.alert('Aluno cadastrado com sucesso!');
	opener.location.href="index.php";
}

function fechar(){
	window.close("#");
}

function validarLogin(form) {
//	login = window.document.forms[1].getElementById('usuario').value;
//	senha = form.senha.value;
//	if (login == "" || typeof(login) == 'undefined' || login == null || login.length == 0) {
//		document.login.getElementById('usuario').style = "border-color: #F00;";
//		return false;
//	}
//	if (senha == "" || typeof(senha) == 'undefined' || senha == null || senha.length == 0) {
//		document.login.getElementById('senha').style = "border-color: #F00;";
//		return false;
//	}
	return true;	
}

function buscarAluno(nome, page, estado) {
	if (nome == '' || nome == undefined) {
		try {
			nome = document.getElementById('nome').value;
		} catch (e) {}
	}
	if (estado == '' || estado == undefined) {
		try {
			estado = document.getElementById('estado').value;
		} catch (e) {}
	}
	url="search_aluno.php?nome="+nome+"&page="+page+"&estado="+estado;
	ajax(url);
}

function remove_aluno(id) {
	if(confirm('Tem certeza que deseja remover este aluno?'))
		location.href='remove_aluno.php?id='+id;
}

function validarRecuperarSenha(form) {
	if (form.login.value == '') {
		alert('Campo Login obrigatÃ³rio!');
		form.login.focus();
		return false;
	}
	if (form.email.value == '') {
		alert('Campo E-mail obrigatÃ³rio!');
		form.email.focus();
		return false;
	}
	return true;	
}

