//
//-------------------------------------------------------------------
// 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.
//------------------------------------------------------------------- 
//

var busy = false;
//
// ***
// * 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.
// ***
//

function Add2ShopCart(form)
{  
	// If the form's FormType is not undefined:
	// RangePage --- the action was trigger by the Range page
	// ......... ---  .............
	if(typeof(form.FormTriggerType) != "undefined" && form.FormTriggerType.value == 'RangePage'){
			
		if (!busy) {
    		if (validateSelectedOptions(form, false)) {
           
              if(!checkQtyEntered(form)) {
              	alert(invalidQtyEntered);
              	busy = false;
              	return false;
              } 
              
              busy = true;
              form.action="/webapp/wcs/stores/servlet/OrderItemAdd";
              form.URL.value='OrderCalculate?URL=/webapp/wcs/stores/servlet/MCategory' +'?categoryId=' + form.categoryId.value;
              form.submit();
       		} else {
				if (jQuery) {
					$('.attributeSelector', form).addClass('highlight highlightCart').removeClass('highlightWishlist'); //If form validation fails, we can highlight the selection dropdowns to provide feedback
				}
			}
		}
    }
}

// This javascript function is used by the 'Add to Wish List' button to set appropriate values before the form is submitted
function Add2WishList(form)
{

       if (!busy) {
       	if (validateSelectedOptions(form, false)) {
              busy = true;
              form.action="/webapp/wcs/stores/servlet/InterestItemAdd"
              //form.URL.value='InterestItemDisplay'
              form.submit()
       	} else {
				if (jQuery) {
					$('.attributeSelector', form).addClass('highlight highlightWishlist').removeClass('highlightCart'); //If form validation fails, we can highlight the selection dropdowns to provide feedback
				}
			}
       }
}

function checkQtyEntered(form){
	var qtyEntered = true;
	  
	// loop through the quantity boxes on the form
	var i = 0;
	
	// Ensure the qty is set for certain selected items	  
	if (form.elements['quantity'].value.trim() == '' || isNaN(form.elements['quantity'].value.trim())
		||  parseInt(form.elements['quantity'].value) < 1 || parseInt(form.elements['quantity'].value) > 99 ) {
    	qtyEntered = false;
    }

	return qtyEntered;

}

