/**
 * Copyright(c) 2006-2009, FeyaSoft Inc..
 */
Ext.ns('feyaSoft.openSource');

feyaSoft.openSource.Login = function(config){

    var hideValue = new Ext.form.Hidden({
        id: 'spring-security-redirect',
        value: '/login/ajaxSuccess'
    });

    var username = new Ext.form.TextField({
        name: 'j_username',
        allowBlank: false,
        fieldLabel: 'Username',
        vtype:'alphanum',
        minLength: 2,
        blankText: 'Only alpha and numeric value allowed with 2 min length',
        anchor: '90%'
    });

    var password = new Ext.form.TextField({
        fieldLabel: 'Password',
        allowBlank: false,
        inputType: 'password',
        name: 'j_password',
        minLength: 2,
        anchor: '90%'
    });

    this.loginForm = new Ext.form.FormPanel({
        baseCls: 'x-plain',
        width: 450,
        frame: false,
		height: 70,
        region: 'south',
        defaultType: 'textfield',
        url: 'j_spring_security_check',
	items: [hideValue, username, password]
    });

    feyaSoft.openSource.Login.superclass.constructor.call(this, {
        title: 'Sign in to FeyaSoft with Account',
        width: 550,
        height: 150,
        minWidth: 400,
        minHeight: 100,
        layout: 'fit',
        bodyStyle:'padding:10px;',
        buttonAlign:'center',
        shim:false,
        animCollapse:false,
        constrainHeader:true,
        modal: true,
        items: [this.loginForm],
	resizable:false,

        keys: {
            key: [13],
            fn: this.loginFn,
            scope:this
        },

        buttons: [{
            text: 'Sign In',
            handler: this.loginFn,
            scope:this
        },{
            text: 'Register',
            scope: this,
            handler: function() {
                window.location = "signup";
                //new feyaSoft.main.selfRegister.RegisterWizard({path: ''});
                //window.location.href = "register?redirectPage=myCalendar";
            }
        },{
            text: 'Cancel',
            handler: function(){this.close();},
            scope:this
        }]
    });

    this.show();

};

Ext.extend(feyaSoft.openSource.Login, Ext.Window, {

    loginFn : function() {

        if (this.loginForm.form.isValid()) {

            this.loginForm.form.submit({
                waitMsg:'In processing',
                method: 'POST',
                failure: function(form, action) {
                    Ext.Msg.alert('Login Failed', 'Username or password are not correct - please try again');
                },
                // everything ok...
                success: function(form, action) {
                    if (action.result.success == 'true') {
                        Ext.Msg.alert('Login Success', 'You have succeed login system, please try your action again');
                        this.close();
                    } else {
                        Ext.Msg.alert('Login Failed', 'Username or password are not correct - please try again');
                    }
                },
                scope:this
            });
        } else {
                Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
        }
    }

});





