// when the document is ready...
$(function() {

	var autocheckTimer;
	
	$("#emailaddress").keypress(function()
		{
			// the email address field has been changed, cancel the existing timer and start a new one (so we don't
			// bombard the ajax functions and database with lookup requests)
			
			// clear the timer
			window.clearTimeout(autocheckTimer);
			
			// create a new timer
			autocheckTimer = window.setTimeout('isEmailAvailable()', 500);
		});
	
	$("#username").keypress(function()
	{
		// the username field has been changed, cancel the existing timer and start a new one (so we don't
		// bombard the ajax functions and database with lookup requests)
			
		// clear the timer
		window.clearTimeout(autocheckTimer);
		
		// create a new timer
		autocheckTimer = window.setTimeout('isUsernameAvailable()', 500);
	});
	
	$("#submit").attr("disabled", true);
});

/**
 * Function to check whether the desired email address entered is in use
 */
function isEmailAvailable()
{
	// get the current text inserted in the email address field
	$currentEmail = $("#emailaddress").val();
	
	// create a POST request to the ajax controller action
	// set the curret email address text as a parameter value
	// show an error when we return if in use
	$.post(
		   "/distributor/registration/checkemail",
		   {
			   "e_check" : $currentEmail
			},
		   function(data)
		   {
			   // if the email address wasn't in use, clear any previous error messages
			   $("#emailaddress-element").find(".emailAddressError").remove();
			   
			   // alert("Process email...");
			   if ($currentEmail != "" && data == 'true')
			   {
				   // the email address entered is in use...
				   $("#emailaddress-element").append('<ul class="emailAddressError errors"><li>Email address not available, please select an alternative</li></ul>');
				   
				   // disable the submit button
				   $("#submit").attr("disabled", true);
			   }
			   else
			   {
				   // enable the submit button
				   $("#submit").removeAttr("disabled");
			   }
		   }
	);
}

/**
 * Function to check whether the desired username entered is in use
 */
function isUsernameAvailable()
{
	// get the current text inserted in the username field
	$currentUsername = $("#username").val();
	
	// create a POST request to the ajax controller action
	// set the curret username text as a parameter value
	// show an error when we return if in use
	$.post(
		   "/distributor/registration/checkusername",
		   {
			   "u_check" : $currentUsername
			},
		   function(data)
		   {
			   // if the username wasn't in use, clear any previous error messages
			   $("#username-element").find("#usernameError").remove();

			   // alert("Process email...");
			   if ($currentUsername != "" && data == 'true')
			   {
				   // the email address entered is in use...
				   $("#username-element").append('<ul id="usernameError" class="errors"><li>Username not available, please select an alternative</li></ul>');
				   
				   // disable the submit button
				   $("#submit").attr("disabled", true);
			   }
			   else
			   {
				   // enable the submit button
				   $("#submit").removeAttr("disabled");
			   }
		   }
	);
}
