/* FORMULARIOS DE LOGIN Y ALTA */
$(document).ready(function() { 
	
	$(".formContainer").parent().find(".info").css("display", "none");
	
	$(".rowContainer").parent().find(".info").css("display", "none");
	
	$(".formContainer input").focus(function(){
		$(this).parent().find(".error").css("display", "none");
		$(this).parent().find(".error1").css("display", "none");
		$(this).parent().find(".error2").css("display", "none");
		$(this).parent().find(".error3").css("display", "none");
		$(this).parent().find(".info").css("display", "block");
	}).blur(function(){
		$(this).parent().find(".info").css("display", "none");
	});
	
	$(".rowContainer input").focus(function(){
		$(this).parent().find(".error").css("display", "none");
		$(this).parent().find(".error1").css("display", "none");
		$(this).parent().find(".error2").css("display", "none");
		$(this).parent().find(".error3").css("display", "none");
		$(this).parent().find(".info").css("display", "block");
	}).blur(function(){
		$(this).parent().find(".info").css("display", "none");
	});
	
	$("#facebook").html('<fb:like-box href="http://www.facebook.com/pages/Cursos-y-Formacion/149155575101112" width="300" connections="10" stream="false" header="true"></fb:like-box>');
		
	var loginvalidator = $("#loginform").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			login_user_email: {
				required:true,
				minlength:"7",
				maxlength:"255",
				email:true
				},
			login_user_password: {
				required: true,
				minlength:"5",
				maxlength:"15"
			}
		}, 
		showErrors: function(errorMap, errorList) {
			$(".formContainer input").find(".error").css("display", "none");
			$(".formContainer input").find(".error1").css("display", "none");
			$(".formContainer input").find(".error2").css("display", "none");
			$(".formContainer input").find(".error3").css("display", "none");
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				$("#"+element).parent().find(".error1").css("display", "block");
				$("#"+element).parent().find(".error2").css("display", "block");
				$("#"+element).parent().find(".error3").css("display", "block");
			}
		}	
	});
	
	var subs_newsvalidator = $("#subs_news").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			news_name: {
				required:true,
				minlength:"5",
				maxlength:"50"
				},
			news_email: {
				required: true,
				minlength:"5",
				maxlength:"255",
				email:true
			}
		}, 
		showErrors: function(errorMap, errorList) {
			$(".rowContainer input").find(".error").css("display", "none");
			$(".rowContainer input").find(".error1").css("display", "none");
			$(".rowContainer input").find(".error2").css("display", "none");
			$(".rowContainer input").find(".error3").css("display", "none");
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				$("#"+element).parent().find(".error1").css("display", "block");
				$("#"+element).parent().find(".error2").css("display", "block");
				$("#"+element).parent().find(".error3").css("display", "block");
			}
		}	
	});
		
	var signupvalidator = $("#signupform").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			user_school: {
				required:true,
				minlength:"3",
				maxlength:"255"
				},
			user_name: {
				required: true,
				minlength:"5",
				maxlength:"255"
				},
			user_email: {
				required: true,
				minlength:"7",
				maxlength:"255",
				email: true
				},
			user_phone: {
				required: true,
				minlength:"9",
				maxlength:"50"
				},
			user_dni: {
				required: true,
				minlength:"5",
				maxlength:"15"
				}
		},
	   showErrors: function(errorMap, errorList) {
			$(".formContainer input").find(".error").css("display", "none");
		
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				
			}
		}

	}); 
	
	var forgotvalidator = $("#forgotform").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			forgot_user_email: {
				required:true,
				minlength:"7",
				maxlength:"255",
				email:true
				}
		}, 
	   showErrors: function(errorMap, errorList) {
			$(".formContainer input").find(".error").css("display", "none");
			$(".formContainer input").find(".error1").css("display", "none");
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				$("#"+element).parent().find(".error1").css("display", "block");
			}
		}
	});
	
	var activatevalidator = $("#user_activate").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			user_token: {
				required:true,
				minlength:"10",
				maxlength:"10"
				}
		}, 
	   showErrors: function(errorMap, errorList) {
			$(".formContainer input").find(".error").css("display", "none");
			$(".formContainer input").find(".error1").css("display", "none");
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				$("#"+element).parent().find(".error1").css("display", "block");
			}
		}
	});

	var activatechangeform = $("#changeform").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			cfuser_token: {
				required:true,
				minlength:"10",
				maxlength:"10"
				},
			change_user_password: {
				required: true,
				minlength:"5",
				maxlength:"15"
			},
			change_user_passwordr: {
				required: true,
				minlength:"5",
				maxlength:"15"
			}
		}, 
	   showErrors: function(errorMap, errorList) {
			$(".formContainer input").find(".error").css("display", "none");
			$(".formContainer input").find(".error1").css("display", "none");
			$(".formContainer input").find(".error2").css("display", "none");
			for (element in errorMap) {
				$("#"+element).parent().find(".error").css("display", "block");
				$("#"+element).parent().find(".error1").css("display", "block");
				$("#"+element).parent().find(".error2").css("display", "block");
			}
		}
	});
		
	
	$('#loginform').submit(function() {
		
		valid=$("#loginform").valid();
		
		if (valid!=false) {
      		
			var inputs = [];
			$(':input', this).each(function() {
				if (this.value!="" && this.type!="submit" && this.name!="action" && this.name!="news_name" && this.name!="news_email") {
					inputs.push(this.name + '=' + this.value);
				}	
				
				if (this.name=="action") {
					action=this.value;
				}
			})
			values=inputs.join('&');
			
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
		 	
			if (resultado!="") {
				if (resultado=="1") {
					$("#login_user_email").parent().find(".error1").css("display", "block");
				}
				if (resultado=="2") {
					$("#login_user_password").parent().find(".error1").css("display", "block");
				}
				if (resultado=="3") {
					$("#login_user_email").parent().find(".error2").css("display", "block");
				}	
				if (resultado=="4") {
					$("#login_user_password").parent().find(".error2").css("display", "block");
				}	
				if (resultado=="5") {
					$("#login_user_password").parent().find(".error3").css("display", "block");
				}
			} else {
				window.location="../centros";
			}
		}
		return false;
	})	
	
	$('#signupform').submit(function() {
      
		
		valid=$("#signupform").valid();
		if (valid!=false) { 
			var inputs = [];
			$(':input', this).each(function() {
				if (this.value!="" && this.type!="submit" && this.name!="action" && this.name!="news_name" && this.name!="news_email") {
					inputs.push(this.name + '=' + this.value);
				}
				
				if (this.name=="action") {
					action=this.value;
				}				
			})
			values=inputs.join('&');
			
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
		 	
			
			
			if (resultado!="") {
				switch (resultado) {
					case "1":
						$("#user_school").parent().find(".error").css("display", "block");
						break;
					case "2":	
						$("#user_name").parent().find(".error").css("display", "block");
						break;
					case"3":
						$("#user_email").parent().find(".error").css("display", "block");
						break;
					case"4":
						$("#user_phone").parent().find(".error").css("display", "block");
						break;
					case"5":
						$("#user_dni").parent().find(".error").css("display", "block");
						break;
					case"6":
						$("#user_school").parent().find(".error1").css("display", "block");
						break;
					case"7":
						$("#user_name").parent().find(".error1").css("display", "block");
						break;
					case"8":
						$("#user_email").parent().find(".error").css("display", "block");
						break;
					case"9":
						$("#user_phone").parent().find(".error1").css("display", "block");
						break;
					case"10":
						$("#user_dni").parent().find(".error1").css("display", "block");
						break;
					case "11":
						$("#user_email").parent().find(".error1").css("display", "block");
						break;
				}	
								
			} else {
				window.location="../activarregistro.html";
				/*$('#registrosucesso').show();
				$('#signupform').hide();
				$('#user_activate').show();*/
			}
		}
		return false;
	})
	
	$('#user_activate').submit(function() {
		
		valid=$("#user_activate").valid();
		
		if (valid!=false) {
      		
			var inputs = [];
			$(':input', this).each(function() {
				if (this.value!="" && this.type!="submit" && this.name!="action" && this.name!="news_name" && this.name!="news_email") {
					inputs.push(this.name + '=' + this.value);
				}
				if (this.name=="action") {
					action=this.value;
				}				
			})
			values=inputs.join('&');
		
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
		 	
			if (resultado!="") {
				if (resultado=="1") {
					$("#user_token").parent().find(".error").css("display", "block");
				}
				if (resultado=="2") {
					$("#user_token").parent().find(".error1").css("display", "block");
				}

			} else {
				window.location="../activado.html";
			}
		}
		return false;
	})	
	
	$('#forgotform').submit(function() {
		
		valid=$("#forgotform").valid();
		
		if (valid!=false) {
      		
			var inputs = [];
			$(':input', this).each(function() {
				if (this.value!="" && this.type!="submit" && this.name!="action" && this.name!="news_name" && this.name!="news_email") {
					inputs.push(this.name + '=' + this.value);
				}
				if (this.name=="action") {
					action=this.value;
				}				
			})
			values=inputs.join('&');
		
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
		 	
			if (resultado!="") {
				if (resultado=="1") {
					$("#forgot_user_email").parent().find(".error").css("display", "block");
				}
				if (resultado=="2") {
					$("#forgot_user_email").parent().find(".error1").css("display", "block");
				}				
			} else {
				$('#cambiosucesso').hide();
				$('#cambioenviado').show();
				$('#forgotform').hide();
				$('#changeform').show();
				
			}
		}
		return false;
	})	
	
	
	$('#changeform').submit(function() {
		
	valid=$("#changeform").valid();
		
	if (valid!=false) {
      		
			var inputs = [];
			$(':input', this).each(function() {
				if (this.value!="" && this.type!="submit" && this.name!="action" && this.name!="news_name" && this.name!="news_email") {
					inputs.push(this.name + '=' + this.value);
				}
				if (this.name=="action") {
					action=this.value;
				}				
			})
			values=inputs.join('&');
		
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
		 	
			
		 	
			if (resultado!="") {
				if (resultado=="1") {
					$("#cfuser_token").parent().find(".error").css("display", "block");
				}
				if (resultado=="2") {
					$("#change_user_password").parent().find(".error").css("display", "block");
				}
				if (resultado=="3") {
					$("#change_user_passwordr").parent().find(".error").css("display", "block");
				}
				
				if (resultado=="4") {
					$("#change_user_password").parent().find(".error1").css("display", "block");
				}
				if (resultado=="5") {
					$("#change_user_passwordr").parent().find(".error2").css("display", "block");
				}				
			} else {
				$('#cambioenviado').hide();
				$('#cambiosucesso').show();
				$('#changeform').hide();
				setTimeout('redirect("../login.html")', 1000);
				//$('#loginform').show();
			}
		}
		return false;
	})
	
	$('#subs_news').submit(function() {
		
		valid=$("#subs_news").valid();
		
		if (valid!=false) {
      		action=$("#action").val();
      		name_news=$("#news_name").val();
      		email_news=$("#news_email").val();
      		
      		values="task=news_sumbit&name="+name_news+"&email="+email_news;
			var resultado = $.ajax({
				type: "GET",
			  	url: action+"index.php?"+values,
			  	async: false
			}).responseText;
						
			if (resultado=="") {
	      		$("#news_name").val('');
	      		$("#news_email").val('');
	      		alert("Los datos se enviaron con sucesso!");
			} else {
				alert("Se a producido un error, por favor intente nuevamente!");
			}
			
		}
		return false;
	})	
}); 

function redirect (where) {
	 window.location = where;
}
