// FUNÇÃO QUE VALIDA O FORMULÁRIO
function checkCampos(frm,pagina){
	switch(pagina){
		case "eventos":
			var nomedocampo = Array("idCategoriaInscricao","nomeRecibo");
			var descricao   = Array("Categoria de inscrição","Nome para recibo");		
		break;
		
		case "cartaoCredito":
			var nomedocampo = Array("cartao","nomeTitular","numCartao","codSeguranca","mes","ano");
			var descricao   = Array("Bandeira","Nome do titular","Número","Código de segurança","Mês de validade","Ano de validade");		
		break;
		
		case "categorias":
			var nomedocampo = Array("idCategoriaInscricao");
			var descricao   = Array("Categoria de inscrição");		
		break;
		
		case "trabalhos":
			var nomedocampo = Array("tituloTrabalho","instituicao");
			var descricao   = Array("Título do Trabalho","Instituição");		
		break;
		
		case "componentes":
			var nomedocampo = Array("nome","nomeCientifico");
			var descricao   = Array("Nome","Nome Científico");		
		break;
		
		case "autores":
			var nomedocampo = Array("nomeAutor","nomeCientificoAutor");
			var descricao   = Array("Nome do Autor","Nome Científico do Autor");		
		break;
		
		case "apresentadores":
			var nomedocampo = Array("nomeApresent","nomeCientificoApresent");
			var descricao   = Array("Nome do Apresentador","Nome Científico do Apresentador");		
		break;
		
		case "acompanhante":
			var nomedocampo = Array("nome","idade");
			var descricao   = Array("Nome","Idade");		
		break;
		
		case "usuarios":
			doBrasil = document.getElementById("doBrasil-1").style.display;
			if (doBrasil == 'block') {
				var nomedocampo = Array("email","senha","senha2","cpf","nome","nomeCracha","sexo","endereco");
				var descricao   = Array("Email","Senha","Confirmação da senha","CPF","Nome","Nome para o crachá","Sexo","Endereço");
			} else {
				var nomedocampo = Array("email","senha","senha2","nome","nomeCracha","sexo");
				var descricao   = Array("Email","Senha","Confirmação da senha","Nome","Nome para o crachá","Sexo");
			}
		break;		
	}
	
	var msgAlert = "Complete os seguintes campos:\n\n";
	var msg = msgAlert.length;



	for (var i = 0; i < nomedocampo.length; i++){
		var objetos = frm.elements[nomedocampo[i]];
		if (objetos) {
			switch(objetos.type){
				case "file":
					if(objetos.value == ""){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				case "select-one":
					if (objetos.selectedIndex == -1 || 
						objetos.options[objetos.selectedIndex].text == "" || 
						objetos.options[objetos.selectedIndex].value == ""){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				case "select-multiple":
					if (objetos.selectedIndex == -1){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				case "password":
					if(objetos.value == ""){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				case "text":
					if(objetos.name == "site"){
						if(frm.site.value == "http://"){
							msgAlert += " > " + "Digite o site corretamente" + "\n";	
						}
					}
					
					if(objetos.name == "email"){
						if(objetos.value != ""){
							if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(objetos.value))){
								msgAlert += " > " + "Email inválido" + "\n";	
							}
						}
					}
	
				case "textarea":
					if (objetos.value == "" || objetos.value == null){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				case "radio":
					if (!objetos.checked){
						msgAlert += " - " + descricao[i] + "\n";
					}
				break;
				
				default:
			}
			
			if (objetos.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < objetos.length; j++){
					if (objetos[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					msgAlert += " - " + descricao[i] + "\n";
				}
			}
		}
	}

	cpfCerto = true;
	if(document.getElementById("cpf")) {
		if(document.getElementById("cpf").value != "") {
			var cpf = document.getElementById("cpf").value;
			if (cpf.length < 11 ) {
				msgAlert += " - CPF deve ter 11 dígitos\n";
				fdados.cpf.focus();
				cpfCerto = false;
			} else if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
				msgAlert += " - Número de CPF inválido\n";
				fdados.cpf.focus();
				cpfCerto = false;
			} else {
				var a = [];
				var b = new Number;
				var c = 11;
				for (i=0; i<11; i++){
					a[i] = cpf.charAt(i);
					if (i < 9) b += (a[i] * --c);
				}
				if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
				b = 0;
				c = 11;
				for (y=0; y<10; y++) b += (a[y] * c--); 
				if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
				if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
					msgAlert += " - Dígito verificador do CPF incorreto\n";
					fdados.cpf.focus();
					cpfCerto = false;
				}
			}
		}
	}


	// CONFIRMACAO DE SENHA
	if(pagina=="usuarios"){
		var fSenha = document.getElementById('fdados').senha.value;
		var fSenha2 = document.getElementById('fdados').senha2.value;
		
		if ((fSenha != '' && fSenha2 != '') && (fSenha != fSenha2))
			msgAlert += " - As senhas estão desiguais\n";
			
		var tel1 = document.getElementById('fdados').telRes.value;
		var tel2 = document.getElementById('fdados').telCom.value;
		var tel3 = document.getElementById('fdados').telCel.value;
		
		if (tel1 == '' && tel2 == '' && tel3 == '')
			msgAlert += " - Preencha algum dos telefones\n";
	
	
		if (msgAlert.length == msg && cpfCerto == true){
			return true;
		}else{
			alert(msgAlert);
			return false;
		}
	}else{
		if (msgAlert.length == msg){
			return true;
		}else{
			alert(msgAlert);
			return false;
		}
	}
} 

// APENAS NUMEROS
function limpa_string(S){
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++){
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0){
			temp=temp+digito
		}
	}
	return temp
}
function numerico(campo){
	nome = campo.name
	num = limpa_string(campo.value);
	campo.value = num;
}


