
  	 
   	$.registration = 
	{ 
			uid : 0
      	,	uniqName : true
		,	uniqEmail : true
	  	,	build : function()
			{
				try
				{	
					$("a.registreer-submit").bind("click", function()
					{
						if($.registration.validateForm($(this), false))
						   document.registreer_form.submit();
				
						return false; 
					});
					
					$("input.username").bind("blur", function()
					{
						$("div.username-error").hide();
						$.registration.uniqName = true;
						$("div.user-name").removeClass("add-error");
					
						$.getJSON("http://pb.top2008.radio2.nl/profile_check_username/username_"+$("input.username").val()+"/?pbcall=?", function(data)
						{
							var	data = eval(data);
							
							if(data.success)
							{
								$("div.user-name").addClass("add-error");
								$.registration.uniqName = false;
								$("div.username-error").show();
							}
							
							$.registration.log( "info", "Gebruiker bestaat: "+data.success);
						});
						
						return false; 
					});
					
					$("input.email").bind("blur", function()
					{			
						$("div.email-error").hide();
						$.registration.uniqEmail = true;
						$("div.email").removeClass("add-error");
					
						$.getJSON("http://pb.top2008.radio2.nl/profile_check_email/email_"+$("input.email").val()+"/?pbcall=?", function(data)
						{
							var	data = eval(data);
							
							if(data.success)
							{
								$("div.email").addClass("add-error");
								$.registration.uniqEmail = false;
								$("div.email-error").show();
							}
							
							$.registration.log( "info", "Email is al gebruikt: "+data.success);
						});
						
						return false; 
					});
					
					$("a.ww-vergeten").bind("mousedown", function(){ return false; });
					$("a.ww-vergeten").bind("click", function()
					{	
						top.location.href = "#ww-vergeten";			
						$.registration.openForgotLogin();
						
						return false;
					});
					
					$("a.login-verzoek").bind("mousedown", function(){ return false; });
					$("a.login-verzoek").bind("click", function()
					{
						$("div.sww").hide();
						$("div.nbk").hide();
						
						$.getJSON("http://pb.top2008.radio2.nl/user_password_forgot/input_"+$("input.user_forgot_password").val()+"/?pbcall=?", function(data)
						{
							var	data = eval(data);
							
							if(data.success)
								$("div.sww").fadeIn();
							else
								$("div.nbk").fadeIn();
						});
					
						return false; 
					});
					
					$.registration.log( "info", "Class \"cabinet\" wordt geladen");
				}
				catch(err)
				{
					$.registration.log( "error", "Class \"cabinet\" wordt NIET geladen - " + err);
				}	
            }
			
		,	openForgotLogin : function()
			{
				$("div.get-ww").animate({ height:300 }, 500);
			}
				
		,   validateForm : function( obj, preferences )
			{
				var	errors = false; 
				
				$(".add-error").removeClass("add-error");
				$("tr.ww-error td div").show();
				$("tr.ww-error").hide();
			
				if(!$.registration.uniqName)
				{
					$("div.user-name").addClass("add-error");
					errors = true;
				}
				
				if(!$.registration.uniqEmail)
				{
					$("div.email").addClass("add-error");
					errors = true;
				}
				
				if(!$("input#input-username").val() && !preferences)
				{
					$("div.user-name").addClass("add-error");
					errors = true;
				}
	
				if($("input#input-user_real_name").attr("checked") && !$("input#input-first_name").val())
				{
					$("div.first-name").addClass("add-error");
					errors = true;
				}
				
				if(!$("input#input-username").val() && !preferences)
				{
					$("div.user-name").addClass("add-error");
					errors = true;
				}
				
				if(!$("input#input-email").val())
				{
					$("div.email").addClass("add-error");
					errors = true;
				}
				else
				{
					var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	
					if(!emailPattern.test($("input#input-email").val()))
					{
						$("div.email").addClass("add-error");
						errors = true;
					}
				}
		
				if(!$("input#input-password1").val() && !preferences)
				{
					$("div.password1").addClass("add-error");
					errors = true;
				} 
	
				if(!$("input#input-password2").val() && !preferences || ( preferences  && $("input#input-password1").val() && !$("input#input-password2").val() ))
				{
					$("div.password2").addClass("add-error");
					errors = true;
				}
				
				if($("input#input-password1").val() != $("input#input-password2").val())
				{
					$("tr.ww-error").show();
					errors = true;
				}
				
				if(!$("input#vw").attr("checked") && !preferences)
				{
					$("label.aw-label").addClass("add-error");
					errors = true;
				}
	
				if(errors)
				{
					$("div.wrong-input").show();
					top.location.href = "#error-anchor";
				}	
				
				$.registration.log( "info", "Found errors: "+ errors);
				
				if(errors)
                	return false;
				else
					return true;
            }
			
		,   loginForm : function()
			{	
				$("input.submit").bind("mousedown", function(){ return false; });
				
				$(this).bind("submit", function()
				{ 			
					var	postVar = 
					{
							username : $("input[@name=username]").val() || ""
						,	password : $("input[@name=password]").val() || ""	
					};
	
					$.getJSON($(this).attr("action")+"?pbcall=?", postVar, function(data)
					{
						var	data = eval(data);
	
						if(data.success == true)
							top.location.href="http://top2008.radio2.nl/page/08_profiel/"+data.uid+"/#tabId_profiel-overzicht";
							//top.location.href="http://top2008.radio2.nl/page/08_homepage";
						else	
							$("div.nrl").fadeIn();
							
					});
			
					return false; 
				});
			}
			
		,   loginInfo : function()
			{	
				$("div.niet-ingelogd").css({ visibility:"visible" }).hide();
				$("div.wel-ingelogd").css({ visibility:"visible" }).hide();
	
				$.getJSON("http://pb.top2008.radio2.nl/user_load/?pbcall=?", function(data)
				{
					var	data = eval(data);
			
					if(data.success === true)
					{
						$.registration = $.extend($.registration, data.user);
						
						$("div.wel-ingelogd").show();
						
						$("table.header tr td.login").html("<a href=\"http://top2008.radio2.nl/page/08_profiel/"+$.registration.uid +"/#tabId_profiel-overzicht\">Mijn profiel</a>");
						$("table.header tr td.register").html("<a class=\"user-logout\" href=\"#\">Uitloggen</a>");
						
						$("table.post-message tr td.sender").html("<img src=\"http://pb.top2008.radio2.nl/data/avatars/52/"+data.user.avatar+"\" width=\"52\" height=\"52\" />");
						
						$("a.user-logout").bind("mousedown", function(){ return false; });
						$("a.user-logout").bind("click", function()
						{ 	
							$.getJSON("http://pb.top2008.radio2.nl/user_logout/?pbcall=?", function(data)
							{
								top.location.href="http://top2008.radio2.nl/page/08_homepage";
								//$.registration.loginInfo();
							});
							
							return false; 
						});
					}
					else
					{
						$("div.niet-ingelogd").show()
		
						$("table.header tr td.login").html("<a href=\"/page/08_inloggen\">Inloggen</a>");
						$("table.header tr td.register").html("<a href=\"/page/08_registreer\">Registreer</a>");
					}
				});
			}
			
		,	setUserPreferences : function()
			{
				$.getJSON("http://pb.top2008.radio2.nl/user_load_settings/?pbcall=?", function(data)
				{
					var	data = eval(data);
					
					if(data.success === true)
					{
						var	birthday = (data.user.birthday * 1000);
							birthday = new Date(birthday);
				
						$("table.preferences tr td select#b-day option[value="+birthday.getDate()+"]").attr("selected", "selected");
						$("table.preferences tr td select#b-month option[value="+((birthday.getMonth()*1)+1)+"]").attr("selected", "selected");
						$("table.preferences tr td select#b-year option[value="+birthday.getFullYear()+"]").attr("selected", "selected");
						
						$("table.preferences tr td input#input-first_name").val(data.user.firstname);
						$("table.preferences tr td input#last_name").val(data.user.lastname);
						$("table.preferences tr td input#input-email").val(data.user.email);
						$("table.preferences tr td input#location").val(data.user.woonplaats);
						$("table.preferences tr td input#telephone").val(data.user.telefoonnummer);
						
						if(data.user.use_real_name)
							$("table.preferences tr td input#input-user_real_name").attr("checked", "checked");
							
						$("table.preferences tr td").find("#"+data.user.avatar.split(".")[0]).attr("checked", "checked");
					}
				});	
						
				$(this).bind("submit", function()
				{
					return $.registration.validateForm($(this), true); 
				});
			}
			
					
		,	log : function( type, msg )
			{
				$.pbLog.debug( { title:"Registration", msgs : [{type:type, msg:msg }] } );
			}
	};
	
	$.fn.extend
	({
			registration : $.registration.build
		,	loginInfo : $.registration.loginInfo 
		,	login : $.registration.loginForm
		,	preferences : $.registration.setUserPreferences
	});

