// This function accepts the user's selection of coffees for the purpose of customizable selections.
// Accepts their input and then checks to make sure they bought enough to add up to the minimum quantity.
// Throws alert if they do not.
// Creates product if they do, and allows them to add it to their cart.

function buildyourown(form, minimumqty, maximumqty){

var totalprice = 0;

// Pricing array. Additional products can be added at any time. Make sure to keep updated.
var price = new Array();
price["Culi Robusta"] = 395;
price["Robusta Arabica"] = 450;
price["Arabica Se"] = 475;
price["Premium Culi"] = 575;
price["Culi Arabica"] = 695;
price["Passiona"] = 595; 
price["An Nam Blend"] = 695; 
price["Brio Sun Blend"] = 495; 
price["Indochine Dark"] = 650; 
price["Le Marais"] = 850; 
price["Indochine Espresso"] = 795; 

integerTest = /^\d+$/; // Regular expression to ensure all input is integers
totalqty = 0;
contentsDescription = "";
var inputboxes = form.getElementsByTagName("input");
for (var i=0; i < inputboxes.length - 1; i++) {
     var item = inputboxes[i].name;
      var qty = inputboxes[i].value;
      if(integerTest.test(qty)) { // If people enter a null or letter, sets to zero
         qty = Math.abs(parseInt(inputboxes[i].value, 10));
         }
      else {
         if(qty != "") {
            alert("The box for "+item+" has a letter or other character in it. Please change it to a number.");
            }
         qty = 0;
         }
      itemprice = price[item] * qty;
      totalprice = totalprice + itemprice;
      totalqty += qty;
      if (qty > 0 && contentsDescription == "") { // Building the description to enter into the shopping cart
          contentsDescription += qty+" "+item;
          }
      else if (qty > 0) {
          contentsDescription += ", "+qty+" "+item +"";
          }
      }

if(totalqty < minimumqty) {
   alert("You need to select a total of at least "+minimumqty+" bags of coffee! Please add more coffee to your selection.");
   }
else if (totalqty > maximumqty) {
   alert("We are sorry, there is a maximum order of "+maximumqty+" bags of coffee. Please reduce the quantity or your selection.");
   }
else {
   
   var pricedisplay = document.getElementById("price");
   pricedisplay.value = (totalprice/100); // Sets the price in the product form
   
   var pricedisplay2 = document.getElementById("pricedisplay");
   pricedisplay2.innerHTML = "$"+(totalprice/100)+"<br><em>Shipping: $0.00</em><br><input class=\"addToCart\" type=\"submit\" name=\"Action1\" value=\"Add to cart\" style=\"margin-top: 6px;\">"; 
       // ^Sets the price as displayed to the user and reminds them that shipping is free, and displays the Add to Cart button
   
   var contents = document.getElementById("contents");
   contents.value = contentsDescription; // Sets the description in the product form
   
   document.getElementById("addToCart").style.visibility = "visible"; // Makes it possible to now add the product to the cart
   }
}