//Global Variables
		var orders = new Array();
		var cart_contents = new Array();

function readCookie(name) { //Fucntion reads Cookie and returns as string when called as a variable
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(location,selector) { //Fucntion erases Cookie by adding an expiry that is already in the past

    	var date = new Date();
		date.setTime(date.getTime()+(-1*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		var value = "";
    document.cookie = location+"="+value+expires+"; path=/"; 
	if (selector != "false") {
		window.location.reload();	
	}
}

function check_existing_order() { //Function checks cookies to see if there is anything currently in the cookie basket
	var cookie_contents = readCookie('basket');
	if (cookie_contents) {
		//for index.htm page loads basket div if there are contents
		
		var cart_contents = cookie_contents.split("++");
			var total = 0;
			for (k=0; k < cart_contents.length; k++) {
				orders[k] = cart_contents[k].split(":");
				total = parseFloat(orders[k][1]) + total;
			}
			/* John this line creates the HTML for the DIV id="cart". The code within the quote marks 
			are all HTML based and will accept most things including css selectors. If you need to add
			an element that contains quote marks i.e a hyperlink use the character reference &quot; */
			/* This could also equally be changed to produce a series of relative DIV's to make layout
			easier, but we can discuss that closer to the finalised page */
			if(document.getElementById('cart')) {document.getElementById('cart').innerHTML = "Number of items in basket: <b>" + cart_contents.length + "</b><br /> Last Item Ordered: " + orders[cart_contents.length-1][0] + "<br />Total: £" + total.toFixed(2) + " <br />";}
		return cart_contents;
	} else {
		document.getElementById("basket").style.background = "#e0e0e0 url(images/basket.gif) no-repeat top right";
		/* This line empties the box if there is nothing in the cart, it should ultimately be used to
		push the basket DIV behind the 'How to Give' DIV for a better look */
		document.getElementById('cart').innerHTML = document.getElementById('cart').innerHTML = "Number of items in basket: <b>0</b><br /> Last Item Ordered: -<br />Total: £0.00 <br />";
		var cart_contents = new Array();
		return cart_contents;
	}
}

function cart(Name,Price) { //Function is called everytime someone selects an item to put into the basket
		var cart_contents = check_existing_order();
		var orders = new Array();
			// change background image
			document.getElementById("basket").style.background = "#e0e0e0 url(images/basket_full.gif) no-repeat top right";
		var total = parseFloat(Price);
		if (cart_contents != "") {

			for (j=0; j < cart_contents.length; j++) {
				var priz = cart_contents[j].split(":");
				total = parseFloat(priz[1]) + total;
			}
			orders = cart_contents;
		}

    	var date = new Date();
		date.setTime(date.getTime()+(60*60*1000));
		//cookie expires after an hour so that you get the instructions again.
		var expires = "; expires="+date.toGMTString();
		
					orders[cart_contents.length] = Name + ":" + Price;
					total_orders = orders.join("++");
					document.cookie = "basket="+total_orders+expires+"; path=/";
		/* This line is the same as the one above and but because it is called through a different 
		function it uses different variables, so it is necessary to change all the editing to match
		the line above, so the variables must remain the same */
		document.getElementById('cart').innerHTML = "Number of items in basket: <b>" + orders.length + "</b><br /> Last Item Ordered: " + Name + "<br />Total: £" + total.toFixed(2) + " <br />";
	}
	
function remove_item(item_val) { //Function removes items from the lytebox basket and saves the results to the cookie
	var arr = readCookie('basket').split("++");
	for(var k=0; k<arr.length;k++ ) { 
        if(k == parseFloat(item_val)) {arr.splice(k,1);}	
	}
		    	var date = new Date();
		date.setTime(date.getTime()+(60*60*1000));
		//cookie expires after an hour so that you get the instructions again.
		var expires = "; expires="+date.toGMTString();
		
		total_orders = arr.join("++");
		if (arr.length == "0") {
			eraseCookie('basket');
			eraseCookie('giftaid');
		} else {
		document.cookie = "basket="+total_orders+expires+"; path=/";
		}
		window.location.reload();	
}

function update_basket() { //Function is called when you leave the lytebox to makes sure the DIV based on the main page matches the basket page called by lytebox's lyteframe 
	var cart_contents = readCookie('basket')
	if (!cart_contents) {
	document.getElementById('cart').innerHTML = "";
	} else {
	check_existing_order();
	}
}

function update_items() { //Function takes the changes made in the basket and applies them to the cookie
    	var date = new Date();
		date.setTime(date.getTime()+(60*60*1000));
		//cookie expires after an hour so that you get the instructions again.
		var expires = "; expires="+date.toGMTString();
		
	var to = new Array();
	for(var l=0; l<orders.length;l++ ) { 
        to[l] = document.getElementById('to' + l).value;
		orders[l] = orders[l] + ":" + to[l];	
	}
	total_orders = orders.join("++");
	document.cookie = "basket="+total_orders+expires+"; path=/";
}

