//createCookie() creates a cookie with standard settings. This function calls SetCookie.
//This function is called by the index page on load.
function createcookie()
{	
	SetCookie ("TalynPrice", " ", null, "/");
	SetCookie ("TalynShears", " ", null, "/");
	SetCookie ("TalynNum", "0", null, "/");
}

//This function displays the shopping cart
function DisplayCart()
{
	var ar_price = new Array(30);
	var ar_shears = new Array(30);
	var number;
	var str;
	var qt = '"';
	var index = 0;
	var shownumber = 0;
	var showstr;
	var i = 0;
	var OrderTotal = 0;

	var ShippingTotal;

//Get the shears list and store it in ar_shears
	str = GetCookie("TalynShears");
	ar_shears = str.split(",");

//Get the price list and store it in ar_price
	str = GetCookie("TalynPrice");
	ar_price = str.split(",");

//Get the number of items in the list and store it in number
	number = parseInt(GetCookie("TalynNum"));

//Open the html document to be written to by the JavaScript
	document.open();

	document.write ("<CENTER>");


	document.write ("<style type=" + qt + "text/css" + qt + ">");

	document.write ("#shoppingdiv {");
	document.write ("	border: 1px solid #ccc;");
	document.write ("	background-color: #f3f3f3;");
	document.write ("}");

	document.write ("</style>");

	document.write ("<div id=" + qt + "shoppingdiv" + qt + ">");

//Setup the HTML page
	document.write("<html>");
	document.write("<head>");

	document.write("<title>Shopping Cart</title>");
	document.write("</head>");

	
//Write the shopping cart to the document.
	document.write ("<body bgcolor=" + qt + "#ffffff" + qt + ">");
	document.write ("<font face=" + qt + "Charlemagne Std" + qt + "size="  + qt + "6" + qt +">");
	document.write ("<p>Talyn Shears - shopping cart:</p>");

	document.write ("<font face=" + qt + "Charlemagne Std" + qt + "size="  + qt + "2" + qt + ">");

	document.write ("<table border=" + qt + "0" + qt + " cellpadding=" + qt + "0" + qt + " cellspacing=" + qt + "0" + qt + " style=" + qt + "border-collapse: collapse" + qt + " bordercolor=" + qt + "#111111" + qt + " width=" + qt + "500" + qt + " id=" + qt + "AutoNumber1" + qt + ">");
	document.write ("<tr>");
	document.write ("<td bordercolor="+ qt + "#C0C0C0" + qt + " bgcolor=" + qt + "#C0C0C0" + qt + " width=" + qt + "296" + qt + ">ITEM</td>");
	document.write ("<td bordercolor=" + qt + "#C0C0C0" + qt + " bgcolor=" + qt + "#C0C0C0" + qt + " width=" + qt + "125" + qt + ">PRICE</td>");
	document.write ("<td bordercolor=" + qt + "#C0C0C0" + qt + " bgcolor=" + qt + "#C0C0C0" + qt + " width=" + qt + "79" + qt + " align=" + qt + "center" + qt + ">REMOVE</td>");
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td width=" + qt + "114" + qt + "> </td>");
	document.write ("<td width=" + qt + "135" + qt + "> </td>");
	document.write ("<td width=" + qt + "79" + qt + " align=" + qt + "center" + qt + "></td>");
	document.write ("</tr>");

//Loop through the number of items in the cart and print them to a table in on the page.
	while (index < number)
	{	
		shownumber = parseInt(index) + 1;
		showstr = shownumber + '';
  		document.write ("<tr>");
		document.write ("<td width=" + qt + "296" + qt + ">" + ar_shears[index] + "</td>");
		document.write ("<td width=" + qt + "125" + qt + ">" + ar_price[index] + "</td>");

//This adds the remove button. This button has a link to reload the shoppingcart page to redraw the cart after removal. 
//***Note: Remember that the index is actually part of an array so item 0 is really item 1. The array is indexed from zero.
		document.write ("<td width=" + qt + "79" + qt + " align=" + qt + "center" + qt + "><input type=" + qt + "button" + qt + " VALUE=" + qt + "Remove" + qt + " onclick=" + qt + "DeletefromCart(" + index + ")" + qt +"></td>");
		document.write ("</tr>");

		index = index + 1;
	}

	document.write("</table>");



	document.write ("<br>");
//complete the total calculation and the shipping calculationTotoal
	var ShippingTotal = (parseInt(number)-1) + 5;
	if (number == 0)
	{
		ShippingTotal = 0;
	}
	document.write ("<p><font face=" + qt + "Charlemagne Std" + qt + ">Shipping total: $" + ShippingTotal + "</font></p>");
	
	while (i < number)
	{
		OrderTotal = parseInt(OrderTotal) + parseInt(ar_price[i]);
		i = parseInt(i) + 1;
	}
	
	OrderTotal = parseInt(OrderTotal) + parseInt(ShippingTotal);
	
	document.write ("<p><font face=" + qt + "Charlemagne Std" + qt + ">order total: $ " + OrderTotal + "</font></p>");

//Add two buttons one for continue shopping and one for checkout
	document.write("<input type=" + qt + "button" + qt + " VALUE=" + qt + "Continue Shopping" + qt + " onclick=" + qt + "history.go(-1)" + qt +">");
        document.write("<input type=" + qt + "button" + qt + " VALUE=" + qt + "Checkout"          + qt + " onclick=" + qt + "location.href='Checkout.html'" + qt +">");
//close the document
	document.write("</body>");
	document.write("</html>");
	
	document.write("</div>");
	
	document.close();

}

