/* Drop Records Website JS Functions */
/* Copyright Kerve Design */

/* SHOPPING CART FUNCTIONS */

/* Remove Item */
function cartRemove(unit_item,size) {
	if(confirm('Are you sure you want to remove this item?')) {
		document.location = '/cart/index.html?action=remove&id='+unit_item;
	}
}

/* Add item to cart */
function cartAdd() {
	document.location = '/cart/index.html?action=add&id='+document.getElementById('form1').prod_id.value;
}

/* Update item in cart */
function cartUpdate(unit_item) {
	var filter  = /^([a-zA-Z0\.\-\+\=\_\|\?\/\>\<\,\!\"\£\$\%\^\&\*\(\)])+$/;
	var filter2  = /^(\-[1-9])+$/;
	var filter3  = /^([0-9]+\.[0-9]+)+$/;
	var filter4  = /^(\-[0-9]+\.[0-9]+)+$/;
	var maxquantityitems = 999;
	
	if(
	   (!filter.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter2.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter3.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter4.test(document.getElementById(unit_item).quantity.value)) && 
	   (document.getElementById(unit_item).quantity.value < maxquantityitems) && 
	   (document.getElementById(unit_item).quantity.value != '')
	) {
		var quant = eval("document." + unit_item + ".quantity.value");
		
		document.location = '/cart/index.html?action=update&id='+unit_item+"&quantity="+quant;
	} else {
		if(document.getElementById(unit_item).quantity.value == '0') {
			cartRemove(unit_item);
		} else {
			alert('Invalid entry');
		}
	}
}

function cartCollection(action) {
	if(action == 'show') {
		document.location = '/cart/index.html?action=calculator&event=show';
	}
	if(action == 'hide') {
		document.location = '/cart/index.html?action=calculator&event=hide';
	}
}

function cartChangeWhiteglove() {
	document.location = '/cart/index.html?action=whiteglove&event='+document.getElementById('cart_delivery_whiteglove').delivery_option_whiteglove.value;
}

function cartCheckPostcode(weight) {
	/*var postcode;
	postcode = document.getElementById('cart_delivery_postcode').delivery_option_postcode.value;
	getData('/includes/cart_functions_postcode.php?postcode='+postcode+'&weight='+weight,'cart_delivery_postcode_response');
	*/
	
	var errormessage = 'Please ensure that you have completed the following:\n';
	if (
		(document.cart_delivery_postcode.delivery_option_postcode.value != '')
	) {
		document.cart_delivery_postcode.submit();
	} else {
		if(document.cart_delivery_postcode.delivery_option_postcode.value == '') {
			errormessage += "  > Postcode\n";
		}
		alert(errormessage);
	}
	
}

/* Confirm order, then send user to ProTX credit card pages */
function cartCheckout() {
	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.shipaddress.fullname.value != '') &&
		(document.shipaddress.email.value != '') &&
		(filter.test(document.shipaddress.email.value)) &&
		(document.shipaddress.address.value != '') &&
		(document.shipaddress.postcode.value != '') &&
		(document.shipaddress.telephone.value != '') &&
		(document.shipaddress.tandc.checked == true) &&
		(document.shipaddress.country.value != '')
	) {
		document.shipaddress.submit();
	} else {
		if(document.shipaddress.email.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.shipaddress.email.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.shipaddress.fullname.value == '') {
			errormessage += "  > Your name\n";
		}
		if(document.shipaddress.address.value == '') {
			errormessage += "  > Your address\n";
		}
		if(document.shipaddress.postcode.value == '') {
			errormessage += "  > Your postcode\n";
		}
		if(document.shipaddress.country.value == '') {
			errormessage += "  > Your country\n";
		}
		if(document.shipaddress.telephone.value == '') {
			errormessage += "  > Your telephone\n";
		}
		if(document.shipaddress.tandc.checked == false) {
			errormessage += "  > Read and understood the terms and conditions\n";
		}
		alert(errormessage);
	}
}


/* Place order */
function cartCheckoutConfirm() {
	document.cart_confirm.submit();
}