Ext.onReady(function(){

	var wndLogin = new Ext.Window({
		autoTabs:false,
        width:305,
        
        shadow:true,
        minWidth:250,
        minHeight:152,
        proxyDrag: false,
        resizable: true,
        collapsible: false,
        title: 'Login',
        keys: [{
        	key: 13,
        	handler: doLogin
        },{
        	key: 27,
        	handler: function(){
        		wndLogin.hide();
        	}
        }]
	});
	
	var showLogin = Ext.get('login_link');
    showLogin.on('click', function(){
    	wndLogin.show();
    	wndLogin.anchorTo(this,'tr-br');
    	txtEmail.focus();
    });
    
    var loginForm = new Ext.form.FormPanel({
    	labelWidth: 75, 
        url:'login.php',
        frame: true,
        bodyStyle:'padding:5px 5px 0, 5px',
        width: 305,
        defaults: {width: 230},
        defaultType: 'textfield'
    	
    });
    
    var txtEmail = loginForm.add({
    	fieldLabel: 'Email Address',
    	name: 'email_address',
    	vtype: 'email',
    	width: 200
    });
    
    var txtPwd = loginForm.add({
    	fieldLabel: 'Password',
    	name: 'pwd',
    	inputType: 'password',
    	width: 100
    });
        
    var forgotLink = loginForm.add({
		xtype: 'tbtext',
		text: 'Password is case-sensitive. <a href="lostpwd.html">Forgot Password?</a>'
	});
    
        
    wndLogin.add(loginForm);
    
    
    var btnLogin = wndLogin.addButton({
    	type: 'submit',
    	text: 'Log In',
    	handler: doLogin
    });
    
    var btnClose = wndLogin.addButton({
    	text: 'Close',
    	handler: function(){
    		wndLogin.hide();
    	}
    });
    
	
    var rpForm = new Ext.form.FormPanel({
    	labelWidth: 75, 
        url:'login.php',
        frame: true,
        bodyStyle:'padding:5px 5px 0, 5px',
        width: 305,
        defaults: {width: 230},
        defaultType: 'textfield',
        title: 'iDeliver Password Reset',
        bbar: [{
        	xtype: 'tbfill'
        },{
        	text: 'Reset Password'
        }],
        items: [{
        	fieldLabel: 'First Name',
        	width: 200
        },{
        	fieldLabel: 'Last Name',
        	width: 200
        }]
    });
    
    if (window.location.pathname == '/ideliver35/lostpwd.html'){
    	rpForm.render('rp_form');
    }
    
    function doLogin(){
    	var hash = hex_md5(txtPwd.getValue());
    	Ext.Ajax.request({
    		url: 'login.php',
    		params: { action: '1', email_address: txtEmail.getValue(), pwd: hash},
    		method: 'POST',
    		success: function (result, request) {
    			if (result.responseText != "app.html" && result.responseText != "home2.html"){
    				Ext.MessageBox.alert("Failure", result.responseText);
    			} else {
    				window.location = result.responseText;
    			}
    		},
    		failure: function (result, request){
    			Ext.MessageBox.alert("Failure", result.responseText);
    		}
    	});
    }
    
});
