var action="";

$(document).ready(function(){	

	$(".rowContainer").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");
	});


	var loginvalidator = $("#login_form").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			username: {
				required:true,
				minlength:"7",
				maxlength:"100"
				},
			password: {
				required: true,
				minlength:"5",
				maxlength:"15"
			}
		}, 
		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 activatevalidator = $("#activate_form").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
				act_code: {
				required:true,
				number:true,
				minlength:"10",
				maxlength:"10"
				}
		}, 
		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 forgotvalidator = $("#activate_form").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
				fg_email: {
				required:true,
				minlength:"7",
				maxlength:"100",
				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 registervalidator = $("#register_form").validate({
		onkeyup:false,
		onfocusout: false,
		onclick: false,
		focusInvalid: false,
		rules: { 
			name: {
				required:true,
				minlength:"4",
				maxlength:"30"
				},			
			email: {
				required:true,
				minlength:"7",
				maxlength:"100",
				email:true
				},
			usernamer: {
				required:true,
				minlength:"6",
				maxlength:"20"
				},							
			passwordr: {
				required: true,
				minlength:"4",
				maxlength:"20"
				},
			passwordf: {
				required: true,
				minlength:"4",
				maxlength:"20"
				}			
		}, 
		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");
			}
		}	
	});
		
	$('#login_form').submit(function() {
		
		valid=$("#login_form").valid();
		if (valid==true) {
		
			action=$("#action").val();
			vuser=$("#username").val();
			vpass=$("#password").val();
			values="user="+vuser+"&password="+vpass;
			var result = $.ajax({
				type: "GET",
			  	url: action+"index.php?task=login_user&"+values,
			  	async: false
			}).responseText;
			
			if (result=="") {
				window.location="../";
			} else {
				if (result=="1") {
					$("#username").parent().find(".error1").css("display", "block");
				}
				if (result=="2") {
					$("#password").parent().find(".error2").css("display", "block");
				}
				if (result=="3") {
					$("#username").parent().find(".error").css("display", "block");
				}
				if (result=="4") {
					$("#password").parent().find(".error1").css("display", "block");
				}				
			}
			
			
			return false;
		} else {
			return false;
		}		
	});
	
	
	$('#activate_form').submit(function() {
		
		valid=$("#activate_form").valid();
		
		if (valid==true) {
			action=$("#action").val();
			vcode=$("#act_code").val();
			values="act_code="+vcode;
			var result = $.ajax({
				type: "GET",
			  	url: action+"index.php?task=activate_user&"+values,
			  	async: false
			}).responseText;
			
			if (result!="") {
				$("#act_code").parent().find(".error").css("display", "block");
			} else {
				$("#frm_activate").slideToggle("slow",function(){$("#frm_sucess").slideToggle("slow");});
				
				//alert("user activado");
			}
			return false;
		} else {
			return false;	
		}
	});
	
	$('#forgot_form').submit(function() {
		
		valid=$("#forgot_form").valid();
		
		if (valid==true) {
			action=$("#action").val();
			vemail=$("#fg_email").val();
			values="fg_email="+vemail;
			var result = $.ajax({
				type: "GET",
			  	url: action+"index.php?task=forgot_details&"+values,
			  	async: false
			}).responseText;
			
			alert(result);
			
			if (result!="") {
				//$("#act_code").parent().find(".error").css("display", "block");
			} else {
				//$("#frm_activate").slideToggle("slow",function(){$("#frm_sucess").slideToggle("slow");});
				
				//alert("user activado");
			}
			return false;
		} else {
			return false;	
		}
	});
	
	
	$('#register_form').submit(function() {
		
		valid=$("#register_form").valid();
		
		
		if (valid==true) {
			if ($("#passwordr").val()!=$("#passwordf").val()) {
				$("#passwordf").parent().find(".error").css("display", "block");
				return false;
			}
			
			
			
			var inputs = [];
			$('#:input', this).each(function() {
				if (this.name!="action") {
					if (this.name=="usernamer") {
						inputs.push('username=' + this.value);
					}
					if (this.name=="passwordr") {
						inputs.push('password=' + this.value);
					}
					if (this.name!="usernamer" && this.name!="passwordr") {
							inputs.push(this.name + '=' + this.value);
					}
				} else {
					action=this.value;
				}
			})
			values=inputs.join('&');
			var result = $.ajax({
				type: "GET",
			  	url: action+"index.php?task=register_form&"+values,
			  	async: false
			}).responseText;
			
			if (result!="") {
				
				//$("#loading").slideToggle("slow",function(){$("#frm_register").slideToggle("slow");});
				
				switch (result) {
					case "1":
						$("#name").parent().find(".error1").css("display", "block");
						break;
					case "2":
						$("#email").parent().find(".error").css("display", "block");
						break;
					case "3":
						$("#usernamer").parent().find(".error1").css("display", "block");
						break;
					case "4":
						$("#passwordr").parent().find(".error1").css("display", "block");
						break;
					case "5":
						$("#passwordf").parent().find(".error").css("display", "block");
						break;
					case "6":
						$("#usernamer").parent().find(".error2").css("display", "block");
						break;
					case "7":
						$("#email").parent().find(".error1").css("display", "block");
						break;						
				}

				
			} else {
				$("#frm_register").slideToggle("slow",function(){$("#loading").slideToggle("slow");});
				
				$("#loading").slideToggle("slow",function(){$("#succes_register").slideToggle("slow");});
				
				
			}
			
			
			return false;
		} else {
			return false;
		}
		
		
	});	
	
	
	$("*").live("click", function(){ 
		if ( $(this).attr('class')!='puntos' ) {
			$('li.puntos').html("+");
		}		

	});
	
	$("ul.paginado li.puntos").click(function () { 
					
		if ( $(this).html()!="+" ) { $(this).html("+"); } else { //si haces 2 click en los mismos puntos, se cierra
		
			var inverso=false;
			if( $(this).attr("id").indexOf("a")!=-1){
				var idli=$(this).attr("id").split("a");
				inverso=true;
			} else {
				var idli=$(this).attr("id").split("b");
			}
			
			$("ul.paginado li.puntos").html("+");						
			
			
			var anterior=parseInt(idli[1]);
			var posterior=parseInt(idli[2]);
			var nelementos=parseInt(idli[3]);		

			if ( inverso ) {
				var href_anterior=$(this).prev("li").children("a").attr("href");
				href_anterior_trozos = href_anterior.split("/");
				href_anterior_trozos[href_anterior_trozos.length-1]="";
				href_limpio=href_anterior_trozos.join("/");


				var titulo_anterior=$(this).prev("li").children("a").attr("title");
				titulo_anterior_trozos = titulo_anterior.split(" ");
				titulo_anterior_trozos[titulo_anterior_trozos.length-1]="";
				titulo_limpio=titulo_anterior_trozos.join(" ");
			} else {
				var href_anterior=$(this).prev("li").children("a").attr("href");
				href_anterior_trozos = href_anterior.split("=");
				href_anterior_trozos[href_anterior_trozos.length-1]="";
				href_limpio=href_anterior_trozos.join("=");

				var titulo_anterior=$(this).prev("li").children("a").attr("title");
				titulo_anterior_trozos = titulo_anterior.split(" ");
				titulo_anterior_trozos[titulo_anterior_trozos.length-1]="";
				titulo_limpio=titulo_anterior_trozos.join(" ");						
			}
				
			var texto="";
			var contador=0;
			if ( (posterior-anterior)>36 ) {
				var t=parseInt((posterior-anterior)/20);
				
				if ( t<1 ) { t=1; }
				
				if ( t==1 ) {
					for ( i=(anterior+1); i<=(posterior-1); i++ ) {
						if ( inverso ) {
							texto=texto+"<a href='./_"+i+"' title='"+i+"'>"+i+"</a>";
						} else {
							texto=texto+"<a href='./_"+i+"' title='"+i+"'>"+i+"</a>";
						}
					}													
				} else {
					var i=1;
					while ( (anterior+((i+1)*t))<posterior  ) {	
						contador=(anterior+(i*t));
						if ( inverso ) {
							texto=texto+"<a href='./_"+contador+"' title='"+contador+"'>"+contador+"</a>";
						} else {
							texto=texto+"<a href='./_"+contador+"' title='"+contador+"'>"+contador+"</a>";
						}
						i++;
					}
				}
			} else {
				for ( i=(anterior+1); i<=(posterior-1); i++ ) {
					if ( inverso ) {
						texto=texto+"<a href='./_"+i+"' title='"+i+"'>"+i+"</a>";
					} else {
						texto=texto+"<a href='./_"+i+"' title='"+i+"'>"+i+"</a>";
					}
					contador++;
				}
			}

			texto="<span>"+texto+"</span>";
			
			$(this).append(texto);
			
			//para ie6
			if ( $("div.paginadoie").attr("class")=="paginadoie" ) { 
				$(this).children("span").children("a").css("width", $(this).children("span").children("a:last").width() ); 
			} else { 
				$(this).children("span").children("a").css("min-width", $(this).children("span").children("a:last").width() );
			}
			
			if ( ( $(this).children("span").children("a").length )<5 ) {
				$(this).children("span").css("width", parseInt( ( $(this).children("span").children("a:last").width() ) * 6 ) );
				$(this).children("span").css("margin-left", -( parseInt( $(this).children("span").children("a").length * 10 ) ) );
			} else if (  $(this).children("span").children("a").length>4 && $(this).children("span").children("a").length<17  ) {
				$(this).children("span").css("width", 80 );
				$(this).children("span").css("margin-left", -( parseInt( $(this).children("span").width() /2 ) ) );					
			} else {
				$(this).children("span").css("width", parseInt( $(this).children("span").children("a").length/4 )*( ($(this).children("span").children("a:last").width())+10 ) );
				$(this).children("span").css("margin-left", -( parseInt( $(this).children("span").width() /2 ) ) );
			}
			
			$(this).children("span").css("margin-top", -(($(this).children("span").height())+10) );
		}					
	});
	
	
});


function change_form(frm) {
	if (frm=="") {
		if ($("#frm_register").css("display")=="none") {
			$("#frm_login").slideToggle("slow",function(){$("#frm_register").slideToggle("slow");});
		} else {
			$("#frm_register").slideToggle("slow",function(){$("#frm_login").slideToggle("slow");});
		}
	} else {
		if (frm=="1") {
			if ($("#frm_forgot").css("display")=="none") {
				$("#frm_login").slideToggle("slow",function(){$("#frm_forgot").slideToggle("slow");});
			} else {
				$("#frm_forgot").slideToggle("slow",function(){$("#frm_login").slideToggle("slow");});
			}
		}
		if (frm=="2") {
			
			$("#frm_sucess").slideToggle("slow",function(){$("#frm_login").slideToggle("slow");});

			
		}
	}
}
