  // ----------------------------------------------------------------
  // IMPORTANT NOTE : all the scripts below need a variable 'lang'
  // that contains the user current language.
  // ----------------------------------------------------------------

  // Team Membership - Team manager
  var TEAM_MEMBERSHIP_TEAM_MANAGER = 'teamManager';

  // Team Membership - Crew Manager
  var TEAM_MEMBERSHIP_CREW_MANAGER = 'crewManager';

  // Team Membership - Crew Member
  var TEAM_MEMBERSHIP_CREW_MEMBER = 'crewMember';

  // Team Membership - Coach
  var TEAM_MEMBERSHIP_COACH = 'coach';

  // Team Membership - Replacement
  var TEAM_MEMBERSHIP_SUBSTITUTE = 'substitute';

  // Team Membership - Guest
  var TEAM_MEMBERSHIP_GUEST = 'guest';

  // ----------------------------------------------------------------
  // LOGIN ON USER ACCOUNT
  // ----------------------------------------------------------------
  // Login on the user account represented by
  // the form name is 'userAccountLoginForm'
  function loginOnUserAccount() {

    // Form validation
    var myForm = document.userAccountLoginForm;
    resetFormErrors(myForm);

    // we start with mandatory fields
    checkMandatoryTextField(myForm.login);
    checkMandatoryTextField(myForm.password);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    // Validation went OK we continue
    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("loginOnUserAccountCallback()", 1000);
  }

  // Same method as above but without the popup
  function loginOnUserAccountCallback() {

    // We create the return function
    var ajaxReturnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // hide the 'save done' message after one second
        setTimeout("window.location.href='index.php?xref=page.main'", 500);
    }

    // we post the login request
    ajax_GET('index.php', 'xref=command.login&lang='+document.userAccountLoginForm.lang.value
                           +'&login='+document.userAccountLoginForm.login.value
                           +'&password='+hex_md5(document.userAccountLoginForm.password.value), ajaxReturnFunction, lang);

  }

  // ----------------------------------------------------------------
  // SWITCH ON TEAM FOR A USER ACCOUNT
  // ----------------------------------------------------------------

  // Switch the current team displayed
  // @param teamId teamId
  function switchTeamForUserAccount(teamId) {

    // Validation went OK we continue
    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("switchTeamForUserAccountCallback("+teamId+")", 1000);
  }

  // Same method as above but without the popup
  function switchTeamForUserAccountCallback(teamId) {

    // We create the return function
    var ajaxReturnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // hide the 'save done' message after one second
        setTimeout("window.location.href='index.php?xref=page.main'", 500);
    }

    // we post the switch request
    ajax_GET('index.php', 'xref=command.team.switch&idTeam='+teamId, ajaxReturnFunction, lang);
  }

  // ----------------------------------------------------------------
  // SHOW USER ACCOUNT
  // ----------------------------------------------------------------

  // Show a user account detail in the main content area
  // @param userAccountId id of the user account
  function showUserAccount(userAccountId) {
    if(lastMenuItemIndex>=4) {
      // no place to show the detail !
      return;
    }

    selectMainMenuItem(lastMenuItemIndex+1, 'index.php', 'xref=command.user&userAccountId='+userAccountId, 'Compte')
  }

  // ----------------------------------------------------------------
  // REMOVE USER PHOTO
  // ----------------------------------------------------------------

  var currentUserAccountIdForRemove;

  // Removes the user photo
  // @param userAccountId user account id
  function removeUserPhoto(userAccountId) {
     currentUserAccountIdForRemove = userAccountId;

      if(lang=='FR') {
         dialog_confirm("Voulez-vous vraiment supprimer cette photo ?", removeUserPhotoCallback);
      }
      else {
         dialog_confirm("Are you sure you want to remove this photo ?", removeUserPhotoCallback);
      }
  }

  // Direct removal of the user photo
  function removeUserPhotoCallback(returnValue) {
     if(!returnValue || returnValue==false) {
        return;
     }

     // Send an AJAX request to the server
     var ajaxReturnFunction = function(responseText) {
         showUserAccount(currentUserAccountIdForRemove);
     }

     ajax_GET('index.php', 'xref=command.user.photo.delete&userAccountId='+currentUserAccountIdForRemove, ajaxReturnFunction, lang);
  }

  // ----------------------------------------------------------------
  // USER EDITION/SAVE/REMOVE
  // ----------------------------------------------------------------

  // Edit a user in the main content area
  // @param userId id of the event representing the user
  function editUserAccount(userAccountId) {
    if(lastMenuItemIndex>=4) {
      // no place to show the detail !
      return;
    }

    selectMainMenuItem(lastMenuItemIndex+1, 'index.php', "xref=command.user.edit&userAccountId="+userAccountId, 'Edition');
  }

  // Saves the currently edited userAccount
  // the form name is 'userAccountEditForm'
  function saveUserAccount() {

    // Form validation
    var myForm = document.userAccountEditForm;
    resetFormErrors(myForm);

    // we start with mandatory fields
    checkMandatoryTextField(myForm.firstname);
    checkMandatoryTextField(myForm.lastname);
    checkMandatoryTextField(myForm.nickname);

    checkMandatoryTextField(myForm.weight);
    checkMandatoryTextField(myForm.birthyear);

    if(myForm.password.value!='' || myForm.password2.value!='') {
       checkMandatoryTextField(myForm.password);
       checkMandatoryTextField(myForm.password2);
    }

    checkMandatoryTextField(myForm.town);
    checkMandatoryTextField(myForm.mail);
    checkMandatoryTextField(myForm.year);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    // format validation
    checkTextareaField(myForm.sports,200);
    checkIntegerField(myForm.weight);
    checkIntegerField(myForm.birthyear);
    checkIntegerField(myForm.year);
    checkEmailField(myForm.mail);
    checkPasswordFields(myForm.password, myForm.password2);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("saveUserAccountCallback()", 1000);
  }

  // Save method as above but without the popup
  function saveUserAccountCallback() {

    // We create the return function
    var returnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // display a 'save done' message
        if(lang=='FR') {
           dialog_alert("Sauvegarde effectu&eacute;e.");
        }
        else {
           dialog_alert("Save done.");
        }

        // hide the 'save done' message after one second
        setTimeout("dialog_hidePopUp(null)", 1000);
    }

    // ajax_POST
    ajax_POST("index.php", document.userAccountEditForm, returnFunction, lang);
  }


  // Creates the user account from its form
  // the form name is 'userAccountCreationForm'
  function createUserAccount() {

    // Form validation
    var myForm = document.userAccountCreationForm;
    resetFormErrors(myForm);

    // we start with mandatory fields
    checkMandatoryTextField(myForm.firstname);
    checkMandatoryTextField(myForm.lastname);
    checkMandatoryTextField(myForm.nickname);
    checkMandatoryTextField(myForm.weight);
    checkMandatoryTextField(myForm.birthyear);
    checkMandatoryTextField(myForm.town);
    checkMandatoryTextField(myForm.mail);
    checkMandatoryTextField(myForm.year);
    checkMandatoryTextField(myForm.login);
    checkMandatoryTextField(myForm.password);
    checkMandatoryTextField(myForm.password2);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    // format validation
    checkTextareaField(myForm.sports, 200);
    checkIntegerField(myForm.weight);
    checkIntegerField(myForm.birthyear);
    checkIntegerField(myForm.year);
    checkEmailField(myForm.mail);
    checkLoginField(myForm.login);
    checkPasswordFields(myForm.password, myForm.password2);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    // Validation went OK we continue
    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("createUserAccountCallback()", 1000);
  }

  // Same method as above but without the popup
  function createUserAccountCallback() {

    // We create the return function
    var returnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // display a 'save done' message
        if(lang=='FR') {
           dialog_alert("Cr&eacute;ation effectu&eacute;e.");
        }
        else {
           dialog_alert("Creation done.");
        }

        // hide the 'save done' message after one second
        setTimeout("dialog_hidePopUp(null);", 1000);
        setTimeout("window.location.href='index.php?xref=page.user.add.done&lang="+lang+"&login="+escape(document.userAccountCreationForm.login.value)+"';", 500);
    }

    // ajax_POST
    ajax_POST("index.php", document.userAccountCreationForm, returnFunction, lang);
  }


  // ----------------------------------------------------------------
  // TEAM MEMBERSHIP CREATION/REMOVE
  // ----------------------------------------------------------------

  // Currently selected team membership
  var currentTeamMembershipType;

  // Currently selected crew id or team id depending on membership
  var currentCrewTeamId;

  // Add a team member from the given type
  // @param teamMembershipType the type of membership
  // @param crewTeamId current crew id or the team id it all depends on membership type
  function addTeamMembership(teamMembershipType, crewTeamId) {

    // We save the selected team membership
    currentTeamMembershipType = teamMembershipType;
    currentCrewTeamId = crewTeamId;

    // UserId Prompt
    var msgPrompt;
    if(lang=='FR') {
       if(currentTeamMembershipType==TEAM_MEMBERSHIP_TEAM_MANAGER) {
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme gestionnaire d\'&eacute;quipe :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MANAGER) {
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme gestionnaire d\'&eacute;quipage :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MEMBER) {
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme membre :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_COACH) {
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme coach :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_SUBSTITUTE) {
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme rempla&ccedil;ant :';
       }
       else {
         // default is 'guest'
         currentTeamMembershipType=TEAM_MEMBERSHIP_GUEST;
         msgPrompt = 'Veuillez entrer l\'identifiant de l\'utilisateur &agrave; ajouter comme invit&eacute; :';
       }
    }
    else {
       if(currentTeamMembershipType==TEAM_MEMBERSHIP_TEAM_MANAGER) {
         msgPrompt = 'Please enter the identifier of the user you want to add as team manager :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MANAGER) {
         msgPrompt = 'Please enter the identifier of the user you want to add as crew manager :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MEMBER) {
         msgPrompt = 'Please enter the identifier of the user you want to add as crew member :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_COACH) {
         msgPrompt = 'Please enter the identifier of the user you want to add as coach :';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_SUBSTITUTE) {
         msgPrompt = 'Please enter the identifier of the user you want to add as substitute :';
       }
       else {
         // default is 'guest'
         currentTeamMembershipType=TEAM_MEMBERSHIP_GUEST;
         msgPrompt = 'Please enter the identifier of the user you want to add as a guest :';
       }
    }

    dialog_prompt(msgPrompt, addTeamMembershipValidate, '', 320, 120);
  }

  // The login of the current user we are adding
  var currentUserIdToAdd;

  // Validate function for the "Add a team member" pop-up
  function addTeamMembershipValidate(userId) {
      if(userId==null || userId=='') {
         return; // nothing to do
      }

      currentUserIdToAdd=userId;

       // we create a return function to handle the response text
       var ajaxReturnFunction = function(responseText) {
           window.defaultStatus = responseText;
           
           // refresh the team section
           selectMainMenuItem(1,'index.php', 'xref=command.team', null, executeScriptFromHTML);
           
           // and display an info message
           if(lang=='FR') {
              dialog_alert("L'utilisateur '"+currentUserIdToAdd+"' a &eacute;t&eacute; ajout&eacute;.");
           }
           else {
              dialog_alert("The user '"+currentUserIdToAdd+"' has been added.");
           }
       }

       // we save the action on the server side
       ajax_GET("index.php", "xref=command.team.add.membership&teamMembershipType="+currentTeamMembershipType+"&userId="+userId+"&crewTeamId="+currentCrewTeamId, ajaxReturnFunction, lang);
  }

  // User id of the user to remove
  var currentUserIdToRemove;
  var currentCrewTeamId;

  // Remove a team membership from the given type
  // @param teamMembershipType the type of membership
  function removeTeamMembership(teamMembershipType, userIdToRemove, crewTeamId) {

    // We save the selected team membership + id
    currentTeamMembershipType = teamMembershipType;
    currentUserIdToRemove = userIdToRemove;
    currentCrewTeamId = crewTeamId;

    // UserId Prompt
    var msgConfirm;
    if(lang=='FR') {
       if(currentTeamMembershipType==TEAM_MEMBERSHIP_TEAM_MANAGER) {
         msgConfirm = 'Voulez-vous vraiment retirer ce gestionnaire d\'&eacute;quipe ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MANAGER) {
         msgConfirm = 'Voulez-vous vraiment retirer ce gestionnaire d\'&eacute;quipage ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MEMBER) {
         msgConfirm = 'Voulez-vous vraiment retirer ce membre ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_COACH) {
         msgConfirm = 'Voulez-vous vraiment retirer ce coach ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_SUBSTITUTE) {
         msgConfirm = 'Voulez-vous vraiment retirer ce rempla&ccedil;ant ?';
       }
       else {
         // default is 'guest'
         currentTeamMembershipType=TEAM_MEMBERSHIP_GUEST;
         msgConfirm = 'Voulez-vous vraiment retirer cet invit&eacute; ?';
       }
    }
    else {
       if(currentTeamMembershipType==TEAM_MEMBERSHIP_TEAM_MANAGER) {
         msgConfirm = 'Are you sure you want to remove this team manager ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MANAGER) {
         msgConfirm = 'Are you sure you want to remove this crew manager ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_CREW_MEMBER) {
         msgConfirm = 'Are you sure you want to remove this crew member ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_COACH) {
         msgConfirm = 'Are you sure you want to remove this coach ?';
       }
       else if(currentTeamMembershipType==TEAM_MEMBERSHIP_SUBSTITUTE) {
         msgPrompt = 'Are you sure you want to remove this substitute ?';
       }
       else {
         // default is 'guest'
         currentTeamMembershipType=TEAM_MEMBERSHIP_GUEST;
         msgConfirm = 'Are you sure you want to remove this guest ?';
       }
    }

    dialog_confirm(msgConfirm, removeTeamMembershipValidate);
  }

  // Validate function for the "Add a team member" pop-up
  function removeTeamMembershipValidate(returnValue) {
      if(returnValue==null) {
         return; // nothing to do
      }

       // we create a return function to handle the response text
       var ajaxReturnFunction = function(responseText) {
           window.defaultStatus = responseText;
           
           // refresh the team section
           selectMainMenuItem(1,'index.php','xref=command.team', null, executeScriptFromHTML);

           // and display an info message
           if(lang=='FR') {
              dialog_alert("L'acc&egrave;s de l'utilisateur a &eacute;t&eacute; retir&eacute;.");
           }
           else {
              dialog_alert("The user membership has been removed.");
           }
       }

       // we save the action on the server side
       ajax_GET("index.php", "xref=command.team.remove.membership&teamMembershipType="+currentTeamMembershipType
                      +"&userId="+currentUserIdToRemove+"&crewTeamId="+currentCrewTeamId, ajaxReturnFunction, lang);
  }


  // ----------------------------------------------------------------
  // TEAM EDITION/SAVE/REMOVE
  // ----------------------------------------------------------------

  // Edit a team in the main content area
  // @param teamId id of the team to edit
  function editTeam(teamId) {
    if(lastMenuItemIndex>=4) {
      // no place to show the detail !
      return;
    }

    var menuTitle;

    if(lang=='FR') {
        menuTitle = 'Edition';
    }
    else {
        menuTitle = 'Edit';
    }

    selectMainMenuItem(lastMenuItemIndex+1, 'index.php', "xref=command.team.edit&teamId="+teamId, menuTitle);
  }

  // Saves the currently edited team info
  // the form name is 'teamEditForm'
  function saveTeam() {

    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("saveTeamCallback()", 1000);
  }

  // Save method as above but without the popup
  function saveTeamCallback() {

    // We create the return function
    var returnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // display a 'save done' message
        if(lang=='FR') {
           dialog_alert("Sauvegarde effectu&eacute;e.");
        }
        else {
           dialog_alert("Save done.");
        }

        // hide the 'save done' message after one second
        setTimeout("dialog_hidePopUp(null)", 1000);
    }

    // ajax_POST
    ajax_POST("index.php", document.teamEditForm, returnFunction, lang);
  }

  // ----------------------------------------------------------------
  // CREW EDITION/SAVE/REMOVE
  // ----------------------------------------------------------------

  // Edit a crew in the main content area
  // @param crewId id of the crew to edit
  function editCrew(crewId) {
    if(lastMenuItemIndex>=4) {
      // no place to show the detail !
      return;
    }

    var menuTitle;

    if(lang=='FR') {
        menuTitle = 'Edition';
    }
    else {
        menuTitle = 'Edit';
    }

    selectMainMenuItem(lastMenuItemIndex+1, 'index.php', "xref=command.crew.edit&crewId="+crewId, menuTitle);
  }

  // Saves the currently edited crew info
  // the form name is 'crewEditForm'
  function saveCrew() {

    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("saveCrewCallback()", 1000);
  }

  // Save method as above but without the popup
  function saveCrewCallback() {

    // We create the return function
    var returnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // display a 'save done' message
        if(lang=='FR') {
           dialog_alert("Sauvegarde effectu&eacute;e.");
        }
        else {
           dialog_alert("Save done.");
        }

        // hide the 'save done' message after one second
        setTimeout("dialog_hidePopUp(null)", 1000);
    }

    // ajax_POST
    ajax_POST("index.php", document.crewEditForm, returnFunction, lang);
  }

  // ----------------------------------------------------------------
  // GUEST EDITION/SAVE
  // ----------------------------------------------------------------

  // Edit a guest in the main content area
  // @param guestAccountId id of the guest
  // @param eventId event id this guest is linked to
  // @param eventType event type linked to this guest
  function editGuestAccount(guestAccountId,eventId,eventType) {
    if(lastMenuItemIndex>=4) {
      // no place to show the detail !
      return;
    }

    // If add we test if we've not reached the max user.
    if(guestAccountId==0) { 

       // Max participants number reached for practice ?
       // the 'maximumPracticeParticipants' is defined in the main HTML page.
       if(eventType=='practice'
          && 22<=userNbSubscription) {

          if(lang=='FR') {
             dialog_alert("Attention: il y a d&eacute;j&agrave; "+userNbSubscription
                           +" inscrits pour cette pratique. Le bateau est complet.", null, 350, 130);
          }
          else {
             dialog_alert("Warning: there are already "+userNbSubscription
                           +" subscriptions for this practice. The boat is full.", null, 350, 130);
          }

       }
       else if(eventType=='festival'
          && 22<=userNbSubscription) {

          if(lang=='FR') {
             dialog_alert("Attention: il y a d&eacute;j&agrave; "+userNbSubscription
                           +" inscrits pour ce festival. Le bateau est complet.", null, 350, 130);
          }
          else {
             dialog_alert("Warning: there are already "+userNbSubscription
                           +" subscriptions for this festival. The boat is full.", null, 350, 130);
          }

       }
    }

    // Edit / Creation display
    var titleItem = '';
    
    if(lang=='FR') {
       titleItem = "Invit&eacute;";
    }
    else {
       titleItem = "Guest";
    }

    selectMainMenuItem(lastMenuItemIndex+1, 'index.php',
        "xref=command.event.guest.edit&guestAccountId="+guestAccountId+"&eventId="+eventId+"&eventType="+eventType, titleItem);
  }


  // Saves the currently edited guest
  // the form name is 'guestAccountEditForm'
  function saveGuestAccount(eventId, eventType) {

    // Form validation
    var myForm = document.guestAccountEditForm;
    resetFormErrors(myForm);

    // we start with mandatory fields
    checkMandatoryTextField(myForm.guestName);
    checkMandatoryTextField(myForm.invitedByName);
    checkMandatoryTextField(myForm.weight);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    checkIntegerField(myForm.weight);

    if(hasFormErrors()==true) {
      displayFormErrors();
      return; // no need to go further
    }

    if(lang=='FR') {
       dialog_alert("Veuillez patienter.");
    }
    else {
       dialog_alert("Please wait.");
    }

    setTimeout("saveGuestAccountCallback("+eventId+", '"+eventType+"')", 1000);
  }

  // Save method as above but without the popup
  function saveGuestAccountCallback(eventId, eventType) {

    // We create the return function
    var returnFunction = function(htmlText) {

        // we hide the 'please wait' popup
        dialog_hidePopUp(true);

        // display a 'save done' message
        if(lang=='FR') {
           dialog_alert("Sauvegarde effectu&eacute;e.");
        }
        else {
           dialog_alert("Save done.");
        }

        // hide the 'save done' message after one second
        setTimeout("dialog_hidePopUp(null)", 1000);
		
		if(eventType=='practice') {
            showPractice(eventId);
		}
		else if(eventType=='festival') {
            showFestival(eventId);
		}
    }

    // ajax_POST
    ajax_POST("index.php", document.guestAccountEditForm, returnFunction, lang);
  }
