Ext.onReady(function(){
    Ext.QuickTips.init();
    
    function loginForm(){
	login.getForm().submit({ 
	    method:'POST', 
	    waitTitle:'Connecting', 
	    waitMsg:'Sending data...',    
	    url:'/v7/login.php',    
	    success:function(){ 
		var redirect = '/v7/dashboard.html'; 
		window.location = redirect;
	    },
	    failure:function(form, action){ 
		if(action.failureType == 'server'){ 
		    obj = Ext.util.JSON.decode(action.response.responseText); 
		    Ext.Msg.alert('Login Failed!', 'Please try again or contact customer service at (323) 653-1900.'); 
		}else{ 
		    Ext.Msg.alert('Warning!', 'Login Failed.  Please try again or contact customer service at (323) 653-1900.'); 
		} 
		login.getForm().reset(); 
	    } 
	}); 
    }
 
	// Create a variable to hold our EXT Form Panel. 
	// Assign various config options as seen.	 
    var login = new Ext.FormPanel({ 
        labelWidth:80,
	id: 'loginForm',
        url:'/v7/login.html', 
        frame:true, 
        title:'WebClipping - Please Login', 
        width:230, 
        padding:200, 
        defaultType:'textfield',
	monitorValid:true,
	keys: [{
	    key: [10,13],
	    fn: loginForm
	}],  
 
	// Specific attributes for the text fields for username / password. 
	// The "name" attribute defines the name of variables sent to the server.
        items:[{ 
                fieldLabel:'Account #', 
                name:'aid',
		id: 'aidField',
                allowBlank:false 
            },{ 
                fieldLabel:'Password', 
                name:'pass',
		id: 'passField',
                inputType:'password', 
                allowBlank:false 
            }],
 
	// All the magic happens after the user clicks the button     
        buttons:[{ 
                text:'Login',
                formBind: true,	 
                handler:loginForm
            }] 
    });
 
 
	// This just creates a window to wrap the login form.  
	// The login object is passed to the items collection.       
    var win = new Ext.Window({
        layout:'fit',
        width:300,
        height:150,
        closable: false,
        resizable: false,
        plain: true,
        items: [login]
    });
    
    win.show();
    
    //pickup the AID if passed in URL request.  (Probably from e-mail login link)
    var paramAID = Ext.urlDecode(window.location.search.substr(1));
    if(paramAID['aid'] > 1){
	Ext.getCmp('aidField').setValue(paramAID['aid']);
	Ext.getCmp('passField').selectText;
    }
    

    
    function processResult(e){
	if(e == 'no'){
	    var redirect = '/v6/login.php'; 
	    window.location = redirect;
	}	
    }
});
