
function registerSignupEvents(e) {

	if($('register')) {


			var passWord = $('password');
			if(passWord) {
				startPasswordCheck();
				passWord.setAttribute('autocomplete','off');
				passWord.onblur = startPasswordCheck;
				if (passWord.captureEvents) passWord.captureEvents(Event.BLUR);
			}

			var passWordConf = $('confirm');
			if(passWordConf) {
				startPasswordConfCheck();
				passWordConf.setAttribute('autocomplete','off');
				passWordConf.onblur = startPasswordConfCheck;
				if (passWordConf.captureEvents) passWordConf.captureEvents(Event.BLUR);
			}

			var eMail = $('email');
			if(eMail) {
				startEmailCheck();
				eMail.onblur = startEmailCheck;
				if (eMail.captureEvents) eMail.captureEvents(Event.BLUR);
			}

			var firstName = $('firstname');
			if(firstName) {
				startFirstNameCheck();
				firstName.onblur = startFirstNameCheck;
				if (firstName.captureEvents) firstName.captureEvents(Event.BLUR);
			}

			var lastName = $('lastname');
			if(lastName) {
				startLastNameCheck();
				lastName.onblur = startLastNameCheck;
				if (lastName.captureEvents) lastName.captureEvents(Event.BLUR);
			}

			var address1 = $('address1');
			if(address1) {
				startAddress1Check();
				address1.onblur = startAddress1Check;
				if (address1.captureEvents) address1.captureEvents(Event.BLUR);
			}

			var address2 = $('address2');
			if(address2) {
				startAddress2Check();
				address2.onblur = startAddress2Check;
				if (address2.captureEvents) address2.captureEvents(Event.BLUR);
			}

			var address3 = $('address3');
			if(address3) {
				startAddress3Check();
				address3.onblur = startAddress3Check;
				if (address3.captureEvents) address3.captureEvents(Event.BLUR);
			}

			var address4 = $('address4');
			if(address4) {
				startAddress4Check();
				address4.onblur = startAddress4Check;
				if (address4.captureEvents) address4.captureEvents(Event.BLUR);
			}

			var postcode = $('postcode');
			if(postcode) {
				startPostCodeCheck();
				postcode.onblur = startPostCodeCheck;
				if (postcode.captureEvents) postcode.captureEvents(Event.BLUR);
			}

			var phone = $('phone');
			if(phone) {
				startPhoneCheck();
				phone.onblur = startPhoneCheck;
				if (phone.captureEvents) phone.captureEvents(Event.BLUR);
			}

			var mobile = $('mobile');
			if(mobile) {
				startMobileCheck();
				mobile.onblur = startMobileCheck;
				if (mobile.captureEvents) mobile.captureEvents(Event.BLUR);
			}

			var registerButton = $('sub_register');
			//registerButton.disabled = true;
			//registerButton.className = 'disabledbutton';

	}
}

var timeOutA = 0;
var timeOutB = 0;
var timeOutC = 0;
var timeOutD = 0;
var timeOutE = 0;
var timeOutF = 0;

var timeOutG = 0;
var timeOutH = 0;
var timeOutI = 0;
var timeOutJ = 0;
var timeOutK = 0;

var timeOutL = 0;
var timeOutM = 0;

var passwordIsValid = false;
var passwordConfIsValid = false;
var usernameIsValid = true;
var emailIsValid = false;
var firstNameIsValid = false;
var lastNameIsValid = false;

var addressIsValid = false;

var postCodeIsValid = false;

var phoneIsInvalid = false;


function startUsernameCheck(e) {
	if (timeOutA)
	{
		window.clearTimeout(timeOutA);
	}
	
	timeOutA = window.setTimeout("doUsernameCheck()", 1150);
}

function startPasswordCheck(e) {
	if (timeOutB)
	{
		window.clearTimeout(timeOutB);
	}
	
	timeOutB = window.setTimeout("doPasswordCheck()", 2000);
}

function startEmailCheck(e) {
	startUsernameCheck();
	if (timeOutC)
	{
		window.clearTimeout(timeOutC);
	}
	
	timeOutC = window.setTimeout("doEmailCheck()", 1750);
}

function startPasswordConfCheck(e) {
	if (timeOutD)
	{
		window.clearTimeout(timeOutD);
	}
	
	timeOutD = window.setTimeout("doPasswordConfCheck()", 2000);
}

function startFirstNameCheck(e) {
	if (timeOutE)
	{
		window.clearTimeout(timeOutE);
	}
	
	timeOutE = window.setTimeout("doFirstNameCheck()", 1750);
}


function startLastNameCheck(e) {
	if (timeOutF)
	{
		window.clearTimeout(timeOutF);
	}
	
	timeOutF = window.setTimeout("doLastNameCheck()", 1750);
}

