// 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.
// The baseprice allows a set price for the item, to which the prices of the coffees will be added.
// For example, a gift basket may start at 500 pennies, simply due to the cost of the basket.
// priceinput is the unique ID of the form input named 1_cost_1, pricedisplay is the unique
// ID of the span containing the HTML display of the new product's price, and contents is the unique ID
// of the form input named 1_type_1_name.

function buildyourown(form, minimumqty, maximumqty, baseprice){

var totalprice = baseprice;

// Pricing array. Additional products can be added at any time. Make sure to keep updated.
var price = new Array();
price["Creative One"] = 395;
price["Creative One Ground"] = 395;
price["Creative One Whole Bean"] = 395;
price["Creative Two"] = 450;
price["Creative Two Ground"] = 450;
price["Creative Two Whole Bean"] = 450;
price["Creative Three"] = 550;
price["Creative Four"] = 625;
price["Creative Four Whole Bean"] = 625;
price["Creative Five"] = 725;
price["Creative Five Whole Bean"] = 725;
price["Passiona"] = 625; 
price["An Nam Blend"] = 695; 
price["Brio Sun Blend"] = 495; 
price["Brio Sun Blend Ground"] = 495; 
price["Brio Sun Blend Whole Bean"] = 495; 
price["Indochine Dark"] = 650; 
price["Indochine Dark Ground"] = 650; 
price["Indochine Dark Whole Bean"] = 650; 
price["Le Marais"] = 850; 
price["Le Marais WHOLE BEAN"] = 850; 
price["Indochine Espresso"] = 795; 
price["Provence Hazelnut"] = 695; 
price["G7 Black"] = 250; 
price["Classic Green Tea"] = 295; 
price["Lotus Green Tea"] = 295; 
price["Jasmine Green Tea"] = 295; 
price["Jasmine Phoenix"] = 150; 
price["Waterlily"] = 150; 
price["Rising Sun"] = 150; 

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+" items! Please add more coffee to your selection.");
   }
else if (totalqty > maximumqty) {
   alert("We are sorry, there is a maximum order of "+maximumqty+" items. Please reduce the quantity of your selection.");
   }
else {
   var options = form.getElementsByTagName("select");
   for (var i=0; i < options.length; i++) {
        contentsDescription += ", " + options[i].value;
        }
	 
	 /* Here is a line added for the buy2get1 special of august 2009 */
	 totalprice = totalprice*0.66666666;
   totalprice = Math.round(totalprice);
   totalprice = totalprice/100;
	 contentsDescription += ", with Nacha Pods";
   
	 decimalCheck = /\d+\./;
   centsCheck = /\d+\.\d{2}$/;
   if (!centsCheck.test(totalprice)) { 
      if (!decimalCheck.test(totalprice)) {
          totalprice = totalprice+".00";
          }
      else {
           totalprice = totalprice+"0";
           }
      }
   
   var pricedisplay = document.getElementById("price");
   pricedisplay.value = totalprice; // Sets the price in the product form
   
   var priceDisplayContent = "<h3>Selection: "+contentsDescription+"<br><br>";
	 priceDisplayContent += "Total Price: $"+totalprice+"<br><br>Happy with your selection?</h3><input class=\"addToCart\" type=\"submit\" name=\"Action1\" value=\"Add to cart\" style=\"margin-top: 6px;\" onClick=\"addingToCart(this)\">";
   priceDisplayContent += "<br><br>";
   priceDisplayContent += "<h3>Want to change your selections? </h3> <input type=\"button\" value=\"Try Again\" onClick=\"window.location.reload()\" class=\"addToCart\">";
   priceDisplayContent += "<br><br>";
   priceDisplayContent += "<h3>Done shopping?<br></h3>";
   priceDisplayContent += "<button style=\"margin-top: 4px;\" type=\"button\" onClick=\"window.location='php/checkout.php'\">Checkout</button>";
   var pricedisplay2 = document.getElementById("pricedisplay");
   pricedisplay2.innerHTML = priceDisplayContent; 
       // ^Sets the price as displayed to the user, and displays the buttons
   
   var contents = document.getElementById("contents");
   contents.value = contentsDescription; // Sets the description in the product form
   
   var tryAgainContent = document.getElementById("tryAgain");
   tryAgainContent.innerHTML = "";
   
   // var buildProductButton = document.getElementById("buildProduct");
   // buildProductButton.value = "Re-Calculate and Rebuild";
   }
}