//This function adds an item to the cart
function AddtoCart(Shear, Price)
{
	var ar_price = new Array(30);
	var ar_shears = new Array(30);
	var number;
	var str;

//Get the shears list and store it in ar_shears
	str = GetCookie("TalynShears");
	ar_shears = str.split(",");

//Get the price list and store it in ar_price
	str = GetCookie("TalynPrice");
	ar_price = str.split(",");

//Get the number of items in the list and store it in number
	number = parseInt(GetCookie("TalynNum"));

	ar_shears[number] = Shear;
	ar_price[number] = Price;
	number = number + 1;	
	
	SetCookie ("TalynPrice", ar_price, null, "/");
	SetCookie ("TalynShears", ar_shears, null, "/");
	SetCookie ("TalynNum", number, null, "/");

	location.href='ShoppingCart.html';

}

//This function deletes an item from the cart
function DeletefromCart(num)
{

	var ar_price = new Array(30);
	var ar_shears = new Array(30);
	var number;
	var str;

//Get the shears list and store it in ar_shears
	str = GetCookie("TalynShears");
	ar_shears = str.split(",");

//Get the price list and store it in ar_price
	str = GetCookie("TalynPrice");
	ar_price = str.split(",");

//Get the number of items in the list and store it in number
	number = parseInt(GetCookie("TalynNum"));

//Loop through the cart and remove the item and then back fill the arrays
	var i = parseInt(num);
	while (i <= 29)
	{
		ar_shears[i] = ar_shears[i+1];
		ar_price[i] = ar_price[i+1];
		i = i+1;
	}
	number = number - 1;	

	SetCookie ("TalynShears", ar_shears, null, "/");	
	SetCookie ("TalynPrice", ar_price, null, "/");
	SetCookie ("TalynNum", number, null, "/");
//Refresh the shopping cart
	location.href="ShoppingCart.html";
}

//This funtion is for debug purposes - it prints everything contained within the cookies
function AlertCart()
{
	var ar_price = new Array(30);
	var ar_shears = new Array(30);
	var number;
	var str;
	var sstr;

//Get the shears list and store it in ar_shears
	sstr = GetCookie("TalynShears");
	ar_shears = sstr.split(",");

//Get the price list and store it in ar_price
	str = GetCookie("TalynPrice");
	ar_price = str.split(",");

//Get the number of items in the list and store it in number
	number = GetCookie("TalynNum");

	alert(ar_shears);
	alert(ar_price);
	alert(number);
}


//
// “Internal” function to return the decoded value of a cookie
//

function getCookieVal(offset) 
{
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) 
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

//
// Function to correct for 2.x Mac date bug. Call this function to
// fix a date object prior to passing it to SetCookie.
// IMPORTANT: This function should only be called *once* for
// any given date object! See example at the end of this document.
//


function FixCookieDate (date) 
{
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0) 
	{ // Except on the Mac - ahead of its time
		date.setTime (date.getTime() - skew);
	}
}

//
// Function to return the value of the cookie specified by “name”.
// name - String object containing the cookie name.
// returns - String object containing the cookie value, or null if
// the cookie does not exist.
//

function GetCookie (name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) 
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) 
		{
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) 
		{
			break;
		}
	}
	return null;
}

//
// Function to create or update a cookie.
// name - String object containing the cookie name.
// value - String object containing the cookie value. May contain
// any valid string characters.
// [expires] - Date object containing the expiration data of the
// cookie. If omitted or null, expires the cookie at the end of the
// current session.
// [path] - String object indicating the path for which the cookie is
// valid.
// If omitted or null, uses the path of the calling document.
// [domain] - String object indicating the domain for which the cookie
// is valid. If omitted or null, uses the domain of the calling
// document.
// [secure] - Boolean (true/false) value indicating whether cookie
// transmission requires a secure channel (HTTPS).
//
// The first two parameters are required. The others, if supplied, must
// be passed in the order listed above. To omit an unused optional
// field, use null as a place holder. For example, to call SetCookie
// using name, value and path, you would code:
//
// SetCookie (“myCookieName”, “myCookieValue”, null, “/”);
//
// Note that trailing omitted parameters do not require a placeholder.
//
// To set a secure cookie for path “/myPath”, that expires after the
// current session, you might code:
//
// SetCookie (myCookieVar, cookieValueVar, null, “/myPath”, null,
// true);
//

function SetCookie (name,value,expires,path,domain,secure) 
{
	document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

// Function to delete a cookie. (Sets expiration date to start of epoch)
// name - String object containing the cookie name
// path - String object containing the path of the cookie to delete.
// This MUST be the same as the path used to create the
// cookie, or null/omitted if
// no path was specified when creating the cookie.
// domain - String object containing the domain of the cookie to
// delete. This MUST be the same as the domain used to
// create the cookie, or null/omitted if no domain was
// specified when creating the cookie.
//

function DeleteCookie (name,path,domain) 
{
	if (GetCookie(name)) 
	{
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

