// This script is the sole property of 4D Internet Solutions. Copyright 2007, 4D Internet Solutions, Inc. All rights reserved.
//read cookie
var cartCookie = unescape(readCookie('mssFloatingCart'));
var storeId = "oneofakindkids"; // add store id between quotes.


function writeCart(){
	var links = new Array();
	var titles = new Array();
	var pids = new Array();
	var prices = new Array();
	var units = new Array();
	var total = "";
	var cartURL = "http://order.store.yahoo.net/cgi-bin/wg-order?catalog=" + storeId;
	var myCheckout = "https://order.store.yahoo.net/ymix/MetaController.html?ysco_key_event_id=1&ysco_key_store_id=" + storeId + "&sectionId=ysco.ship-bill"
	var showCart = 1; // 1 = yes; 0 = no;
	var showChechout = 1; // 1 = yes; 0 = no;
	
	//these variables are for the optional gift message module
	var giftLevel = 300; // amount they need in their cart to get a free gift
	var giftEl = document.getElementById('cartFreeGift'); // id of element to be updated
	var giftStatement = "Congratulations!  You've qualified for a free gift with your purchase! <a href=\"javascript:OpenWin ('discount-popup.html');\">What's This?</a>";
	var mssCartTotal = 0;
	var giftLeft = 0;
	
	if (cartCookie && cartCookie != ""){
		var eachArray = cartCookie.split("^");
		if (eachArray[0] && eachArray[0].length > 0){
			var eachElement = eachArray[0].split("|:|");
			total = eachElement[0];
			mssCartTotal = (total.substr(1) * 1);
            giftLeft = giftLevel - mssCartTotal;
		}
		
		if (giftEl && mssCartTotal > 0){
			if (mssCartTotal > (giftLevel - .01)){
				giftEl.innerHTML=giftStatement;
			}
			else {
				giftEl.innerHTML= "&nbsp; Add $" + giftLeft.toFixed(2) + " more to your cart for your free gift! <a href=\"javascript:OpenWin ('discount-popup.html');\">What's This?</a>";
			}
		}
		
		if (eachArray[1] && eachArray[1].length > 0) {
			var numItems = eachArray[1].split("|:|").length - 1;
			} else {
				return null; 
		}
		// write floating cart header and number of items in cart	
		if (numItems > 0 ){
			document.write("<div class=\"fcartBG\">");
			document.write("<div class=\"floatingCartHeader\">Items in your Cart: <span>" + numItems + "</span>&nbsp;</div>");
			
			// loop through each array from cookie
			for (i = 0; i < eachArray.length -1; i++){
				if (eachArray[i] && eachArray[i] != ""){
					var eachElement = eachArray[i].split("|:|");	
					var x = 0;
					// build javascript arrays from cookie data
					for (j = 0; j < eachElement.length; j++){
						if (eachElement[j] && eachElement[j] != ""){
							if (i == 1){
								links[x] = eachElement[j]
								}
							if (i == 2){
								titles[x] = eachElement[j]; 
								}
							if (i == 3){
								pids[x] = eachElement[j]; 
								}
							if (i == 4){
								prices[x] = eachElement[j]; 
								}
							if (i == 5){
								units[x] = eachElement[j]; 
								}
							x = x + 1;
							}
						}
					}
				}
			//loop through each cookie array and write the cart  using the javascript arrays
			for (i = 0; i < numItems; i++){
				if (eachArray[i] && eachArray[i] != "" && links[i] && links[i] != ""){
					document.write("<div class=\"fcart\">");
					document.write("<div class=\"fcart-name\"><a href=\"" + links[i].toLowerCase() + "\">" + titles[i] + "</a></div>");
					document.write("<div class=\"fcart-price\">Price: " + prices[i] + "</div>");
					document.write("<div class=\"fcart-qty\"> Qty: " + units[i] + "</div>");
					document.write("</div>");
					}
				}
				
			// write subtotal	
			document.write("<div class=\"fcart-total\">Sub Total: " + total + "</div>");
		
			// write View Cart Link	
			
			if (showCart == 1 ){
				document.write("<div class=\"fviewCart\"><a href=\"" + cartURL + "\">View Cart</a></div>");
			}
		
			if (showChechout == 1 ){
				document.write("<div class=\"fcheckout\"><a href=\"" + myCheckout + "\">"); 
				document.write("Checkout&gt;&gt;</a></div>");
			}
			
			// Close fcartBG div
			document.write("</div>");
			
		
		} // close test for numItems > 0 
	} // close test for cartCookie 
} // close function 
