// JavaScript Document

// -- Validação simples e máscaras de campo -- ////////////////////////////////////////////////
function VCampo(cmpo){
	var Valor = cmpo.value;
	var VPadrao = cmpo.defaultValue;
	
	if(Valor != VPadrao && Valor != "")
		return true;
	else {
	   alert("Campo ["+cmpo.title+"] inválido!");
	   return false;
	}
}

// -- Validar o formato do email
function FormatEmail(cmpo){
	if((/^[a-zA-Z0-9_.-]+\@[a-z]+\.[a-z]?/).test(cmpo.value)){
		return "OK";
	} else {
		alert("Campo "+cmpo.name+" inválido!");
	}
}

// -- Aplicar o formato correto do telefone
function MaskFone(cmpo){
	if(cmpo.value.length == 10){
		var Mask = "("+cmpo.value.substr(0,2)+")"+cmpo.value.substr(2,4)+"-"+cmpo.value.substr(6,9);
		cmpo.value = Mask;
	}
}

function ValorPadrao(obj, caso){
	switch(caso){
		case 0:
			if(obj.value == obj.defaultValue){
				obj.value = "";
			};
		break;
		case 1:
			if(obj.value == ""){
				obj.value = obj.defaultValue;
			};
		break;
	}
}
// -- Fim da Validação Simples e máscaras de campos -- //////////////////////////////////////////

// --  Cria cardápio -- /////////////////////////////////////////////////////////////////////////
function CriaCont(titulo, img, texto){
	// Seleciona os Objs
	var Img = document.getElementById("CardImg");
	var Txt = document.getElementById("txtExpl");
	var TdList = document.getElementById("TdList");
	
	// Remove os nós atuais
	Txt.removeChild(Txt.firstChild);
	
	// Cria os texto que vão compôr o conteúdo
	var VrExpl = document.createTextNode(texto);// Texto de explicação
	var List = document.createElement("ul");
	List.setAttribute("id", "List");
	
	// Atribui os textos aos Objs
	Img.src = "img/"+img;// Imagem a ser exibida
	Txt.appendChild(VrExpl);
	TdList.appendChild(List);
	
	document.getElementById("Select").src = "SelCardapio.php?t="+titulo;
}

function DeletaLista(){
	var Obj = document.getElementById("TdList");
	Obj.removeChild(Obj.firstChild);
}

function CriaLista(valor){
	var Obj = document.getElementById("List");
	var Li = document.createElement("li");
	
	Obj.appendChild(Li);
	
	var Txt = document.createTextNode(valor);
	Li.appendChild(Txt);
}

// -- Fim do Cardápio -- ////////////////////////////////////////////////////////////////////////

// -- Galeria de fotos -- ///////////////////////////////////////////////////////////////////////
function PreviewFoto(img, titulo, legenda, image, name){
	var Img = document.getElementById("ImPreview");
	var Tit = document.getElementById("TdTitulo");
	var Leg = document.getElementById("Legenda");
	
	// Removem os textos que já existem
	Tit.removeChild(Tit.firstChild);
	Leg.removeChild(Leg.firstChild);
	
	// Criar textos
	var TxtTitulo = document.createTextNode(titulo);
	var TxtLeg = document.createTextNode(legenda);
	
	Img.setAttribute("onclick", "window.open('"+image+"')");  // -- Ampliar imagem
	Img.setAttribute("name", name);                           // -- Definir o novo nome da imagem
	
	Img.src = img;                                            // -- Alterar a imagem
	Tit.appendChild(TxtTitulo);                               // -- Cria o titulo
	Leg.appendChild(TxtLeg);                                  // -- Cria a legenda
	
	// -- Configura os botões para passagem da foto /////////////////////////////////////////////////////////////////////////
	var ImgNova = Fotos.document.getElementById(eval(Img.name)+1);    // -- Próxima imagem
	var LinkProx = document.getElementById("LinkProx");               // -- Link para a próxima imagem
	var Click = ImgNova.getAttribute("onclick");                      // -- Seleciona a ação da imagem atual
	LinkProx.setAttribute("onclick", Click.replace("parent.", ""));   // -- Define a ação que passa para a próxima imagem

	var ImgAnt = Fotos.document.getElementById(eval(Img.name)-1);     // -- Imagem anterior
	var LinkAnt = document.getElementById("LinkAnt");                 // -- Link para a imagem anterior
	var ClickAnt = ImgAnt.getAttribute("onclick");                    // -- Seleciona a ação da imagem anterior
	LinkAnt.setAttribute("onclick", ClickAnt.replace("parent.", "")); // -- Define a ação que passa para a imagem anterior
}

function AlteraTipo(atual, novo, tipo){
	var ObjAtual = document.getElementById(atual);
	var ObjNew = novo;
	
	ObjAtual.id = "FotoTipo";
	ObjNew.id = "TipoAtivo";
	
	Fotos.location="vFotos.php?t="+tipo;
}

function TrocaFoto(){
	var ImgAtual = document.getElementById("ImPreview");
	var ImgNova = Fotos.document.getElementById(ImgAtual.name+1);
	var Link = document.getElementById("LinkProx");
	
	var Click = ImgNova.getAttribute("onclick");
	Link.setAttribute("onclick", Click.replace("parent.", ""));
	alert(Link.onclick);
}

function ImgHover(objeto, source){
	var Obj = objeto;
	Obj.src = source;
}
// -- Fim da Galeria de Fotos -- ////////////////////////////////////////////////////////////////////////////////////////////

// -- Mascaras de campos -- ////////////////////////////////////////////////////////////////////
function MaskIdade(v){
	var Valor = v.value;
	if(Valor.length == 2){
		v.value += "o";
	}
}

function MaskData(v){
	var Valor = v.value;
	switch(Valor.length){
		case 2: v.value += "/"; break;
		case 5: v.value += "/"; break;
	}
}

function MaskHora(v){
	var Valor = v.value;
	switch(Valor.length){
		case 2: v.value += ":"; break;
	}
}
// -- //////////////////////////////////////////////////////////////////////////////////////////

// -- //////////////////////////////////////////////////////////////////////////////////////////

function PreVis(convite){
	document.getElementById("previs").src=convite+".jpg";
}
// -- //////////////////////////////////////////////////////////////////////////////////////////

// -- Funcao para validar form de login -- /////////////////////////////////////////////////////
function VLogin(form){
	var login = form.login;
	var senha = form.senha;
	
	if( VCampo(login) && VCampo(senha) )
		return true;
	else{
		return false;
	}
}
// -- //////////////////////////////////////////////////////////////////////////////////////////

// -- Funcao para abrir a janela de convite -- /////////////////////////////////////////////////
function OpenConvite(){
	window.open("convites/index.php", "ConviteVirtual", "width=810px, height=410px");
}
// -- //////////////////////////////////////////////////////////////////////////////////////////
