function changeStage(one, two, three) {
	//createCookie('stage','9&9',1);

	if(typeof(three) == 'undefined') { three = 'x' }

	var string = null;
		// Order Status
	if (readCookie('stage') != null) {

		// Read Cookie
		stageValues = readCookie('stage').split('&');
		
		// Process
		curStage = parseInt(stageValues[0]);
		curStatus = parseInt(stageValues[1]);
		curDetails = parseInt(stageValues[2]);
		
		if (one != 1 && one != 0) {
		
			if (one == 'x' || one < curStage) { 
		 		one = curStage
			}
			
		}
		if (two == 'x') { two = curStatus }
		if (three == 'x') { three = curDetails }
		
		string = one + '&' + two + '&0';
		
		createCookie('stage', string, 1);
		
				
	} else {
		
		string = '0&' + two + '&0';
		createCookie('stage',string ,1);
	}

}

function splitCookie(){
	
	// cookieProducts is the cookies split from the cookie
	var cookieProducts = new Array();
	
	// cartValues is the final array containing all of the information from the cookie
	var cartValues = new Array();
	cookieProducts = cookieContents.split('&');

	for ( var i=0, len=cookieProducts.length; i<len; ++i ){
		document.write(cookieProducts[i] + "<br />");
	}
	
	for ( var i=0, len=cookieProducts.length; i<len; ++i ){
		cartValues[i] = cookieProducts[i].split(',');
	}
	
	// Write out the contents
	for ( var i=0, len=cartValues.length; i<len; ++i ){
		document.write("ID: " + cartValues[i][0] + "<br />"); // ID
		document.write("Frame: " + cartValues[i][1] + "<br />"); // Frame
		document.write("Quantity: " + cartValues[i][2] + "<br />"); // Quantity
	}

}


function removeFromCart(id, frame, price){
	var cookieProducts = new Array();
	var cartValues = new Array();
	var singleCart = null;
	cookieString = '';
	cartSize = 0
	quantity = null
	cartPosition = null
	if(typeof(pageType) == 'undefined') { pageType = 'default' }
	
	if (readCookie('jade') != '') {
		
		// Open Cookie
		cookieProducts = readCookie('jade').split('&');
		for ( var i=0, len=cookieProducts.length; i<len; ++i ){
			cartValues[i] = cookieProducts[i].split(',');
		}
		

		if (singleCart != true) { 
	
			// Find a match Update or Delete as neccesary 
			for ( var i=0, len=cartValues.length; i<len; ++i ){
				
				if (cartValues[i][0] == id) {
					if (cartValues[i][1] == frame) {
						quantity = parseInt(cartValues[i][2], 10) - 1;
						
						if (quantity == 0) {
							if (cookieProducts.length == 1) {
								singleCart = true
								break;
							} else {
								cookieProducts.splice(i, 1);
								cartPosition = i;
								break;
							}
							
						} else {
							newEntry = id + "," + frame + "," + quantity;
							cookieProducts.splice(i, 1, newEntry);	
							cartPosition = i;
							break;
						}
						
					}
				}
				
			}
		}
	
	
		if (singleCart == true) {
			eraseCookie('jade')
		} else {				
		    cookieString = ''			
			// Merge Data and save to Cookie
			for ( var i=0, len=cookieProducts.length; i<len; ++i ){
				cartSize = cartSize + 1
				if (i == cookieProducts.length - 1) {
					cookieString = cookieString + cookieProducts[i];
				} else {
					cookieString = cookieString + cookieProducts[i] + "&";
				}
			}
		
			createCookie('jade',cookieString,1);	
			
		}
	
	}
	
	
	if (pageType == 'checkout') {
		
		if (singleCart == true) {
				
				screenDraw = "<div id='checkout-content'><ul class='shopping-cart'><li class='name'>Cart Empty</li></ul></div>"
				$("#checkout-content").replaceWith(screenDraw);
				$("#next-btn a").replaceWith("<a href='javascript:photo.php'>Back</a>");
				
		} else {
				
				if (quantity == 0) {
					$("ul.shopping-cart:eq(" + cartPosition + ")").remove(); //.empty();
				} else {
					
					$("ul.shopping-cart:eq(" + cartPosition + ") .price").replaceWith("<li class='price'>\&pound; " + (quantity * price) + "</li>");
					$("ul.shopping-cart:eq(" + cartPosition + ") .quantities").replaceWith("<li class='quantities'>" + quantity + "<small>x</small></li>");
					
				}
				
				totalPrice = totalPrice - price;
				$(".total-price").replaceWith("<li class='total-price'>\&pound;" + totalPrice + "</li>");
						
		}
			
				if (cartSize > 0) { 
					$(".nav ul #menu-cart").replaceWith("<li id='menu-cart' class='nav-small'><a href='checkout.php'>Shopping Cart (" + cartSize + ")</a></li>");
				} else {
					$(".nav ul #menu-cart").replaceWith("<li class='nav-small'><a href='checkout.php'>Shopping Cart Empty</a></li>");
				}
		
		
		
	} else {
	
			if (singleCart == true) {
				
				screenDraw = "<ul class='emptyCart'><li class='name'>Cart Empty</li></ul>"
				$("#small-cart ul").replaceWith(screenDraw);
				$("#checkout-btn a").replaceWith("<a href='#'>Cart Empty</a>");
				
			} else {
				
				if (quantity == 0) {
					$("#small-cart ul:eq(" + cartPosition + ")").remove(); //.empty();
				} else {
					$("#small-cart ul:eq(" + cartPosition + ") .quantities").replaceWith("<li class='quantities'>" + quantity + "<small>x</small></li>");
					
				}
				
				totalPrice = totalPrice - price;
				
				//if ($.browser.name != "msie" && $.browser.versionX != '6') { 
					$("#checkout-btn a").replaceWith("<a href='checkout.php'>Checkout - \&pound;" + totalPrice + "</a>");
				//} else {*/
					//$("#checkout-btn a").text("Checkout - \&pound;" + totalPrice);
				//}
				
				//$("#checkout-btn a").replaceWith("<a href='#'>Checkout - \&pound;" + totalPrice + "</a>");
			
			}
			
			if (cartSize > 0) { 
				$(".nav ul #menu-cart").replaceWith("<li id='menu-cart' class='nav-small'><a href='checkout.php'>Shopping Cart (" + cartSize + ")</a></li>");
			} else {
				$(".nav ul #menu-cart").replaceWith("<li class='nav-small'><a href='checkout.php'>Shopping Cart Empty</a></li>");
			}
				
				//} else {
				//echo("<li class='nav-small'><a href='checkout.php'>Shopping Cart Empty</a></li>");
	}
	
}

