//---------------------ATENÇÃO-----------------------//
//-----TODOS OS CÓDIGOS UTILIZADOS NESSA PÁGINA------//
//----SÃO DE AUTORIA DA GENIART E DEMAIS AUTORES-----//
//-------DEVIDAMENTE IDENTIFICADOS PELO NOME.--------//
//---------------------------------------------------//

function validaCamposContato(){
	with(document.frm_contato_novo){
		if(txt_nome.value == ""){
			alert('Por favor informe seu nome');
			txt_nome.focus();
			return false;
		}else if(txt_email.value == ""){
			alert('Por favor informe seu e-mail');
			txt_email.focus();
			return false;
		}else if(!validaEmailSimples(txt_email)){
			txt_email.focus();
			return false;
		}else if(txt_mensagem.value == ""){
			alert('Por favor informe sua mensagem');
			txt_email.focus();
			return false;
		}else{
			submit();
			return true;
		}
	}

}


function validaCampos(qual){
	if(qual == "2"){
		with (document.frm_cadastro){
			if(txt_nome.value == ""){
				alert('Por favor informe seu nome');
				txt_nome.focus();
				return false;
			}
			if(txt_email.value == ""){
				alert('Por favor informe seu e-mail');
				txt_email.focus();
				return false;
			}else if (!validaEmail(txt_email, "frm_contato", 2)){
				txt_email.focus();
				txt_email.select();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}else if(qual == "1"){
		with (document.frm_contato){
			if(txt_nome.value == ""){
				alert('Por favor informe seu nome');
				txt_nome.focus();
				return false;
			}
			if(txt_email.value == ""){
				alert('Por favor informe seu e-mail');
				txt_email.focus();
				return false;
			}else if (!validaEmail(txt_email, "frm_contato", 2)){
				txt_email.focus();
				txt_email.select();
				return false;
			}
			if(txt_mensagem.value == ""){
				alert('Por favor digite uma mensagem');
				txt_mensagem.focus();
				return false;
			}
			
			if(ls_conheceu.value == "NM"){
				alert('Por favor informe como nos conheceu');
				ls_conheceu.focus();
				return false;
			}else{
				var indic = document.getElementById("cont_indique");
				if(indic.style.display == ""){
					if (txt_indicacao.value == ""){
						alert('O campo ESPECIFIQUE é de preenchimento obrigatório.');
						txt_indicacao.focus();
						return false;
					}
				}
			}
			
			submit();
			limpaForm(); // Não está funcionando
			//return true;
		}
		
	}else if(qual == "3"){
		with (document.frm_opniao){
			if(ls_pousada.value == "NM"){
				alert('Por favor informe o nome da pousada que você está avaliando');
				ls_pousada.focus();
				return false;
			}else{
				submit();
				return true;
			}
		}
	}
}

function limpaForm(){ // Não está funcionando
	document.frm_contato.reset();
}

//valida CPF
function validaCpf(cpf){ 
	var i;
	cpf = cpf.substr(0,3) + cpf.substr(4,3) + cpf.substr(8,3) + cpf.substr(12,2);
	var c = cpf.substr(0,9); 
	var dv = cpf.substr(9,2); 
	var d1 = 0; 
	for (i = 0; i < 9; i++) { 
		d1 += c.charAt(i)*(10-i); 
	} 
	if (d1 == 0){
// 		alert("CPF Invalido") 
		return false; 
	} 
	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0; 
	if (dv.charAt(0) != d1){ 
// 		alert("CPF Invalido") 
		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){ 
// 		alert("CPF Invalido")
		return false; 
	} 
	return true; 
} 

//Formata CPF / CNPJ
function ajustarCpf(input, evento){
	if(!soNumero(evento)){
	    return false;
	} else {
			if ((input.value.length == 3)||(input.value.length == 7)) {
				input.value = input.value + "." ;
			}
			if (input.value.length == 11) {
				input.value = input.value + "-" ;
			}
			if(input.value.length == 15){
				input.value = input.value.replace('.','');
				input.value = input.value.replace('.','');
				input.value = input.value.replace('-','');
				input.value = input.value.substr(0, 2) + '.' + input.value.substr(2, 3) + '.' + input.value.substr(5, 3) + '/' + input.value.substr(8, 4);
				input.value = input.value + "-" ;
			}
		}
	return true;
}

//Formata CEP
function ajustarCep(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (input.value.length == 5) {
		   input.value = input.value + "-";
		}
	}
	return true;
}

function criaLista(p, l){
// 	if(l == "bairro"){
// 		with(document.frm_busca){
// 			for(var i = 0; i < ls_bairro.options.length; i++){
// 				ls_bairro.options[i] = '';
// 			}
// 		}
// 	}
	window.open('cria_lista.php?listar=' + l + '&p=' + p, 'ifr_executa');
}

function validaEmailSimples(email){
	var sMail = new String(email.value)
	var index = sMail.indexOf("@");
	if (index > 0){
		var pindex = sMail.indexOf(".",index);
		if ((pindex > index+1) && (sMail.length > pindex+1)){
			result = true;
		}else{
			result = false;
		}
	}else{
		result = false;
	}
	if(!result){
		alert('EMAIL inexistente, por favor insira um e-mail válido.');
		return false;
	}else{
		return true;
	}
}

function validaEmail(email, formu, tipo){
	var sMail = new String(email.value)
	var index = sMail.indexOf("@");
	if (index > 0){
		var pindex = sMail.indexOf(".",index);
		if ((pindex > index+1) && (sMail.length > pindex+1)){
			result = true;
		}else{
			result = false;
		}
	}else{
		result = false;
	}
	if(!result){
		alert('EMAIL inexistente, por favor insira um e-mail válido.');
		if(tipo == 1){
			formu.elements[0].focus();
			formu.elements[0].select();
		}
	}else{
		if(tipo == 1){
			formu.submit();
		}else{
			return result;
		}
	}
}

/*function ajustarFone(input, evento){
	var tecla = evento.keyCode;
	valor = input.value;
	valor = valor.replace( "(", "" );
	valor = valor.replace( ")", "" );
	valor = valor.replace( " ", "" );
	valor = valor.replace( "-", "" );
	tamanhoMaximo = 10;
	tamanho = valor.length;
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (tamanho < tamanhoMaximo && tecla != 8) {
		tamanho = valor.length + 1 ;
		}
		if (tecla == 8 ) {
			tamanho = tamanho - 1 ;
		}
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tamanho <= 4 ) { 
		 		input.value = valor ;
			}
		 	if ( (tamanho > 4) && (tamanho <= 8) ) {
		 		input.value = valor.substr(0,tamanho-4) + '-' + valor.substr( tamanho - 4, tamanho ) ;
			}
		 	if ( (tamanho >= 9) && (tamanho <= 10) ) {
				input.value = '(' + valor.substr(0,2) + ') ' + valor.substr(2,tamanho-6) + '-' + valor.substr(tamanho-4,tamanho) ;			
			}
		}
	}
	return true;
}*/
function ajustarFone(input, evento){
	valor = input.value;
	valor = valor.replace( "(", "" );
	valor = valor.replace( ")", "" );
	valor = valor.replace( " ", "" );
	valor = valor.replace( "-", "" );
	tamanhoMaximo = 10;
	tamanho = valor.length;
    if(!soNumero(evento)){
	    return false;
	} else { 
		if (tamanho < tamanhoMaximo) {
		tamanho = valor.length + 1 ;
		}
		if ( tamanho <= 4 ) { 
			input.value = valor ;
		}
		if ( (tamanho > 4) && (tamanho <= 8) ) {
			input.value = valor.substr(0,tamanho-4) + '-' + valor.substr( tamanho - 4, tamanho ) ;
		}
		if ( (tamanho >= 9) && (tamanho <= 10) ) {
			input.value = '(' + valor.substr(0,2) + ') ' + valor.substr(2,tamanho-6) + '-' + valor.substr(tamanho-4,tamanho) ;			
		}
	}
	return true;
}


//Completa a data com as barras "/"
function ajustarData(input, evento){
    if(!soNumero(evento)){
	    return false;
	} else { 
		if ((input.value.length == 2)||(input.value.length == 5)) {
		   input.value = input.value + "/" ;
		   }
	}
	return true;
}

function soNumero(evento){
	var BACKSPACE=  8; 
	var TAB= 9;
    var DEL=  46; 
    var FRENTE=  39; 
    var TRAS=  37; 
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    
    if (( tecla == BACKSPACE )||(tecla == DEL)||(tecla == FRENTE)||(tecla == TRAS)||(tecla == TAB)){
         return true; 
	}
    if ( tecla == 13 )     return false; 

	if ((tecla >= 48 && tecla <= 57) || (tecla == 44)) {
		return true;
	}else{
		evento.returnValue = false; 
		return false;
	}
}

function abreDigitacao(fim, tpCampos, sel){
	var campo1 = document.getElementById("tt_indique");
	var campo2 = document.getElementById("cont_indique");
	if(tpCampos == 2){
		if((sel != "Indicacao") && (sel != "Outros")){
			fim = 0;
		}
	}
	if(fim == 1){
		campo1.style.display="";
		campo2.style.display="";
	}else if(fim == 0){
		campo1.style.display="none";
		campo2.style.display="none";
	}
}

function guardaFoto(nome, valor, opcaoFoto){
//	alert('guarda-foto.php?foto='+valor+'&opcao='+opcaoFoto);
	var campo = document.getElementById(nome);
	var caixa = document.getElementById('caixa_'+nome);
	document.getElementById(caixa).innerHTML = '<P>No carrinho</P>';
	if(campo.checked)
		window.open('guarda-foto.php?foto='+valor+'&opcao='+opcaoFoto, 'ifr_executa');
}

function mostraImagem(a,l,pg){
	l = eval(l) + 33;
	a = eval(a) + 57;
	if(a > 580) a = 580;
	if(l > 640) l = 660;
	nova_janela = window.open('','mostra_imagem','scrollbars=yes');
	
	pg = pg.replace(/ã/g,'%E3');
	
	nova_janela.document.writeln('<HTML><HEAD><TITLE>COMFIE</TITLE><SCRIPT>window.resizeTo('+l+','+(a+15)+'); function click(e) { if (e.button == 2 || e.button == 3) { oncontextmenu="return false"; } } if (document.layers) { document.captureEvents(event.MOUSEDOWN); document.onmousesdown = click; } else { document.onmousedown = click; } document.oncontextmenu = new Function("return false;"); </SCRIPT></HEAD><BODY style="margin:0;text-align=left; background: url('+pg+') top no-repeat;" onclick="window.close();"></BODY></HTML>');
	nova_janela.focus();
	nova_janela.document.close();
}

function mostraImagem2(a,l,pg){
	l = eval(l) + 33;
	a = eval(a) + 33;
	nova_janela = window.open('','mostra_imagem','scrollbars=yes, status=no');
	nova_janela.document.writeln('<HTML><HEAD><TITLE>COMFIE</TITLE><SCRIPT>window.resizeTo('+l+','+(a+15)+'); function click(e) { if (e.button == 2 || e.button == 3) { oncontextmenu="return false"; } } if (document.layers) { document.captureEvents(event.MOUSEDOWN); document.onmousesdown = click; } else { document.onmousedown = click; } document.oncontextmenu = new Function("return false;"); </SCRIPT></HEAD><BODY style="margin:0;text-align=left; background: url('+pg+') top no-repeat;" onclick="window.close();"></BODY></HTML>');
	nova_janela.focus();
	nova_janela.document.close();
}

function acessoTurma(turma, event){
	x = event.x + (screen.width/2);
	y = event.y + (screen.height/2);
	
	if(turma == 'ga'){
		nomeTurma = 'Gládio Azul';
		numTurma = 1;
	}else if(turma == 'fb'){
		nomeTurma = 'Fênix Branco';
		numTurma = 2;
	}

	// Titulo da caixa
	resultado = '<div style="width: 100%; text-align: right; background: #efefef; border-bottom: 1px solid #cccccc"><span style="width: 90%; text-align: left; color: #26576b; font-size: 11px">Login de acesso <b style="font-size: 13px">'+nomeTurma+'</b></span><span onClick=javascript:parent.document.getElementById("mostra_carrinho").style.display="none" title="Fechar Login" style="cursor: pointer; background: #333333; color: #ffffff">&nbsp;&nbsp;x&nbsp;&nbsp;</span></div><div id="quadro_login" style="margin-top: 35px">';
	// Formulário
	resultado = resultado + '<form name="frm_login" action="login.php" target="ifr_executa" method="POST"><input type="hidden" name="turma" value="'+numTurma+'"><div id="login" style="width: 250px;"><div id="login_esquerda" style="width: 97px; text-align: right">Nome Guerra:<BR><BR>Senha:</div><div id="login_direita"><input type="text" name="login" class="campo" style="width: 110px; margin-bottom: 13px"><br><input type="password" name="senha" class="campo" style="width: 110px;"></div><div id="login_baixo" align="right">Para se cadastrar <a href="cadastro.php?ls_turma='+numTurma+'">clique aqui</a>. <input type="submit" name="bt_envia" value="ok" style="background: #efefef; border: 1px solid #999; width: 40px; color: #777;"></div></div></form>';
	carrinho = parent.document.getElementById("mostra_carrinho");
	
	carrinho.style.top = y;
	carrinho.style.left = x;
	carrinho.style.width = '270px';
	
	carrinho.style.display = '';
	carrinho.innerHTML = resultado; 
	
}

function carregando(campo){
	figCarregando = '<div style="width: 100%; text-align: center;"><img src=figuras/carregando.gif></div>';
	caixa = document.getElementById(campo);
	caixa.innerHTML = figCarregando; 
	return true;
}

function formataMilhao(input, evento){
	if(!soNumero(evento)){
	    return false;
	}else{
		if ((input.value.length == 2)) {
			input.value = input.value + "/" ;
		}
		return true;
	}
}

function carregaFilme(){
	var so = new SWFObject("topo.swf", "mymovie", "100%", "100%", "7", "#336699");
	so.addParam("quality", "high");
	so.addParam("wmode", "transparent");
	so.addParam("salign", "t");
	so.write("animacao");
}