function startAddress1Check(e) {
	if (timeOutG)
	{
		window.clearTimeout(timeOutG);
	}
	
	timeOutG = window.setTimeout("doAddress1Check()", 1750);
}

function startAddress2Check(e) {
	if (timeOutH)
	{
		window.clearTimeout(timeOutH);
	}
	
	timeOutH = window.setTimeout("doAddress2Check()", 1750);
}

function startAddress3Check(e) {
	if (timeOutI)
	{
		window.clearTimeout(timeOutI);
	}
	
	timeOutI = window.setTimeout("doAddress3Check()", 1750);
}

function startAddress4Check(e) {
	if (timeOutJ)
	{
		window.clearTimeout(timeOutJ);
	}
	
	timeOutJ = window.setTimeout("doAddress4Check()", 1750);
}

function startPostCodeCheck(e) {
	if (timeOutK)
	{
		window.clearTimeout(timeOutK);
	}
	
	timeOutK = window.setTimeout("doPostCodeCheck()", 1750);
}



function startPhoneCheck(e) {
	if (timeOutL)
	{
		window.clearTimeout(timeOutL);
	}
	
	timeOutL = window.setTimeout("doPhoneCheck()", 1750);
}

function startMobileCheck(e) {
	if (timeOutM)
	{
		window.clearTimeout(timeOutM);
	}
	
	timeOutM = window.setTimeout("doMobileCheck()", 1750);
}



function doUsernameCheck() {

	var checkText = $F('email');	
	
	if (checkText.length >= 3)
	{
		var url = '/signupinterface/';
		
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkusername&username=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});

	}

}

function doPasswordCheck() {

	var checkText = $F('password');	
	var checkText2 = $F('confirm');	
	
	if (checkText.length >= 3)
	{
		var url = '/signupinterface/';
		
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkpassword&password=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});

		if(checkText2.length >= 3 && timeOutD == null) {
			new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkpasswordconf&password=' + escape(checkText) + '&passwordconf=' + escape(checkText2), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
		}
	}

}

function doPasswordConfCheck() {

	var checkText = $F('confirm');	
	var checkText2 = $F('password');	

	if (checkText.length >= 3)
	{
		var url = '/signupinterface/';
		if(checkText2.length >= 3) {
			new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkpasswordconf&password=' + escape(checkText2) + '&passwordconf=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
			
		}
	}

}


