
function hasValue(theSelect, value)
{
   var maxLen = theSelect.length;
   for(var cnt = 0; cnt < maxLen; cnt++)
   if((theSelect.options[cnt].value.indexOf(value) != - 1) &&(theSelect.options[cnt].value.length == value.length))
   return true;
   return false;
}

function setLotList(theForm)
{
   theForm.lotNumbers.options[theForm.catNumbers.selectedIndex].selected = true;
}

function setCatList(theForm)
{
   theForm.catNumbers.options[theForm.lotNumbers.selectedIndex].selected = true;
}

function setFocus(theForm)
{
   theForm.catNum.focus();
}

function addToList(theForm, isInternal)
{
   var value = getValue(theForm, isInternal);
   if(value.indexOf("~") == - 1)
   {
      alert(value);
      return false;
   }
   arrLotCat = value.split('~');
   var lotNum = arrLotCat[0];
   var catNum = arrLotCat[1];
   var errMsg = "";
   if(hasValue(theForm.lotNumbers, value))
   {
      errMsg += " + Value was already added. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.lotNum.value.length > 7) 
   {
   	  errMsg += " + To many #'s entered for Lot Number. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.lotNum.value.length < 5) 
   {
   	  errMsg += " + To little #'s entered for Lot Number. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.catNum.value.length > 5) 
   {
   	  errMsg += " + To many #'s entered for Cat Number. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.catNum.value.length < 4) 
   {
   	  errMsg += " + To little #'s entered for Cat Number. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.catNum.value.length == 5 && theForm.lotNum.value.length != 7)
   {
   	  errMsg += " + The Lot Number requires 7 characters. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if (theForm.catNum.value.length == 4 && theForm.lotNum.value.length != 5)
   {
   	  errMsg += " + The Lot Number requires 5 characters. \n";
      alert(errMsg + "\n\nPlease try again.");
      return false;
   }
   if(theForm.lotNumbers.disabled && theForm.lotNumbers.options.length > 0)
   {
      theForm.lotNumbers.options[0] = null;
      theForm.catNumbers.options[0] = null;
   }
   // stores the LOT CAT combo in the id/value of the lotNumbers select
   // so that the combo can be sent to the server as a pair
   var opt1 = new Option(lotNum, value, false, true);
   // the id/value for the catNumbers is not needed
   var opt2 = new Option(catNum, "", false, true);

   if(theForm.lotNumbers.disabled && theForm.lotNumbers.options.length > 0)
   {
      theForm.lotNumbers.options[0] = opt1;
      theForm.catNumbers.options[0] = opt2;
   }
   else
   {
      theForm.lotNumbers.options[theForm.lotNumbers.options.length] = opt1;
      theForm.catNumbers.options[theForm.catNumbers.options.length] = opt2;
   }

   theForm.lotNum.value = "";
   theForm.catNum.value = "";
   if(theForm.catNumbers.length == 10) theForm.btnAddToList.disabled = true;
   theForm.btnRemoveFromList.disabled = false;
   theForm.btnResetForm.disabled = false;
   theForm.catNumbers.disabled = false;
   theForm.lotNumbers.disabled = false;
   setFocus(theForm);
   return true;
}

function removeFromList(theForm)
{
   if(theForm.catNumbers.selectedIndex != - 1)
   {
      theForm.catNumbers.options[theForm.catNumbers.selectedIndex] = null;
      theForm.lotNumbers.options[theForm.lotNumbers.selectedIndex] = null;
      theForm.btnAddToList.disabled = false;
   }
   if(theForm.catNumbers.length == 0)
   {
      theForm.btnRemoveFromList.disabled = true;
      theForm.btnResetForm.disabled = true;
   }
}

function resetForm(theForm)
{
   var len = theForm.lotNumbers.length;
   for(var cnt = len; cnt > 0; cnt--)
   {
      theForm.lotNumbers.options[cnt - 1] = null;
      theForm.catNumbers.options[cnt - 1] = null;
   }
   theForm.lotNum.value = "";
   theForm.catNum.value = "";
   theForm.btnAddToList.disabled = false;
   theForm.btnRemoveFromList.disabled = true;
   theForm.btnResetForm.disabled = true;
   theForm.lotNumbers.disabled = true;
   theForm.catNumbers.disabled = true;
   setFocus(theForm);
}

function isAddToListAllowed(theForm)
{
   return(theForm.lotNumbers.options.length < 10 || theForm.lotNumbers.disabled) &&(trim(theForm.catNum.value) != "" || trim(theForm.lotNum.value) != "");
}

function hasSearchCriteria(theForm)
{
   return !theForm.lotNumbers.disabled;
}

function validateLotAndCat(theForm, isInternal)
{
   if(isAddToListAllowed(theForm))
   {
      if(!addToList(theForm, isInternal))
      {
         return false;
      }
   }
   else if(!hasSearchCriteria(theForm))
   {
      alert("Please enter your search criteria before pressing the Search button.");
      return false;
   }
   populateLotsAndCats(theForm);
   theForm.submit();
   return true;
}

function getValue(theForm, isInternal)
{
   var errMsg = "";
   var catNum = trim(theForm.catNum.value);
   var lotNum = trim(theForm.lotNum.value);
   if(isInternal)
   {
      if(catNum.length == 0 && lotNum.length == 0) errMsg += " + A Catalog Number and/or Lot Number combination must be entered. \n";
   }
   else 
   {
      if(catNum.length == 0) errMsg += " + Catalog Number cannot be blank. \n";
      if(lotNum.length == 0) errMsg += " + Lot Number cannot be blank. \n";
      if(catNum.length == 0 && lotNum.length == 0) errMsg += " + A Catalog Number and/or Lot Number combination must be entered. \n";
   }
   if(errMsg.length > 0)
   return errMsg;
   else 
   return(lotNum + "~" + catNum).toUpperCase();
}
// pulls the combo LOT CAT values built in the addToList function.  the reason this
// works this way is that the selects are NOT multiselect.  this being the case,
// only one LOT CAT combo would be sent back versus N.

function populateLotsAndCats(theForm)
{
   // TODO change the LOT CAT combo to CAT LOT to match the GUI
   var len = theForm.lotNumbers.length;
   var value = "";
   for(var cnt = 0; cnt < len; cnt++)
   {
      value += theForm.lotNumbers.options[cnt].value + ";";
   }
   theForm.lotsAndCats.value = value;
}
