function sendFriendEmail() {
	var error = '';
	if ($F('contactName') == '' || $F('contactEmail') == '') {
		error += 'Please enter your name and email address.\n';
	}
	if ($F('friendName') == '' || $F('friendEmail') == '') {
		error += 'Please enter your friend\'s name and email address.\n';
	}
	if (error != '') {
		alert(error);
		return false;
	}
	$('emailForm').submit();
}

function sendEmailEnquiry() {
	var error = '';
	if ($F('contactName') == '') {
		error += 'Please enter your name.\n';
	}
	if ($F('contactTelephone') == '' && $F('contactEmail') == '') {
		error += 'Please enter either your email address or a telephone number.\n';
	}
	if ($F('enquiryText') == '') {
		error += 'Please enter a message.';
	}
	if (error != '') {
		alert(error);
		return false;
	}
	$('emailForm').submit();
}

function validateLoginForm(f) {

	if (f.username.value == '' || f.password.value == '') {
		alert("Please enter your username and password.");
		if (f.username.value == '') {
			f.username.focus();
		} else {
			f.password.focus();
		}
		return false;
	}

	f.submit();

	return false;
}

function submitLogin(f) {
	if (f.username.value == '' || f.password.value == '') {
		alert("Please enter your username and password to login.");
	} else {
		f.formAction.value = 'doLogin';
		f.action = '/login.php';
		f.submit();
	}
}

function numeralsOnly(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	}
	return true;
}

function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}

/**
 * Form validation functions
 *
 **/

wizbitFormValidator = function(f) {
	this.form = f;
}

wizbitFormValidator.prototype.form = null;
wizbitFormValidator.prototype.formIsValid = true;
wizbitFormValidator.prototype.highlightErrors = true;
wizbitFormValidator.prototype.errorClass = 'adminFormError';
wizbitFormValidator.prototype.standardClass = 'adminForm';

wizbitFormValidator.prototype.validate = function(req) {
	for (var i = 0; i < req.length; i++) {
		if (!this.form.elements[req[i]]) {
			alert('Field ' + req[i] + ' not found!');
			this.formIsValid = false;
			return false;
		}
		with (this.form.elements[req[i]]) {
			if (this.highlightErrors) {
				className = this.standardClass;
			}
			switch (tagName) {
				case 'INPUT':
					switch (type) {
						case 'text':
						case 'password':
							if (value == '') {
								this.formIsValid = false;
								if (this.highlightErrors) {
									className = this.errorClass;
								}
							}
							break;
					}
					break;
				case 'TEXTAREA':
					if (value == '') {
						this.formIsValid = false;
						if (this.highlightErrors) {
							className = this.errorClass;
						}
					}
					break;
				case 'SELECT':
					if (multiple) {
						var madeSelection = false;
						for (var j = 0; j < options.length; j++) {
							if (options[j].selected) {
								madeSelection = true;
							}
						}
						if (!madeSelection) {
							this.formIsValid = false;
							if (this.highlightErrors) {
								className = this.errorClass;
							}
						}
					} else {
						if (options[selectedIndex].value == '') {
							this.formIsValid = false;
							if (this.highlightErrors) {
								className = this.errorClass;
							}
						}
					}
					break;	
			}
		}
	}
}