function addToCart(id,name,frame,price, override){
	
	var cookieProducts = new Array();
	var cartValues = new Array();
	cookieString = '';
	cartPosition = null;
	quantity = 0;
	cartSize = 0;
	frameName = null;
	//totalPrice = 0;
	updateIndex = null;
	startEmpty = null;
	if(typeof(pageType) == 'undefined') { pageType = 'default' }
	
	if(typeof(override) == 'undefined') { changeStage(1,1); }
	
	
	

	if (readCookie('jade') != null) {
		startEmpty = false;
		
		// Read Cookie
		cookieProducts = readCookie('jade').split('&');
		for ( var i=0, len=cookieProducts.length; i<len; ++i ){
			cartValues[i] = cookieProducts[i].split(',');
			
		}
		
		for ( var i=0, len=cartValues.length; i<len; ++i ){
			if (cartValues[i][0] == id) {
				if (cartValues[i][1] == frame) {
					quantity = parseInt(cartValues[i][2], 10) + 1;
					
					newEntry = id + "," + frame + "," + quantity;
					cookieProducts.splice(i, 1, newEntry);	
					
					//cookieProducts.splice(i, 1);
					cartPosition = i;
					//break 3;
				}
			}
		}
		
		if (cartPosition == null) {
			quantity = 1;
			cookieProducts[cookieProducts.length] = id + "," + frame + "," + quantity;	
		}
		// sort array numarically 
		//.sort(function(a,b){return a - b})
		//cookieProducts[cookieProducts.length] = id + "," + frame + "," + quantity;
		
		// Merge Array into Cookie Friendly String
		for ( var i=0, len=cookieProducts.length; i<len; ++i ){
			cartSize = cartSize + 1
			
			if (i == cookieProducts.length - 1) {
				cookieString = cookieString + cookieProducts[i];
			} else {
				cookieString = cookieString + cookieProducts[i] + "&";
			}
		}
	
		createCookie('jade',cookieString,1);
	
	
	} else {
		startEmpty = true;
		cookieString = id + "," + frame + ",1";
		createCookie('jade',cookieString,1);
		cartPosition = null;
	 	quantity = 1;
		cartSize = 1;
		
	}
	
	
	// Draw Page
		
	switch (frame) {
			
		case 1 :
			frameName = "Medium Unframed";
		break;
		
		case 2 :
			frameName = "Medium Framed";
		break;
		
		case 3 :
			frameName = "Large Unframed";
		break;
		
		case 4 :
			frameName = "Large Framed";
		break;
		
		
	}
		
		
	if (pageType == 'checkout') {
		
		if (cartPosition == null) {
			screenDraw = "<ul><li class='product-img'><img src='photo/sample/thumb/1.jpg' width='45px' height='45px' /></li><li class='quantities'>" + quantity + "<small>x</small></li><li class='name'><a href='photo.php?p=" + id + "'>" + name + "<br /><small>" +  frameName + "</small></li><li class='price'>\&pound;" + (quantity * price) + "</li><li class='adjust'><a onclick='javascript:addToCart(" + id + ",\"" + name + "\",2," + frame + ")'>+</a>&nbsp;<a class='minus' href='#' onclick='javascript:removeFromCart(" + id + "," + frame + "," + price + ")'>&minus;</a></li></ul>"
			
			
			if (startEmpty == true){ 
				$(".emptyCart").replaceWith(screenDraw);
			} else if (startEmpty == false){
				$("ul.shopping-cart ul:last").after(screenDraw);
			}
		
		} else {
			$("ul.shopping-cart:eq(" + cartPosition + ") .quantities").replaceWith("<li class='quantities'>" + quantity + "<small>x</small></li>");
			$("ul.shopping-cart:eq(" + cartPosition + ") .price").replaceWith("<li class='price'>\&pound; " + (quantity * price) + "</li>");
						
		}
		// Get Total Price
		totalPrice = totalPrice + price;
		$(".total-price").replaceWith("<li class='total-price'>\&pound;" + totalPrice + "</li>");
		$(".nav ul #menu-cart").replaceWith("<li id='menu-cart' class='nav-small'><a href='checkout.php'>Shopping Cart (" + cartSize + ")</a></li>");
	
	
	} else {
	
	
		if (cartPosition == null) {
			screenDraw = "<ul><li class='quantities'>" + quantity + "<small>x</small></li><li class='name'>" + name + "<br /><small>" +  frameName + "</small></li><li class='price'>\&pound;" + price + "</li><li class='adjust'><a href='#' onclick='javascript:removeFromCart(" + id + "," + frame + "," + price + ")'>&minus;</a></li></ul>"
			
			if (startEmpty == true){ 
				$(".emptyCart").replaceWith(screenDraw);
			} else if (startEmpty == false){
				$("#small-cart ul:last").after(screenDraw);
			}
		
		} else {
			$("#small-cart ul:eq(" + cartPosition + ") .quantities").replaceWith("<li class='quantities'>" + quantity + "<small>x</small></li>");
			
		}
		// Get Total Price
		totalPrice = totalPrice + price;
		
		//if ($.browser.name != "msie" && $.browser.versionX != '6') { 
			$("#checkout-btn a").replaceWith("<a href='checkout.php'>Checkout - \&pound;" + totalPrice + "</a>");
		//} else {
			//$("#checkout-btn a").text("Checkout - \&pound;" + totalPrice);
		//}
	
		/*$("#checkout-btn a").attr("href", "checkout.php");
		$("#checkout-btn a").text("Checkout - \&pound;" + totalPrice);*/
		
		$(".nav ul #menu-cart").replaceWith("<li id='menu-cart' class='nav-small'><a href='checkout.php'>Shopping Cart (" + cartSize + ")</a></li>");

	}
	
	
}


function readCart(){
	cookieProducts = Array();
	cookieString = '';
	 
	
	if (readCookie('jade') != null) {
		cookieProducts =  readCookie('jade').split('&');

		for ( var i=0, len=cookieProducts.length; i<len; ++i ){
			document.write(cookieProducts[i] + "<br />");
		}
	}
}


/*<script src='js/jadeCart.js' language='javascript' type='text/javascript' ></script>
<script type='text/javascript'>");

	if(isset($_GET['f'])) { 
		if ($_GET['f'] == "del") { 
			echo("eraseCookie('jade') ");
		} elseif ($_GET['f'] == "create") {
			echo("createCookie('jade','1,1,1&2,2,5&4,1,15',1);");
	
		}
		};
	
echo("//eraseCookie('jade');


cookieContents = readCookie('jade');
splitCookie();
//createCookie('jade','1,1,1&2,2,5&4,1,15',1);

</script>*/

function retimeCookie(name) {

	cookieString = readCookie(name);
	createCookie('jade', cookieString, 0.01);

}


//*************************************************************************************************
// Thanks to Peter-Paul Koch for these cookie functions! (http://www.quirksmode.org/js/cookies.html)
//*************************************************************************************************
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	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(name) {
	createCookie(name,"",-1);
}
