/*
 * Funciones ALKLA
 *
 * Copyright (c) 2010 Alkla Estudio de Diseņo
 * http://www.alkla.es
 *
 */
 
$(document).ready(function(){
	
	// Listeners
	$("#contacto-form").bind('submit', validar_contacto);
	$("#seguros-form").bind('submit', validar_seguros);
	$("#sidebar-form").bind('submit', validar_sidebar);
	$("#newsletter-form").bind('submit', validar_newsletter);
	$("a.back").bind("click", return_back);
	$("#dudas a").bind("click", show_information);
	
});


function return_back(event) {
	history.back();
	return false;
}

function show_information() {
	$('#dudas').slideUp('slow', function() {
		$('#informacion').slideDown('slow');
  	});
	return false;	
}

function show_caja(obj){
	var so = new SWFObject(obj,"cajas-fuertes-clip","100%","100%","7","#FFFFFF");
	so.addParam("play", "true");
	so.addParam("loop", "true");
	so.write("cajas-fuertes");
	return false;
}


/**********************************************/
/*
/*   SEGUROS
/*
/**********************************************/

function validar_seguros()
{
	
	var email = $("#seguros-email").val();
	var nombre = $("#seguros-nombre").val();
	var apellidos = $("#seguros-apellidos").val();
	var direccion = $("#seguros-direccion").val();
	var cp = $("#seguros-cp").val();
	var poblacion = $("#seguros-poblacion").val();
	var dni = $("#seguros-dni").val();
	var telefono = $("#seguros-telefono").val();
	var fecha = $("#seguros-fecha").val();
	var tipo = $("#seguros-tipo").val();
	var mensaje = $("#seguros-mensaje").val();
	var path = $("#seguros-path").val();
	var error = false;
	var error_msg = "";
	
	$("#seguros-msg").removeClass();

	if (nombre == ""){
		error = true;
		error_msg = error_msg + $("#seguros-error_nombre").val() + '<br/>';
	}
	if (telefono == ""){
		error = true;
		error_msg = error_msg + $("#seguros-error_telefono").val() + '<br/>';
	}
	
	if (!error){	
		var param = "email=" + email + "&nombre=" + nombre + "&apellidos=" + apellidos + "&telefono=" + telefono + "&mensaje=" + mensaje + "&direccion=" + direccion + "&cp=" + cp + "&poblacion=" + poblacion + "&dni=" + dni + "&fecha=" + fecha + "&tipo=" + tipo;
		$("#seguros-msg").html("...");
		$.ajax({
			async:true,
			type: "post",
			dataType: "xml",
			contentType: "application/x-www-form-urlencoded",
			url: path + "/library/seguros.php",
			data:param,
			success:validar_seguros_complete,
			timeout:10000,
			error:null
		});
	}else{
		$("#seguros-msg").addClass("error");
		$("#seguros-msg").html(error_msg);
	}
	return false;
}
function validar_seguros_complete(t)
{	
	var path = $("#seguros-path").val();
	var error_msg = $("#seguros-ok1").val() + '<br/>' + $("#seguros-ok2").val() + '<br/>' + $("#seguros-ok3").val();
	$("#seguros-msg").removeClass("error");
	$("#seguros-msg").html(error_msg);
	document.getElementById("seguros-form").reset();
}



				 
/**********************************************/
/*
/*   CONTACTO
/*
/**********************************************/

function validar_contacto()
{
	
	var email = $("#contacto-email").val();
	var nombre = $("#contacto-nombre").val();
	var apellidos = $("#contacto-apellidos").val();
	var telefono = $("#contacto-telefono").val();
	var mensaje = $("#contacto-mensaje").val();
	var path = $("#contacto-path").val();
	var aviso = $("#contacto-aviso").attr("checked");
	var error = false;
	var error_msg = "";
	
	$("#contacto-msg").removeClass();

	if (nombre == ""){
		error = true;
		error_msg = error_msg + $("#contacto-error_nombre").val() + '<br/>';
	}
	if (telefono == ""){
		error = true;
		error_msg = error_msg + $("#contacto-error_telefono").val() + '<br/>';
	}
	if (aviso == false){
		error = true;
		error_msg = error_msg + $("#contacto-error_aviso").val() + '<br/>';
	}
	
	if (!error){	
		var param = "email=" + email + "&nombre=" + nombre + "&apellidos=" + apellidos + "&telefono=" + telefono + "&mensaje=" + mensaje;
		$("#contacto-msg").html("...");
		$.ajax({
			async:true,
			type: "post",
			dataType: "xml",
			contentType: "application/x-www-form-urlencoded",
			url: path + "/library/contacto.php",
			data:param,
			success:validar_contacto_complete,
			timeout:10000,
			error:null
		});
	}else{
		$("#contacto-msg").addClass("error");
		$("#contacto-msg").html(error_msg);
	}
	return false;
}
function validar_contacto_complete(t)
{	
	var path = $("#contacto-path").val();
	var error_msg = $("#contacto-ok1").val() + '<br/>' + $("#contacto-ok2").val() + '<br/>' + $("#contacto-ok3").val();
	$("#contacto-msg").removeClass("error");
	$("#contacto-msg").html(error_msg);
	document.getElementById("contacto-form").reset();
}

function validar_sidebar()
{
	
	var email = $("#sidebar-email").val();
	var nombre = $("#sidebar-nombre").val();
	var apellidos = $("#sidebar-apellidos").val();
	var telefono = $("#sidebar-telefono").val();
	var path = $("#sidebar-path").val();
	var error = false;

	if (nombre == ""){
		error = true;
	}
	if (telefono == ""){
		error = true;
	}
	
	if (!error){	
		var param = "email=" + email + "&nombre=" + nombre + "&apellidos=" + apellidos + "&telefono=" + telefono;
		$("#contacto-msg").html("...");
		$.ajax({
			async:true,
			type: "post",
			dataType: "xml",
			contentType: "application/x-www-form-urlencoded",
			url: path + "/library/sidebar.php",
			data:param,
			success:validar_sidebar_complete,
			timeout:10000,
			error:null
		});
	}
	return false;
}
function validar_sidebar_complete(t)
{	
	$('#informacion').slideUp('slow', function() {
		$('#dudas').slideDown('slow');
  	});
	document.getElementById("sidebar-form").reset();
}



/**********************************************/
/*
/*   NEWSLETTER
/*
/**********************************************/

function validar_newsletter()
{
	var email = $("#newsletter-email").val();
	var baja = $("#newsletter-baja").attr("checked");
	var path = $("#newsletter-path").val();
	
	if (email != ""){
		
		var patron=/^[^@ ]+@[^@ ]+.[^@ .]+$/;
		var t1 = patron.test(email);
		if (t1 == true){ 
			var param = "email=" + email + "&baja=" + baja;
			$.ajax({
				async:true,
				type: "post",
				dataType: "xml",
				contentType: "application/x-www-form-urlencoded",
				url: path + "/library/newsletter.php",
				data:param,
				success:validar_newsletter_complete,
				timeout:10000,
				error:null
			});
		}
	}
	return false;
}
function validar_newsletter_complete(t)
{	
	document.getElementById("newsletter-form").reset();
}
