﻿// JavaScript Document

function mostrar(id) {
	document.getElementById(id).style.display = "block";
}

function apagar(id) {
	document.getElementById(id).style.display = "none";
}

function mudarAba(aba,evento,qts) {
	
	atual = aba;

	//Colocando todas as abas com fundo branco e cor #BAB3D6
	if(evento == "click") {
		for(i=0;i<qts;i++) {
			div = "divAba" + i;
			document.getElementById(div).style.color = "#BAB3D6";
			esq = div + "Esq";
			meio = div + "Meio";
			dir = div + "Dir";
			document.getElementById(esq).style.backgroundImage = "url('imagens/colunas/fundo_esq_aba_branca.png')";
			document.getElementById(meio).style.backgroundColor = "#FFF";
			document.getElementById(dir).style.backgroundImage = "url('imagens/colunas/fundo_dir_aba_branca.png')";
		}
		anterior = atual;
	}
	
	//Definindo propriedades da aba clicada	
	esq = atual + "Esq";
	meio = atual + "Meio";
	dir = atual + "Dir";
	
	cor = "#FFF";
	fundo = "#BAB3D6";
	fundo_esq = "url('imagens/colunas/fundo_esq_aba.png')";
	fundo_dir = "url('imagens/colunas/fundo_dir_aba.png')";
	
	//Modificando as propriedades caso tenha sido somente "out" e não "click"
	if (evento == "out" && anterior!=atual) {
		cor = "#BAB3D6";
		fundo = "#FFF";
		fundo_esq = "url('imagens/colunas/fundo_esq_aba_branca.png')";
		fundo_dir = "url('imagens/colunas/fundo_dir_aba_branca.png')";
	}
	
	//Aplicando modificações
	document.getElementById(atual).style.color = cor;
	document.getElementById(esq).style.backgroundImage = fundo_esq;
	document.getElementById(dir).style.backgroundImage = fundo_dir;
	document.getElementById(meio).style.backgroundColor = fundo;
	
}

function passarCampo(campoAtual) {
	var qtdCar = document.getElementById(campoAtual).value.length;
	switch (campoAtual) {
		case "txtDiaNascimento" :
			num = 2;
			proximo = "txtMesNascimento";
			break;
		case "txtMesNascimento" :
			num = 2;
			proximo = "txtAnoNascimento";
			break;
		case "txtAnoNascimento":
			num = 4;
			proximo = "txtBairro";
			break;
		case "txtDDDTelefone":
			num = 2;
			proximo = "txtTelefone";
			break;
		case "txtTelefone":
			num = 8;
			proximo = "txtDDDCelular";
			break;
		case "txtDDDCelular":
			num = 2;
			proximo = "txtCelular";
			break;
		case "txtCelular":
			num = 8;
			proximo = "selFicaSabendo";
			break;
	}
	if(qtdCar >= num) {
		document.getElementById(proximo).focus();
	}
}

function valida_data(date) {
	var array_data = new Array;
	var ExpReg = new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
	array_data = date.split("/");
	var erro = false;
	if ( date.search(ExpReg) == -1 )
		var erro = true;
	else if ( ( ( array_data[1] == 4 ) || ( array_data[1] == 6 ) || ( array_data[1] == 9 ) || ( array_data[1] == 11 ) ) && ( array_data[0] > 30 ) )
		var erro = true;
	else if ( array_data[1] == 2 ) {
		if ( ( array_data[0] > 28 ) && ( ( array_data[2] % 4 ) != 0 ) )
			var erro = true;
		if ( ( array_data[0] > 29 ) && ( ( array_data[2] % 4 ) == 0 ) )
			var erro = true;
	}
	if(erro) {
		return false;
	} else {
		return true;
	}
}

