function register(frm)
{
   if (frm.email.value == ''){
      window.alert('Veuillez entrer Votre Email.');
      frm.email.focus();
      return false;
   }
   if (checkEmail(frm.email.value) == false)
   {
      window.alert('Email invalide.');
      frm.email.focus();
      return false;
   }
   // Password
   if (frm.password.value == ''){
      window.alert('Veuillez entrer Votre mot de passe.');
      frm.password.focus();
      return false;
   }
   if (frm.confpassword.value == ''){
      window.alert('Veuillez entrer la confirmation de votre mot de passe.');
      frm.confpassword.focus();
      return false;
   }
   if (frm.password.value.length < 5){
      window.alert('Mot de passe trop court, 5 caractéres minimum');
      frm.password.focus();
      return false;
   }
   if (frm.password.value != frm.confpassword.value){
      window.alert('La confirmation du mot de passe ne correspond pas au mot de passe.');
      frm.confpassword.focus();
      return false;
   }
   // Prenom
   if (frm.prenom.value == ''){
      window.alert('Veuillez entrer Votre Prénom.');
      frm.prenom.focus();
      return false;
   }
   // Nom
   if (frm.nom.value == ''){
      window.alert('Veuillez entrer votre Nom.');
      frm.nom.focus();
      return false;
   }
   // Adresse
   if (frm.adresse.value == ''){
      window.alert('Veuillez entrer votre Adresse.');
      frm.adresse.focus();
      return false;
   }
   // Code Postal
   if (frm.codepostal.value == ''){
      window.alert('Veuillez entrer votre Code Postal.');
      frm.codepostal.focus();
      return false;
   }
   // Ville
   if (frm.ville.value == ''){
      window.alert('Veuillez entrer votre Ville.');
      frm.ville.focus();
      return false;
   }
   // Pays
   if (frm.pays.value === ''){
      window.alert('Veuillez entrer votre Pays.');
      frm.pays.focus();
      return false;
   }

   var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=accountRegister&email='+frm.email.value+'&password='+frm.password.value+'&confpassword='+frm.confpassword.value+'&civ='+frm.civ.value+'&prenom='+frm.prenom.value+'&nom='+frm.nom.value+'&adresse='+frm.adresse.value+'&compadresse='+frm.compadresse.value+'&codepostal='+frm.codepostal.value+'&ville='+frm.ville.value+'&pays='+frm.pays.value+'&tel='+frm.tel.value+'&telpt='+frm.telpt.value+'&day='+frm.day.value+'&month='+frm.month.value+'&year='+frm.year.value,'application/x-www-form-urlencoded');
   xhr.onreadystatechange = function()
   {
      if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            var docXML = cleanXML(xhr.responseXML);
            var items = docXML.getElementsByTagName("item");
            var html = '';
            html += xmlRead(items[0], "error_view");
            if(xmlRead(items[0], "ok") == '1')
            {
               accountLogin();
               Rideau(html,'1','profile.html');
            }
            else
            {
               Rideau(html,'0');
            }
         }
      }
   };
}

function accountLogin(email, password, activeLogin)
{
   if(activeLogin == 1)
   {
      loading('login' , 'Identification en cours');
   }
   var currentPage = getVarUrl();
   if(email != '' && password != '')
   {
      var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=accountLogin&email='+email+'&password='+password, 'application/x-www-form-urlencoded');
   }
   else
   {
      var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=accountLogin', 'application/x-www-form-urlencoded');
   }
   xhr.onreadystatechange = function()
   {
      if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            var docXML = cleanXML(xhr.responseXML);
            var items = docXML.getElementsByTagName("item");
            if(activeLogin == 1 && xmlRead(items[0], "error") == 1)
            {
               Rideau(xmlRead(items[0], "error_view"),'1','javascript:email.focus();');
            }
            if(xmlRead(items[0], "error") == '0')
            {
               var html = '<div class="login">';
               html += 'Bonjour <br/><b class="underline">'+xmlRead(items[0], "name")+'</b>';
               html += '<br/><br/><a href="profile.html">Mon espace client</a><br/><a href="#" onclick="javascript:accountLogout();return false;">Se déconnecter</a>';
               html += '</div>';
               var currentPage = getVarUrl();
               if(currentPage == 'profile' && activeLogin == 1)
               {
                  refresh();
               }
            }
            else
            {
               var html = '<div class="login">';
               html += '<a href="register.html">Créer un compte</a>';
               html += '<form action="#" method="post" onsubmit="javascript:accountLogin(email.value,password.value,\'1\');return false;">';
               html += '<p>';
               html += '<input type="text" class="email" name="email" value="Email ?" onfocus="if (value==\'Email ?\') value=\'\'" onblur="if(value==\'\') value=\'Email ?\'"/><br/>';
               html += '<input type="password" class="password" name="password" value="Password ?" onfocus="if (value==\'Password ?\') value=\'\'" onblur="if(value==\'\') value=\'Password ?\'"/>';
               html += '&nbsp;<input type="submit" class="button_small" value=" Ok"/><br/>';
               html += '</p>';
               html += '</form>';
               html += '<a href="sendpassword.html">Mot de passe oublié ?</a>';
               html += '</div>';
            }
            document.getElementById("login").innerHTML = html;
         }
      }
   };
}

function accountLogout()
{
   loading('login' , 'Déconnexion en cours');
   var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=accountLogout', 'application/x-www-form-urlencoded');
   accountLogin();
}

function accountPasswordLost(email)
{
   if (email == ''){
      window.alert('Veuillez entrer Votre Email.');
      return false;
   }
   if (checkEmail(email) == false)
   {
      window.alert('Email invalide.');
      return false;
   }
   loading('passwordLost' , 'Veuillez patienter.');
   var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=accountPasswordLost&email='+email, 'application/x-www-form-urlencoded');
   xhr.onreadystatechange = function()
   {
      if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            var docXML = cleanXML(xhr.responseXML);
            var items = docXML.getElementsByTagName("item");
            var html = '';
            if(xmlRead(items[0], "error") == '0')
            {
               html += 'Votre nouveau mot de passe à été envoyé à l\'adresse e-mail: <b>'+email+'</b>';
            }
            else
            {
               html += 'Aucun compte n\'est associer à l\'adresse email: <b>'+email+'</b>';
            }
            document.getElementById("passwordLost").innerHTML = html;
         }
      }

   };
}
