// Checkout Beginning Control File.
//
// Contains various functions to control the form elements on the default checkout page.

// Add the onload function for this form to the window's onload handler.
addWindowOnLoad('checkout_begin_onLoad()');



// checkout_begin_onLoad()
//
// Adds the necessary event handlers to the form and its elements.
function checkout_begin_onLoad(){
	document.forms[0].onsubmit = validateForm;
	for (var i = 0; i < document.forms[0].elements['accountaction'].length; i++){
		document.forms[0].elements['accountaction'][i].onclick = toggleCustomerType;
		}
	}



// toggleCustomerType()
// 
// Called when the user changes the customer type radio button state.

function toggleCustomerType(){
	//alert('toggling');
	// If type is "New", make sure the "Password" field is available.
	if (getRadioButtonValue(this.form.elements['accountaction']) == 'new'){
		this.form.elements['password'].readOnly = true;
		}
	// If type is "Returning", make sure the password field is not available.
	else {
		this.form.elements['password'].readOnly = false;
		}
	}
	

// validateForm()
//
// Called when the form is submitted.

function validateForm()
{
	//alert('validating');
	//Since this function is a form object's onsubmit function, the 
	// keyword 'this' will refer to the form object itself.

	// Make sure the email address is valid.
	if (!checkEmail(document.forms[0].elements['email'], false))
	{
		return(false);
	}
	else
	{
		// If the customer type is "Returning", make sure the password is not blank.
		if (getRadioButtonValue(document.forms[0].elements['accountaction']) == 'returning')
		{
			if (!checkString(document.forms[0].elements['password'], 'Password', false))
			{
				return(false);
			}
			else
			{
				return(true);
			}
		}
		else
		{
			return(true);
		}
	}
}


function validateEmail() // used for the Email password page
{
	//alert('validating');
	//Since this function is a form object's onsubmit function, the 
	// keyword 'this' will refer to the form object itself.

	// Make sure the email address is valid.
	if (!checkEmail(document.forms[0].elements['email'], false))
	{
		return(false);
	}
	else
	{
		return(true);
	}
}