// <![CDATA[

// on DOM ready, let the magic begin
$(document).ready(function(){
	
	// check username once the field is out of focus
	var usernameError = false;
	$("#username").blur(function(){
		var username = this.value;
		$("#usernameStatus").addClass("ajaxLoader");
		$.ajax({
			type: "POST",
			url: "ajax_check_username.php",
			data: "q=" + username,
			success: function(ajaxResponse){
				$("#usernameStatus").removeClass("ajaxLoader");
				switch(ajaxResponse){
					case "FOUND": // username Unique (OK)
						$("#usernameStatus").removeClass("checkNotOK");
						$("#usernameStatus").addClass("checkOK");
						$("#usernameStatus").text("Gotcha!");
						usernameError = false;
					break;
					case "NOTFOUND": // alrady taken
						$("#usernameStatus").removeClass("checkOK");
						$("#usernameStatus").addClass("checkNotOK");
						$("#usernameStatus").text("Hmm, are you sure that's correct?");
						usernameError = true;
					break;
					
					case "FAILED":
					default: // something's up
						$("#usernameStatus").removeClass("checkOK");
						$("#usernameStatus").addClass("checkNotOK");
						$("#usernameStatus").text("That can't be right, right?");
						usernameError = true;
					break;
					
				}
			}
		});
	});
	
	// check if password is long enough
	var passwordError = false;
	$("#password").blur(function(){
		passwordError = false;
		if(this.value.length < 6){ // too short
			$("#passwordStatus").removeClass("checkOK");
			$("#passwordStatus").addClass("checkNotOK");
			$("#passwordStatus").text("That seems too short...");
			passwordError = true;
		}
		
		if(passwordError == false){ // if no error, add check as OK
			$("#passwordStatus").removeClass("checkNotOK");
			$("#passwordStatus").addClass("checkOK");
			$("#passwordStatus").text("Could be, we'll see...");
		}
	});

	// if user needs help logging in
	$("#helpToggle").toggle(function(){
		$("#help").show("fast");
	},function(){
		$("#help").hide("fast");
	});
	
	// toggles remember me notice
	var rememberError = false;
	$("#remember").click(function(){
		// If checked
		if(!$("#remember").is(":checked")){
			//otherwise, hide it
			$("#rememberNote").hide("fast");
			tosError = true;
		}else{     
			//show the hidden div
			$("#rememberNote").show("fast");
			tosError = false;
		}
	});
	
	// submit
	$("#loginForm").submit(function(e){
		var validates = true;
		$("#errorsList").html("");
		$("#errors").hide("fast");
		
		if(!$("#username").val()){
			$("#errorsList").append("<li>Please enter your username</li>");
			validates = false;
		}
		
		if(!$("#password").val()){
			$("#errorsList").append("<li>Please enter your password</li>");
			validates = false;
		}
		
		if(validates == false){ // show error and stop submit
			$("#errorTitle").show();
			$("#errors").show("fast");
			$('html, body').animate({scrollTop:0}, 'slow'); 
			return false;
		}
		
		// if all OK - then disable submit button while processing
		$("#submitButton").attr("disabled", "false");
		$("#submitStatus").addClass("ajaxLoader");
	});
	
});

// ]]>