function doEmailCheck() {
	var checkText = $F('email');	
	
	if (checkText.length >= 6)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkemail&email=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doFirstNameCheck() {
	var checkText = $F('firstname');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkfirstname&firstname=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doLastNameCheck() {
	var checkText = $F('lastname');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checklastname&lastname=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}


function doAddress1Check() {
	var checkText = $F('address1');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkaddress1&address1=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doAddress2Check() {
	var checkText = $F('address2');	
		
	var url = '/signupinterface/';
	new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkaddress2&address2=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	


}

function doAddress3Check() {
	var checkText = $F('address3');	
	if (checkText.length > 0)
	{	
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkaddress3&address3=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	}
}

function doAddress4Check() {
	var checkText = $F('address4');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkaddress4&address4=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doPostCodeCheck() {
	var checkText = $F('postcode');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkpostcode&postcode=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doPhoneCheck() {
	var checkText = $F('phone');	
	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkphone&phone=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}

function doMobileCheck() {
	var checkText = $F('mobile');	
	if (checkText.length > 0)
	{
		
		var url = '/signupinterface/';
		new Ajax.Request(url, {method:'post', postBody:'ajax_action=checkmobile&mobile=' + escape(checkText), onSuccess: updateCheck, onFailure: errFunc, on404: errFunc});
	
	}

}




function errFunc(t) {
	alert(t.responseText);
}

function clearElementAll(element) {
	var children = $A(element.childNodes);
	children.each(function(child) { 
			element.removeChild(child);
		
			} );

}

function handleButtonEnable() {
	//alert(passwordIsValid + ":" + passwordConfIsValid + ":" + usernameIsValid + ":" + emailIsValid);
	/*if(passwordIsValid && usernameIsValid && emailIsValid && passwordConfIsValid && firstNameIsValid && lastNameIsValid && addressIsValid && phoneIsValid) {
		var registerButton = $('sub_register');
		registerButton.disabled = false;
		registerButton.className = 'button';
		registerButton.name = 'sub_register';
		registerButton.value = "Make Your Account";
	}*/
}

function updateCheck(t) {
	
	eval('var rsp = ' + t.responseText + ';');
	
	var passwordValid = true;
	var passwordConfValid = true;
	var usernameValid = true;
	var emailValid = true;
	var firstNameValid = true;
	var lastNameValid = true;

	var addressValid = true;

	var phoneValid = true;

	switch(rsp[0]) {
		case "checkusername":
			var usernameExists = rsp[1];
			usernameValid = rsp[3];
			var usernameSearched = rsp[2];
			var errorField = $('userexists');
			if(!errorField) { return false; }
			var checkType = 0;
		break;

		case "checkpassword":
			passwordValid = rsp[1];
			var errorField = $('passwordinvalid');
			if(!errorField) { return false; }
			var checkType = 1;
		break;

		case "checkpasswordconf":
			passwordConfValid = rsp[1];
			var errorField = $('passwordconfinvalid');
			if(!errorField) { return false; }
			var checkType = 2;
		break;

		case "checkemail":
			emailValid = rsp[2];
			var emailSearched = rsp[1];
			var errorField = $('emailinvalid');
			if(!errorField) { return false; }
			var checkType = 3;
		break;

		case "checkfirstname":
			firstNameValid = rsp[2];
			var firstNameSearched = rsp[1];
			var errorField = $('firstnameinvalid');
			if(!errorField) { return false; }
			var checkType = 4;
		break;

		case "checklastname":
			lastNameValid = rsp[2];
			var lastNameSearched = rsp[1];
			var errorField = $('lastnameinvalid');
			if(!errorField) { return false; }
			var checkType = 5;
		break;

		case "checkaddress1":
			addressValid = rsp[2];
			var addressSearched = rsp[1];
			var errorField = $('address1invalid');
			if(!errorField) { return false; }
			var addressLine = "first";
			var checkType = 6;
		break;

		case "checkaddress2":
			addressValid = rsp[2];
			var addressSearched = rsp[1];
			var errorField = $('address2invalid');
			if(!errorField) { return false; }
			var addressLine = "second";
			var checkType = 6;
		break;

		case "checkaddress3":
			addressValid = rsp[2];
			var addressSearched = rsp[1];
			var errorField = $('address3invalid');
			if(!errorField) { return false; }
			var addressLine = "third";
			var checkType = 6;
		break;
		
		case "checkaddress4":
			addressValid = rsp[2];
			var addressSearched = rsp[1];
			var errorField = $('address4invalid');
			if(!errorField) { return false; }
			var addressLine = "City / Town";
			var checkType = 6;
		break;

		case "checkpostcode":
			addressValid = rsp[2];
			var addressSearched = rsp[1];
			var errorField = $('postcodeinvalid');
			if(!errorField) { return false; }
			var checkType = 7;
		break;

		case "checkphone":
			phoneValid = rsp[2];
			var phoneSearched = rsp[1];
			var errorField = $('phoneinvalid');
			if(!errorField) { return false; }
			var checkType = 8;
		break;

		case "checkmobile":
			phoneValid = rsp[2];
			var phoneSearched = rsp[1];
			var errorField = $('mobileinvalid');
			if(!errorField) { return false; }
			var checkType = 9;
		break;
	
	
		default:
		
		break;

	}
	

	var registerButton = $('sub_register');
	var visible = Element.visible(errorField);
	
	//Checktype = password
	if(checkType == 1) {
		if(!passwordValid) {
			passwordIsValid = false;

			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your password is invalid. It may only contain letters [ A-Z ] numbers [ 0-9 ], the following punctuation characters [ _ ! @ # $ % & ? ], and be between 6 and 12 characters in length.');
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checkb'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your password is invalid. It may only contain letters [ A-Z ] numbers [ 0-9 ], the following punctuation characters [ _ ! @ # $ % & ? ], and be between 6 and 12 characters in length.');
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checkb'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}

		} else {
			passwordIsValid = true;

			if(visible) {
				

				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checkb'}});
				
			}
			
			handleButtonEnable();
			
		}
	//Checktype = passwordconf
	} else if(checkType == 2) {
		if(!passwordConfValid) {
			passwordConfIsValid = false;

			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your Password and Password Confirmation do not match.');
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checkd'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your Password and Password Confirmation do not match.');
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checkd'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}

		} else {
			passwordConfIsValid = true;

			if(visible) {
				

				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checkd'}});
				
			}

			handleButtonEnable();
			
		}

	//Checktype = email
	} else if(checkType == 3) {
		
		if(!emailValid) {
			emailIsValid = false;

			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your email <strong>' + emailSearched + '</strong> is invalid. It must be in the form foo@bar.com and may only contain letters [ A-Z ], numbers [ 0-9 ], and the following punctuation characters [ _ @ . - ].');
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checkc'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your email <strong>' + emailSearched + '</strong> is invalid. It must be in the form foo@bar.com and may only contain letters [ A-Z ], numbers [ 0-9 ], and the following punctuation characters [ _ @ . - ].');
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checkc'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}

		} else {
			emailIsValid = true;
			
			if(visible) {
				
				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checkc'}});
				
			}

			handleButtonEnable();
			
		}

	//Checktype = firstname
	} else if(checkType == 4) {
		
		if(!firstNameValid) {
			
			firstNameIsValid = false;
		
			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your first name <strong>' + firstNameSearched + '</strong> is invalid. It may only contain letters [ A-Z ], and the following punctuation characters [ - \' ].');
				new Effect.BlindDown(errorField, {fps: 25, duration: 0.3, queue: {position: 'start', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your first name <strong>' + firstNameSearched + '</strong> is invalid.  It may only contain letters [ A-Z ], and the following punctuation characters [ - \' ].');
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}

		} else {

			firstNameIsValid = true;
			
			
			if(visible) {
				
				new Effect.BlindUp(errorField, {fps: 25, duration: 0.3, queue: {position: 'end', scope: 'checke'}});
				
			}

			handleButtonEnable();
			
		}
	//Checktype = lastname
	} else if(checkType == 5) {
		if(!lastNameValid) {
			lastNameIsValid = false;
			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your last name <strong>' + lastNameSearched + '</strong> is invalid. It may only contain letters [ A-Z ], and the following punctuation characters [ - \' ].');
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,'Your last name <strong>' + lastNameSearched + '</strong> is invalid.  It may only contain letters [ A-Z ], and the following punctuation characters [ - \' ].');
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}
		} else {
			lastNameIsValid = true;
			if(visible) {
				
				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checke'}});
				
			}
			handleButtonEnable();
		}

	//Checktype = username
	} else if(checkType == 0) {

		if(usernameValid) {
			if(usernameExists) {
				usernameIsValid = false;

				if(!visible) {
					clearElementAll(errorField);
					new Insertion.Top(errorField,'Sorry, the email address <strong>' + usernameSearched + '</strong> is already being used.');
					new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checka'}});
					registerButton.disabled = true;
					registerButton.className = 'disabledbutton';
				} else {
					clearElementAll(errorField);
					new Insertion.Top(errorField,'Sorry, the email address <strong>' + usernameSearched + '</strong> is already being used.');
					new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checka'}});
					registerButton.disabled = true;
					registerButton.className = 'disabledbutton';
				}
			} else {
				usernameIsValid = true;
				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checka'}});
			}

			handleButtonEnable();

		}
	//Checktype = address / postcode
	} else if(checkType == 6 || checkType == 7) {
		if(checkType == 6) {
			var addressLineMessage = 'The ' + addressLine + ' line of your address, <strong>' + addressSearched + '</strong> is invalid. It may only contain letters [ A-Z ], numbers [0-9], and the following punctuation characters [ - \' . ].';
		} else {
			var addressLineMessage = 'The postcode you entered, <strong>' + addressSearched + '</strong> is invalid. It may only contain UPPERCASE letters [ A-Z ], numbers [0-9], and spaces, in the form \'M14 5PR\'';
		}
		if(!addressValid) {
			addressIsValid = false;
			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField, addressLineMessage);
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,addressLineMessage);
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}
		} else {
			addressIsValid = true;
			if(visible) {
				
				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checke'}});
				
			}
			handleButtonEnable();
		}

	} else if(checkType == 8 || checkType == 9) {
		if(checkType == 8) {
			var phoneLineMessage = 'The landline number you entered, <strong>' + phoneSearched + '</strong> is invalid. It must be in the form \'01323 293374\'.';
		} else {
			var phoneLineMessage = 'The mobile number you entered, <strong>' + phoneSearched + '</strong> is invalid. It must be in the form \'079 12345678\' or \'07713 345678\'.';
		}
		if(!phoneValid) {
			phoneIsValid = (checkType == 8)? false : true;
			if(!visible) {
				clearElementAll(errorField);
				new Insertion.Top(errorField, phoneLineMessage);
				new Effect.BlindDown(errorField, {duration: 0.3, queue: {position: 'start', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			} else {
				clearElementAll(errorField);
				new Insertion.Top(errorField,phoneLineMessage);
				new Effect.Highlight(errorField, {queue: {position: 'end', scope: 'checke'}});
				//registerButton.disabled = true;
				//registerButton.className = 'disabledbutton';
			}
		} else {
			phoneIsValid = (checkType == 8)? false : true;
			if(visible) {
				
				new Effect.BlindUp(errorField, {duration: 0.3, queue: {position: 'end', scope: 'checke'}});
				
			}
			handleButtonEnable();
		}
	}
}



onloads.push(registerSignupEvents);
if (window.captureEvents) window.captureEvents(Event.LOAD);