document.observe('dom:loaded', bindUI);
	
	function bindUI(){
		$('reg_form').observe('submit',function(e){CheckForm(e)});
	}
	
	function errormessage(text, element){
		if (!element){
			//If no element was sent then the element is the first input of the form
			element=$('reg_form').findFirstElement();
		}
		//setting the message box in the right place
		var position=element.cumulativeOffset();
		position[0]+=element.getWidth()+35;
		position[1]+=element.getHeight()-30;
		var styles ={
                left      : position[0] + 'px',
                top     : position[1] + 'px'
            };
		
		$('messagebox').setStyle(styles);
		$(element).addClassName('field_input_error');
		$(element).activate();
		$('error_message').innerHTML=text;
		
		$('messagebox').show();
	}
	
	
	
	function CheckForm(e, FormErr){
		if (e){
			Event.stop(e);
		}
		
		//setting all the errors to zero
		var inputs= $('reg_form').getInputs('text');
		for (var i=0, arr=inputs.size(); i<arr; i++){
			$(inputs[i]).removeClassName('field_input_error');
		}
		
		var i, FormError="";
		
		//Checking password and confirm as equal
		if ($F('confirm_password')!=$F('password')){
			FormError="PasswordNoMatch";
		}
		
		//Checking the confirm_password
		if ($F('confirm_password')==''){
			FormError="ConfirmPassword";
		}
		
		//Checking the password is at least 6 char
		if ($F('password').length<6){
			FormError="PasswordShort";
		}
		
		//Checking the password
		if ($F('password')==''){
			FormError="PasswordMissing";
		}
		
		//Checking the email can exist
		if(!validateEmail('email')){
				FormError='BadEmail';
		}
		
		//checking the email
		if ($F('email')==''){
			FormError="EmailMissing";
		}
		
		
		
		//checking last name
		if ($F('last_name')==''){
			FormError="last_name";
		}
		
		//checking first name
		if ($F('first_name')==''){
			FormError="first_name";
		}
		
		if (FormErr){
			FormError=FormErr;
		}
	
		if(FormError != "" ){
			switch (FormError){
				case "Missing":
					errormess="You did not fill out all the needed information."
					errormess+="<br>Please fill out an email, password, confirm the password and then submit."
					errormessage(errormess);
					break;
				case "EmailMissing":
					errormess="Please write in your email address."
					errormess+="<br>You will use this email to receive notifications and login to the system."
					errormessage(errormess, $('email'));
					break;
				case "BadEmail":
					errormess="Please write in an email address <b>that can exist</b><br>Something like timebuddy@timebuddy.com"
					errormess+="<br>You will use this email to receive notifications and login to the system."
					errormessage(errormess, $('email'));
					$('email').activate();
					break;
				case "PasswordMissing":
					errormess="Please select a password you will remember."
					errormess+="<br>You will use this password to login to the system."
					errormessage(errormess, $('password'));
					break;
				case "PasswordShort":
					errormess="The password must be at least 6 characters."
					errormess+="<br>You will use this password to login to the system."
					errormessage(errormess, $('password'));
					break;
				case "ConfirmPassword":
					errormess="Please confirm the password you selected."
					errormess+="<br>You will use this password to login to the system."
					errormessage(errormess, $('confirm_password'));
					break;
				case "PasswordNoMatch":
					errormess="The passwords do not match."
					errormess+="<br>You will use this password to login to the system."
					errormessage(errormess, $('confirm_password'));
					break;
				case "EmailExists":
					errormess="<b>There is such an email in the system.</b><Br><br>"
					errormess+=" If you have already registered to <b>any one of the groups / trips / programs</b> on this site, please "
					errormess+="<a href='login.asp'> login </a><br>"
					errormess+=" If not, please select a different email.<Br>"
					errormess+="Thank you<br>"
					errormessage(errormess);
					break;
				case "first_name":
					errormess="<b>Please write in your first name.</b><Br><br>"
					errormessage(errormess, $('first_name'));
					break;
				case "last_name":
					errormess="<b>Please write in your last name.</b><Br><br>"
					errormessage(errormess, $('last_name'));
					break;
				case "Error":
					errormess="<b>Sorry, there was an error.</b><Br><br>"
					errormess+=" Please try again. Thank you "
					errormessage(errormess);
					break;
			}
				
		}
		else{
			$('reg_form').submit();
		}
	}
	
	
	
