Ext.onReady(function(){
    
    Ext.QuickTips.init();
 
	// Create a variable to hold our EXT Form Panel. 
	// Assign various config options as seen.	 
    var login = new Ext.FormPanel({ 
        labelWidth:100,
        url:'bib_ajax_admin.php', 
        frame:true, 
        title:'Identification', 
        width:230, 
        padding:200, 
        defaultType:'textfield',
		monitorValid:true,
		onSubmit: Ext.emptyFn,
        submit: function() {
			// affichage de la boite 'connexion'
			Ext.MessageBox.show({
				msg: 'Identification en cours',
				progressText: 'Connexion...',
				width:300,
				wait:true,
				waitConfig: {interval:200}
			});
			// requete server !
			// on recupere l'id du formulaire
			form_id = login.getForm().getEl().id;
			
			new Ajax.Request('bib_ajax_admin.php', 
			{
				method:'post',
				parameters : $(form_id).serialize(true),
				onSuccess : function(transport,json)  { 
					Ext.MessageBox.hide();
					
					if (transport.responseText.length > 0) 
					{
						AjaxError(transport.responseText);
						return;
					}

					if (json.code == 0) // login ok
					{
						win_login.hide();
						Ext.MessageBox.show({
							msg: 'Authentification ok',
							progressText: 'Connexion au backoffice',
							width:300,
							wait:true,
							waitConfig: {interval:100}
						});
						window.location = json.redirect;
					}
					else
					{
						Ext.Msg.show({
							title:'Erreur',
							msg: 'Login incorrect',
							buttons: Ext.Msg.OK,
							icon: Ext.MessageBox.WARNING,
							fn : function() { login.getForm().reset(); }
						});
						
					}
				},
				onException : function(request,exception) { 
					Ext.MessageBox.hide();
					alert("exception " + exception); 
				},
				onComplete : function() {
					
				}
				
			});
        },
 
		// Attirubts des champs texte pour le login et le mot de passe
		// L'attribut 'name' définit le nom des variables envoyées au serveur
        items:[ new Ext.form.Hidden({	
				fieldLabel:'function', 
                name:'function', 
				hideLabel : true,
				value : 'login'
            }),{ 
				fieldLabel:'Login ', 
                name:'loginUsername', 
                allowBlank:false,
				blankText : 'Ce champ est obligatoire'
            },{ 
                fieldLabel:'Mot de passe ', 
                name:'loginPassword', 
                inputType:'password', 
                allowBlank:false,
				blankText : 'Ce champ est obligatoire'
            }],
 
		// Submit  
        buttons:[{ 
            text:'Login',
            formBind: true,	 
            // Function that fires when user clicks the button 
                handler:function(){ 
                    login.getForm().submit(); 
                } 
            }] 
    });
 
	// fenetre principale
	var win_login = new Ext.Window({

        layout:'fit',
        width:350,
		height:150,
        plain: true,
        closable:false,
		draggable:true,
		resizable:false,
		items : [login]
    });
    
	win_login.show();

});
