document.observe('dom:loaded', bindUI);
	
	function bindUI(){
		$('login_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=$('login_form').findFirstElement();
		}
		//setting the message box in the right place
		var position=element.cumulativeOffset();
		position[0]+=element.getWidth()+30;
		position[1]+=element.getHeight()-25;
		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){
		Event.stop(e);
		
		//setting all the errors to zero
		var inputs= $('login_form').getInputs('text');
		for (var i=0, arr=inputs.size(); i<arr; i++){
			$(inputs[i]).removeClassName('field_input_error');
			
		}
		
		var i, FormError="";
			
			
		//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";
		}
		
		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 and then submit.";
					errormessage(errormess);
					break;
				case "EmailMissing":
					errormess="Please write in your email address.";
					errormessage(errormess, $('email'));
					break;
				case "BadEmail":
					errormess="Please write in an email address <b>that can exist</b><br>Something like contactus@israelexperience.org.il";
					errormess+="<br>You will use this email to receive notifications and login to the system.";
					errormessage(errormess, $('email'));
					break;
				case "PasswordMissing":
					errormess="Please write in a password you will remember.";
					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 "NoUser":
					errormess="No registered user was found with the email and password your specified.";
					errormess+="<br>Please try again.";
					errormess+="<br>---------- OR -----------";
					errormess+="<br>Maybe you have <a href='register.asp'>not registered</a> yet?";
					errormessage(errormess);
					break;
				case "Error":
					errormess="<b>Sorry, there was an error.</b><Br><br>"
					errormess+=" Please try again. Thank you "
					errormessage(errormess);
					break;
			}
				
		}
		else{
			$('login_form').submit();
		}
	}
