//
//-------------------------------------------------------------------
// 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.
// *
// * REQUIRES ErrorMessages.js, className.js, and ValidationHelper.js for validateProdForm() function
// ***
//



var busy = false;
function Add2ShopCart(form, catEntryId, catEntryQuantity)
{
       if (!busy) {
              busy = true;
              form.action="OrderItemAdd";
              form.catEntryId.value = catEntryId;
              form.quantity.value = catEntryQuantity;
              form.errorViewName.value = "CatalogItemAddErrorView";
              form.URL.value='SetPendingOrder?item_quantity*=&URL=OrderCalculate?URL=OrderItemDisplay&updatePrices=1&calculationUsageId=-1&orderId=.';
              form.submit();
       }
}

function NpAdd2ShopCart(form, catEntryId, catEntryQuantity, customLengthQty, URLbase)
{
	if (validateProdForm() && !busy) 
	{
    	busy = true;
    	
    	var originalUrl = URLbase;
        form.action="OrderItemAdd";
        form.catEntryId.value = catEntryId;
        form.quantity.value = catEntryQuantity;
        if(customLengthQty != 0) {
	        form.field1.value = customLengthQty;
        } else {
        	form.field1.value = "";
        }
        
        form.errorViewName.value = "CatalogItemAddErrorView";
        form.addType.value = "shopCart";
        form.URL.value='SetPendingOrder?item_quantity*=&URL=OrderCalculate?URL=' + originalUrl;
        form.submit();
	}
}

function NpAdd2ShopList(form, catEntryId, catEntryQuantity, customLengthQty, shopListId, userId, URLbase)
{
	
	if (validateProdForm() && !busy) 
	{
    	busy = true;
    	
    	var originalUrl = URLbase;
    	form.catEntryId.value = catEntryId;
        form.quantity.value = catEntryQuantity;
        if(customLengthQty != 0) {
	        form.field1.value = customLengthQty;
        } else {
        	form.field1.value = "";
        }
        form.addType.value = "shopList";
    	
    	
    	if(shopListId.length == 0)
    	{
        	form.action='RequisitionListItemUpdate?catEntryId_1=' + catEntryId + '&quantity_1=' + catEntryQuantity + '&field1_1=' + customLengthQty + '&orderDesc=ShoppingList' + userId + '&status=Y&URL=' + originalUrl;
        } else
        {
        	form.action='RequisitionListItemUpdate?catEntryId_1=' + catEntryId + '&quantity_1=' + catEntryQuantity + '&field1_1=' + customLengthQty + '&requisitionListId=' + shopListId + '&status=Y&URL=' + originalUrl;
        }
        form.submit();
       }
}

function Add2RFQ(form) {
	if (form.Type[0].checked) {
		form.action="RFQCreateDisplay?endresult=0";
	} else 	{
		form.action="AddToExistRFQListDisplay?isContract=Y";
	}
	form.submit();		
}

function validateProdForm(section)
{
	resetValidation();
	
	//default to 'all' if section param is not supplied. Section param provides way to narrow validation to one particular section instead of all 3
	if(typeof(section) == 'undefined') section = 'all';
	
	// validate options
	var catEntryId = window.document.getElementById('itemCatentryId');
	if((catEntryId != null) && (section == 'all' || section == 'options'))
	{
		if(catEntryId.value == "")
		{
			//alert('Please choose option.');
 			if(location.href.match(/langId=-11/) || location.href.match(/_-11/)) //match uk langId
 			{msg = 'Please choose a type, size, and/or colour.';}
			else
			{msg = 'Please choose a type, size, and/or color.';}
			insertErrorMessage(msg);
			var valElm = 'val_itemCatentryId';
			var classes = new Array(validationBorderCSSClasses[0]);
			massAddClassName(valElm);
	
			return false;
		}
	}
	
	// validate quantitiy
	var catEntryQuantity = window.document.getElementById('item_quantity');
	if((catEntryQuantity != null) && (section == 'all' || section == 'quantity'))
	{
		if(isNaN(catEntryQuantity.value) || parseFloat(catEntryQuantity.value) < 1 || catEntryQuantity.value == '')
		{
			//alert('Please enter a valid quantity.');
			msg = 'Please enter a valid quantity.';
			insertErrorMessage(msg);
			var valElm = 'val_quantity';
			catEntryQuantity.parentNode.id = valElm;
			var classes = new Array(validationBorderCSSClasses[0]);
			massAddClassName(valElm,classes);
				
			return false;
		}
	}
	
	// validate custom length
	var custLengthQuantity = window.document.getElementById('cust_length_qty');
	if((custLengthQuantity != null) && (section == 'all' || section == 'length'))
	{
		if(custLengthQuantity.value == "")
		{
			//alert('Please choose length.');
			msg = 'Please choose length.';
			insertErrorMessage(msg);
			var valElm = 'val_length';
			custLengthQuantity.parentNode.id = valElm;
			var classes = new Array(validationBorderCSSClasses[0]);
			massAddClassName(valElm,classes);
				
			return false;
		}
	}
	
	return true;
}