// JavaScript Document

Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'under';

    var fs = new Ext.FormPanel({
        frame: false,
        title:'',
        labelAlign: 'right',
        labelWidth: 150,
        width:500,
		 
        waitMsgTarget: true,

        // configure how to read the XML Data
        reader : new Ext.data.XmlReader({
            record : 'contact',
            success: '@success'
        }, [
            'txtusername' // custom data types
        ]),

        // reusable eror reader class defined at the end of this file
        errorReader: new Ext.form.XmlErrorReader(),

        items: [
            new Ext.form.FieldSet({
                title: 'Account Information',
                autoHeight: true,
                defaultType: 'textfield',
                items: [
					{
						inputType: 'hidden',
                        name: 'server_action',
						value: 'login_form'
                    },
						{
                        fieldLabel: 'Username',
                        id: 'username',
                        name: 'username',
						allowBlank:false,
						minLength: '4',
                        width:190
                    }, {
                        fieldLabel: 'Password',
                        name: 'password',
						id: 'password',
						inputType: 'password',
						allowBlank:false,
						minLength: '6',
                        width:190
                    },
					{
							xtype: 'tbtext',
							width: 400,
							text: '<a href="'+forgot_pass_url+'" style="padding-left: 160px; font-weight:bold;">Forgot Password?</a>'
							
						
					}
                ]
            })

			 
        ]
    });

    // simple button add
   // fs.addButton('Load', function(){
   //     fs.getForm().load({url:'xml-form.xml', waitMsg:'Loading'});
    //});

    // explicit add
    var submit = fs.addButton({
        text: 'Login',
        handler: function(){
            fs.getForm().submit({url: SITE_URL+'login/index/', waitMsg:'Please Wait...'});
        }
    });
	
    fs.render('form_login');

    fs.on({	
        actioncomplete: function(form, action){
            if(action.type == 'submit'){
                 Ext.MessageBox.show({
					   title: 'Please wait',
					   msg: 'Logging in...',
					   progressText: 'Initializing...',
					   width:300,
					   progress:true,
					   closable:false,
					   animEl: 'mb6'
				   });
			
				   // this hideous block creates the bogus progress
				   var f = function(v){
						return function(){
							if(v == 12){
								window.location=SITE_URL+"main/";
							}else{
								var i = v/11;
								Ext.MessageBox.updateProgress(i, Math.round(100*i)+'% completed');
							}
					   };
				   };
				   for(var i = 1; i < 13; i++){
					   setTimeout(f(i), i*200);
				   }
            }
        }
    });

});

// A reusable error reader class for XML forms
Ext.form.XmlErrorReader = function(){
    Ext.form.XmlErrorReader.superclass.constructor.call(this, {
            record : 'field',
            success: '@success'
        }, [
            'id', 'msg'
        ]
    );
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);