
//Check for existing cookie

var username = GetCookie('ShopCart');

var updateBtn = document.getElementById("updateCart");

if(updateBtn!=null)
{
	if(!username)
	{
	updateBtn.disabled=true;
	}
	else
	{
	updateBtn.disabled=false;
	}
}
		
if (!username) {

document.writeln("0 &nbsp;Items | $ 0 &nbsp;&nbsp;");


}
else
{

	//Test first, next step to list down items purchased
	var purchase = document.cookie.indexOf("ShopCart");
results = document.cookie.split(';');
results1 = results[1].replace("ShopCart=","").split("&");

//document.writeln("You have " + geSStPurchaseDetails(results1) + " product(s) in your Shopping Cart.");

totItems=0;
totCosts=0;

var i;

for(i=0;i<results1.length;i++)
{
r2 = results1[i].replace("ShopCart=","").split("-");

	if(totItems==0)
	{
		totItems = parseInt(r2[4]);
	}
	else
	{
		totItems = parseInt(totItems) + parseInt(r2[4]);
	}
	
	if(totCosts==0)
	{
		totCosts = parseInt(r2[5])*parseInt(r2[4]);
	}
	else
	{
		totCosts = parseInt(totCosts) + parseInt(r2[5])*parseInt(r2[4]);
	}
		
}

document.writeln(totItems + "&nbsp;Items | $" +  totCosts + " &nbsp;&nbsp;");


}


function getPurchaseDetails(results1)
{
	
	count = results1.length;
	return count;
	
}

function GetCookie (name) {
var arg = name;// + "=";
//var alen = arg.length;

//alert(document.cookie);
if(document.cookie.indexOf(arg)!=-1)
{
	return true;
}
else
{
	return false;
}
}


function removeCookie()
{
	var kill_time = new Date("January 1, 1970");
	var kill_string = "ShopCart=;expires=" + kill_time.toGMTString();
	document.cookie = kill_string; 
}

function updateShopCart()
{

	var purchase = document.cookie.indexOf("ShopCart");
	
	results = document.cookie.split(';');
	results1 = results[1].replace("ShopCart=","").split("&");
	
	var i;
	var purchasedItems="";
	for(i=0;i<results1.length;i++)
	{
	r2 = results1[i].replace("ShopCart=","").split("-");
	r3 = r2[0].split("=");
	total = document.getElementById("TR_" + r3[1]);
	// total.value gives you the item in the box
	
	//Find out how to update the boxed figures
	if(purchasedItems=="")
	{
	purchasedItems = r3[1]+ "-" + r2[1] + "-" + r2[2]+ "-" + r2[3]+ "-" + total.value + "-" + r2[5];
	}
	else
	{
	purchasedItems = purchasedItems + "," + r3[1]+ "-" + r2[1] + "-" + r2[2]+ "-" + r2[3]+ "-" + total.value + "-" + r2[5];
	}
	
	}
	document.location.href="checkout/PurchaseRecord.aspx?array=true&u=" + location.href + "&d=" + escape(purchasedItems);	
	
	//javascript:location.reload(true);
	
	
	
	
}






