/* ---// FONTES //--- */
IncludeJavaScript("scripts/cufon.js"); // Núcleo do Cufón
IncludeJavaScript("scripts/fontes.js"); // Fontes
//IncludeJavaScript("scripts/helvetica-neue-47.js"); 
//IncludeJavaScript("scripts/helvetica-neue-77.js"); 
/* ---// PLUGINS //--- */
IncludeJavaScript("scripts/jquery.cycle.js"); // Cycle
IncludeJavaScript("scripts/jquery.easing.js"); // Cycle
IncludeJavaScript("scripts/jquery.flash.js"); // Flash
IncludeJavaScript("scripts/jquery.facebox.js"); // Facebox
IncludeJavaScript("scripts/jquery.lightbox.js"); // Lightbox
//IncludeJavaScript("scripts/jquery.cookie.js"); // Cookies
//IncludeJavaScript("scripts/jquery.scroll.js"); // Barras de rolagem
//IncludeJavaScript("scripts/jquery.mousewhell.js"); // Barraras de rolagem - scroll do mouse
IncludeJavaScript("scripts/jquery.scrollto.js"); // Smooth Scroll na tela
//IncludeJavaScript("scripts/jquery.ui.js"); // jQuery UI completo
//IncludeJavaScript("scripts/jquery.easing.js"); // Easing
IncludeJavaScript("scripts/jquery.maskedinput.js"); // Máscaras para formulários 

$(function() {

	Cufon.replace('.vani', { fontFamily: 'Vani', hover: true });
	Cufon.replace('.vani-s', { fontFamily: 'Vani', hover: true, textShadow: "#2c1c46 1px 2px" });
	// Cufon.replace('.neue-77', { fontFamily: 'Helvetica Neue 77' });
	// if (typeof $.fn.cycle != "undefined") { $("ul.cycle").cycle({ fx: "scrollHorz", prev: ".arrow-left", next: ".arrow-right", timeout: 0 }); }
	if (typeof $.fn.mask != "undefined") { $(".masc_telefone").mask("(99)9999-9999"); $(".masc_cpf").mask("999.999.999-99"); $(".masc_data").mask("99/99/9999"); $(".masc_cep").mask("99999-999"); }
	
	// LINKS EXTERNOS
	$("a[rel=external], .texto-confirmar a").attr('target','_blank');
	$("a[rel=external nofollow]").attr('target','_blank');
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");
	$(".bt-voltar").click(function(){ history.back(); return false; });
	
/*
	// Função pra tirar o value do input e botar devolta
	var input = document.getElementsByTagName('input');
	for (var a=input.length-1; a >= 0; a--) {
		if (input[a].type != 'text') { continue; }
		input[a].mask = input[a].value;
		input[a].onfocus = function() {
			if (this.value == this.mask) { this.value = ''; }
		};
		input[a].onblur = function() {
			if (this.value == '') { this.value = this.mask; }
		};
	}
*/
	
	// Flash principal home
	$(".flash-home").flash({
	    src: "swf/header.swf",
	    width: 960,
	    height: 570,
		wmode: "transparent",
		expressInstall: true
	});
	
	// Flash menu interno
	$(".flash-menu-interno").flash({
	    src: "swf/interna.swf",
	    width: 740,
	    height: 60,
		wmode: "transparent",
		expressInstall: true
	});
	
	// TRANSPARENCIA
	$(".opac").css("opacity","0.5");
	
	
	//Link-topo
	$(".bt-topo").click(function() {
		$.scrollTo( { top:0,left:0 } , 1000 );
		return false;
	});

});
/**
  * Função limpa espaço
  * @param str String
  * @return nada
  **/
function getTrim(str) {
	if((typeof(str) !== 'undefined') && (str != null)){
		return str.replace(/^\s+|\s+$/g, "");
	}else{
		return "";
	}
}

/**
  * Função chaca o valor do radio
  * @param String $radioObj
  * @return boolean
  **/
