Ext.onReady(function(){
	var unfea_site_url		= $('#UNFEA_SITE_URL').val();
	if (unfea_site_url != ''){
		var autoconnect = $('#autoconnect').val();

		if (autoconnect == 1) {
			var site_login_script	= document.createElement('script');
			site_login_script.setAttribute("type", "text/javascript");
	        site_login_script.setAttribute("src", unfea_site_url + '/unfea/authentification');
	        site_login_script.onload = function (){
	        	// l'utilisateur est identifié sur le site

	        	if (autoconnect_id != '' && autoconnect_id.length == 32) {
	        		/* écrit le cookie autoconnect_id et recharche */
	        		var expDate = new Date();
					expDate.setTime(expDate.getTime() + (365 * 24 * 3600 * 1000))
					document.cookie = "autoconnect_id=" + escape(autoconnect_id) + ";expires=" + expDate.toGMTString();

					// recharge pour identification par l'autoconnect id
					if ($('#ROOT_URL').val() != ''){
						window.location.href = $('#ROOT_URL').val() + '?autoconnect=0';
					}
	        	}
	        }
	        document.getElementsByTagName("head")[0].appendChild(site_login_script);
		}
	}

	LoginEmail = new Ext.form.TextField({
		fieldLabel: 'Email',
        allowBlank: false,
        maxLength: 100
	});

	LoginPassword = new Ext.form.TextField({
		fieldLabel: 'Mot de passe',
        allowBlank: false,
        inputType: 'password',
        maxLength: 50
	});

	LoginPanel = new Ext.FormPanel({
       labelAlign: 'left',
       bodyStyle: 'padding: 5px',
       width: '250',
       height: 100,
       renderTo: 'contenu-page',
       items: [{
         layout: 'form',
         border: false,
         items: [LoginEmail, LoginPassword],
         buttons: [{
         	text: 'Se connecter',
         	handler: DoLogin
         	}]
         }]
	});
	LoginPanel.show();
});

function DoLogin(){
	Ext.Ajax.request({
      waitMsg: 'Please wait...',
      url: '/ajax/dologin/',
      params: {
         task: "create",
         mail: LoginEmail.getValue(),
         password: LoginPassword.getValue()
      },
      success: function(response){
         var result=eval(response.responseText);
         var int_result = parseInt(result);
         if (int_result > 0) {
         	window.location.href = $('#ROOT_URL').val() + '/stock';
         } else {
         	str = '';
         	if (result.length > 0) {
				for (i = 0; i < result.length; i++) {
					str += result[i] + "<br />";
				}
         	} else {
         		str = "Erreur d\'identification : \n";
         	}
            Ext.MessageBox.alert('Erreur d\'identification', str);
         }
      },
      failure: function(response){
         /*var result=response.responseText;
         Ext.MessageBox.alert('error','could not connect to the database. retry later');*/
      }
   });
}

function LoginUtilisateurSite(){

}
