// JavaScript Document
function EsVacio(x){
	var filter=/^\s*$/
	return filter.test(x);
}

function EsCorreo(x){
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	return filter.test(x);
}

function valFormN(){
	if(EsVacio($("#user_login").val())){ alert("Escribe un nombre de usuario o e-mail.");  $("#user_login").focus(); return false;}
	return true;
}

$(document).ready(function(){
	$("#form-login").hide();
	$("#a-ini-sesion").bind("click",function(){
		$("#form-login").show();
		$(this).hide();
		return false;
	});
	$("#btn-cancel-login").bind("click",function(){
		$("#form-login").hide();
		$("#a-ini-sesion").show();
		return false;
	});
	$(".busca-1").bind({
		focus:
			function(){
				$(this).val("");
				return false;
			},
		blur:
			function(){
				if($(this).val()=="") $(this).val("B\u00fasqueda")
				return false;
			}
	});
	$('#slider-cont').cycle({ 
		fx:     'fade', 
		speed:  1500, 
		timeout: 5500, 
		next:   '#boton-der-slider',
		cleartype: true, 
		cleartypeNoBg: true
	});
	$("#header .row-3 li a").mouseover(function(){
		$(this).stop().animate({"margin-top":"10px"},"fast");
	}).mouseout(function(){
		$(this).stop().animate({"margin-top":"0px"},"fast");
	});
	
	$("#fecha_nac").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		yearRange: '1950:'+new Date(),
		maxDate: new Date()
	});
	$("#password").passStrength({
		userid: "#nom_user"
	});
	$("#form-user").validate({
		rules: {
			nom_user: "required",
			nombre: "required",
			apellido: "required",
			estado: "required",
			pais: "required",
			ciudad: "required",
			acepto_contrato: "required",
			correo: {
				required : true,
				email: true
			},
			password: {
				required: false,
				minlength: 5
			},
			password2: {
				required: false,
				minlength: 5,
				equalTo: "#password"
			}
		},
		messages: {
			nom_user: "Por favor, ingresa tu nombre de Usuario.",
			nombre: "Por favor, ingrea tu Nombre.",
			apellido: "Por favor, ingresa tu Apellido.",
			estado: "Por favor, ingresa tu Estado",
			ciudad: "Por favor, ingresa tu Ciudad",
			pais: "Por favor, ingresa tu Pa&iacute;s",
			correo: "Por favor, ingresa una cuenta de correo valida.",
			password: "Por favor, porporciona un password de al menos 5 caracteres.",
			password2: "Por favor, repite tu password.",
			acepto_contrato: "Por favor, acepta los t&eacute;rminos y condiciones para continuar"
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.appendTo( element.parent() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
	$(".ceebox").ceebox();
});

function muestraPop(){
	$.fn.ceebox.popup("<a href=\"popup.php\"><\/a>",{htmlWidth:600,htmlHeight:500});
}

function sendContact(){
	if(EsVacio($("#nombre").val())){ $("#msj_resultadoC").html('El campo "Nombre" no puede quedar vacio.'); $("#nombre").focus(); return false;}
	if(!EsCorreo($("#email").val())){ $("#msj_resultadoC").html('Ingresa un correo valido.'); $("#email").focus(); return false;}
	
	$.ajax({
		type: "POST",
		url: "procesa-contacto.php",
		data: "metodo=ajax&btnPostC=Enviar&nombre="+$("#nombre").val()+"&email="+$("#email").val()+"&telefono="+$("#telefono").val()+"&empresa="+$("#empresa").val()+"&comentario="+$("#comentario").val()+"&sendTo="+$("#sendTo").val(),
		beforeSend: function(objeto){
           $("#msj_resultadoC").html("enviando...");
        },
		success:
			function(datos){
				if(datos!=""){
					$("#msj_resultadoC").html(datos);
					$("#nombre").val("");
					$("#email").val("");
					$("#telefono").val("");
					$("#comentario").val("");
				}else{
					$("#msj_resultadoC").html("<?php echo _NOSEPUDOENVIAR?>.");
				}
			},
		error:
			function(){
				 $("#msj_resultadoC").html("<?php echo _OCURRIOUNERRROR?>");
			}
	});
	
	return false;
}

function sendMail(){
	if(!EsCorreo($("#email-newsletter").val())){ $("#msj_resultado").html("Ingresa un correo valido"); $("#email-newsletter").focus(); return false;}

	$.ajax({
		type: "POST",
		url: "procesa-newsletter.php",
		data: "metodo=ajax&btnPostM=Enviar&email-newsletter="+$("#email-newsletter").val(),
		beforeSend: function(objeto){
           $("#msj_resultado").html("enviando...");
        },
		success:
			function(datos){
				if(datos!=""){
					$("#msj_resultado").html(datos);
					$("#email-newsletter").val("");
				}else{
					$("#msj_resultado").html("No se pudo guardar la informaci&oacute;n.");
				}
			},
		error:
			function(){
				 $("#msj_resultado").html("Ocurrio un error!");
			},
	});
	return false;
}

function pEncuesta(){
	var miVariable= "";
	$("#form-encuesta input[name='resp']:radio").each(function(i){
        if ($(this).is(':checked')) {
            miVariable = $(this).val();
        }
    });

	if(miVariable!=""){
		$.ajax({
			type: "GET",
			url: "procesa-encuesta.php",
			data: "metodo=ajax&"+$("#form-encuesta").serialize(),
			beforeSend: function(objeto){
			   $("#cont-encuesta").html("Procesando encuesta...");
			},
			success:
				function(datos){
					if(datos!=""){
						$("#cont-encuesta").html(datos);
					}
				},
			error:
				function(){
					 $("#cont-encuesta").html("Ocurrio un error!");
				},
		});
	}else{
		alert("Selecciona una opci\u00F3n para votar!");
	}
	return false;
}

function valForm(){
	if(EsVacio($("#user").val())){ $("#user").focus(); return false;}
	if(EsVacio($("#pass").val())){ $("#pass").focus(); return false;}
	var pass = hex_sha1($("#pass").val());
	$("#metodo").val("1");
	$("#pass").val(pass);
	return true;
}
