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

/* SHOPPING CART FUNCTIONS */


/* Add item to cart */
/*
function cartBeerAdd(item,crate_id) {
	getData('/includes/cart_functions.php?action=add_beer&id='+item+'&crate_id='+crate_id,'');
	getData('/includes/product_search.php?category=beer','search_result');
	getData('/includes/product_search_beer.php','search_result_beerheader');
}
*/
function cartBeerAdd(unit_item,crate_id) {
	document.location = '&action=add_beer&id='+unit_item+'&crate_id='+crate_id;
}
/*
function cartBeerRemove(unit_item,crate_id) {
	getData('/includes/cart_functions.php?action=remove_beer&id='+unit_item+'&crate_id='+crate_id,'');
	getData('/includes/product_search.php?category=beer','search_result');
	getData('/includes/product_search_beer.php','search_result_beerheader');
}
*/
function cartBeerRemove(unit_item,crate_id) {
	document.location = '&action=remove_beer&id='+unit_item+'&crate_id='+crate_id;
}
/*
function cartBeerNewCrate() {
	getData('/includes/cart_functions.php?action=create_crate','');
	getData('/includes/product_search.php?category=beer','search_result');
	getData('/includes/product_search_beer.php','search_result_beerheader');
}
*/
function cartBeerNewCrate() {
	document.location = '&action=create_crate';
}
/*
function cartBeerAddCrate() {
	getData('/includes/cart_functions.php?action=add_crate','');
	getData('/includes/cart_functions.php?action=clear_crate','');
	getData('/includes/cart_functions.php?action=create_crate','');
	getData('/includes/product_search_beer.php','search_result_beerheader');
	getData('/includes/product_search.php?category=beer','search_result');
	getData('/includes/cart_functions.php?action=show&section=mini','store_mini_cart_items');
}
*/
function cartBeerAddCrate() {
	document.location = '&action=add_crate';
}
/*
function cartBeerClearCrate() {
	getData('/includes/cart_functions.php?action=clear_crate','');
	getData('/includes/product_search_beer.php','search_result_beerheader');
	getData('/includes/product_search.php?category=beer','search_result');
}
*/
function cartBeerClearCrate() {
	document.location = '&action=clear_crate';
}



/* Remove Item */
/*
function removeCartItem(item,size) {
	if(confirm('Are you sure you want to remove this item?')) {	
		getData('/includes/cart_functions.php?action=remove&id='+item+'&size='+size,'');
		
		getData('/includes/cart_functions.php?action=show&section=mini','store_mini_cart_items');
		getData('/includes/cart_functions.php?action=show&section=cart','cart_holder');
	}
}
*/
function removeCartItem(unit_item,size) {
	if(confirm('Are you sure you want to remove this item?')) {
		document.location = '?action=remove&id='+unit_item+'&size='+'&size='+size;
	}
}

/* Add item to cart */
/*
function cartAdd(unit_item,itemid) {
	var prod_id = 'product_' + itemid;
	if(document.getElementById(prod_id).quantity) {
		getData('/includes/cart_functions.php?action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value+'&quantity='+document.getElementById(prod_id).quantity.value,'store_mini_cart_items');
	} else {
		getData('/includes/cart_functions.php?action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value,'store_mini_cart_items');
	}
	getData('/includes/cart_functions.php?action=show&section=mini','store_mini_cart_items');
}
*/
function cartAdd(unit_item,itemid,start,end,pageno,view) {
	var prod_id = 'product_' + itemid;

	if(start == null) {
		if(document.getElementById(prod_id).quantity) {
			document.location = '&action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value+'&quantity='+document.getElementById(prod_id).quantity.value;
		} else {
			document.location = '&action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value;
		}
	} else {
		if(document.getElementById(prod_id).quantity) {
			document.location = '&action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value+'&quantity='+document.getElementById(prod_id).quantity.value+'&start='+start+'&end='+end+'&pageno='+pageno+'&view='+view;
		} else {
			document.location = '&action=add&id='+unit_item+'&size='+document.getElementById(prod_id).size.value+'&start='+start+'&end='+end+'&pageno='+pageno+'&view='+view;
		}
	}
}

/* Updat/Add a promotional code */
function cartAddPromo() {
	if(document.cart_promo.promo_code.value != '') {
		getData('/includes/cart_functions.php?action=promo&code='+document.cart_promo.promo_code.value,'cart_promo_message');
		
		/* refresh the mini cart and the main cart */
		getData('/includes/cart_functions.php?action=show&section=mini','store_mini_cart_items');
		getData('/includes/cart_functions.php?action=show&section=totals','cart_totals');
		/*getData('includes/cart_functions.php?action=show&section=promo','cart_promocode');*/
	} else {
		alert('Please insert a promotional code');
	}
}


/* Add item to cart */
function cartUpdate(item,size) {
	var filter  = /^([a-zA-Z0\.\-\+\=\_\|\?\/\>\<\,\!\"\£\$\%\^\&\*\(\)])+$/;
	var filter2  = /^(\-[1-9])+$/;
	var filter3  = /^([0-9]+\.[0-9]+)+$/;
	var filter4  = /^(\-[0-9]+\.[0-9]+)+$/;
	var cart_id = 'product_' + item + '_' + size;
	var maxquantityitems = 9999;
	
	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 < maxquantityitems) && 
	   (document.getElementById(cart_id).quantity.value != '')
	) {
		var quant = eval("document.product_" + item + '_' + size + ".quantity_" + item + ".value");
		getData('/includes/cart_functions.php?action=update&id='+item+'&size='+size+'&quantity='+quant,'');
		
		getData('/includes/cart_functions.php?action=show&section=mini','store_mini_cart_items');
		getData('/includes/cart_functions.php?action=show&section=cart','cart_holder');
	} else {
		if(document.getElementById(cart_id).quantity.value == '0') {
			removeCartItem(item,size);
		} else {
			alert('Invalid entry');
		}
	}
}

/* Confirm order, then send user to ProTX credit card pages */
function placeOrderSubmit() {
	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_send.user_name.value != '') &&
		(document.cart_send.user_email.value != '') &&
		(filter.test(document.cart_send.user_email.value)) &&
		(document.cart_send.user_address.value != '') &&
		(document.cart_send.user_postcode.value != '') &&
		(document.cart_send.user_country.value != '')
	) {
		document.cart_send.submit();
	} else {
		if(document.cart_send.user_email.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.cart_send.user_email.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.cart_send.user_name.value == '') {
			errormessage += "  > Your name\n";
		}
		if(document.cart_send.user_address.value == '') {
			errormessage += "  > Your address\n";
		}
		if(document.cart_send.user_postcode.value == '') {
			errormessage += "  > Your postcode\n";
		}
		if(document.cart_send.user_country.value == '') {
			errormessage += "  > Your country\n";
		}
		alert(errormessage);
	}
}


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