function isValidEmail(sEmail) {
	// Regex based email validation
	// Same as used in PHP validation
	return (/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i.test(sEmail));
}

function isValidDate(sDate) {
	// Regex based MySQL Date validation
	// Same as used in PHP validation
	return (/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/i.test(sDate));
}

function validateBuyForm() {
	var form = document.getElementById('registrationForm');
	var firstname = form.firstname;
	if (firstname.value.length < 1) {
		firstname.focus();
		alert('You must enter your first name.');
		return false;
	}
	var surname = form.surname;
	if (surname.value.length < 1) {
		surname.focus();
		alert('You must enter your surname.');
		return false;
	}
	var email = form.email;
	if (!isValidEmail(email.value)) {
		email.focus();
		alert('You must enter a valid email address.');
		return false;
	}
	var password = form.password;
	var password2 = form.password2;
	if (password.value.length < 6) {
		password.value = "";
		password.focus();
		alert('Your password must be at least 6 characters long.');
		return false;
	} else {
		if (password.value != password2.value) {
			password.value = "";
			password2.value = "";
			password.focus();
			alert('The passwords you entered were not the same.');
			return false;
		}
	}
	var secretquestionanswer = form.secretquestionanswer;
	if (secretquestionanswer.value.length < 1) {
		secretquestionanswer.focus();
		alert('You must answer a secret question.');
		return false;
	}
	return true;
}

function validateContactForm() {
	var form = document.getElementById('contactForm');
	var email = form.email;
	if (!isValidEmail(email.value)) {
		email.focus();
		alert('The email address you entered was not valid. Please try again.');
		return false;
	}
	var firstname = form.firstname;
	if (firstname.value.length < 1) {
		firstname.focus();
		alert('You must enter your firstname.');
		return false;
	}
	var surname = form.surname;
	if (surname.value.length < 1) {
		surname.focus();
		alert('You must enter your surname.');
		return false;
	}
	var subject = form.subject;
	if (subject.value.length < 3) {
		subject.focus();
		alert('The subject must be at least 3 characters long.');
		return false;
	}
	var message = form.message;
	if (message.value.length < 6) {
		message.focus();
		alert('Your message must be at least 6 characters long.');
		return false;
	}
}

function validateAccountChangePasswordForm() {
	var form = document.getElementById('accountChangePasswordForm');
	var oldpassword = form.oldpassword;
	if (oldpassword.value.length < 1) {
		oldpassword.focus();
		alert('You must enter your current password.');
		return false;
	}
	var password = form.password;
	var password2 = form.password2;
	if (password.value.length < 6) {
		password.value = "";
		password.focus();
		alert('Your password must be at least 6 characters long.');
		return false;
	} else {
		if (password.value != password2.value) {
			password.value = "";
			password2.value = "";
			password.focus();
			alert('The passwords you entered were not the same.');
			return false;
		}
	}
	return true;
}

function validateUserEditForm() {
	var form = document.getElementById('userEditForm');
	var firstname = form.firstname;
	if (firstname.value.length < 1) {
		firstname.focus();
		alert('You must enter your first name.');
		return false;
	}
	var surname = form.surname;
	if (surname.value.length < 1) {
		surname.focus();
		alert('You must enter your surname.');
		return false;
	}
	var email = form.email;
	if (!isValidEmail(email.value)) {
		email.focus();
		alert('You must enter a valid email address.');
		return false;
	}
	return true;
}

function validateCodeForm() {
	var form = document.getElementById('codeForm');
	var code = form.code;
	if (code.value.length < 4 || code.value.length > 10) {
		code.focus();
		alert('Code must be between 4 and 10 characters long.');
		return false;
	}
	var description = form.description;
	if (description.value.length < 1 || description.value.length > 30) {
		description.focus();
		alert('Description must be between 1 and 30 characters long.');
		return false;
	}
	var amount = form.amount;
	if (amount.value.length < 1) {
		amount.focus();
		alert('You must enter an amount.');
		return false;
	}
	if (isNaN(amount.value)) {
		amount.focus();
		alert('The amount you entered must be a number');
		return false;
	}
	var expires = form.expires;
	if (expires.checked) {
		var expirydate = form.expirydate;
		if (!isValidDate(expirydate.value)) {
			expirydate.focus();
			alert('Expiry date must be in format YYYY-MM-DD.');
			return false;
		}
	}
	return true;
}

function validateAgentForm() {
	var form = document.getElementById('agentForm');
	var code = form.code;
	if (code.value.length < 2 || code.value.length > 10) {
		code.focus();
		alert('Agent Code must be between 2 and 10 characters long.');
		return false;
	}
	var name = form.name;
	if (name.value.length < 1 || name.value.length > 30) {
		name.focus();
		alert('Description must be between 1 and 100 characters long.');
		return false;
	}
	return true;
}