//
//-------------------------------------------------------------------
// 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,maxquant,minquant)
{
       if (!busy) {
              busy = true;
              var quantity = document.getElementById("qtyBox").value ;
              if (quantity > maxquant){
					document.getElementById("qtyBox").style.borderColor = 'red';
					alert('Please enter a quantity between'+" " + minquant + " " + 'and'+" "+ maxquant);
					busy = false;
			  }
              else{
              form.action="OrderItemAdd";
              form.URL.value='OrderCalculate?URL=ProductDisplay';
              form.submit();  
              }
              
       }
}

// This function Adds a product to shopping cart used for x-sell
function Add2ShopCartMerchandise( url, fieldName, productId, minQty, maxQty ) {
	if ( !busy ) {
		busy = true;

		var productQty = parseInt ( document.getElementById( fieldName ).value );
		
		if( productQty >= minQty && productQty <= maxQty ) {
			if(isInCart ( productId ) == true){
					alert( 'The selected product already exists in your shopping cart' );
				}
				else {
					url += productQty;
					navigateTo ( url );
				}
			document.getElementById( fieldName ).style.borderColor = '#717171';
		}
		else {
			document.getElementById( fieldName ).style.borderColor = 'red';
			alert( 'Please enter a quanity between ' +minQty + ' and ' + maxQty + '.' );
		}
	}
	else {
		alert( 'Please be patient. Still Processing your previous request!' );
	}
	busy = false;
}


// This function checks if the product already exists in cart
function isInCart ( productId ) {
	var shopCartForm = document.forms.myBasketCatIdListForm;
	var productId = parseInt ( productId );
	
	for ( var i = 0; i < shopCartForm.basketCatId.length; i++ ) {
		if ( productId == parseInt ( shopCartForm.basketCatId [ i ].value ) ) {
			return true;
		}
	}

	return false;
}


// Function for navigating to specified URL for different browsers
function navigateTo ( url ) {
	//Mozilla Firefox v1.0+
	if ( navigator.userAgent.indexOf ( "Firefox" )!= -1 ) {
	
	var versionindex = navigator.userAgent.indexOf ( "Firefox" ) + 8;
	
	if ( parseInt ( navigator.userAgent.charAt ( versionindex ) ) >=1 )
	window.location.href = url;
	}
	
	//IE v5.5+
	version=0
	
	if ( navigator.appVersion.indexOf ( "MSIE" ) != -1 ) {
	temp = navigator.appVersion.split ( "MSIE" );
	version = parseFloat ( temp[1] );
	}
	
	//NON IE browser will return 0
	if ( version >= 5.5 ) {
	window.navigate ( url );
	}
}

// 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 Add2WishListMerchandise( url, fieldName, productId, minQty, maxQty ) {
	if ( !busy ) {
		busy = true;

		var productQty = parseInt ( document.getElementById( fieldName ).value );

		if( productQty >= minQty && productQty <= maxQty ) {
			url += "&catEntryId_1=" + productId + "&quantity_1=" + productQty + "&count=1&wishListFlag=true";
			navigateTo ( url );
			document.getElementById( fieldName ).style.borderColor = '#717171';
		}
		else {
			document.getElementById( fieldName ).style.borderColor = 'red';
			alert( 'Please enter a quanity between ' +minQty + ' and ' + maxQty + '.' );
		}
	}
	else {
		alert( 'Please be patient. Still Processing your previous request!' );
	}
	busy = false;
}


//Start: Check for the maximum orderable quantity
function QtyBoxChange ( maxOrderQty )  {
	if ( document.getElementById  ( "qtyBox" ).value < 1 )
		document.getElementById  ( "qtyBox" ).value = 1;
}


// Allow only numbers to type in
function isNumberKey(evt) {
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}