// cartlinks.js
// This js contains the functions for creating links related to the cart at the 
// right side of the header
// author Rajib Das < rajirdas@in.ibm.com >
//
// Note: Pls take extreme care while modyfying 
//

	var TempHost=location.host;
	var hasLoginCookie = document.cookie.indexOf("UserID=Yes");
	var hasClientIDCookie = document.cookie.indexOf("ClientId=");
	var hasMiniCartCookie = document.cookie.indexOf("MiniCart=");
	//var hasCartItemCountCookie = document.cookie.indexOf("CartItemCount=");
	//var hasCartSubTotalCookie = document.cookie.indexOf("CartSubTotal=");
	//var hasCartCurrencyCookie = document.cookie.indexOf("CartCurrency=");
	var hasCustomItemCountCookie = document.cookie.indexOf("CustomItemCount=");
	
	if (hasClientIDCookie != -1 || hasLoginCookie != -1)
	{				
		document.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		document.write("<tr><td height=\"4\" colspan=\"3\"><img src=\"/images/spacer1.gif\" alt=\"\" width=\"1\" height=\"4\" border=\"0\"></td></tr>");
		if (hasMiniCartCookie != -1 || hasCustomItemCountCookie != -1){
			document.write("<tr>");
			document.write("<td width=\"10\" height=\"10\" class=\"shoppingbox\"><img src=\"/images/g_corner_top_left.gif\" width=\"10\" height=\"10\" alt=\"\" border=\"0\"></td>");
			document.write("<td height=\"26\" rowspan=\"2\" valign=\"middle\" class=\"shoppingbox\">");
			document.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
			document.write("<tr>");
			if (hasCustomItemCountCookie != -1)
			{
				var startcustomcount = hasCustomItemCountCookie + 16;	     // Start cookie value
				var endcustomcount = document.cookie.indexOf(";", startcustomcount); // End cookie value
				if (endcustomcount == -1) endcustomcount = document.cookie.length;
				var CustomItemCount = document.cookie.substring(startcustomcount, endcustomcount); //Extract value
				document.write("<td class=\"status\"><a href=\"https://"+TempHost+"/webapp/wcs/stores/servlet/?storeId=11001&langId=-1\" class=\"status\" title=\"Custom Items\">Custom Items</a>: <b>" + CustomItemCount + "</b> item(s),</td>");
			}
			
			if (hasMiniCartCookie != -1)
			{
				var startcount = hasMiniCartCookie + 9;	     // Start cookie value
				var endcount = document.cookie.indexOf(";", startcount); // End cookie value
				if (endcount == -1) endcount = document.cookie.length;
				//var CartItemCount = document.cookie.substring(startcount, endcount); //Extract value
				
				var miniCartCookieVal = document.cookie.substring(startcount, endcount); //Extract value
				// decode the extracted value
				miniCartCookieVal = decodeURI(miniCartCookieVal);
				var cartCookieArray = new Array();
				cartCookieArray = miniCartCookieVal.split("|");
				var CartItemCount = "";
				var CartSubTotal = "";
				var CartCurrency = "";
				if (null != cartCookieArray) {
					for (var i = 0; i < cartCookieArray.length; i++) {						
						if (i == 0) {
							CartItemCount = cartCookieArray[i];
						} 
						else if (i == 1) {
							CartSubTotal = cartCookieArray[i];
						}
						else if (i == 2) {
							CartCurrency = cartCookieArray[i];
						}				
					}
				}			
		
				document.write("<td><img src=\"/images/g_cart.gif\" width=\"20\" height=\"20\" alt=\"\"></td>");
				document.write("<td class=\"status\"><a href=\"https://"+TempHost + buildWcsUrl("CartDisplay" , "11001") + "\" class=\"status\" title=\"Shopping Cart\">Shopping Cart</a>");
				if (CartSubTotal != "")
				{
					/*
					var starttotal = hasCartSubTotalCookie + 13;
					var endtotal = document.cookie.indexOf(";", starttotal);
					if (endtotal == -1) endtotal = document.cookie.length;
					var CartSubTotal = document.cookie.substring(starttotal, endtotal);
					*/
					CartSubTotal = unescape(CartSubTotal);
					
					if (CartCurrency != "")
					{
						/*
						var startcur = hasCartCurrencyCookie + 13;
						var endcur = document.cookie.indexOf(";", startcur);
						if (endcur == -1) endcur = document.cookie.length;
						var CartCurrency = document.cookie.substring(startcur, endcur);
						*/
						document.write(": <b>" + CartItemCount + "</b> item(s), <b>" + CartSubTotal + "&nbsp;" + CartCurrency + "</b></td>");
					 }
				 }
				else
				{
				document.write(": <b>" + CartItemCount + "</b> item(s)</td>");
				}
			}
			document.write("</tr>");
			document.write("</table>");
			document.write("</td>");
			document.write("<td width=\"10\" height=\"10\" class=\"shoppingbox\"><img src=\"/images/g_corner_top_right.gif\" width=\"10\" height=\"10\" alt=\"\" border=\"0\"></td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td width=\"10\" height=\"16\" class=\"shoppingbox\"><img src=\"/images/spacer1.gif\" width=\"10\" height=\"1\" alt=\"\" border=\"0\"></td>");
			document.write("<td width=\"10\" height=\"16\" class=\"shoppingbox\"><img src=\"/images/spacer1.gif\" width=\"10\" height=\"1\" alt=\"\" border=\"0\"></td>");
			document.write("</tr>");
		}
		document.write("</table>");
	 }