
/*
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 formValidatorRegister()
{
	theForm = window.document.frmRegister;
	
	if (isEmpty(theForm.email, 'Please Enter Your Email') || isEmpty(theForm.password, 'Please Enter Your Password') || isEmpty(theForm.confirmpassword, 'Please Confirm Your Password')) {
			return false;
		} else {
		
			if (theForm.password.value == theForm.confirmpassword.value){
				//theForm.submit();
				var x = theForm.email.value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!(filter.test(x))){
					alert('Incorrect email address');
					return false;
				} else {
					return true;
				}
			 }else{
				alert("Passwords Do Not Match");
				return false;
			}
		}
	
	
}

function formValidatorPassword()
{
	theForm = window.document.frmAccountPassword;
	
	if (isEmpty(theForm.password, 'Please Enter Your Password') || isEmpty(theForm.confirmpassword, 'Please Confirm Your Password')) {
			return false;
		} else {
		
			if (theForm.password.value == theForm.confirmpassword.value){
			 }else{
				alert("Passwords Do Not Match");
				return false;
			}
		}
	
	
}

function formValidatorDetails()
{
	theForm = window.document.frmAccountDetails;
	
	if (isEmpty(theForm.firstname, 'Please Enter Your First Name') || isEmpty(theForm.lastname, 'Please Enter Your Last Name') || isEmpty(theForm.telday, 'Please Enter Your Daytime Telephone Number') || isEmpty(theForm.televe, 'Please Enter Your Evening Telephone Number') || isEmpty(theForm.email, 'Please Enter Your Email')) {
			return false;
		} else {
				var x = theForm.email.value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!(filter.test(x))){
					alert('Incorrect email address');
					return false;
				} else {
					return true;
				}
			 
		}
	
	
}

function formValidatorAddress()
{
	theForm = window.document.frmAccountAddress;
	
	if (isEmpty(theForm.address1, 'Please Enter Your Address') || isEmpty(theForm.city, 'Please Enter Your Town/City') || isEmpty(theForm.county, 'Please Enter Your County') || isEmpty(theForm.postcode, 'Please Enter Your Postcode')) {
			return false;
		} else {
				
			return true;
 
		}
	
	
}

function formValidatorLogin()
{
	theForm = window.document.frmLogin;
	
	if (isEmpty(theForm.email, 'Please Enter Your Email') || isEmpty(theForm.password, 'Please Enter Your Password')) {
			return false;
		} else {
		
			return true;
		}
	
	
}

function formValidatorLost()
{
	theForm = window.document.frmLost;
	
	if (isEmpty(theForm.email, 'Please Enter Your Email')) {
			return false;
		} else {
		var x = theForm.email.value;
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!(filter.test(x))){
					alert('Incorrect email address');
					return false;
				} else {
					return true;
				}
		}
	
	
}
