Ext.ns('xPanel');
xPanel.login_imod_Form = function(){
									  
		if(!Ext.get("div_login_fp"))
		{return "";}
		
		login_fp	=	new Ext.form.FormPanel({
			baseCls: 'x-plain'
			,id:			'login_fp'
			,layout:		'form'
			,buttonAlign:	'left'
			//,renderTo: 		'xl_registo_imod_divFormPanel'
			,defaults:		{width:200}
			,defaultType: 	'textfield'
			,labelWidth:120
			,items: [
					 {
								xtype:'textfield'
								,fieldLabel: login_imod_lang.email
								,name: 'email'
								,id: 'email'
								,minLength: 1
								,maxLength: 100
								,allowBlank:false
							},{
								xtype: 'checkboxgroup'
								,width: 200
								,layout: "absolute"
								,x:		500
								,y:		200
								,hideLabel:true
								,vertical: true
								,columns: 1
								,style:	"margin-top:-10px;margin-left:125px;"
								,items: [
									{name: 'aux_domain', 	id: 'aux_domain', 	boxLabel:login_imod_lang.aux_domain,		checked: true}
								]
							}
				 	,{
						fieldLabel:		login_imod_lang.password
						,name:			'pwd'
						,id:			'pwd'
						,inputType: 	'password'
						,allowBlank:	false
						,minLength: 	4
						,maxLength: 	14
					}
					,{
						xtype:			'radiogroup'
						,id:			'level'
						,hideLabel:		true
						//,anchor: 		'100%'
						,vertical:		false
						,allowBlank:	true
						,columns: [80,80]
						,items: [
								 {boxLabel: login_imod_lang.alunos,		name: 'level', inputValue: "aln"/*,checked: true*/}
								,{boxLabel: login_imod_lang.docentes,	name: 'level', inputValue: "dct"}
							]
					}
					,{
						xtype: 'checkboxgroup'
						,width: 900
						,hideLabel:true
						,vertical: true
						,columns: 1
						,style:	"margin-top:-10px;"
						,items: [
							{name: 'autologin', 	id: 'autologin', 	boxLabel:login_imod_lang.autologin,		checked: false}
						]
					}
				]
		});
		
		direct_fp	=	new Ext.form.FormPanel({
			baseCls: 'x-plain'
			,id:			'direct_fp'
			,layout:		'form'
			,buttonAlign:	'left'
			//,renderTo: 		'xl_registo_imod_divFormPanel'
			,defaults:		{width:200}
			,defaultType: 	'textfield'
			,labelWidth:100
			,items: [
				 	{
						xtype:'textfield'
						,fieldLabel: login_imod_lang.email2
						,name: 'direct_email'
						,id: 'direct_email'
						,minLength: 3
						,maxLength: 100
						,allowBlank:false
					}
					,{
						xtype:			'radiogroup'
						,id:			'direct_level'
						,hideLabel:		true
						//,anchor: 		'100%'
						,vertical:		false
						,allowBlank:	true
						,columns: [80,80]
						,items: [
								 {boxLabel: login_imod_lang.alunos,		name: 'direct_level', inputValue: "aln"/*,checked: true*/}
								,{boxLabel: login_imod_lang.docentes,	name: 'direct_level', inputValue: "dct"}
							]
					}
				]
		});
		
		
		
		
		
		var login_fp_bt_save= new Ext.Button({
					text: 		login_imod_lang.enviar
					,handler:	go_login
				});
		
		var direct_fp_bt_save= new Ext.Button({
					text: 		login_imod_lang.enviar2
					,handler:	go_mlogin
				});
		
		
		
		
		login_fp.render(Ext.get("div_login_fp_form"))
		direct_fp.render(Ext.get("div_direct_fp_form"))
		Ext.get("div_direct_fp").addClass("hide")
		
		login_fp_bt_save.render(Ext.get("div_login_bts"))
		direct_fp_bt_save.render(Ext.get("div_direct_bts"))
		
		if(typeof autoData != "undefined" && autoData.email && autoData.pwd)
		{	Ext.getCmp("autologin").setValue("on")

			Ext.getCmp("email").setValue(autoData.email);
			//Ext.getCmp("pwd").setValue(autoData.pwd);
		}


		Ext.get('link_recuperar').on({
			click:function(e,el){
				var target = e.getTarget('a');
				e.stopEvent();
				Ext.get("div_login_fp").removeClass("hide")
				Ext.get("div_login_fp").addClass("hide")
				Ext.get("div_direct_fp").addClass("show")
				//top.location	=	target.href;
			}
		});
		
		
		
		
		
		function go_login() {
			
			var frm	=	Ext.getCmp("login_fp").getForm();
			if(!frm.isValid()){
				Ext.MessageBox.alert("Erro", login_imod_lang.VER_CAMPOS)
				return;
			}
			//var values	=	rawEncode(frm.getValues());
			var values	=	frm.getValues();
			if(Ext.getCmp("captcha"))
			{
				values['captchaValue']	=	 Ext.getCmp("captcha").botAnswer
			}
			
			Ext.MessageBox.wait(login_imod_lang.msgwait);
			Ext.Ajax.request(
			{
				url: "/imod/login_imod",
				params:{
					a: "go_login"
					,c: "login_imod_action"
					,token: token
					,data: escape(Ext.util.JSON.encode(values))
				}
				,callback: function ( options, success, response ) {
					Ext.MessageBox.updateProgress(1);
					Ext.MessageBox.hide();
					if (success){
						data	=	Ext.util.JSON.decode(response.responseText);
						if(data.error)
						{
							Ext.MessageBox.alert("Erro", unescape(data.msg))
							
							if(data.captcha)
							{
								
								addCaptcha();
								//console.log(Ext.getCmp("captcha"))
							}
							
							return false;
						}else{
							if(values.autologin=="on")
							{
								//var today = new Date();
								//cookie	=	Ext.util.JSON.encode({email:values.email, pwd:mask(values.pwd)})
								//expires	=	today.getTime()+(60*60*24*12);
								//set_cookie("user", cookie, expires,"/")
							}else{
								//Delete_Cookie("user")
							}
							url	=	data.url?data.url:"/";
							
							Ext.get("page_root").mask("A carregar")
							document.location	=	url;
						}
					} else {
						Ext.MessageBox.alert("Erro", login_imod_lang.VERIFICAR_DADOS)
					}
				}
				
			});
		}
		
		
		
		function go_mlogin() {
			
			var frm	=	Ext.getCmp("direct_fp").getForm();
			if(!frm.isValid()){
				Ext.MessageBox.alert("Erro", login_imod_lang.VER_CAMPOS)
				return;
			}

			
			var values	=	frm.getValues();
			Ext.MessageBox.wait(login_imod_lang.msgwait);
			Ext.Ajax.request(
			{
				url: "/imod/login_imod",
				params:{
					action: "go_mlogin"
					,token: token
					,data: escape(Ext.util.JSON.encode(values))
				}
				,callback: function ( options, success, response ) {
					Ext.MessageBox.updateProgress(1);
					Ext.MessageBox.hide();
					if (success){
						data	=	Ext.util.JSON.decode(response.responseText);
						if(data.msg)
						{
							Ext.MessageBox.alert("Info", unescape(data.msg))
							frm.reset();
						}
					} else {
						Ext.MessageBox.alert("Erro", login_imod_lang.VERIFICAR_DADOS)
					}
				}
				
			});
		}
		
		
		function addCaptcha(){
			
			if(Ext.getCmp('captcha'))
			return;
			
			var f = Ext.getCmp('login_fp');
			f.add.apply(f, [
				{
					fieldLabel: login_imod_lang.captcha
					,name: 'captcha'
					,id: 'captcha'
					,xtype: 'botfield'
					,tooltip:{
						//tip:'To verify you are not a robot, please enter the value that satisfies this equation:',
						//width: 220
					}
				}
			]);
			
			f.doLayout();
		}
		

		function captcha()
		{
			Ext.get('div_login_fp').createChild({
				tag: 'center',
				cn: {
					tag: 'img',
					src: this.urls.system + 'code/'
				}
			});
		}
		
		
}

xPanel.funcs	=	xPanel.funcs?xPanel.funcs:{};
xPanel.funcs.login_imod_Form="wait"


