// 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;
			default:
				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 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;
}

-->