/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function setPaymentInfo(isChecked)
{
	with (window.document.frmCheckout) {
		if (isChecked) {
			txtPaymentFirstName.value    = txtShippingFirstName.value;
			txtPaymentLastName.value     = txtShippingLastName.value;
			txtPaymentAddress1.value     = txtShippingAddress1.value;
			txtPaymentAddress2.value     = txtShippingAddress2.value;
			txtPaymentSuburb.value       = txtShippingSuburb.value;
			txtPaymentPostalCode.value   = txtShippingPostalCode.value;
			txtPaymentState.value        = txtShippingState.value;			
			txtPaymentCountry.value      = txtShippingCountry.value;			
			txtPaymentPhone.value        = txtShippingPhone.value;
			txtPaymentEmail.value        = txtShippingEmail.value;
			
			txtPaymentFirstName.readOnly  = true;
			txtPaymentLastName.readOnly   = true;
			txtPaymentAddress1.readOnly   = true;
			txtPaymentAddress2.readOnly   = true;
			txtPaymentSuburb.readOnly     = true;
			txtPaymentPostalCode.readOnly = true;			
			txtPaymentState.readOnly      = true;			
			txtPaymentCountry.readOnly    = true;			
			txtPaymentPhone.readOnly      = true;
			txtPaymentEmail.readOnly      = true;
		} else {
			txtPaymentFirstName.readOnly  = false;
			txtPaymentLastName.readOnly   = false;
			txtPaymentAddress1.readOnly   = false;
			txtPaymentAddress2.readOnly   = false;
			txtPaymentSuburb.readOnly     = false;
			txtPaymentPostalCode.readOnly = false;			
			txtPaymentState.readOnly      = false;			
			txtPaymentCountry.readOnly    = false;			
			txtPaymentPhone.readOnly      = false;
			txtPaymentEmail.readOnly      = false;
		}
	}
}


function checkShippingAndPaymentInfo()
{
	with (window.document.frmCheckout) {
		if (isEmpty(txtShippingFirstName, 'Enter first name')) {
			return false;
		} else if (isEmpty(txtShippingLastName, 'Enter last name')) {
			return false;
		} else if (isEmpty(txtShippingAddress1, 'Enter shipping address')) {
			return false;
		} else if (isEmpty(txtShippingSuburb, 'Enter shipping address suburb')) {
			return false;
		} else if (isEmpty(txtShippingPostalCode, 'Enter the shipping address postal/zip code')) {
			return false;
		} else if (isEmpty(txtShippingState, 'Enter shipping address state')) {
			return false;
		} else if (isEmpty(txtShippingCountry, 'Enter shipping address country')) {
			return false;
		} else if (isEmpty(txtShippingPhone, 'Enter phone number')) {
			return false;
		} else if (isEmpty(txtShippingEmail, 'Enter email address')) {
			return false;
		} else if (isEmpty(txtPaymentFirstName, 'Enter first name')) {
			return false;
		} else if (isEmpty(txtPaymentLastName, 'Enter last name')) {
			return false;
		} else if (isEmpty(txtPaymentAddress1, 'Enter Payment address')) {
			return false;
		} else if (isEmpty(txtPaymentSuburb, 'Enter Payment address suburb')) {
			return false;
		} else if (isEmpty(txtPaymentPostalCode, 'Enter the Payment address postal/zip code')) {
			return false;
		} else if (isEmpty(txtPaymentState, 'Enter Payment address state')) {
			return false;
		} else if (isEmpty(txtPaymentCountry, 'Enter Payment address country')) {
			return false;
		} else if (isEmpty(txtPaymentPhone, 'Enter phone number')) {
			return false;
		} else if (isEmpty(txtPaymentEmail, 'Enter email address')) {
			return false;
		} else {
			return true;
		}
	}
}
