/*
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;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function checkRegisterForm()
{

	
	with (window.document.frmAddCustomer) {
		if (isEmpty(txtUserName, 'Enter user name')) {
			return false;
		} else if (isEmpty(txtPassword, 'Enter password')) {
			return false;
		} else if (txtPassword.value!== confirm_password.value) {
			alert ('Passwords must match');
			return false;
		} else if (checkNumber(telephone)) {
			alert ('Telephone must caontain numbers');
			return false;
		} else {
			true;
		}
	}
	
	
}

function Form1_Validator(theForm)
{
	if (theForm.firstname.value == "")
	{
		alert("Please enter a first name.");
		theForm.firstname.focus();
		return (false);
	}
	
	if (theForm.lastname.value == "")
	{
		alert("Please enter last name.");
		theForm.lastname.focus();
		return (false);
	}
	
	
	if (theForm.telephone.value == "")
	{
		alert("Please enter a telephone number.");
		theForm.telephone.focus();
		return (false);
	}
	
	if (theForm.email.value == "")
	{
		alert("Please enter an email address.");
		theForm.email.focus();
		return (false);
	}
	
		if (theForm.email.value!== theForm.confirm_email.value) {
			alert ('email addresses must match');
			return (false);
	}
	
	if (theForm.txtUserName.value == "")
	{
		alert("Please enter a Username.");
		theForm.txtUserName.focus();
		return (false);
	}
	
	if (theForm.txtPassword.value == "")
	{
		alert("Please enter a Password.");
		theForm.txtPassword.focus();
		return (false);
	}
	
	if (theForm.txtPassword.value!== theForm.confirm_password.value) {
			alert ('Passwords must match');
			return (false);
	}
	
	
	if (theForm.Address1.value == "")
	{
		alert("Please enter first line of address.");
		theForm.Address1.focus();
		return (false);
	}
	if (theForm.town.value == "")
	{
		alert("Please enter your City or Town.");
		theForm.town.focus();
		return (false);
	}
	
	if (theForm.postcode.value == "")
	{
		alert("Please enter a post code.");
		theForm.postcode.focus();
		return (false);
	}
  
  	return (true);
} 

function checkRegisterForm()
{

	
	with (window.document.frmAddCustomer) {
		if (isEmpty(txtUserName, 'Enter user name')) {
			return false;
		} else if (isEmpty(txtPassword, 'Enter password')) {
			return false;
		} else if (txtPassword.value!== confirm_password.value) {
			alert ('Passwords must match');
			return false;
		} else if (checkNumber(telephone)) {
			alert ('Telephone must caontain numbers');
			return false;
		} else {
			true;
		}
	}
	
	
}

function myaccount_Validator(theForm)
{
	if (theForm.firstname.value == "")
	{
		alert("Please enter a first name.");
		theForm.firstname.focus();
		return (false);
	}
	
	if (theForm.lastname.value == "")
	{
		alert("Please enter last name.");
		theForm.lastname.focus();
		return (false);
	}
	
	
	if (theForm.telephone.value == "")
	{
		alert("Please enter a telephone number.");
		theForm.telephone.focus();
		return (false);
	}
	
	if (theForm.email.value == "")
	{
		alert("Please enter an email address.");
		theForm.email.focus();
		return (false);
	}
	
		if (theForm.email.value!== theForm.confirm_email.value) {
			alert ('email addresses must match');
			return (false);
	}
	
	if (theForm.txtUserName.value == "")
	{
		alert("Please enter a Username.");
		theForm.txtUserName.focus();
		return (false);
	}
	
	if (theForm.txtPasswordc.value == "")
	{
		alert("Please enter a Password.");
		theForm.txtPasswordc.focus();
		return (false);
	}
	
	
	if (theForm.Address1.value == "")
	{
		alert("Please enter first line of address.");
		theForm.Address1.focus();
		return (false);
	}
	if (theForm.town.value == "")
	{
		alert("Please enter your City or Town.");
		theForm.town.focus();
		return (false);
	}
	
	if (theForm.postcode.value == "")
	{
		alert("Please enter a post code.");
		theForm.postcode.focus();
		return (false);
	}
  
  	return (true);
}  

