/* Kerstin Florian Website JS Functions */
/* Copyright Kerve Design */

/* SHOPPING CART FUNCTIONS */

/* Remove Item */
function removeCartItem(thisid) {
	if(confirm('Are you sure you want to remove this item?')) {
		document.location.href='/cart/?product=remove&item='+thisid;
	}
}

/* Check quantity values */
function updateCartItem(cart_item) {
	var filter  = /^([a-zA-Z0\.\-\+\=\_\|\?\/\>\<\,\!\"\£\$\%\^\&\*\(\)])+$/;
	var filter2  = /^(\-[1-9])+$/;
	var filter3  = /^([1-9]+\.[1-9]+)+$/;
	var filter4  = /^(\-[1-9]+\.[1-9]+)+$/;
	var cart_id = 'product_' + cart_item;
	if((!filter.test(document.getElementById(cart_id).quantity.value)) && (!filter2.test(document.getElementById(cart_id).quantity.value)) && (!filter3.test(document.getElementById(cart_id).quantity.value)) && (!filter4.test(document.getElementById(cart_id).quantity.value)) && (document.getElementById(cart_id).quantity.value != '')) {
		document.getElementById(cart_id).submit();
	} else {
		if(document.getElementById(cart_id).quantity.value == '0') {
			removeCartItem(document.getElementById(cart_id).pid.value);
		} else {
			alert('Invalid entry');
		}
	}
}

/* update cart shipping values */
function updateCartShipping() {
	document.cart_shipping.submit();
}

/* update cart promo values */
function updateCartPromo() {
	document.cart_promo.submit();
}

/* update cart giftwrapping values */
function updateCartGiftWrapping() {
	document.cart_giftwrapping.submit();
}

/* add product to cart */
function addProduct(cart_id) {
	var buy_item = 'buy_'+cart_id;
	
	document.getElementById(buy_item).submit();
}

/* */
function goToCheckout() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	
	if (
		(document.cart_shipping.shipping.value != '')
	) {
		self.location='/cart/checkout.html';
	} else {
		if(document.cart_shipping.shipping.value == '') {
			errormessage += "  > Shipping Destination\n";
		}
		alert(errormessage);
	}
}

/* Place order */
function placeOrder() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (
		(document.cart_checkout.user_name.value != '') &&
		(document.cart_checkout.user_email.value != '') &&
		(filter.test(document.cart_checkout.user_email.value)) &&
		(document.cart_checkout.user_address.value != '') &&
		(document.cart_checkout.user_postcode.value != '') &&
		(document.cart_checkout.user_country.value != '')
	) {
		document.cart_checkout.submit();
	} else {
		if(document.cart_checkout.user_email.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.cart_checkout.user_email.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.cart_checkout.user_name.value == '') {
			errormessage += "  > Your name\n";
		}
		if(document.cart_checkout.user_address.value == '') {
			errormessage += "  > Your address\n";
		}
		if(document.cart_checkout.user_postcode.value == '') {
			errormessage += "  > Your postcode\n";
		}
		if(document.cart_checkout.user_country.value == '') {
			errormessage += "  > Your country\n";
		}
		alert(errormessage);
	}
}