//
//-------------------------------------------------------------------
// 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;
var flag = 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 (!busy) {
              busy = true;
              form.action="OrderItemAdd";
              form.URL.value='OrderCalculate?URL=OrderItemDisplay';
              form.submit();
       }
}


function Add2ShopCart1(form, sizeCounter)
{
	if (!busy) {
		var selectlistvalue = new Array();
		var tempValues;
		
		busy = true;
        form.action="OrderItemAdd";
        
        if (form.elements["nodropdownlist"].value == 'N'){
			for (var i=0; i<sizeCounter; i++){
				if (form.elements['quantity_' + parseInt(i+1)].value > 0){
					selectlistvalue[i] = document.getElementById("attrValue" + parseInt(i+1)).value
					tempValues = selectlistvalue[i].split("_",5);
					document.getElementById("attrValue" + parseInt(i+1)).value = tempValues[0];
					form.elements['catEntryId_' + parseInt(i+1)].value = tempValues[4];
					flag = true;
				}
			}
		}
		if (form.elements["nodropdownlist"].value == 'Y'){
			for (var i=0; i<sizeCounter; i++){				
				if (form.elements['quantity_' + parseInt(i+1)].value > 0){
					tempValues = document.getElementById("attrcatentry_" + parseInt(i+1)).value;
					form.elements['catEntryId_' + parseInt(i+1)].value = tempValues;
					//alert("tempValues = " + tempValues);
					flag = true;
				}
			}
		}
		if (flag == true){
			if (form.elements["nodropdownlist"].value == 'Y'){
				for (var i=0; i<sizeCounter; i++){
					if (form.elements['quantity_' + parseInt(i+1)].value == 0 && sizeCounter > 1){
						//alert("0 = " + 0);
						var d = document.getElementById('catentryDiv_' + parseInt(i+1));
						var olddiv = document.getElementById('catEntryIdInput_' + parseInt(i+1));
						//alert("d = " + d);
						//alert("olddiv = " + olddiv);
						d.removeChild(olddiv);					
					}				
				}
			}
			form.submit();				
		}else{
			alert("Please fill out the 'Qty' quantity input field(s) you want to order!");	
			busy = false;		
		}
			
    }
}


// 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) {
              busy = true;
              form.action="InterestItemAdd"
              form.URL.value='InterestItemDisplay'
              form.submit()
       }
}
function Add2WishList1(form, sizeCounter)
{
       if (!busy) {
       		  var selectlistvalue = new Array();
			  var tempValues;
		
              busy = true;
              form.action="InterestItemAdd"
              form.URL.value='InterestItemDisplay'
              
              if (form.elements["nodropdownlist"].value == 'N'){
	              for (var i=0; i<sizeCounter; i++){
						if (form.elements['quantity_' + parseInt(i+1)].value == 0){
							form.elements['quantity_' + parseInt(i+1)].value = 1;
						}
						selectlistvalue[i] = document.getElementById("attrValue" + parseInt(i+1)).value
						tempValues = selectlistvalue[i].split("_",5);
						document.getElementById("attrValue" + parseInt(i+1)).value = tempValues[0];
						form.elements['catEntryId_' + parseInt(i+1)].value = tempValues[4];
				  }
			  }
			  if (form.elements["nodropdownlist"].value == 'Y'){
					for (var i=0; i<sizeCounter; i++){
						if (form.elements['quantity_' + parseInt(i+1)].value == 0){
							form.elements['quantity_' + parseInt(i+1)].value = 1;
						}
					}
			  }
              form.submit()
       }
}
