Event.observe(window, 'load', function() {
	if ($('contactForm')) {
		var args = getArgs();
		if (args.product) {
			var form = $('contactForm');
			var input = form['Comments'];
			input.value = "I'm enquiring about " + args.product + ".";
		}

		Event.observe('contactForm', 'submit', function(ev) {
			if (
				validate_first_selection('contactForm', 'Title', 'Please select your title.') ||
				validate_empty_input('contactForm', 'FirstName', 'Please enter your first name.') ||
				validate_empty_input('contactForm', 'LastName', 'Please enter your last name.') ||
				validate_empty_input('contactForm', 'Address1', 'Please enter your address.') ||
				validate_empty_input('contactForm', 'Town', 'Please enter your town/city.') ||
				validate_empty_input('contactForm', 'Postcode', 'Please enter your post code.') ||
				validate_first_selection('contactForm', 'Country', 'Please select your country.') ||
				validate_empty_input('contactForm', 'WorkTelephone', 'Please enter your work telephone number.') ||
				validate_empty_input('contactForm', 'submit_by', 'Please enter your email address.') ||
				validate_invalid_email('contactForm', 'submit_by', 'Your email address appears invalid.')
			) {
				Event.stop(ev);
			}
		});
	}

	if ($('callbackButton')) {
		Event.observe('callbackButton', 'click', function(ev) {
			if (
				validate_empty_input('callbackForm', 'name', 'Please enter your first name.') ||
				validate_empty_input('callbackForm', 'telephone', 'Please enter your telephone number.') ||
				validate_first_selection('callbackForm', 'product', 'Please select a product of interest.')
			) {
				Event.stop(ev);
			} else {
				$('callbackForm').submit();
				Event.stop(ev);
			}
		});
	}
});

function validate_empty_input(formElement, field, message) {
	var form = $(formElement);
	var input = form[field];
	var value = $F(input);

	if (value.length < 1) {
		input.focus();
		alert(message);
		return true;
	} else {
		return false;
	}
}

function validate_first_selection(formElement, field, message) {
	var form = $(formElement);
	var input = form[field];

	if (input.selectedIndex < 1) {
		input.focus();
		alert(message);
		return true;
	} else {
		return false;
	}
}

function validate_invalid_email(formElement, field, message) {
	var form = $(formElement);
	var input = form[field];
	var value = $F(input);

	if (!valid_email(value)) {
		input.focus();
		alert(message);
		return true;
	} else {
		return false;
	}
}

function valid_email(email) {
   mailregexp = /^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i;
   return mailregexp.test(email);
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

