// Scripts used for the "select products" module leading to the product comparison page

// Counts the number of selected products.
// nb: the initial number of products (should be 0 at the first call)
// val: the value of the "pids" field of the form
function kk_nbImages(nb, val) {
   var i = val.indexOf("-c");
   if (i > -1) {
      return kk_nbImages(nb + 1, val.substr(i + 1));
   }
   else {
      return nb;
   }
}

// Selects a product.
// pid: the product id
// WARNING: before this function is called, the variable "max" should be defined
function kk_selectProduct(pid) {
   var cb = "c" + pid;
   var pids = document.forms["compareProductsF"].pids;
   var val = pids.value;
   if (val.length == 0) {
      val = "-";
   }
   if (document.getElementById(cb).checked == true) {
      var limit = maxComparison-1;
      if (kk_nbImages(0, val) > limit) {
         alert(max);
         document.getElementById(cb).checked = false;
      }
      else {
         var liste = val + cb + "-";
         pids.value = liste;
      }
   }
   else {
      var key = "-" + cb;
      var i = val.indexOf(key + "-");
      if (i > -1) {
         var liste = val.substr(0, i + 1) + val.substr(i + key.length + 1);
         pids.value = liste;
      }
   }
}

// Checks the checkboxes.
// pids: the list of pids that must be checked, separated by "-" (without "-" at the beginning)
function kk_preCheck(pids) {
   var i = pids.indexOf("-");
   if (i > 0) {
      var cb = pids.substr(0,i);
      if (document.getElementById(cb)) {
         document.getElementById(cb).checked = true;
      }
      kk_preCheck(pids.substr(i + 1));
   }
}

// Resets all checkboxes and the value of the "pids" field.
// pids: the value of the "pids" request parameter
function kk_preUncheck(pids) {
   var j = 0;
   for (var i = 0; i < document.forms["compareChoiceF"].elements.length; i++) {
      if ((document.forms["compareChoiceF"].elements[i].id != null) && (document.forms["compareChoiceF"].elements[i].id.length > 0)) {
         if (document.forms["compareChoiceF"].elements[i].checked) {
            j = 1;
            document.forms["compareChoiceF"].elements[i].checked = false;
         }
      }
   }
   if (j == 0 && pids.length > 0) {
      kk_preCheck(pids.substring(1));
      document.forms["compareProductsF"].pids.value = pids;
   }
   else {
      document.forms["compareProductsF"].pids.value = "-";
   }
}

// Resets the state of the "sort by" select.
// module: the value of the module request parameter
function kk_preSelect(module) {
   var select = document.forms["compareChoiceF"].sortselect;
   for (i = 0; i < select.length; ++i) {
      if (select.options[i].value == module) {
         select.options[i].selected = true;
      }
   }
}

// Compares the selected products.
// WARNING: before this function is called, the variable "min" should be defined
function kk_compareProducts() {
   var val = document.forms["compareProductsF"].pids.value;
   var prefix = "-c";
   if (kk_nbImages(0, val) > 1) {
      var i = val.indexOf(prefix);
      while (i > -1) {
         val = val.replace(prefix, "-");
         i = val.indexOf(prefix);
      }
      document.forms["compareProductsF"].pids.value = val;
      document.forms["compareProductsF"].submit();
   }
   else {
      alert(min);
   }
}

// Goes to the next page.
// no: the page number
function kk_nextPage(no) {
   var oForm = document.forms["selectProductsF"];
   oForm.pids.value = document.forms["compareProductsF"].pids.value;
   var val = document.forms["compareChoiceF"].sortselect.value;
   if (no > 1) {
      val = val + "_" + no;
   }
   oForm.module.value = val;
   oForm.submit();
}

// Changes the view (order by brand or popularity).
function kk_changeView() {
   var oForm = document.forms["selectProductsF"];
   oForm.pids.value = "-";
   oForm.module.value = document.forms["compareChoiceF"].sortselect.value;
   oForm.submit();
}
