//
//-------------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (c) Copyright IBM Corp. 2006
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//-------------------------------------------------------------------
//

//
// ***
// * This javascript function is used by the 'Add to Shopcart' button.  Since the HTML form is shared by both 'Add to Shopcart' and 'Add to Wish List' button,
// * appropriate values are set using this javascript before the form is submitted.
// * The variable 'busy' is used to avoid submitting the same forms multiple times when users click the button more than once.
// ***
//
var busy = false;
function Add2ShopCart(form, catEntryId, catEntryQuantity)
{
       if (!busy) {
              busy = true;
              form.action="OrderItemAdd";
              form.catEntryId.value = catEntryId;
              form.quantity.value = catEntryQuantity;
              form.externalId.value=form.forRegistry.value;
              form.URL.value='OrderCalculate?item_quantity*=&URL=OrderItemDisplay';
              form.submit();
       }
}
// This javascript function is used by the 'Add to Wish List' button to set appropriate values before the form is submitted
function Add2WishList(form, catEntryId)
{
       if (!busy) {
              busy = true;
              form.action="InterestItemAdd";
              form.catEntryId.value = catEntryId;
              form.URL.value='InterestItemDisplay';
              form.submit();
       }
}

function getCookieValue(cookieNVPs,cookieName)
{
   var splitValues;
   var i;
   for(i=0;i<cookieNVPs.length;++i) {
      splitValues=cookieNVPs[i].split("=");
      if(delBlanks(splitValues[0])==cookieName) {
      	return splitValues[1];
      }
   }
   return "";
}

function setPromoCodeFromCookie(form, cookieName) {
   var cookie=document.cookie;
   var nvpair=cookie.split(";");
   form.promoCode.value = getCookieValue(nvpair, cookieName);
}

function delBlanks(str) {
   var result="";
   var i;
   var chrn;
   for (i=0;i<str.length;++i) {
      chrn=str.charAt(i);
      if (chrn!=" ") { result += chrn; }
   }
   return result;
}


	function checkQuantities(theForm) {		
	
			var item = "cart";
			
			var qFlag = 0;
			var maxFlag = 0;
			
			  for (var i = 0; i < theForm.elements.length; i++) {
				var elem = theForm.elements[i];
			    if (elem.name.length >= 8  && elem.name.substring(0, 8) == "quantity") {
			      	var quantityField = theForm.elements[i];
					if(quantityField.value != 0 ){
						qFlag = 1;
						break;
					}
				 }
			  }			

			if(qFlag){
			
			  for (var i = 0; i < theForm.elements.length; i++) {
				var elem = theForm.elements[i];
			    if (elem.name.length >= 8  && elem.name.substring(0, 8) == "quantity") {
					var index = elem.name.substring(8);
			      	var quantityField = theForm.elements[i];
					if(quantityField.value > 500 ){
						maxFlag = 1;
						break;
					}
				 }
			  }			
			
				if(maxFlag){
					alert("Quantity values should be 500 or less.");
					return false;
				} 
				else {
					return true;
				}
			} 
			else {
				alert("Before we can add items to your " + item + ", you must specify a quantity.");	
							
				return false;
			}

		return false;
	}
