UserLoginAjaxForm = Class.create();

UserLoginAjaxForm.prototype = {
	form   : null,
	initialize : function(form)
    {
        this.form = $(form);
        this.form.observe('submit', this.onSubmit.bindAsEventListener(this));        
    },
	showAlert : function(key, msg)
    {
		alert(msg);
		if($(key)){
			$(key).focus();
		}
		return false;
    },	
	onSubmit : function(e){
		try{
			Event.stop(e);
			var xmlHttpReq = Ajax.getTransport();	
			if(!xmlHttpReq) throw new Error('');
			this.form.disable();
			var email = pass = myDict = null;			
			email = $("txtEmail").getValue().strip();
			pass = $("txtPassword").getValue().strip();	
			
			if(email.empty()){
				this.form.enable();
				this.showAlert('txtEmail','Please enter a Email address.');				
				return false;
			}
			if(!MD.isEmail(email)){
				this.form.enable();
				$('txtEmail').value = '';				
				this.showAlert('txtEmail','The email address you entered is invalid.');				
				return false;
		    }
			if(pass.empty()){
				this.form.enable();
				this.showAlert('txtPassword','Please enter a Email address.');				
				return false;
			}			
			var myDict = {'action': 'LogIn','username':email,'password':pass,'login':1,'nocache': (new Date()).getTime()}
			var options = {
				postBody  : MD.encodeQueryComponents(myDict),
				method    : this.form.method,
				onSuccess : this.onFormSuccess.bind(this),
				on404     :function(){throw new Error('')},
				onFailure : function(){throw new Error('')}
			};	
			 new Ajax.Request(this.form.action, options);
		}catch(e){
			MD.location(this.form.action);	
			return false;
		}		
	},
	onFormSuccess : function(transport){
		try{
			this.form.enable();
			this.form.reset();
			var request = transport.responseText.evalJSON(true);
			if(transport.status==200&&request){
				if(request.success == 1){
					window.location.reload(true);
					return false;
				}
				this.showAlert('txtEmail',request.msg.strip());	
				return false;
			}
			throw new Error('');
		}catch(e){
			MD.location(this.form.action);
			return false;
		}		
	}
	
};