//FUNÇÃO PARA A MASCARA DA DATA DE NASCIMENTO
function mascara_data(data){ 
	var mydata = ''; 
	mydata = mydata + data; 
	if (mydata.length == 2){ 
		mydata = mydata + '/'; 
	} 
	if (mydata.length == 5){ 
		mydata = mydata + '/'; 
	}
	return mydata; 
}



//SE PAIS FOR BRASIL, OBRIGATORIO CPF E OUTROS DADOS
function paises(pais) {
	nomePais = document.fdados.idPais.options[pais.selectedIndex].text
	
	blocos = 3
	for (i=1; i<=blocos; i++) {
		if (nomePais == 'Brasil')
			document.getElementById("doBrasil-"+i).style.display = "block";
		else
			document.getElementById("doBrasil-"+i).style.display = "none";
	}
}


//OCULTAR OS BLOCOS NA CRIACAO DE UM EVENTO
function abreBloco(id) {
	if (document.getElementById("span-"+id).style.display == "block")
		document.getElementById("span-"+id).style.display = "none";
	else
		document.getElementById("span-"+id).style.display = "block";
}


Number.prototype.formatMoney = function(c, d, t){ //Serve para formatar um number em formato dinheiro
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t,
i = parseInt(n = (+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
+ (c ? d + (n - i).toFixed(c).slice(2) : "");
};

//MASCARA DE DINHEIRO
function moeda(cur,len) {
	n = '__0123456789';
	d = cur.value;
	l = d.length;
	r = '';
	if (l > 0) {
		z = d.substr(0,l-1);
		s = '';
		a = 2;
		for (i=0; i < l; i++) {
			c=d.charAt(i);
			if (n.indexOf(c) > a) {
				a=1;
				s+=c;
			}
		}
		l=s.length;
		t=len-1;
		if (l > t) {
			l=t;
			s=s.substr(0,t);
		}
		if (l > 2) {
			r=s.substr(0,l-2)+','+s.substr(l-2,2);
		} else {
			if (l == 2)	{
				r='0,'+s;
			} else {
				if (l == 1)	{
					r='0,0'+s;
				}
			}
		}
		if (r == '') {
			r='0,00';
		} else {
			l=r.length;
			if (l > 6) {
				j=l%3;
				w=r.substr(0,j);
				wa=r.substr(j,l-j-6);
				wb=r.substr(l-6,6);
				if (j > 0) {
					w+='.';
				}
				k=(l-j)/3-2;
				for (i=0; i < k; i++) {
					w+=wa.substr(i*3,3)+'.';
				}
				r=w+wb;
			}
		}
	}
	if (r.length <= len) {
		cur.value=r;
	} else {
		cur.value=z;
	}
	return 'ok';
}



// MASCARA DE HORAS
function horario(hora){ 
	var myhora = ''; 
	horaCampo = hora.name;

	myhora = myhora + hora.value; 
	if (myhora.length == 2){ 
		myhora = myhora + ':'; 
		document.forms[0][horaCampo].value = myhora; 
	} 
	if (myhora.length == 5)
		verifica_hora(horaCampo); 
}

function verifica_hora(horaCampo){ 
	hrs = (document.forms[0][horaCampo].value.substring(0,2)); 
	min = (document.forms[0][horaCampo].value.substring(3,5)); 
	
	if (navigator.appName.indexOf('Microsoft') != -1)  
		clientNavigator = "IE";  
	else  
		clientNavigator = "Outro";  
	
	situacao = ""; 
	// verifica data e hora 
	if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59))
		situacao = "falsa"; 
	
	if (document.forms[0][horaCampo].value == "") 
		situacao = "falsa"; 
	
	if (clientNavigator == "IE") {  
		if (event.keyCode < 48 || event.keyCode > 57)
			situacao = "falsa"; 
	} else {  
		if ((event.charCode < 48 || event.charCode > 57) && event.keyCode == 0) 
			situacao = "falsa"; 
	} 	
	
	if (situacao == "falsa") { 
		alert("Hora inválida!"); 
		document.forms[0][horaCampo].focus(); 
	} 
} 













