//
//-------------------------------------------------------------------
// 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)
{  
      //debugger;
       if (!busy) {
       
              // error if no qty
              if(!addToBagSelected(form)) {
              	alert(noItemsSelected);
              	busy = false;
              	return false;
              }          
              busy = true;
                            
              // loop through the quantity boxes on the form
              var i = 1;

				// Only add those items selected with a qty > 0
              while(form.elements['catEntryId_' + i] != null) {
	            if(!form.elements['catEntryId_' + i].checked ) {
	            	form.elements['quantity_' + i].name = 'ignore_quantity_' + i;
	            }
	              
              	i++;
              }
                            
              form.action="/webapp/wcs/stores/servlet/OrderItemAdd";
              //form.URL.value='OrderCalculate?URL=/webapp/wcs/stores/servlet/ProductDisplay';
              // fixed for IE7
              //form.URL.value='OrderCalculate?URL=ProductDisplay';
              form.submit();
       }
}


function addToBagSelected(form) {

	//
	var oneSelected = false;
  
	// loop through the check boxes to add to wishlist	              
	var i = 1;
	while(form.elements['catEntryId_' + i] != null) {
	    if(form.elements['catEntryId_' + i].checked) {
	    	oneSelected = true;
	    }
  		i++;
	}   
	return oneSelected;           
}

function checkEmailForm(form)
{
	form.sender_name.value = form.sender_name.value.replace(/^\s+/g, "");
	form.recipient.value = form.recipient.value.replace(/^\s+/g, "");

	if (form.sender_name.value == '')
		alert(missingName);
	else if (form.recipient.value == '')
		alert(missingEmail);
	else
		form.submit();
}

