function addToCart(id) {

	/*
	* Set working mouse pointer
	*/
	document.body.style.cursor = 'wait';

	if (window.location.protocol != 'https:') {
	
		var cartURL			= 'https://johanna-n-com.loopiasecure.com/do/addToCart.php?id=' + id;
		var iframe			= document.createElement('iframe');
		iframe.id			= id;
		/*
		* IE8
		*/
		if (iframe.attachEvent) {
			iframe.attachEvent('onload', function(e) {
				ajax('/do/addToCart.php?id=' + e.srcElement.id);
				document.location = document.location;
			});
		} else {
			iframe.onload		= function() {
				ajax('/do/addToCart.php?id=' + this.id);
				document.location = document.location;
			};
		}
		iframe.src			= cartURL;
		iframe.className	= 'cartIframe';
		document.body.appendChild(iframe);
		
	} else {
	
		ajax('/do/addToCart.php?id=' + id);
		document.location = document.location;
		
	}
	
}

function removeFromCart(id) {

	if (window.location.protocol != 'https:') {
	
		var cartURL			= 'https://johanna-n-com.loopiasecure.com/do/removeFromCart.php?id=' + id;
		var iframe			= document.createElement('iframe');
		iframe.id			= id;
		/*
		* IE8
		*/
		if (iframe.attachEvent) {
			iframe.attachEvent('onload', function(e) {
				ajax('/do/removeFromCart.php?id=' + e.srcElement.id);
				document.location = document.location;
			});
		} else {
			iframe.onload		= function() {
				ajax('/do/removeFromCart.php?id=' + this.id);
				document.location = document.location;
			};
		}
		iframe.src			= cartURL;
		iframe.className	= 'cartIframe';
		document.body.appendChild(iframe);
		
	} else {
	
		ajax('/do/removeFromCart.php?id=' + id);
		document.location = document.location;
		
	}
	
}

function updateCartContents(form) {

	var itemInputs = form.getElementsByTagName('input');
	var items = Array();
	for (var i = 0; i < itemInputs.length; i++) {
		if (itemInputs[i].type == 'text') {
			items[i] = itemInputs[i].name + '=' + itemInputs[i].value;
		}
	}
	
	if (window.location.protocol != 'https:') {
	
		var cartURL			= 'https://johanna-n-com.loopiasecure.com/do/updateCartContents.php?' + items.join('&');
		var iframe			= document.createElement('iframe');
		iframe.id			= 'updateCart';
		/*
		* IE8
		*/
		if (iframe.attachEvent) {
			iframe.attachEvent('onload', function(e) {
				ajax('/do/updateCartContents.php?' + items.join('&'));
				document.location = document.location;
			});
		} else {
			iframe.onload		= function() {
				ajax('/do/updateCartContents.php?' + items.join('&'));
				document.location = document.location;
			};
		}
		iframe.src			= cartURL;
		iframe.className	= 'cartIframe';
		document.body.appendChild(iframe);
		
	} else {
	
		ajax('/do/updateCartContents.php?' + items.join('&'));
		document.location = document.location;
		
	}
	
	/*
	* Don't send the form really since this is handled
	* by the onload handler of the iframe or after the
	* ajax call.
	*/
	return false;
	
}

function updateCartValue() {
	var toPay = parseFloat($('#finalCartTotal').html()) + parseFloat($('#finalCartShippingFee').html()) + parseFloat($('#finalCartPaymentMethodFee').html()) + parseFloat($('#finalCartServiceFee').html());
	$('#finalCartToPay').html(toPay.toFixed(2));
	var vat = toPay * 0.2;
	$('#finalCartVAT').html(vat.toFixed(2));
	/*
	* Update SEK value as well
	*/
	try {
		$('#toPayInSEK').html(ajax('/do/getCartValueInSEK.php'));
	} catch (err) {}
}

function setShippingFee(methodValue) {
	var url = '/do/getSetting.php?settingName=shipping_fee&settingValue=' + methodValue;
	var cost = ajax(url);
	$('#finalCartShippingFee').html(cost);
	updateCartValue();
}

function setServiceFee(serviceName) {
	var url = '/do/getSetting.php?settingName=service_fee&settingValue=' + serviceName;
	var cost = ajax(url);
	if ($('#serviceFee').css('display') == 'none') {
		$('#finalCartServiceFee').html(cost);
		$('#serviceFee').show();
	} else {
		$('#finalCartServiceFee').html('0');
		$('#serviceFee').hide();
	}
	updateCartValue();
}

function checkPaymentMethod(methodValue) {
	var url = '/do/getSetting.php?settingName=payment_method_fee&settingValue=' + methodValue;
	var cost = ajax(url);
	if (cost > 0) {
		$('#finalCartPaymentMethodFee').html(cost);
		$('#paymentMethodFee').show();
	} else {
		$('#finalCartPaymentMethodFee').html(0);
		$('#paymentMethodFee').hide();
	}
	updateCartValue();
}

