function nodeCleaner(n) {
   if(!n.data.replace(/\s/g,'')) n.parentNode.removeChild(n);
}

function cleanXML(docElement) {
   var node = docElement.getElementsByTagName('*');
   for(i = 0; i < node.length; i++) {
      a = node[i].previousSibling;
      if(a && a.nodeType == 3) nodeCleaner(a);
      b = node[i].nextSibling;
      if(b && b.nodeType == 3) nodeCleaner(b);
   }
   return docElement;
}

function xmlHttpRequest(url, method, asynch,  args, contentType)
{
   this.url = url;
   this.method = method;
   this.asynch = asynch;
   this.args = args;
   this.contentType = contentType;
   this.xhr = null;
   if(window.XMLHttpRequest)
   {
      this.xhr = new XMLHttpRequest();
   }
   else if(window.ActiveXObject)
   {
      try
      {
         this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
         this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
      }
   }
   this.xhr.open(this.method,this.url,this.asynch);
   this.xhr.setRequestHeader("Content-Type", this.contentType);
   this.xhr.send(this.args);
   return this.xhr;
}

function xmlRead(vari, balise)
{
   this.vari = vari;
   this.balise = balise;
   this.balise = this.vari.getElementsByTagName(""+this.balise+"")[0].firstChild.nodeValue;
   return this.balise;
}

function refresh()
{
   window.location.replace(document.URL);
}

function getVarUrl()
{
   var urlArgs = window.location.pathname;
   var urlArgs = urlArgs.split('/');
   var urlArgs = urlArgs[urlArgs.length-1].split('.');
   return urlArgs[0];
}

function checkEmail(email)
{
   var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-zA-Z]{2,6}$/
   return (reg.exec(email)!=null)
}

String.prototype.wordWrap = function(m, b, c){
   var i, j, s, r = this.split("\n");
   if(m > 0) for(i in r){
      for(s = r[i], r[i] = ""; s.length > m;
          j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
          || m,
          r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
         );
         r[i] += s;
   }
   return r.join("\n");
};

function logged(arg, picsdir, thumbsdir)
{
   var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=logged', 'application/x-www-form-urlencoded');
   xhr.onreadystatechange = function()
   {
      if(xhr.readyState == 4)
      {
         if(xhr.status == 200)
         {
            var a = 0;
            var docXML = cleanXML(xhr.responseXML);
            var items = docXML.getElementsByTagName("item");
            if(items.length > 0)
            {
               if(arg == '1')
               {
                  cartSave();
               }
               else if(arg == '2')
               {
                  personalInfo();
               }
               else if(arg == '3')
               {
                  commandes(picsdir, thumbsdir);
               }
               else if(arg == '4')
               {
                  cartSaves(picsdir, thumbsdir);
               }
               else if(arg == '5')
               {
                  window.location.replace('commande1.html');
               }
            }
            else
            {
               Rideau('Vous devez être identifier pour continuer.','1','javascript:email.focus();');
            }
         }
      }
   };
}

function loading(div , str)
{
   document.getElementById(div).innerHTML = '<div class="loading"><img src="images/loading.gif" alt="loading"/><br/>'+str+'</div>';
}

function toend(frm)
{
   if (frm.adresse.checked == false)
   {
      window.alert('Cocher la case de vérification si l\'adresse de livraison est correct');
      frm.adresse.focus();
      return false;
   }
   if (frm.paiement.value == '')
   {
      window.alert('Veuillez entrer Votre Mode De Paiement.');
      frm.paiement.focus();
      return false;
   }
   return true;
}

function detectCookie()
{
   var xhr = xmlHttpRequest('ajax/action.php', 'POST', 'true', 'action=detectCookie', '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(xmlRead(items[0], "cookie") == '0')
            {
               Rideau('Vous devez activée la gestion des cookies dans les options de votre navigateur.<br/>(Le réglage par défaut de votre navigateur suffit amplement)');
            }
         }
      }
   };
}