lang.login = {
	'es':{'cancel':"Cancelar",
		  'login':"Login",
		  'reset_form':"Limpiar",
		  'auth':"Autentificando contraseña",
		  'init':"Inicializando login",
		  'send':"Mandando información",
		  'sendingmail':"Busca para account (es)",
		  'mailsend':"Email has been sent (es).",
		  'invaliduser':"Invalid User"
		  },
	'en':{'cancel':"Cancel",
		  'reset_form':"Reset",
		  'login':"Login",
		  'auth':"Authenticating password",
		  'init':"Initializing login",
		  'send':"Sending information",
		  'sendingmail':"Searching for account",
		  'mailsend':"Account data has been sent.",
		  'invaliduser':"Invalid User"
		  }
};

login_pane = {};


LoginPane = Class.create();
LoginPane.prototype = {
	
	initialize: function(options) {
		this.id = 'login_win';
		this.self = $(this.id);
		//this.self.onkeyup = this.keyupHandler.bindAsEventListener(this);
		
		//this.form = $('login_form');
		this.wrapper = $('login_form_wrapper');
		
		this.btn = {};
		this.btn.cancel = {};
		this.btn.login = {};
		
		this.options = options || {};
		
		updaters[this.id+'_update'] = new Updater(this.id + '_update', '/user', '', {parent:this});
		this.updater = updaters[this.id+'_update'];
		ax_forms[this.id+'_form'] = new AxForm(this.id+'_form', {app:'/user', 
																 updater:this.id+'_update', 
																 service:'user.login', 
																 key:this.options.key, 
																 use_return:true, 
																 state:'open', 
																 no_hide:true});
		this.form = ax_forms[this.id+'_form'];
		
		this.status = this.updater.status;
		this.forgot_cnt;
		
		this.state = 0;
		
		this.injectBehaviours();
	
	},
	
	injectBehaviours: function() {
		this.btn.cancel = new ButtonComponent(this, 'login_btn_cancel', 'cancel', lang.login[cur_lang].cancel);
		this.btn.login = new ButtonComponent(this, 'login_btn_login', 'go', lang.login[cur_lang].login);
		this.btn.cancel.setAction(this.cancelHandler);
		this.btn.login.setAction(this.loginHandler);
	},
	
	cancelHandler: function() {
		if (this.options.overlay) {
			screen_overlay.hide();
		} else {
			this.form.reset();
		}
	},
	
	loginHandler: function() {
		var orig = this.encryptForm();
		this.status.show(lang.login[cur_lang].auth);
		this.attemptLogin();
		this.revertForm(orig);
		
	},
	
	logout: function() {
		document.location = '/?logout=1';	
	},
	
	forgotHandler: function() {
		this.status.show(lang.login[cur_lang].sendingmail);
		username = $('remind_user').value;
		var callParams = [];
		callParams.push( this.id + '_request');
		callParams.push('id=' + this.id);
		callParams.push('user=' + username);
		callParams.push('cmd=PasswordReminder');		
		this.callRicoAjaxEngine(callParams);				
		
	},
	
	revertForm: function(orig) {
		for(var i=0; i<orig.length; ++i) {
			var ob = orig[i];
			ob.self.value = ob.val;
		}
	},
	
	encryptForm: function() {
		var elmts = this.form.elements;
		var original = [];
		for(var i=0; i<elmts.length; i++) {
			var e = elmts[i];
			if (e.getAttribute('encrypt') && e.value) {
				var ob = {self:e, val:e.value};
				original.push(ob);
				var res = encodeStr(this.options.key, e.value);
				e.value = res.join('|');
			}
		}
		return original;
	},	
	
	attemptLogin: function() {
		this.status.show(lang.login[cur_lang].send);
		var callParams = [];
		this.extractForm(this.form, callParams);
		this.updater.callService('user.login', callParams);
	},
	
	ajaxUpdate: function(ajaxResponse) {
		this.status.hide();
		this.updater.update(ajaxResponse);
		/*
		if (this.options.overlay) screen_overlay.status.hide();
		switch(ajaxResponse.getAttribute('id')) {
			case this.id+'_key': this.setKey(ajaxResponse); break;
			case this.id+'_result': this.handleResult(ajaxResponse); break;
			case this.id+'_remindresult': this.mailSend(ajaxResponse.getElementsByTagName('success')[0]); break;
			case this.id+'_logout': window.top.location = '/home'; break;
		}*/
	},
	
	setKey: function(ajaxResponse) {
		var key = ajaxResponse.getElementsByTagName('public_key')[0];
		this.key = RicoUtil.getContentAsString(key);
		if (console) console.log('Setting Key to : '+this.key);
		this.loadForm(ajaxResponse.getElementsByTagName('loginform')[0]);
		this.self.style.display = '';
		if (this.options.overlay) {
			this.centerPane();
		}
		
		this.self.style.display = 'none';
		this.self.style.visibility = '';
		var effect = new Effect.Appear(this.self, 
						   { duration: 0.25,
						   transition: Effect.Transitions.linear, 
						   from: 0.0, to: 1.0
						   });
		
		this.forgotpasswd = ajaxResponse.getElementsByTagName('forgotpasswd')[0];
		
	},
	
	loadForm: function(form_html) {
		//reset all Buttons
		this.btn.cancel = new ButtonComponent(this, 'login_btn_cancel', 'cancel', lang.login[cur_lang].cancel);
		this.btn.login = new ButtonComponent(this, 'login_btn_login', 'go', lang.login[cur_lang].login);
		this.btn.cancel.setAction(this.cancelHandler);
		this.btn.login.setAction(this.loginHandler);
		
		this.state = 0;
		if (form_html) {
			$('login_form_wrapper').innerHTML = RicoUtil.getContentAsString(form_html);
			if (console) console.log(this.form.innerHTML);
		}
	},	
	
	mailSend: function(msg) {
		var success = RicoUtil.getContentAsString(msg);
		if (success==1) {				
			this.status.show(lang.login[cur_lang].mailsend);
			this.timeoutID = setTimeout(this.show.bind(this), 2000);		 				
		} else {			
			this.status.show(lang.login[cur_lang].invaliduser);
			this.forgot_cnt++;
			this.timeoutID = setTimeout(this.showForgotPassword.bind(this), 2000);		 											
		}	
	},
									
	saveResults: function(result, msg) {
		
		if (result == 'error') {
			this.status.show(msg, result);	
		} else {
			self.location = msg;	
		}
	},
	
	show: function() {	
		this.status.hide();
		screen_overlay.show();
		screen_overlay.parent = this;
		this.centerPane();
		Element.show(this.id);
	},
		
	hide: function() {
		this.self.style.display = 'none';
	},
		
		
	centerPane: function() {
		
		this.self.style.visibility = 'hidden';
		Element.show(this.id);
		
		var w = this.self.offsetWidth;
		var h = this.self.offsetHeight;
		
		//var top = screen_overlay.arrayPageScroll[1] + 100;
		
		var pageScroll = ApexUtil.getPageScroll();
		
		var left = ((screen_overlay.arrayPageSize[2] - w) / 2);
		var top = ((screen_overlay.arrayPageSize[3] - h) / 2);

		if (console) console.log('page coords: width:'+w+', height:'+h);
		
		this.self.style.top = (top < 0) ? "0px" : (pageScroll[1]+top) + "px";
		this.self.style.left = (top < 0) ? "0px" : left + "px";
		
		Element.hide(this.id);
		this.self.style.visibility = '';
		
	},
	
	
	evalScripts: function(div) {
		var scripts = div.getElementsByTagName('script');
		for(var i=0; i<scripts.length; ++i) {
			var script = scripts[i];
			if (script.getAttribute('type') == 'text/javascript') {
				var code = RicoUtil.getContentAsString(script);
				if (console) console.log(code);
				eval(code);
			}
		}
		
	},
	
	extractForm: function(form, params) {
		var elmts = form.elements;
		for(var i=0; i<elmts.length; i++) {
			var e = elmts[i];
			if (console) console.log(e.type);
			switch(e.type) {
				case 'checkbox':
					if (e.checked) {
						params.push(e.name +'=1');	
					} else {
						params.push(e.name +'=0');	
					}
					break;
				case 'text':
					if (e.value) {
						if (e.getAttribute('encrypt') == 1) {
							var res = encodeStr(this.options.key, e.value)
							params.push(e.name +'='+res.join('|'));
						} else {
							params.push(e.name +'='+e.value);
						}
						
					}
					break;
				case 'password':
					if (e.value) {
						if (e.getAttribute('encrypt') == 1) {
							var res = encodeStr(this.options.key, e.value)
							params.push(e.name +'='+res.join('|'));
						} else {
							params.push(e.name +'='+e.value);
						}
						
					}
					break;
			}
		}
	},
	
	keyupHandler: function(e) {		
		if (e.keyCode == 13) {
			if (this.state==1) {
				this.forgotHandler();
			} else {
				this.loginHandler();
			}
		}
	},
	
	showForgotPassword: function() {		
		this.status.hide();
		if (this.forgot_cnt>2) {
			this.show();
		} else {
			this.state = 1;
			$('login_form_wrapper').innerHTML = RicoUtil.getContentAsString(this.forgotpasswd);		
			this.btn.login = new ButtonComponent(this, 'login_btn_login', '/view/theme/'+theme+'/media/shared/icon_ok.gif', 'Send me my password');
			this.btn.login.setAction(this.forgotHandler);
			this.btn.login.enable();		
		}
	},
	
	resize: function() {
		this.centerPane();
		Element.show(this.id);
	}
	
	
	
};


