var storeId = "yhst-91162705613769"; 
var fCartShow = (true) ? true : false;

var fCartLoadNum = 0;
var fCartLen = 0;



function writeCart(){
	var cartCookie = unescape(readCookie('eFloCart'));
	var eCartTotal = 0;
	var giftLeft = 0;
	var links = new Array();
	var titles = new Array();
	var pids = new Array();
	var prices = new Array();
	var units = new Array();
	var total = "";
	var cartEl = document.getElementById('eCrtE');
	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 = 0; // 1 = yes; 0 = no;

	//these variables are for the optional gift message module
	var giftEl = document.getElementById('cartFreeGift'); 
	var giftLevel = 300; 
	var giftStatement = "Congratulations!  You've qualified for a free gift with your purchase! <a href=\"javascript:OpenWin ('discount-popup.html');\">What's This?</a>";


	if (cartCookie && cartCookie != ""){
		var eachArray = cartCookie.split("^");
		if (fCartLoadNum > 0 && fCartLen == eachArray.length){reloadCart2()}
		if (eachArray[0] && eachArray[0].length > 0){
			var eachElement = eachArray[0].split("|:|");
			total = eachElement[0];
			eCartTotal = (total.substr(1) * 1);
      giftLeft = giftLevel - eCartTotal;

		}

		if (giftEl && eCartTotal > 0){
			if (eCartTotal > (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 ){
		    cartEl.innerHTML='';
		    var ecartBg = document.createElement('div');
		    ecartBg.setAttribute('id','ecartBg');
		    cartEl.appendChild(ecartBg);
		    var CartHeader = document.createElement('div');
		    CartHeader.setAttribute('id','CartHeader');
		    ecartBg.appendChild(CartHeader);
				CartHeader.innerHTML="Items in your Cart: <span>" + numItems + "</span>&nbsp;";

			// 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;
					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] != ""){
					var fcart = document.createElement('div');
					fcart.setAttribute('id','fcart' + i);
					ecartBg.appendChild(fcart);
					
					var cName = document.createElement('div');
					cName.setAttribute('id','cName');
					fcart.appendChild(cName);
					cName.innerHTML="<a href=\"" + links[i].toLowerCase() + "\">" + titles[i] + "</a>";
					
					var cartPrice = document.createElement('div');
					cartPrice.setAttribute('id','cartPrice');
					fcart.appendChild(cartPrice);
					cartPrice.innerHTML="Price: " + prices[i];
					
					var cQuant = document.createElement('div');
					cQuant.setAttribute('id','cQuant');
					fcart.appendChild(cQuant);
					cQuant.innerHTML=" Qty: " + units[i];
					}
				}

			// write subtotal	
			var fcTotal = document.createElement('div');
		    fcTotal.setAttribute('id','fcTotal');
		    ecartBg.appendChild(fcTotal);
				fcTotal.innerHTML="Sub Total: " + total;

			if (showCart == 1 ){
    			var vCart = document.createElement('div');
    		    vCart.setAttribute('id','vCart');
    		    ecartBg.appendChild(vCart);
    				vCart.innerHTML="<a href=\"" + cartURL + "\">View Cart</a>";
			}

			if (showChechout == 1 ){
    			var fcheckout = document.createElement('div');
    		    fcheckout.setAttribute('id','fcheckout');
    		    ecartBg.appendChild(fcheckout);
    				fcheckout.innerHTML="<a href=\"" + myCheckout + "\">Checkout &gt;&gt;</a>";
			}

		} // close test for numItems > 0 
	}// close test for cartCookie 
	 else {if(fCartLoadNum > 1 && fCartLoadNum > 2)reloadCart2()}
} // close writeCart function 

if (fCartShow) writeCart();

function reloadCart(){
  fCartLoadNum++;
	if (fCartShow) setTimeout('writeCart()',3500);
}

function reloadCart2(){
  if (fCartLoadNum = 2 ) return;
  fCartLoadNum++;
	if (fCartShow) setTimeout('writeCart()',1500);
}
