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 PickUpNotificationChanged(clientIdPrefix, notMethodId, enabledisableRb) {
	var notEmailTxtBox = $get(clientIdPrefix + 'notEmailTxtBox');
	var notGSMTxtBox = $get(clientIdPrefix + 'notGSMTxtBox');
	var rbEmail = $get(clientIdPrefix + 'rbEmail');
	var rbGSM = $get(clientIdPrefix + 'rbGSM');

	if (enabledisableRb) {
		if (notEmailTxtBox && notGSMTxtBox && rbEmail && rbGSM) {
			if (notMethodId == 1) {
				rbEmail.checked = true;
				rbGSM.checked = false;
				notGSMTxtBox.disabled = true;
				notEmailTxtBox.disabled = false;
			}
			else {
				rbGSM.checked = true;
				rbEmail.checked = false;
				notGSMTxtBox.disabled = false;
				notEmailTxtBox.disabled = true;
			}
		}
	}

	var checkedTextBox = (notMethodId == 1 ? notEmailTxtBox : notGSMTxtBox);
	//only update cart if input is valid
	if (CheckValidationForControl(checkedTextBox, checkedTextBox.getAttribute("validationGroup"))) {
		// Disable the confirm link
		var confirm = document.getElementById(confirmButton);
		confirm.disabled = true;
		ShowLoadingDialog();
		// update cart
		eXpress.Web.UI.Website.UiService.NotificationChanged(notMethodId, checkedTextBox.value, PickUpNotificationChangedCompleted);
	}
}

function PickUpNotificationChangedCompleted(result) {
    // 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.replace(',', '.')) == 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.replace(',', '.')) == 0) {
		$get(cartTotalsDiscountId).parentNode.style.display = 'none';
	}
	else {
		$get(cartTotalsDiscountId).parentNode.style.display = '';
		$get(cartTotalsDiscountId).innerHTML = obj.Discount;
	}

	HideLoadingDialog();
}
function CheckTermsClickedAndInputValid(btn) {
	var termsCheck = $get(cartTotalsTermsId);
	if (termsCheck) {
		if (termsCheck.checked == false) {
			lalert('Cart.Checkout.TermsNotChecked');
			return false;
		}
		else {
			if (TriggerValidationCallout("NotificationInput")) {
				// Disable all checkboxes
				var boxes = document.getElementsByTagName("input");
				for (var i = 0; i < boxes.length; i++) {
					boxes[i].style.visibility = 'hidden';
				}
				DisableExpressButton(btn);

				return true;
			}
			else {
				lalert('Cart.Checkout.PickUpInputNotValid');
				return false;
			}
		}
	}
	return false;
}

function ValidateRequiredNotificationInput(source, args) {
	if (source) {
		var controlToValidate = $get(source.getAttribute('controlToValidate'));

		if (controlToValidate && controlToValidate.disabled == false && controlToValidate.value.trim().length == 0) {
			args.IsValid = false;
		}
		else {
			args.IsValid = true;
		}
	}
}

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();
}

//Code for google Maps//
////////////////////////
var map;
function ShowGoogleMap(address, lat, lng) {
	var myOptions = {
		zoom: 15,
		//center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	if (lat && lng) {
		// set mat by lat and lng
		SetMarkerOnMap(new google.maps.LatLng(lat, lng), 'latLngPosition');
	}
	else {
		// get lat and lng by address (google webservice call)
		var geocoder = new google.maps.Geocoder();
		if (geocoder) {			
			geocoder.geocode({ 'address': address }, SetMarkerOnMap);
		}
	}
}

function SetMarkerOnMap(results, status) {
	var latLngPosition;
	if (status == 'latLngPosition') {
		latLngPosition = results;
	}
	else if (status == google.maps.GeocoderStatus.OK) {
		latLngPosition = results[0].geometry.location;
	}
	else {
		alert("Geocode was not successful for the following reason: " + status);
		return;
	}

	map.setCenter(latLngPosition);
	var marker = new google.maps.Marker({
		map: map,
		position: latLngPosition
	});
}

var pickUpAddress = "";
var pickUpLat = 0;
var pickUpLng = 0;
function CallGoogleMapsDialog(address, geoCodeAddress, lat, lng) {
	ShowLoadingDialog();

	pickUpAddress = geoCodeAddress
	pickUpLat = lat;
	pickUpLng = lng;
	eXpress.Web.UI.Website.UiService.GetGoogleMapsDialog(address, GoogleMapsDialogComplete);
}

function GoogleMapsDialogComplete(res) {
	CreateOverlayDialog(res, true, 'dialogMedium');
	ShowGoogleMap(pickUpAddress, pickUpLat, pickUpLng);
}