function getCheckedValue(radioObj) {
	if (!radioObj) {
		return "";
	}
	var radioLength = radioObj.length;
	if (radioLength == undefined) {
		if (radioObj.checked) {
			return radioObj.value;
		} else {
			return "";
		}
	}
	for (var i = 0; i < radioLength; i++) {
		if (radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

/**
  * Função Validar Formulário
  * @param form Identificador do form
  * @return Boolean
  **/
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        var nomeObj = getTrim(document.getElementById(form).elements[i].name);
        var idObj 	= getTrim(document.getElementById(form).elements[i].id);
		var relObj 	= getTrim(document.getElementById(form).elements[i].getAttribute("rel"));
		var altObj 	= getTrim(document.getElementById(form).elements[i].getAttribute("alt"));
		switch (relObj) {
			case "email":
				expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
				if (!expressao.test(document.getElementById(form).elements[i].value)) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "required":
				if (
					(document.getElementById(form).elements[i].value == null) 
					|| (document.getElementById(form).elements[i].value == "")
					|| (document.getElementById(form).elements[i].value == altObj)
				) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "cpf":
				if (
					(!validaCpf(document.getElementById(form).elements[i].value)) 
					|| (document.getElementById(form).elements[i].value == null) 
					|| (document.getElementById(form).elements[i].value == "")
					|| (document.getElementById(form).elements[i].value == altObj)
				) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "data":
				if (
					(!validaData(document.getElementById(form).elements[i].value)) 
					|| (document.getElementById(form).elements[i].value == null) 
					|| (document.getElementById(form).elements[i].value == "")
					|| (document.getElementById(form).elements[i].value == altObj)
				) {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].value = "";
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			case "radio":
			case "checkbox":
				if(getCheckedValue(document.getElementsByName(nomeObj))) {
				} else {
					alert(document.getElementById(form).elements[i].title);
					document.getElementById(form).elements[i].focus();
					return false;
				}
			break;
			default:
			break;
		}
    }
    return true;
}

/**
  * Função Habilita/Desabilita Submit
  * @param form Identificador do form
  * @return 
  **/
function habilitaDesabilitaSubmit(form,parametro) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        var idObj 	= document.getElementById(form).elements[i].id;
        var tipObj 	= document.getElementById(form).elements[i].type;
		switch (tipObj) {
			case "submit":
				switch(parametro){
					case 'disabled':
						if(document.getElementById(form).elements[i].disabled){
							return true;
						}else{
							return false;
						}
					break;
					default:
						document.getElementById(form).elements[i].disabled = parametro;
					break;
				}
			break;
			default:
			break;
		}
    }
}

/**
  * Função envia dados
  * @param form Identificador do form
  * @return nada
  **/
function validarFormPostAjax(form) {
	if (validaForm(form)) {

		if (habilitaDesabilitaSubmit(form, 'disabled')) {
			//Submit está desabilitado
			alert('Aguarde o término do processo. Obrigado!');
		} else {
			//Submit está habilitado
			habilitaDesabilitaSubmit(form, true);
			
			$(document).ready(function(){
				$("#" + form).ajaxForm(function(txt){
					arrtxt = txt.split('<!-- SEPARADOR -->');
					cod = getTrim(arrtxt[0]);
					msg = getTrim(arrtxt[1]);
					reload = getTrim(arrtxt[2]);
					url = getTrim(arrtxt[3]);
					switch (cod) {
						case '0':
						case 'false':
							alert(msg);
						break;

						case '1':
						case 'true':
						case 'ok':
							if(msg != ''){
								alert(msg);
							}
							switch (reload) {
								case '1':
								case 'true':
								case 'ok':
									if (url != '') {
										window.location.href = url;
									}
									else {
										window.location.reload();
									}
								break;

								case '':
								default:
									document.getElementById(form).reset();
								break;
							}
						break;

						default:
							alert(txt);
						break;
					}
					habilitaDesabilitaSubmit(form,false);
				});
			});
			return true;
		}
	}else{
		return false;
	}
}

/**
  * Função validar
  * @param nada
  * @return nada
  **/
function validarComPost(form) {
	validarComPostFacebox(form,false);
}

/**
  * Função validar
  * @param form: id do form
  * @return nada
  **/
function validarComPostFacebox(form,fechar) {
	if (validaForm(form)) {
		if (habilitaDesabilitaSubmit(form, 'disabled')) {
			//Submit está desabilitado
			alert('Aguarde o término do processo. Obrigado!');
		} else {
			//Submit está habilitado
			habilitaDesabilitaSubmit(form, true);

			var str = $("#" + form).serialize();
			$.ajax({
				type: "POST",
				url: "lib/acao-" + form + ".php",
				data: str,
				beforeSend: function(){
				},
				success: function(txt){
					arrtxt = txt.split('<!-- SEPARADOR -->');
					cod = getTrim(arrtxt[0]);
					msg = getTrim(arrtxt[1]);
					reload = getTrim(arrtxt[2]);
					url = getTrim(arrtxt[3]);
					switch (cod) {
						case '0':
						case 'false':
							alert(msg);
						break;

						case '1':
						case 'true':
						case 'ok':
							if(msg != ''){
								alert(msg);
							}
							if (fechar) {
								if ($.facebox) {
									$.facebox.close();
								}
							}
							switch (reload) {
								case '1':
								case 'true':
								case 'ok':
									if (url != '') {
										window.location.href = url;
									}
									else {
										window.location.reload();
									}
								break;

								default:
									document.getElementById(form).reset();
								break;
							}
						break;

						default:
							alert(txt);
						break;
					}
					habilitaDesabilitaSubmit(form,false);
				},
				error: function(txt){
					habilitaDesabilitaSubmit(form,false);
					alert("Erro: " + txt);
				}
			});
		}
	}
}

/**
  * Função monta caixa de seleção
  * @param div DIV
  * @param id Identificador
  * @return nada
  **/
