/**
 * Copyright 2009 Capraro Technologies Inc.
 * @author Dan Klockowski
 */
Ext.onReady(function(){
 
	var ctLoginPanel = new Ext.form.FormPanel({
	    frame: true,
	    title: '',
	    labelAlign: 'right',
	    labelWidth: 65,
	    width: 340,
	    waitMsgTarget: true,
	
        // reusable eror reader class defined at the end of this file
        errorReader: new Ext.form.XmlErrorReader(),

	    items: [
	        new Ext.form.FieldSet({
	            title: 'Enter your name and password to login',
	            autoHeight: true,
	            defaultType: 'textfield',
	            items: [{
	                    fieldLabel: 'Name',
	                    name: 'username',
	                    width: 190
	                }, {
	                    fieldLabel: 'Password',
	                    name: 'password',
	                    width: 190,
	                    inputType: 'password'
	                }
	            ]
	        })
	    ]
	});

    var submit = ctLoginPanel.addButton({
        text: 'Login',
        disabled: false,
        handler: function(){
 			ctLoginPanel.getForm().submit({clientValidation: true,
			    url: '/ajax/login.php',
			    waitMsg: 'Attempting to log in...',
			    success: function(form, action) {
			       //Ext.Msg.alert("Success", action.result.msg);
			       location.href='/admin/homepage.php';
			    },
			    failure: function(form, action) {
			    	//alert("FAILURE TYPE: "+action.failureType);
			        switch (action.failureType) {
			            case Ext.form.Action.CLIENT_INVALID:
			                Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values");
			                break;
			            case Ext.form.Action.CONNECT_FAILURE:
			                Ext.Msg.alert("Failure", "Ajax communication failed");
			                break;
			            case Ext.form.Action.SERVER_INVALID:
			               Ext.Msg.alert("Login Failed", "Invalid username and/or password");
			       }
			    }
			});
        }
    });

    ctLoginPanel.render("ctLoginPanel");
    
	ctLoginPanel.on({
        actioncomplete: function(form, action){
            //if(action.type == 'load'){
                submit.enable();
            //}
        }
    });
});
