function PaymentMethodChanged(paymentMethod) {
    // Disable the confirm link
    var confirm = document.getElementById(confirmButton);
    confirm.disabled = true;

    ShowLoadingDialog();
    eXpress.Web.UI.Website.UiService.PaymentMethodChanged(paymentMethod, OnPaymentMethodChanged);
}

function OnPaymentMethodChanged(obj) {

    // Cart Totals
    $get(cartTotalsProductsId).innerHTML = obj.TotalBeforeCost;
    $get(cartTotalsShippingId).innerHTML = obj.Shipment;
    $get(cartTotalsHandlingId).innerHTML = obj.Handling;
    $get(cartTotalsVatId).innerHTML = obj.Vat;
    $get(cartTotalsTotalId).innerHTML = obj.Total;
    if (!obj.HasDiscount) {
    	$get(cartTotalsDiscountId).parentNode.style.display = 'none';
    }
    else {
    	$get(cartTotalsDiscountId).parentNode.style.display = '';
    	$get(cartTotalsDiscountId).innerHTML = obj.Discount;
    }
    
    // Enable the confirm link
    var confirm = document.getElementById(confirmButton);
    confirm.disabled = false;

    HideLoadingDialog();
}

function DeliveryAddressChanged(contactId, checked) {
	// Disable all checkboxes
	var boxes = document.getElementsByTagName("input");
	for (var i = 0; i < boxes.length; i++) {
		boxes[i].disabled = true;
	}
	// Disable the confirm link
	var confirm = document.getElementById(confirmButton);
	confirm.disabled = true;

	ShowLoadingDialog();
	eXpress.Web.UI.Website.UiService.DeliveryAddressChanged(contactId, checked, OnDeliveryAddressChanged);
}

function OnDeliveryAddressChanged(obj) {
	// Enable all checkboxes
	var boxes = document.getElementsByTagName("input");
	for (var i = 0; i < boxes.length; i++) {
		boxes[i].removeAttribute("disabled");
	}
	// Enable the confirm link
	var confirm = document.getElementById(confirmButton);
	confirm.disabled = false;

	var paymentMethodFromOfflineToOnline = false;
	
	// Payment Method
	if (obj.DisableOnlinePayment && obj.DisableOnlinePayment) {
		if (typeof paymentMethodOfflineControlId != "undefined")
			$get(paymentMethodOfflineControlId).disabled = true;
		if (typeof paymentMethodOnlineControlId != "undefined")
			$get(paymentMethodOnlineControlId).disabled = true;
	}
	else {
		if (obj.DisableOfflinePayment) {
			if (typeof paymentMethodOnlineControlId != "undefined") {
				$get(paymentMethodOnlineControlId).removeAttribute("disabled");
			}
			if (typeof paymentMethodOfflineControlId != "undefined") {
				if ($get(paymentMethodOfflineControlId).checked) {
					if (typeof paymentMethodOnlineControlId != "undefined") {
					    $get(paymentMethodOnlineControlId).checked = true;
					    paymentMethodFromOfflineToOnline = true;
					}
				}
				$get(paymentMethodOfflineControlId).disabled = true;
			}
		}
		else {
			if (typeof paymentMethodOfflineControlId != "undefined") {
				$get(paymentMethodOfflineControlId).removeAttribute("disabled");
			}
			if (obj.DisableOnlinePayment) {
				if (typeof paymentMethodOnlineControlId != "undefined") {
					$get(paymentMethodOnlineControlId).disabled = true;
				}
				if (typeof paymentMethodOfflineControlId != "undefined") {
					$get(paymentMethodOfflineControlId).checked = true;
				}
			}
		}
	}

	if (obj.NoneSelected) {
		// Make sure the first contact is selected (yourself)
		if ($get(deliveryAddressSelf)) {
			$get(deliveryAddressSelf).checked = true;
		}
	}

	if (paymentMethodFromOfflineToOnline) {
	    PaymentMethodChanged('ONLINE'); // When offline is disabled, online will be checked.
	}
	else {
	    // Cart Totals
	    $get(cartTotalsProductsId).innerHTML = obj.TotalBeforeCost;
	    $get(cartTotalsShippingId).innerHTML = obj.Shipment;
	    $get(cartTotalsHandlingId).innerHTML = obj.Handling;
	    $get(cartTotalsVatId).innerHTML = obj.Vat;
	    $get(cartTotalsTotalId).innerHTML = obj.Total;
	    if (parseFloat(obj.Discount) == 0) {
	        $get(cartTotalsDiscountId).parentNode.style.display = 'none';
	    }
	    else {
	        $get(cartTotalsDiscountId).parentNode.style.display = '';
	        $get(cartTotalsDiscountId).innerHTML = obj.Discount;
	    }
	}
	    
	HideLoadingDialog();
}
function DeliveryMethodChanged(express) {
	// Disable the confirm link
	var confirm = document.getElementById(confirmButton);
	confirm.disabled = true;

	ShowLoadingDialog();
	eXpress.Web.UI.Website.UiService.DeliveryMethodChanged(express, OnDeliveryMethodChanged);
}