function montaCaixa(div, id, sel) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-caixa.php",
		data: 	"" + 
				"div=" + div + "&" + 
				"id=" + id + "&" + 
				"sel=" + sel + "&" + 
				"",
		success: function(txt) {
			$("#" + div).html(txt);
		}
	});
}

/**
  * Formata a digitação do CPF / CNPJ
  *
  * @param campo String campo que será atualizado
  * @param e event evento
  * @return void
  **/
function formataCpfCnpj(campo, e) {
	evento = typeof window.event != "undefined" ? window.event.keyCode : e.which;
	if((isNaN(String.fromCharCode(evento)) || (evento == 32)) && (evento != 8 && evento != 0)) {
		return false;
	}
	mascara = false;
	if (evento != 45 && evento != 46 && evento != 8 && evento != 0 ) {
		var str = campo.value;
		str = str.replace('.','');
		str = str.replace('.','');
		str = str.replace('-','');
		str = str.replace('/','');

		if (str.length < 11) {
			mascara = 'cpf';
			temp = str.substr(0,3) ;
			if(temp.length == 3 && str.length != 3) {
				temp += '.';
			}
			temp += str.substr(3,3);
			if(temp.length == 7) {
				temp += '.';
			}
			temp += str.substr(6,3);
			if(temp.length == 11) {
				temp += '-';
			}
			temp += str.substr(9,2);
			campo.value= temp;
		}else{
			mascara = 'cnpj';
			temp = str.substr(0,2);
			if(temp.length == 2) {
				temp += '.';
			}
			temp += str.substr(2,3);
			if(temp.length == 6) {
				temp += '.';
			}
			temp += str.substr(5,3);
			if(temp.length == 10) {
				temp += '/';
			}
			temp += str.substr(8,4);
			if(temp.length == 15) {
				temp += '-';
			}
			temp += str.substr(12,2);
			campo.value= temp;
		}
	}
}
/**
  * Função monta lojas de reparo
  * @param div DIV
  * @param id Identificador
  * @return nada
  **/
function montaLojaReparo(div, id) {
	$.ajax({
		type: "POST",
		url: "lib/acao-monta-loja-reparo.php",
		data: 	"" + 
				"div=" + div + "&" + 
				"id=" + id + "&" + 
				"",
		beforeSend: function(){
			$("#" + div).addClass('loading');
		},
		success: function(txt) {
			$("#" + div).removeClass('loading');
			$("#" + div).html(txt);
		},
		error: function(txt) {
			$("#" + div).removeClass('loading');
		}
	});
}

function makepage(titulo,conteudo) {
  return "<html>\n" +
    "<head>\n" +
    "<title>" + titulo + "</title>\n" +
    "<scr" + "ipt>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
    "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad='step1()'>\n" +
    "" + conteudo + "\n" +
    "</body>\n" +
    "</html>\n";
}

function printme(titulo,id) {
	conteudo = document.getElementById(id).innerHTML;
	link = "about:blank";
	var pw = window.open(link, "_new", "width=620, height=400, scrollbars=yes");
	pw.document.open();
	pw.document.write(makepage(titulo,conteudo));
	pw.document.close();
}

/**
  * Função validar login
  * @param form Identificador do form
  * @return mensagem de erro
  **/
function validarLogin(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-login.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == 0) {
					alert("Sua senha não confere, tente novamente.");
				} else if (getTrim(txt) == 1) {
					window.location = "index.php?cmd=confirmacao-presenca";
				} else if (getTrim(txt) == 2) {
					alert("Seu evento não foi confirmado, tente novamente.");
				} else {
					alert("Não foi possível efetuar o login, tente novamente.");
				}
			}
		});
	}
}

/**
  * Função validar confirmação
  * @param form Identificador do form
  * @param tipo Tipo
  * @return mensagem de erro
  **/
function validarConfirmacao(form) {
	if (validaForm(form)) {
		var str = $("#" + form).serialize();
		$.ajax({
			type: "POST",
			url: "lib/acao-form-confirmacao.php",
			data: str,
			success: function(txt) {
				if (getTrim(txt) == 0) {
					alert("Não foi possível efetuar a presença, tente novamente.");
				} else if (getTrim(txt) == 1) {					
					//$(window.location).attr('href', 'index.php?cmd=confirmacao-presenca');
					//alert("Presença confirmada com sucesso.");
					$confirm = confirm('Presença confirmada com sucesso.\nDeseja sair do painel?');
					if($confirm == true){
						//document.location.href='index.php?cmd=confirmacao-presenca';
						document.location.href='lib/sair.php';
					}else{
						document.getElementById(form).reset();
					}
				} else if (getTrim(txt) == 2) {
					alert("Seu evento não foi confirmado, tente novamente.");
				} else {
					alert("Não foi possível efetuar a presença, tente novamente.");
				}
			}
		});
	}
}
