//Ext.SSL_SECURE_URL="public/images/window/s.gif";
//Ext.BLANK_IMAGE_URL="public/images/window/s.gif";
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = "side";
Ext.util.CSS.swapStyleSheet('theme', './ext-2.0/resources/css/xtheme-gray.css');
Login = function(){
	var dialog, form;
	
	return{
		Init:function(){
		
			var logoPanel = new Ext.Panel({
				baseCls: 'x-plain',
				bodyStyle: 'background:#f9f9f9 url(../images/replica-logo.jpg) no-repeat center center;',
		        region: 'center'
			});
			
			var email = new Ext.form.TextField(
			{
				name: "email",
				fieldLabel: "Email",
				width: 200,
				allowBlank: false,
				vtype: 'email'
			});

			var pwd = new Ext.form.TextField(
				{
					name: "pwd",
					fieldLabel: "Password",
					inputType: "password",
					width: 200,
					allowBlank: false
				}
			);
			
			var formPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'login'
		        },
		        bodyStyle: 'background:#f9f9f9 none; color:#222; padding:5px 35px;',
		        defaults: {
		        	width: 220
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 70,
		        items: [email,pwd],
		        //html : '<a href="http://localhost/replica/services.html">Forgot password?</a>',
				labelWidth:100,
		        region: 'south',
		        url: './class/checkvalidate.php'
		    });	
			
			form = formPanel.getForm();
			
			
			var btnLostPwd = new Ext.Button(
				{
		        	handler: function(){
		        		window.location = "lost_pwd2.php";
		        	},
		        	scope: Login,
		            text: 'Lost Password ? Click Here!',
					iconCls:'icon-lostpwd',
					tooltip : 'This button use to get new password!',
					tooltipType : 'qtip' 
		        }								
			);
			
			var btnReset = new Ext.Button(
				{
		        	handler: function(){
		        		form = formPanel.getForm();
						form.reset();
		        	},
		        	scope: Login,
		            text: 'Reset', 
					iconCls:'icon-reset'
		        }							  
			);
			
			var btnLogin = new Ext.Button(
				{
		        	handler: function(){
		        		if (formPanel.form.isValid()) {
						
							formPanel.form.submit({
								waitMsg:'Please Wait...',
								reset:false,
								success:Login.Success,
								failure: function(form, action) {
									Ext.MessageBox.alert('Error Message', action.result.msge);
								},
								scope:Login
							});
						}else{Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');}
		        	},
		        	scope: Login,
		            text: 'Login',
					iconCls:'icon-login'
		        }			
			);			
			
			
			
			email.on("specialkey", function(t,e){
				if(e.getKey() == Ext.EventObject.ENTER){
					pwd.focus();
					pwd.selectText();
				}
			});
			
			pwd.on("specialkey", function(t,e){
				if(e.getKey() == Ext.EventObject.ENTER){					
					//form.submit();					
					btnLogin.focus();
				}
			});
			
			form.on("actioncomplete", function(t,a) {
				//window.location = "menu.php";
				alert("masuk sini");
				Login.Success;
			});
			form.on("actionfailed", function(t,a) {
				//Ext.MessageBox.alert('Error Message', a.result.msge);
				Ext.MessageBox.alert("Login Failure!", a.result.msge, function(b){email.focus();email.selectText();});				
			});
				
					
			
		   dialog = new Ext.Window({
		        buttons: [btnLostPwd, btnReset, btnLogin],
		        buttonAlign: 'right',
		        closable: false,
		        draggable: true,
		        height: 220,
		        id: 'login-win',
		        layout: 'border',
		        minHeight: 220,
		        minWidth: 430,
		        plain: false,
		        resizable: true,
				modal: false,
		        items: [
		        	logoPanel,
		        	formPanel					
		        ],
				title: 'Login',
		        width: 430,
				resizable : false
		    });
			
			//form = formPanel.getForm();
			
		    dialog.show();
			if(msgalert.length > 0){
				Ext.MessageBox.alert('Warning!', msgalert);
			}
		},
		
		Success: function(f,a){
           
            if(a && a.result){
				dialog.destroy(true);
				if(a.result.admin == 't'){
					window.location = "./adminpanel.php";
				}else{
					window.location = "./main.php";
				}
			}
		}
	};
}();

Ext.BasicForm.prototype.afterAction=function(action, success){
	this.activeAction = null;
	var o = action.options;
	if(o.waitMsg){
		Ext.MessageBox.updateProgress(1);
		Ext.MessageBox.hide();
	}
	if(success){
		if(o.reset){
			this.reset();
		}
		Ext.callback(o.success, o.scope, [this, action]);
		this.fireEvent('actioncompleted', this, action);
	}else{
		Ext.callback(o.failure, o.scope, [this, action]);
		this.fireEvent('actionfailed', this, action);
	}
}


Ext.onReady(Login.Init, Login, true);