﻿var action = "send_url.php";

function FillInput(objeto,valor)
{
	if(objeto.val().length<=0)
	{
		objeto.val(valor);
	}
}

function EmptyInput(objeto,valor)
{
	if(objeto.val()==valor || (objeto.attr('id')=='email' && objeto.val()=='Introduce un e-mail correcto.'))
	{
		objeto.val('');
	}
}

function validarEmail(valor) {
 if (/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/.test(valor)){
 	return true;
 } else {
 	return false;
 }
} 

function ShowFrase(data){
  if(data.indexOf("OK")!=-1){
	return 'formulario enviado correctamente.';  
  }
  
  return 'ha ocurrido un problema.'
}
$(document).ready(function() {

	function ValidateForm(form){
		var txtMail=form.find("#email");
			if(!validarEmail(txtMail.val()))
			{
				txtMail.addClass("error");
				form.find("#errorCon").html("Introduce un e-mail correcto.");
				form.find("#liDebug").html('');
				return false;
			}
			else
			{
				txtMail.removeClass("error");
				form.find("#errorCon").html("");
				form.find("#liDebug").html('');
				return true;
			}
	}
	
	$(".b1").fancybox({
		'titleShow': false,
		'width': 'auto',
		'height' : 'auto',
		'showCloseButton' : true,
		'overlayColor' : '#fd6f6f',
		'overlayOpacity' : '0.8'
	});
	
	$(".b2").fancybox({
		'titleShow': false,
		'width': 'auto',
		'height' : 'auto',
		'showCloseButton' : true,
		'overlayColor' : '#d5d75e',
		'overlayOpacity' : '0.8'
	});
	
	$(".b3").fancybox({
		'titleShow': false,
		'width': 'auto',
		'height' : 'auto',
		'showCloseButton' : true,
		'overlayColor' : '#5ecdfb',
		'overlayOpacity' : '0.8'
	});
	
	$(".fancy").fancybox({
		'titleShow': false,
		'width': 'auto',
		'height' : 'auto',
		'showCloseButton' : true,
		'overlayColor' : '#cccccc',
		'overlayOpacity' : '0.8'

	});
	
	$(".nombreInput").focus(function(){EmptyInput($(this),'Nombre');}).blur(function(){FillInput($(this),'Nombre');}).attr('maxlength','50');
	$(".apellidosInput").focus(function(){EmptyInput($(this),'Apellidos');}).blur(function(){FillInput($(this),'Apellidos');}).attr('maxlength','50');
	$(".mailInput").focus(function(){EmptyInput($(this),'E-mail');}).blur(function(){FillInput($(this),'E-mail');}).attr('maxlength','50');
	$(".telefonoInput").focus(function(){EmptyInput($(this),'Teléfono');}).blur(function(){FillInput($(this),'Teléfono');}).attr('maxlength','20');
	$(".observacionInput").focus(function(){EmptyInput($(this),'Comentarios');}).blur(function(){FillInput($(this),'Comentarios');});
	$(".dniInput").focus(function(){EmptyInput($(this),'DNI');}).blur(function(){FillInput($(this),'DNI');}).attr('maxlength','20');

	$("#frmRojo").find('#enviar').click(function() {
			var boton=$(this);
			var form = $("#frmRojo"); 
			if(ValidateForm(form))
			{
				boton.attr('disabled',true);
				form.find("#liDebug").html('enviando...');           
				var serializeForm = form.serialize();
				$.get(action, serializeForm,function(data){form.find("#liDebug").html(ShowFrase(data));boton.attr('disabled',false);});
			}
     });
	
	$("#frmVerde").find('#enviar').click(function() {
			var boton=$(this);
			var form = $("#frmVerde"); 
			if(ValidateForm(form))
			{
				boton.attr('disabled',true);
				form.find("#liDebug").html('enviando...');        
				var serializeForm = form.serialize();
				$.get(action, serializeForm,function(data){form.find("#liDebug").html(ShowFrase(data));boton.attr('disabled',false);});
			}
     });
	
	$("#frmAzul").find('#enviar').click(function() {
			var boton=$(this);
			var form = $("#frmAzul"); 
			if(ValidateForm(form))
			{
				boton.attr('disabled',true);
				form.find("#liDebug").html('enviando...');
				var serializeForm = form.serialize();
				$.get(action, serializeForm,function(data){form.find("#liDebug").html(ShowFrase(data));boton.attr('disabled',false);});
			}
     });
	
	$("#frmContacto").find('#enviar').click(function() {
			var boton=$(this);
			var form = $("#frmContacto");
			
				if(ValidateForm(form))
				{
					boton.attr('disabled',true);
					form.find("#liDebug").html('enviando...');    
					var serializeForm = form.serialize();
					$.get(action, serializeForm,function(data){form.find("#liDebug").html(ShowFrase(data));boton.attr('disabled',false);});
				}
			
     });
	
	$("#formul").submit(function(){
			var form=$(this);
			var dropdown=form.find("#titulocurso");
			var valido=true;
			
		
			
			form.find(".required").each(function(){
				if($(this).val().indexOf('*')!=-1)
				{
					$(this).addClass("error");
					valido= false;
				}
				else{
					$(this).removeClass("error");				
				}
			});
			
			if(!validarEmail(form.find("#email").val()))
			{
				form.find("#email").addClass("error");
				valido = false;
			}
			
			if(valido){
				if(dropdown.val()=='')
				{
					form.find("#errorCon").html('selecciona un curso.');
					return false;
				}
				else{
					form.find("#errorCon").html('');
					return true;
				}
			}
			else
			{
				form.find("#errorCon").html('Por favor, rellena correctamente los campos marcados.');
				return false;	
			}
			
	});
	
	
});
