$(function() {
  

  $("form#signup").submit(function(){
		// validate and process form
		// first hide any error messages
		
    
		
	  var username = $("input#username").val();
		if (username == "") {
      $("label#username_error").show();
      $("input#username").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var password = $("input#password").val();
		if (password == "") {
      $("div#password_error").show();
      $("input#password").focus();
      return false;
    }
		if (password.length < 6) {
		$("span#validatePassword").removeClass('error').html('6 is the magic number');
		$("input#password").focus();
		
		return false;
    }
	  
		var description = $("textarea#description").val();
		if (description == "") {
      $("label#description_error").show();
      $("textarea#description").focus();
      return false;
    }
	
		var invitation = $("input#invitation_hid").val();
		
		
		var dataString = 'username='+ username + '&email=' + email + '&password=' + password + '&description=' + description +'&invite=' + invitation;
		//alert (dataString);return false;
		
		$("span#working").show();
		
		$.ajax({
      type: "POST",
      url: "_php/saveit.php",
      data: dataString,
	  dataType: "json",
	  success: function(msg){  
                /*$("#formResponse").removeClass('error');  
                $("#formResponse").removeClass('success');  
                $("#formResponse").addClass(msg.status);  
                $("#formResponse").html(msg.message);*/
				$("span#working").hide();
				
				if (msg.message == "Oh no, your username is blank") {
					
					$("#validateUsername").html("<span style='color:red'>" + msg.message + "</span>");
					$("input#username").focus();
				} else if (msg.message == "Something looks wrong here...") {
					
					$("#validateEmail").html("<span style='color:red'>" + msg.message + "</span>");
					$("input#email").focus();
				} else if (msg.message == "Sorry, this username is taken") {
					
					$("#validateUsername").html("<span style='color:red'>" + msg.message + "</span>");
					$("input#username").focus();
				} else if (msg.message == "yay everything worked") {
					
					
					 $('#signuphold').html("<div id='message_signup'></div>");
					 //$('#message_signup').html("Dear "  + username +  ",") // username keeps returning "undefined" here, but works everywhere else??
					 $('#message_signup').html("")
					.append("<p>Thanks! You're great at filling out forms.<br /><br /> graFighters has just launched into private beta.</p>")
					.append("<br />We are hoping to invite you to join in on the fun very very soon. Feel free to bother us on <a href='http://www.twitter.com/grafighters'>twitter</a> while we get ready for more users. ")
					.append("<div id='signoff'>Get busy, <br /> Cornelius </div>")
					
					.hide()
					.fadeIn(1500, function() {
					  $('#message_signup').append("");
					});
					
				} else if (msg.message == "Invitation not found") {
					console.info(msg.message);
					$("#validateUsername").html("<span style='color:red'>" + msg.message + "</span>");
				} else if (msg.message == "Invitations have been maxed out") {
					console.info(msg.message);
					$("#validateUsername").html("<span style='color:red'>" + msg.message + "</span>");
				} else if (msg.message == "you are in!") {
					
					//alert (dataString);return false;
					var username = $("input#username").val();
					var password = $("input#password").val();
					var dataStringCode = 'lusername='+ username + '&lpassword=' + password;
					$.ajax({
						
						type: "POST",
						url: "_php/login.php",
						data: dataStringCode,
						dataType: "json",
						success: function(msg) {  
							if (msg.status == "success") {
								window.location = ("home.php?ft=yes");
							} else {
								console.info(msg.message);
								$("#validateUsername").html("<span style='color:red'>yyy:" + msg.message + "</span>");
							}
						},  
						error: function(){  
							console.info("error");
						}
					});
				} else {
					console.info(msg.message);
					$("#validateUsername").html("<span style='color:red'>" + msg.message + "</span>");
				}
  
            },  
            error: function(msg){
				console.info(msg);
				$("span#working").hide();
				
				$('#signuphold').html("<div id='message_signup'></div>");
				 $('#message_signup').html("Hmmm, it appears that didn't work for some reason that is unknown to the universe. Sorry about that. Maybe try again?")

            }  
	  

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

function doFocus() {
	$("span#working").hide();
	setTimeout("document.signup.username.focus();",10);
	
}
