//javascript class that handles the login
function LoginForm(){
	var objSelf = this;
	
	//get a jquery reference to the form and the preloader div
	this.Form = $("#LoginForm");
	this.Preloader = $("#PreLoader");
	this.LoginError = $("#yellowBtn");
	
	//get the jquery ref to the fields in our contact form
	this.DOMReferences = {
		Password: this.Form.find( "input[name = 'password']" )
	};
	
	//focus the the login box
	this.DOMReferences.Password.focus();
	this.DOMReferences.Password.select();
	
	//if the loginbox has focus remove the error
	this.DOMReferences.Password.focus(function(){
		objSelf.LoginError.val('Login');
		objSelf.DOMReferences.Password.select();
	});
	
	
	//bind the submission event to the form submit event
	this.Form.submit(
		function(objEvent){
			//submit the form via AJAX
			objSelf.SubmitForm();
			
			//cancel default event.
			return( false );	
		}
	);
	
	//Submit form action
	LoginForm.prototype.SubmitForm = function(){
		var objSelf = this;
		
		//start the preloader
		this.LoginError.hide("fast");
		this.Preloader.show("fast");
		
		//Submit form via ajax
		$.ajax(
			{
				//send the request
				type:"get",
				url:"../com/eventusher/services/AccountServices.cfc",
				data:{
					method:"getAccountByPassword",
					password: this.DOMReferences.Password.val()
				},
				datatype: "json",
				
				//define request handlers
				success: function(evt){
					if (evt == 1) {
						objSelf.Preloader.hide("slow",function(){window.location ="offer.cfm"});
					}else{
						objSelf.LoginError.val("Invalid Password");
						objSelf.LoginError.show("fast");
					}
				},
				
				//fired when finished
				complete: function(){
					objSelf.Preloader.hide("fast");
					objSelf.LoginError.show("fast");
				},
				
				//error
				error: function(evt){
					alert("A connection error occured. Please try again later.")	
				}
			}
		);
	}
}

// ----------------------------------------------------- //
// ----------------------------------------------------- //
		 
		 
// When the document is ready, init contact form. It is important
// that we wait for the DOM loaded event to that we have access to
// the DOM elements.
$(
	function(){
		 
		// Create an instance of the contact form.
		var objLoginForm= new LoginForm(); 
	}
);
