$(function() {


  $("form#login").submit(function() {
  
  	
		// validate and process form
		// first hide any error messages

		
	  var username = $("input#lusername").val();
		if (username == "") {
      $("label#lusername_error").show();
      $("input#lusername").focus();
      return false;
    }
		
		var password = $("input#lpassword").val();
		if (password == "") {
      $("label#lpassword_error").show();
      $("input#lpassword").focus();
      return false;
    }
	
		
		var dataString = 'lusername='+ username + '&lpassword=' + escape(password);
		//alert (dataString);return false;
		//console.info(dataString);
		$("span#working").show();
		
		$.ajax({
      type: "POST",
      url: "_php/login.php",
      data: dataString,
	  dataType: "json",
	  success: function(msg){
		  		if (msg.status == "error") {
					$("span#working").hide();
					$('#loginhold').append("<div id='login_error'></div>");
					
					$('#login_error').text(msg.message)
					
					.hide()
					.fadeIn(500, function() {
						$('#login_error').append("");
					});
				} else if (msg.status == "success") {
				
					$("span#working").hide();
					$("#formResponse").removeClass('error');  
					$("#formResponse").removeClass('success');  
					$("#formResponse").addClass(msg.status); 
	
					//if (msg.vetYN == 'n') {
						/* for some reasn this means you are a veteren*/
						
						function getUrlVars() {
							var vars = {};
							var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
								vars[key] = value;
							});
							return vars;
						}
						
						
						var target = getUrlVars()["t"] ? getUrlVars()["t"] : "";
						if(!target.length) {
							window.location = ("home.php");
						} else if (target == "pw") {
							window.location = ("settings.php");
						} else if (target == "pw") {
							window.location = ("settings.php");
						}					
					
					/*} else { //First Time User
						setTimeout("document.secretcode.secretcode.focus();",20);
						 var uid = msg.uid;
						 $('#loginhold').html("<div id='message_login'></div>");
						 $('#message_login').html("It appears this is your first time logging in.")
						 .append("<br />To enjoy what lies beyond this form you will need")
						 zz.append("<div id='secrets'>THE SECRET CODE</div>")
						 .append("<form id='secretcode' name='secretcode' method='POST' action='_php/secretcode.php'><input name='secretcode' type='text' class='textbox' id='secretcode' size='19' /><br/><input type='image' src='../images/submit.png' name='submit' id='submit' class='button' value='submit' /> </form>")
						 
						 $('#message_login').html("It appears this is your first time logging in.")
						 .append("<br />Since we don't have open signups at the moment<br />")
						 .append("<br />You'll need an invitation code to continue.<br />")
						 zz.append("<div id='secrets'>THE CODE</div>")
						 .append("<form id='invitationcode' name='invitationcode' method='POST' action='_php/secretcode.php'><input name='secretcode' type='text' class='textbox' id='secretcode' size='19' /><br/><input type='image' src='../images/submit.png' name='submit' id='submit' class='button' value='submit' /> </form>")
						
						.hide()
						
						.fadeIn(1500, function() {
						  $('#message_login').append("");
						 
						});
						
						
						
						//////////////WHOa///////////////////
						$("form#secretcode").submit(function() {
						
		  
						  var secretcode = $("input#secretcode").val();
						  
						
							
							var dataStringCode = 'username='+ username + '&secretcode=' + secretcode + '&uid=' + msg.uid;
							//alert (dataString);return false;
							
							$.ajax({
						  type: "POST",
						  url: "_php/secretcode.php",
						  data: dataStringCode,
						  dataType: "json",
						  success: function(msg){  
					
									if (msg.codepass == 1) {
	
										window.location = ("home.php?ft=yes");
					
									}
									else {
							
										 $('#loginhold').append("<div id='message_code'></div>");
										 $('#message_code').html("<h2>Nope thats not it</h2>")
									
										.hide()
										.fadeIn(1500, function() {
										  $('#message_code').append("");
										});
					
									}
							
					  
								},  
								error: function(){  
									
								
								}  
						  
					
						  
						  
						 });
						return false;*/
					//});
						
					////WHOA END//	
	
					//}
				}
  
            },  
            error: function(){  
			
				$("span#working").hide();
				 $('#loginhold').append("<div id='login_error'></div>");
				 
				 $('#login_error').text("Hmm, that didn't seem to work. Check your username and password.")
			
				.hide()
				.fadeIn(500, function() {
				$('#login_error').append("");
				});
			
				
			
            }  
	  

	  
	  
     });
    return false;
	});
	
	
	
	
	
	
});


function doFocusLogin() {
	$("span#working").hide();
	
	setTimeout("document.login.lusername.focus();",10);
	
}


 /* success: function(response){
   // if sucessful; response will contain some stuff echo-ed from .php
   // quick tect: alert("Here is your response: " + response);
   // Append this response to some <div> => let's append it to div with id "serverMsg"
   jQuery(".done").append(response);
  }, */
  
  

  