function OnDeliveryMethodChanged(obj) {

	// Enable the confirm link
	var confirm = document.getElementById(confirmButton);
	confirm.disabled = false;

	// Payment Method
	if (obj.DisableOnlinePayment && obj.DisableOnlinePayment) {
		if (typeof paymentMethodOfflineControlId != "undefined")
			$get(paymentMethodOfflineControlId).disabled = true;
		if (typeof paymentMethodOnlineControlId != "undefined")
			$get(paymentMethodOnlineControlId).disabled = true;
	}
	else {
		if (obj.DisableOfflinePayment) {
			if (typeof paymentMethodOnlineControlId != "undefined") {
				$get(paymentMethodOnlineControlId).removeAttribute("disabled");
			}
			if (typeof paymentMethodOfflineControlId != "undefined") {
				if ($get(paymentMethodOfflineControlId).checked) {
					if (typeof paymentMethodOnlineControlId != "undefined") {
						$get(paymentMethodOnlineControlId).checked = true;
					}
				}
				$get(paymentMethodOfflineControlId).disabled = true;
			}
		}
		else {
			if (typeof paymentMethodOfflineControlId != "undefined") {
				$get(paymentMethodOfflineControlId).removeAttribute("disabled");
			}
			if (obj.DisableOnlinePayment) {
				if (typeof paymentMethodOnlineControlId != "undefined") {
					$get(paymentMethodOnlineControlId).disabled = true;
				}
				if (typeof paymentMethodOfflineControlId != "undefined") {
					$get(paymentMethodOfflineControlId).checked = true;
				}
			}
		}
	}

	// Cart Totals
	$get(cartTotalsProductsId).innerHTML = obj.TotalBeforeCost;
	$get(cartTotalsShippingId).innerHTML = obj.Shipment;
	$get(cartTotalsHandlingId).innerHTML = obj.Handling;
	$get(cartTotalsVatId).innerHTML = obj.Vat;
	$get(cartTotalsTotalId).innerHTML = obj.Total;
	if (parseFloat(obj.Discount) == 0) {
		$get(cartTotalsDiscountId).parentNode.style.display = 'none';
	}
	else {
		$get(cartTotalsDiscountId).parentNode.style.display = '';
		$get(cartTotalsDiscountId).innerHTML = obj.Discount;
	}

	HideLoadingDialog();
}
function CheckTermsClicked(btn) {
	var termsCheck = $get(cartTotalsTermsId);
	if (termsCheck) {
		if (termsCheck.checked == false) {
			lalert('Cart.Checkout.TermsNotChecked');
		}
		else {
			// Disable all checkboxes
			var boxes = document.getElementsByTagName("input");
			for (var i = 0; i < boxes.length; i++) {
				boxes[i].style.visibility = 'hidden';
			}
			DisableExpressButton(btn);
		}
		return termsCheck.checked;
	}
	return false;
}

function CallDeleteCartItem(lineId, itemId) {
	eXpress.Web.UI.Website.UiService.GetCartDeleteItemDialog(lineId, itemId, RenderWarnDialog);
}

function CallDeleteOption(optionId, itemId) {
	eXpress.Web.UI.Website.UiService.GetCartDeleteOptionDialog(optionId, itemId, RenderWarnDialog);
}

function DeleteCartItem(lineId, itemId) {
	var params = new eXpress.Web.UI.Website.CartFunctionResult()
	params.LineId = lineId
	params.ItemId = itemId

	eXpress.Web.UI.Website.UiService.DeleteCartItem(params, OnCartDeleteActionComplete);
}

function DeleteOption(optionId, itemId) {
	var params = new eXpress.Web.UI.Website.CartFunctionResult()
	params.OptionId = optionId
	params.ItemId = itemId

	eXpress.Web.UI.Website.UiService.DeleteOption(params, OnCartDeleteActionComplete);
}

function DeleteCartPrintLine(lineId) {
	var params = new eXpress.Web.UI.Website.CartFunctionResult()
	params.LineId = lineId
	params.ItemId = null

	eXpress.Web.UI.Website.UiService.DeleteCartPrintLine(params, OnCartDeleteActionComplete);
}

function OnCartDeleteActionComplete(obj) {
    if (obj.Result) {
        document.location = obj.CartUrl;
		//window.location.reload();
	}
	CloseDialog();
}