function preValidacao(id,valor,tipo) {
	span = "";
	erro = "";
	ER = "";
	Validado = true;
	switch(id) {
		case "txtNome":
			ER = /^.{3,} .{3,}$/;
			erro = "Digite seu nome e sobrenome";
			break;
		case "txtEmail":
			ER = /^[^	 ]{2,}@[^	 ]{2,}\..{2,}.*$/;
			erro = "Digite um e-mail válido";
			break;
		case "pswSenha":
			ER = /^.{6,}$/;
			erro = "Digite uma senha de pelo menos 6 dígitos";
			break;
		case "pswConfirmaSenha":
			ER = "pswConfirmaSenha";
			erro = "Essa senha não confere com a senha anterior";
			break;
		case "txtDiaNascimento":
		case "txtMesNascimento":
		case "txtAnoNascimento":
			ER = "txtDiaNascimento";
			erro = "Digite uma data válida";
			break;
	}
	
	switch(ER) {
		case "pswConfirmaSenha":
			if((document.getElementById("pswSenha").value != valor) || (valor.length == 0)) {
				span = ER;
			}
			break;
		case "txtDiaNascimento":
			diaNasc = document.getElementById("txtDiaNascimento").value;
			mesNasc = document.getElementById("txtMesNascimento").value;
			anoNasc = document.getElementById("txtAnoNascimento").value;
		
			if(diaNasc.length < 2) {
				diaNasc = "0" + diaNasc;
			}
			
			if(mesNasc.length < 2) {
				mesNasc = "0" + mesNasc;
			}
			
			var dataCompleta = diaNasc + "/" + mesNasc + "/" + anoNasc;
			
			if(!valida_data(dataCompleta)) {
				span = ER;
			}
			id = ER;
			break;
		default:
			if(!ER.test(valor)) {
				span = id;
			}
			break;
	}
	
	id += "Validacao";
	
	if(span != "") {
		msg = "<img src='imagens/campoErro.gif' alt='Erro' /> " + erro;
		Validado = false;
	} else {
		msg = "<img src='imagens/campoOk.png' alt='Ok' />";
	}
	
	document.getElementById(id).innerHTML = msg;
	
	if(tipo == "final") {
		return Validado;
	}
	
}

function Validacao() {
	
	var Validado = true;
	var campos = new Array("txtNome","txtEmail","pswSenha","pswConfirmaSenha","txtDiaNascimento");
	for(i=0;i<5;i++) {
		var campo = campos[i];
		var valor = document.getElementById(campo).value;
		if(!preValidacao(campo,valor,"final")) {
			var Validado = false;
		}
	}
	
	return Validado;
}

fileira = 0;

function acharFileira (fotoAtual) {
	fileira = Math.floor(fotoAtual / 5) ;
	if(fileira < 0) {
		fileira = 0;
	}
	defineTopFileira();
}

function defineFileira (sentido, maximo) {
	qtdFileiras = maximo;
	if(sentido == "+") {
		if((fileira + 1) <= maximo) {
			fileira++;
		}
	} else {
		if((fileira - 1) >= 0) {
			fileira--;
		}
	}
	defineTopFileira();
}

function defineTopFileira () {
	var top = fileira * 108;
	var top = "-" + top + "px";
	document.getElementById("divFileiraGaleria").style.top = top;
	checaSetasGaleria();
}

ultimaFoto = "0";

