$(document).ready(function() {
	
	$("#AlertMsgWrap").hide();
	$("#NewAccount_LoadingTxt").hide();
	
	//Date Selector
	$("#dob").datePicker({clickInput:true,startDate:'01/01/1900'});
	
	//Country/State Selectors
	$.getJSON("/games/findgetstate",{id: 1, ajax: 'true'}, function(j){
    var options = '';
    for (var i = 0; i < j.length; i++) {
      options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
    }
		$("select#state").html(options);		
  });

	$("select#country").change(function(){
	  $.getJSON("/games/findgetstate",{id: $(this).val(), ajax: 'true'}, function(j){
	    var options = '';
	    for (var i = 0; i < j.length; i++) {
	      options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	    }
	    $("select#state").html(options);
	  })
	});
	
	$("form#NewAccount").livequery('submit', function() {
		
		var loadingTxt = "Loading, Please Wait...";
		$("#NewAcount_SubmitBtn").hide();
		$("#NewAccount_LoadingTxt").empty()
															.append(loadingTxt)
															.show();
		var errorMsg = "";
		
		//Validation
		var valid = false;
		
		if ($("input#agree_box").val() == false) {
			var errorMsg = errorMsg + "You must agree to our Terms of Use before proceeding!";
		} else if ($("input#username").val() == "") {
			var errorMsg = errorMsg + "Please enter a username!";					
		} else if ($("input#FirstName").val() == "") {
			var errorMsg = errorMsg + "Please enter a First Name!";					
		} else if ($("input#LastName").val() == "") {
			var errorMsg = errorMsg + "Please enter a Last Name!";					
		} else if ($("input#Password").val() == "") {
			var errorMsg = errorMsg + "Please enter a Password!";					
		} else if ($("input#email").val() == "") {
			var errorMsg = errorMsg + "Please enter an Email Address!";					
		} else if ($("input#dob").val() == "") {
			var errorMsg = errorMsg + "Please enter your Date of Birth!";										
		} else {
			valid = true;
		}
		
		
		
		if (valid) {
			var FormData = $("form#NewAccount").serialize();

			$.ajax({
				type: "POST",
				url: "/login/register",
				data: FormData,
				success: function(html){
					var pos = html.indexOf("yes");
					if (pos >= 0) {

						var msg = "<div>Your Account has been successfully created!</div><div style='color:black; font-weight:normal;'>A Confirmation email has been sent to the email address provided.</div><div style='color:black; font-weight:normal;'>Please click the link to activate your accont.</div>";
						$("#AlertMsgWrap").hide();
						$("#alertMsg_public").empty()
																 .append(msg);

						$("#NewAccountForm").slideUp(1000, function() {
							$("#AlertMsgWrap").slideDown(1000);
						});


					} else {

						$("#AlertMsgWrap").show();
						$("#alertMsg_public").empty()
																 .append(html);	

						$("#NewAccount_LoadingTxt").empty()
																			.hide();
						$("#NewAcount_SubmitBtn").show();												
					}

				}
			});
		} else {
			
			$("#AlertMsgWrap").show();
			$("#alertMsg_public").empty()
													 .append(errorMsg);

			$("#NewAccount_LoadingTxt").empty()
																.hide();
			$("#NewAcount_SubmitBtn").show();
			
		}
		
		
		return false;
		
	});
	
});

