// JavaScript Document

<!--

function run_check(){ // args = 1. Object (this), 2. Type (e.g. 'email', ''), 3. value to check against (e.g. 'simon')
	if (args = run_check.arguments){
		var error = '';
		var obj = args[0];
		var check_type = args[1];
		var check_against_value = args[2];
		var name = obj.title ? obj.title : obj.name;
		switch (check_type){
			case 'same_as':
				var other_object = document.getElementById(check_against_value);
				var other_name = other_object.title ? other_object.title : other_object.name;
				if (obj.value == ''){
					error += 'Please complete the ' + name + ' field.\r\n';
					amend_class(obj,'invalid');
				} else if (obj.value != other_object.value){
					error += 'Both the ' + obj.name + ' and the ' + other_name + ' fields must be the same.\r\n';
					amend_class(obj,'invalid');
					amend_class(other_object,'invalid');
				} else {
					amend_class(obj,'valid');
					amend_class(other_object,'valid');
				}
				break;
			case 'min_len':
				if (obj.value == ''){
					error += 'Please complete your ' + name + '\r\n';
					amend_class(obj,'invalid');
				} else if (obj.value.length < check_against_value){
					error += 'Your ' + name + ' needs to be at least ' + check_against_value + ' characters long\r\n';
					amend_class(obj,'invalid');
				} else {
					amend_class(obj,'valid');
				}
				break;
			case 'email':
				if (obj.value == ''){
					error += 'Please complete your ' + name + '\r\n';
					amend_class(obj,'invalid');
				} else if (!validateEmail(obj.value)){
					error += 'You do not appear to have created a valid email address.\r\n';
					amend_class(obj,'invalid');
				} else {
					amend_class(obj,'valid');
				}
				break;
			case 'checkbox':
				if (!obj.checked){
					error += 'Please check the following required field: \r\n' + name + '\r\n';
					//amend_class(obj,'invalid');
				} else {
					//amend_class(obj,'valid');
				}
				break;
			default:
				//alert('value: '+obj.value);
				if (obj.value == ''){
						error += 'Please complete your ' + name + '\r\n';
						amend_class(obj,'invalid');
				} else if (check_against_value){
					if (obj.value == check_against_value){
						error += 'Please amend your ' + name + '\r\n';
						amend_class(obj,'invalid');
					} else {
						amend_class(obj,'valid');
					}
				} else {
					amend_class(obj,'valid');
				}
				break;
		}
	}
	return error;
}

function submit_check(){
	var _error = '';
	if (argmts = submit_check.arguments){
		for (a=0;a<argmts.length;a+=3){
			object = document.getElementById(argmts[a]);
			_error += run_check(object, argmts[a+1], argmts[a+2]);
		}
	}
	if (!_error){
		return true;
	}
	alert (_error);
	return false;
}

function mailing_list_application(obj){
	var url = 'http://visitor.constantcontact.com/email.jsp';
	var pars = 'm=1101491872138&amp;lang=en&amp;ea=' + $('mailing_list_em').value;
	new Ajax.Request('/deleteme2.php',
		{
			parameters: pars,
			method: 'post',
			onFailure: function(){ $(obj).innerHTML = '<div id=\'thank_you\'><h3>Whoops!</h3><p>We appreciate your request but seem to be experiencing some difficulty in accepting it. We apologise for any inconvenience this may cause. Please feel free to contact us on 01534 726 521.</p></div>'; },
			onSuccess: function(){ $(obj).innerHTML = '<div id=\'thank_you\'><h3>Thank you</h3><p>We appreciate your request and look forward to providing you with all the latest news &amp promotions from the Best Western Royal Hotel, Jersey</p></div>'; }
		}
	);
	return false;
}

function amend_class(){
	if (args = amend_class.arguments){
		var obj = args[0].parentNode;
		var newclass = args[1];
		obj.className = newclass;
		return true;
	}
}

function validateEmail(){	
	var email_value = validateEmail.arguments[0];
	apos=email_value.indexOf("@");
	dotpos=email_value.lastIndexOf(".");
	if (apos<1 || dotpos-apos<2){
		return false;
	}
	return true;
}

-->