// basketFunctions.js

//Valid options for calculationUsageId = Gives predefined codes for calculation of discounts (-1), 
	// shipping (-2), sales tax (-3), shipping tax, (-4) coupons (-5), surcharge (-6) and shipping adjustment (-7). 
	function UpdateTotal(iForm1, iForm2) {
      var x = document.getElementById('numberProductsBasket').value;
      var quantity = ""
      for (var y=1;y<=x;y++){
	    quantity = document.getElementById('qty1'+y);
        if (validatequantityID(quantity)==false){
		  alert('The field "Qty" should be numeric, integer and positive');
		  return false;
		  break;
        }
      }        
	  iForm1.orderComment.value = iForm2.orderRef.value;
	  iForm1.page.value ='';
	  iForm1.URL.value = 'OrderPrepare?updatePrices=1&keepAutoAddedOrderItemsBeforeCalculate=true&calculationUsageId=-1&orderItemId*=&quantity*=&URL=OrderItemDisplay';
	  iForm1.submit();
	}
	
	function validatequantityID(quantity){
	  dotphone = quantity.value.lastIndexOf('.');
	  if(isNaN(quantity.value) || quantity.value<=0 || dotphone !=-1){ 
	    if(!isNaN(quantity.value)&&quantity.value>1&& dotphone!=0){
	      quantity.value=parseInt(quantity.value);
	    } else {
	      quantity.value=1;
	    }
	    quantity.focus();
	    return false;
	  }
	  return true;
	}
	
	function UpdateBasketReference(iForm1, oForm2) {
		// order comment to store customer order reference
		oForm2.orderComment.value = iForm1.orderRef.value;
		oForm2.URL.value = 'OrderPrepare?updatePrices=1&keepAutoAddedOrderItemsBeforeCalculate=true&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-7&orderItemId*=&quantity*=&URL=OrderItemDisplay';
		oForm2.submit();
	}
	
	function BasketCheckout(iForm1, iForm2, oForm1, checkoutURL) {
			oForm1.orderComment.value = iForm2.orderRef.value;
			oForm1.URL.value = 'OrderPrepare?URL=OrderItemShippingView&orderItemId*=&quantity*=&requestedShipDate*=&requestedDateYear*=&requestedDateMonth*=&requestedDateDay*=&isShipdateRequested*=&isExpeditedCB*=&tieShipCode*=&shipModeId*=&reverse=*n';
			oForm1.URL.value = checkoutURL;
			oForm1.submit();
			return false;
	}
	
	function UpdateShipping(iForm1, oForm1) {
		if (iForm1.SelectedShippingOption.value != '-23') {
			oForm1.shipModeId.value = iForm1.SelectedShippingOption.value;
			oForm1.URL.value = 'OrderPrepare?URL=OrderItemShippingView&orderItemId*=&quantity*=&requestedShipDate*=&requestedDateYear*=&requestedDateMonth*=&requestedDateDay*=&isShipdateRequested*=&isExpeditedCB*=&tieShipCode*=&shipModeId*=&reverse=*n';
			oForm1.submit();
		} else {
			alert("Please choose delivery type");
			return false;
		}
	}

	function BasketSelectShipping(iForm1, oForm1) {
		if (iForm1.SelectedShippingOption.value != '-23') {
			oForm1.shipModeId.value = iForm1.SelectedShippingOption.value;
			oForm1.URL.value = 'OrderPrepare?URL=SingleShippingAddressView&orderItemId*=&quantity*=&requestedShipDate*=&requestedDateYear*=&requestedDateMonth*=&requestedDateDay*=&isShipdateRequested*=&isExpeditedCB*=&tieShipCode*=&shipModeId*=&reverse=*n';
			oForm1.submit();
		} else {
			alert("Please choose delivery type");
			return false;
		}
	}