// FUNCOES DOS EVENTOS
function evento(id) {
	if (id) {
		location.href = 'geral.php?idEvento='+id;
	} else {
		location.href = 'geral.php';
	}
}



function icone(valor,idEvento) {
	if (valor) {
		document.getElementById("infoCat").src = 'categoriaPopup.php?id='+valor+'&idEvento='+idEvento;
		document.getElementById("icone").style.display = "block";
	} else {
		document.getElementById("icone").style.display = "none";
	}
}

function abrePopup(idEvento) {
	id = document.getElementById("idCategoriaInscricao").value;
	janela = window.open('categoriaPopup.php?id='+id+'&idEvento='+idEvento,'Popup','width=440,height=150,scrollbars=1,top=0,left=0')
}

function abreInsercao(idEvento,tipo) {
	janela = window.open('insercao.php?idEvento='+idEvento+'&tipo='+tipo,'Popup','width=396,height=400,scrollbars=1,top=0,left=0')
}

function abrePopupEv(idEvento) {
	id = "{idEvento}";
	janela = window.open('eventoPopup.php?idEvento='+idEvento,'Popup','width=440,height=150,scrollbars=1,top=0,left=0')
}

function abrePopupOp(id,idCategoria,vis) {
	janela = window.open('opcionais.php?idEvento='+id+'&idCategoria='+idCategoria+'&vis='+vis,'Popup','width=500,height=400,scrollbars=1,top=0,left=0')
}

function abrePopupTrab(id,idArea) {
	if (idArea == '') 
		alert('Escolha uma área para o trabalho');
	else
		janela = window.open('inseretrabalho_act.php?idAreasTrabCientifico='+idArea+'&idEvento='+id+'&tipo=novo','Popup','width=730,height=550,scrollbars=1,top=0,left=0')
}

function abrePopupEditaTrab(id,idTrabalho,idArea) {
	janela = window.open('inseretrabalho.php?idAreasTrabCientifico='+idArea+'&idEvento='+id+'&idTrabalho='+idTrabalho+'&tipo=edicao','Popup','width=730,height=550,scrollbars=1,top=0,left=0')
}

function abrePopupConsultaTrab(id,idTrabalho,idArea) {
	janela = window.open('consulta_trabalho.php?idAreasTrabCientifico='+idArea+'&idEvento='+id+'&idTrabalho='+idTrabalho+'&tipo=edicao','Popup','width=730,height=550,scrollbars=1,top=0,left=0')
}


function trocaValores(tipo) {
	if (tipo == 'c') {
		if (document.getElementById("trocaVCategoria").style.display == "block")
			document.getElementById("trocaVCategoria").style.display = "none";
		else
			document.getElementById("trocaVCategoria").style.display = "block";
		document.getElementById("trocaVOpcional").style.display = "none";
	} else if (tipo == 'o') {
		document.getElementById("trocaVCategoria").style.display = "none";
		if (document.getElementById("trocaVOpcional").style.display == "block")
			document.getElementById("trocaVOpcional").style.display = "none";
		else
			document.getElementById("trocaVOpcional").style.display = "block";
	}
	
}



function abrir(pagina,largura,altura) {
	//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
	window.open(pagina,'','height=' + altura + ', width=' + largura + ', top='+meio1+', left='+meio2+'');
}