/* === this requires the presence of LUTIL.JS === */

/* == ensure the proper number of loot bags have been selected */
function LOOTBAGS_checkLootbagCount(
  formObj,
  minLootBagQty,  // the mininum number of loot bags that must be chosen. This is
                  //  also the number of loot bags per package
  maxLootBags,    // maximum loot bag options
  themeName
) {
  // sum of premium (or standard) lootbag form field values must equal
  var lootBagQty;
  var requiredLootBags;
  var partyPkgQty = parseInt(formObj.standardLootbagsQty.value);
  var packageType;
  var remainder;

  // loop through all possible lootbag form field names
  lootBagQty = 0;
  for ( i = 0; i< maxLootBags; i++ ) {

    // if there is a standard loot bag with this name
    if ( formObj["standard_" + i] ) {

      // if the value is not blank
      if (formObj["standard_" + i].value != "") {
        lootBagQty += parseInt(formObj["standard_" + i].value);
      } /* endif */

    // if there is a premium loot bag with this name
    } else if (formObj["premium_" + i]) {

      // if the value is not blank
      if (formObj["premium_" + i].value != "") {
        lootBagQty += parseInt(formObj["premium_" + i].value);
      } /* endif */
    } /* endif */
  } /* end for */

  // if standard loot bags are being chosen
  if (formObj.standardLootbagsQty.value != "") {
    partyPkgQty = parseInt(formObj.standardLootbagsQty.value);
    packageType = "STANDARD";

  // if premium loot bags are being chosen
  } else if (formObj.premiumLootbagsQty.value != "") {
    partyPkgQty = parseInt(formObj.premiumLootbagsQty.value);
    packageType = "PREMIUM";
  } else {
    alert("Please select the number of packages\n\ryou would like to add to the shopping cart");
    return false;
  } // endif

  // calc how many lootbags must be chosen
  requiredLootBags = partyPkgQty * minLootBagQty;

  // if the user has not chosen enough lootbags
  if (lootBagQty < requiredLootBags) {
    remainder = requiredLootBags - lootBagQty;
    alert("You have " + remainder + " " + packageType + " Party-Pkg " + themeName + " In Favour Loot Bag(s) left to choose");
    return false;

  // if the user has chosen too many lootbags
  } else if (lootBagQty > requiredLootBags) {
    remainder = lootBagQty - requiredLootBags;
    alert("You have chosen " + remainder + " too many " + packageType + " Party-Pkg " + themeName + " In Favour Loot Bags.\n\rPlease adjust your order accordingly");
    return false;
  } // endif

  // if we get here, the correct number of lootbags have been chosen
  return true;

}  /* end of LOOTBAGS_checkLootBagCount */

function LOOTBAGS_checkNan(inputObj) {
  if ( isNaN(inputObj.value) || (inputObj.value < 0) ) {
    if (inputObj.value != "") {
      alert("Please enter a valid number");
    } /* endif */
    inputObj.value = "";
  } /* endif */
}  /* end of LOOTBAGS_checkNan */


