// JavaScript Document
function Login(el){
	
	this.el = el;
	//Load stylesheet
	this.stylesheet = new Asset.css('assets/css/login.css',{'id':'login','title':'login','onload':function(){}});
	//Generate HTML
	this.login = new Element('div',{'id':'login'});
	this.login_userName = new Element('div',{'id':'login_username'});
	this.login_password = new Element('div',{'id':'login_password'});
	this.login_form = new Element('form',{'id':'login_form','action':'index.cfm?action=sec.auth','method':'post'});
	this.login_userName_input = new Element('input',{'name':'user_email','id':'user_email','type':'text','value':'User Name'});
	this.login_password_inputText = new Element('input',{'name':'user_password','id':'user_password','type':'text','value':'Password'});
	this.login_password_inputPassword = new Element('input',{'name':'user_password','id':'user_password','type':'password','value':''});
	this.login_button = new Element('input',{'id':'login_button','type':'button','value':''});
	this.login_loader = new Element('div',{'id':'login_loader'});
	this.login_loaderImage = new Element('img',{'id':'loader_image','src':'assets/images/ajax-loader(3).gif'});
	this.login_loaderText = new Element('div',{'id':'loader_text'});
	this.login_loaderText.set('html','Authenticating, please stand by...');
	
	//Construct HTML
	
	this.login_userName_input.inject(this.login_userName);
	this.login_password_inputText.inject(this.login_password);	
	this.login_form.inject(this.login);
	this.login_userName.inject(this.login_form);
	this.login_password.inject(this.login_form);
	this.login_button.inject(this.login_form);
	this.login_loaderImage.inject(this.login_loader);
	this.login_loaderText.inject(this.login_loader);
	this.login.inject(this.el);
	
	//Submi Login Form
	this.login_button.addEvent('click',function(){
		this.login.set('html','');
		this.login.adopt(this.login_loader);
		var string = 'user_email='+escape(this.login_userName_input.getProperty('value'))+'&user_password='+escape(this.login_password_inputPassword.getProperty('value'));
		var date = new Date();
		var timestamp = date.getTime();
		var ajax = new Request({url:'index.cfm?action=sec.auth&time='+timestamp,evalResponse:true}).send(string);
		ajax.addEvent('onComplete',function(data){
			if(isError){
				this.login.set('html','');
				this.login_form.inject(this.login);
				if(this.login.hasChild() == false){
					this.login_userName_input.inject(this.login_userName);
					this.login_userName.inject(this.login_form);
					this.login_password.set('html','');
					this.login_password_inputText.inject(this.login_password);
					this.login_password.inject(this.login_form);
					this.login_button.inject(this.login_form);
				}
			} 
			else {
					window.location = "index.cfm?action=vs.home";
			}
		}.bind(this));
	}.bind(this));
	this.login_userName.addEvent('click',function(){
		this.login_userName_input.select();
	}.bind(this));
	this.login_password_inputText.addEvent('focus',function(){
		this.login_password.set('html','');
		if(this.login_password.hasChild() == false){
			this.login_password_inputPassword.inject(this.login_password);
			this.login_password_inputPassword.focus();
		}
	}.bind(this));
	this.login_password_inputPassword.addEvent('keydown',function(e){
		if(e.key == "enter")
			this.login_button.fireEvent('click');
	}.bind(this));
	
}