///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                       Copyright (c) 2003-2006                             //
//                            Marc Peterson                                  //
//                     marc.s.peterson at gmail.com                          //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

// Last updated:
//   2007-04-27 - made more field mandatory in validateNewAccount()
//   2006-06-29 - added checkAutoComplete() to enable Sign In button if autocomplete has populated sign-in form


// Used in the main sign in page and the cart sign in page.

function initSignIn()
{
	// Set up the forms to enable their submit buttons and show errors
	initFormOnChange("sign_in", "btn_sign_in", "btn_green");
	initFormOnChange("reset_password", "btn_reset_password", "btn_green");
	initFormOnChange("new_account", "btn_new_account", "btn_green");

	// check if login already has a value in it (from autocomplete).  If so, enable button
	setTimeout("checkAutoComplete()",1000);
}

function checkAutoComplete()
{
	var oForm	= document.getElementById("sign_in");
	if ( oForm )
	{
		if ( trim(getInputValue(oForm["email"])) != "" ) enableButton(null, "btn_sign_in", "btn_green");
	} 
}

function showResetPasswordForm()
{
	hideLayer('reset_password_text');
	setDisplay('reset_password_section', '');
}

function validateSignIn()
{
	// Lock the form to prevent double-click saves
	if ( gForm_lock ) return;
	else gForm_lock = true;

	var errors	= new ErrorList();
	var oForm	= document.getElementById("sign_in");

	clearInputErrors(oForm);

	var email		= trim(getInputValue(oForm["email"]));
	var password	= trim(getInputValue(oForm["password"]));

	if ( !checkEmail(email) ) setError(oForm["email"], errors, "Invalid email address\n");
	if ( !checkAlphaNum(password) ) setError(oForm["password"], errors, "Your password can only be characters and numbers\n");
	else if ( password.length < 6 ) setError(oForm["password"], errors, "Your password must be at least 6 characters/digits long\n");

	if ( errors.getNumErrors() )
	{
		errors.showErrors();
		gForm_lock = false;
	}
	else oForm.submit();
}

function validateResetPassword()
{
	// Lock the form to prevent double-click saves
	if ( gForm_lock ) return;
	else gForm_lock = true;

	var errors	= new ErrorList();
	var oForm	= document.getElementById("reset_password");

	clearInputErrors(oForm);

	var email = getInputValue(oForm["email"]);
	if ( !checkEmail(email) ) setError(oForm["email"], errors, "Invalid email address\n");

	if ( errors.getNumErrors() )
	{
		errors.showErrors();
		gForm_lock = false;
	}
	else
	{
		// Confirm with the user
		if ( ! confirm("This will erase your current password and email a new one.  Are you sure you want to do this?") ) return;
		oForm.submit();
	}
}

function validateNewAccount()
{
	// Lock the form to prevent double-click saves
	if ( gForm_lock ) return;
	else gForm_lock = true;

	var errors	= new ErrorList();
	var oForm	= document.getElementById("new_account");

	clearInputErrors(oForm);

	var first_name		= getInputValue(oForm["account_first_name"]);
	var last_name		= getInputValue(oForm["account_last_name"]);
	var a_title			= getInputValue(oForm["account_title"]);
	var company			= getInputValue(oForm["account_company"]);
	var email			= getInputValue(oForm["account_email"]);
	var phone			= getInputValue(oForm["account_phone"]);
	var password		= getInputValue(oForm["account_password"]);
	var password_2		= getInputValue(oForm["account_password_2"]);

	if ( !checkTheseChars(first_name, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .-") ) setError(oForm["account_first_name"], errors, "Your first name must be alpha-numeric and space . -\n");
	else if ( first_name.length == 0 ) setError(oForm["account_first_name"], errors, "Your first name cannot be blank\n");

	if ( !checkTheseChars(last_name, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .-") ) setError(oForm["account_last_name"], errors, "Your last name must be alpha-numeric and space . -\n");
	else if ( last_name.length == 0 ) setError(oForm["account_last_name"], errors, "Your last name cannot be blank\n");

	if ( a_title == "" ) setError(oForm["account_title"], errors, "Your title cannot be blank\n");
	if ( company == "" ) setError(oForm["account_company"], errors, "Your company cannot be blank\n");
	if ( !checkEmail(email) ) setError(oForm["account_email"], errors, "Invalid email address\n");
	if ( phone == "" ) setError(oForm["account_phone"], errors, "Your phone number cannot be blank\n");

	if ( !checkAlphaNum(password) ) setError(oForm["account_password"], errors, "Your password can only be characters and numbers\n");
	else if ( password.length < 6 ) setError(oForm["account_password"], errors, "Your password must be at least 6 characters/digits long\n");
	else if ( password != password_2 )
	{
		setError(oForm["account_password"], errors, "Your passwords do not match, please re-enter\n");			
		setError(oForm["account_password_2"], errors, "");
	}

	if ( errors.getNumErrors() )
	{
		errors.showErrors();
		gForm_lock = false;
	}
	else oForm.submit();
}
