var x = 0;
function openWin(url,w,h) {
   w += 20; h += 24;
   msgWin = window.open('','newWin','width='+w+',height='+h+',status=1,scrollbars=1,screenX=20,screenY=20,top=20,left=20');
   msgWin.location.href = url;
}

function checkQuantity() {
   var iic = 0;
   var vToCheck = 0;
   var vInStock = 0;
   var vRemUnits = 0;
   var x = 0;
   var y = 0;
   var z = 0;
   var indx = 0;

   var ctgry = document.getElementById("category").value;
   if(ctgry == 3) {
      var liElmnts = document.getElementsByTagName('LI');
      if(liElmnts.length > 0) {
         for(i = 0; i < liElmnts.length; i++) {
            if((liElmnts[i].firstChild.nodeType == 3) && ((liElmnts[i].firstChild.nodeValue.toLowerCase().indexOf('per keg') > 0) || (liElmnts[i].firstChild.nodeValue.toLowerCase().indexOf('per box') > 0))) {
               var pcsKGBOX = liElmnts[i].firstChild.nodeValue.replace(/ (per keg)|(per box)/gi, '');
               pcsKGBOX = pcsKGBOX.replace(/,/, '');
               pcsKGBOX = parseInt(pcsKGBOX);
               break;
            }
         }
      }
   }

   while(document.getElementById("qty"+iic) && document.getElementById("instock"+iic)) {
      if(ctgry == 3)
         vToCheck = parseInt(document.getElementById("qty"+iic).value)/pcsKGBOX;
      else
         vToCheck = parseInt(document.getElementById("qty"+iic).value);
      vInStock = parseInt(document.getElementById("instock"+iic).value);
      vRemUnits = vToCheck - vInStock;

      if(vToCheck > 0) {
         if(vToCheck > vInStock) {
            x = vInStock;
            y = vToCheck;
            z = vRemUnits;
            indx = iic;
            break;
         }
      }
      iic++;
   }

   if(y > 0) {
      if(confirm("The maximum quantity currently available for sale is " + x + ". Would you like to place the remaining " + z + " units on backorder? Please call our office to find out when the remainder will be available.")) {
         document.getElementById("qty"+indx).value = y;
         return true;
      } else {
         document.getElementById("qty"+indx).value = x;
         document.getElementById("qty"+indx).focus();
         return false;
      }
   }
}

function alertLogIn() {alert('Please enter your username and password in the upper right corner of the page!');}

function GoToPage(hiddenName,hiddenValue,bookMark) {
   hiddenName.value = hiddenValue;

   if(bookMark.length > 0)
      document.itemform.action = document.itemform.action+"#"+bookMark;
   document.itemform.submit();
   return true;
}

function checkSearchField() {
   if(document.searchform.keywords.value.length > 0) {
      document.searchform.search.value = 1;
      return true;
   } else {
      alert('Please enter keyword(s)!');
      document.searchform.keywords.focus();
      return false;
   }
}

function confirmLogOut(x) {
   if (confirm("You want to log out?")) return true;
   else return false;
}

function checkOldQuotaSave() {
   if(document.getElementById('loadedQuoteID').value.length > 0 && parseInt(document.getElementById('loadedQuoteID').value) > 0) {
      if(confirm('You have not saved your changes to this quote \(to save, select the Submit Quote button\). Do you still want to select another quote without saving your changes?'))
         document.getElementById('quotesform').submit();
      else
         return false;
   }
}