function passaFoto(iFoto) {
	document.getElementById("divFotoAbertaGaleria").innerHTML = "<img src=\"imagens/galeria/fotos/" + arrayLegendas[iFoto][0] + ".jpg\" alt=\"Carregando...\" title=\"" + arrayLegendas[iFoto][1] + "\" />";
	document.getElementById("divLegendaFotoAberta").innerHTML = "<br /><p style='margin-bottom: 15px;'>" + arrayLegendas[iFoto][1] + "</p><a href='../scripts/download.php?arquivo=../imagens/galeria/fotos/" + arrayLegendas[iFoto][0] + ".jpg'><img src='imagens/baixarFoto.png' border='0' /></a> <a href='javascript:void(0);' onclick=\"mostrar('divEnviarAmigo');\"><img src='imagens/enviarAmigo.png' border='0' /></a>	<div id='divEnviarAmigo' style='display: none;'>	<center><table cellpadding='1' cellspacing='1' border='0' style='width: 400px; text-align; center;'>		<tr>			<td style='text-align: left;'>				<label style='font-family: Verdana; font-weight: bold; font-size: 0.8em;'>Seu Nome: </label>			</td>			<td>				<input type='text' id='txtSeuNome' style='width: 200px;' /><br />			</td>		</tr>		<tr>			<td style='text-align: left;'>				<label style='font-family: Verdana; font-weight: bold; font-size: 0.8em;'>Seu e-mail: </label>			</td>			<td>				<input type='text' id='txtSeuEmail' style='width: 200px;' /><br />			</td>		</tr>		<tr>			<td style='text-align: left;'>				<label style='font-family: Verdana; font-weight: bold; font-size: 0.8em;'>E-mail do seu amigo: </label>			</td>			<td>				<input type='text' id='txtEmailAmigo' style='width: 200px;' />			</td>		</tr>		<tr>			<td colspan='2' style='text-align: center;'>				<input type='button' value='Enviar' onclick='enviarAmigo()' />			</td>		</tr>	</table></center>	<input type='hidden' id='hidDadosEnviarAmigo' value='" + iFoto + "' />	</div>";
	limpaBordasFotos();
	idImgThumb = "img" + arrayLegendas[iFoto][0];
	ultimaFoto = iFoto;
	document.getElementById(idImgThumb).style.border = "3px solid #DD137B";
	acharFileira(iFoto);
}

function defineFoto(sentido,maximo) {
	if(sentido == "+") {
		if((ultimaFoto + 1) <= maximo) {
			ultimaFoto++;
		}
	} else {
		if((ultimaFoto - 1) >= 0) {
			ultimaFoto--;
		}
	}
	passaFoto(ultimaFoto);
}

function checaSetasGaleria() {
	if(fileira == 0) {
		document.getElementById("aSetaEsqThumbsGaleria").style.backgroundImage = "url('imagens/SetaEsqGaleria3.png')";
	} else {
		document.getElementById("aSetaEsqThumbsGaleria").style.backgroundImage = "url('imagens/SetaEsqGaleria.png')";
	}
	
	if(fileira == qtdFileiras) {
		document.getElementById("aSetaDirThumbsGaleria").style.backgroundImage = "url('imagens/SetaDirGaleria3.png')";
	} else {
		document.getElementById("aSetaDirThumbsGaleria").style.backgroundImage = "url('imagens/SetaDirGaleria.png')";
	}
	
	if(ultimaFoto == 0) {
		document.getElementById("aSetaEsqFotosGaleria").style.backgroundImage = "url('imagens/SetaEsqGaleria3-2.png')";
	} else {
		document.getElementById("aSetaEsqFotosGaleria").style.backgroundImage = "url('imagens/SetaEsqGaleria-2.png')";
	}
	
	if(ultimaFoto == qtdFotos) {
		document.getElementById("aSetaDirFotosGaleria").style.backgroundImage = "url('imagens/SetaDirGaleria3-2.png')";
	} else {
		document.getElementById("aSetaDirFotosGaleria").style.backgroundImage = "url('imagens/SetaDirGaleria-2.png')";
	}
}


function limpaBordasFotos() {
		for(i = 0; i <= qtdFotos; i++) {
			var idAtual = "img" + arrayLegendas[i][0];
			document.getElementById(idAtual).style.border = "3px solid #FFF";
		}
}

function enviarAmigo() {
	seuNome = document.getElementById("txtSeuNome").value;
	seuEmail = document.getElementById("txtSeuEmail").value;
	emailAmigo = document.getElementById("txtEmailAmigo").value;
	dadosEmail = aAtual + "," + albumAtual + "," + document.getElementById("hidDadosEnviarAmigo").value;
	
	var todasVariaveis = seuNome + "," + seuEmail + "," + emailAmigo + "," + dadosEmail;
	
	includePage("enviarAmigo.php","divEnviarAmigo",todasVariaveis,"<p style='font-family: Verdana; font-size: 0.8em; font-weight: bold;'>Enviando...</p>");
}
