
// alterError - fixes a rounding bug in Netscape 2
function alterError(value) {
	var newPence = 0;
	var newPounds = 0;
	var newString = '0.00';
	if (value<=0.99) {
		newPounds = '0';
	} else {
		newPounds = parseInt(value);
	}
	newPence = value - newPounds;
	if (newPence>0)
	{
		newPence *= 100.00;
		newPence+=0.08;
		newPence = parseInt(newPence);
		if (newPence>=1)
		{
			newPence = parseInt(newPence);
		}
		else
		{
			newPence = 0;
		}
	}
	else
	{
		newPence = 0;
	}
	
	if (eval(newPence) <= 9) newPence='0'+newPence;
	newString = newPounds + '.' + newPence;
	return (newString);
}


function CheckCookies()
{
	index = document.cookie.indexOf("TheBasket");
	if (document.cookie.length==0)
	{
		index = document.cookie.indexOf("TheBasket");
		// add something to see if we can
		document.cookie="TheBasket=.";
	}
	if (document.cookie.length==0)
	{
		alert("You must have cookies enabled in your browser to place orders on this website");
	}
	
	// return true if cookies work
	return (document.cookie.length!=0);
}

// buyItem - adds an item to the shooping basket
function buyItem(newItem, ItemDesc, newPrice, newQuantity, sOption)
{
	if (!CheckCookies())
	{
		return false;
	}
	
	if (!sOption)
	{
		sOption="";
	}
	if (newQuantity <= 0) {
		rc = alert('Please Enter A Quantity');
		return false;
	}
	
	var promptstring = 'Add '+newQuantity+' x ';
	promptstring += ItemDesc + ' to basket';
	
	newQuantity = RemoveDuplicate(newItem, newQuantity,sOption);
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+","+ItemDesc+"+"+newPrice+"#"+sOption+'%'+newQuantity+"]";
	return true;
}

// resetShoppingBasket - resets to shopping basket to empty
function resetShoppingBasket() {
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket=.";
}

function RemoveDuplicate(newItem, newQuantity, newOption)
{
	var newItemList = null;
	var retval = 0;
	
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);
	itemlist = 0;
	for (var i = 0; (retval==0) && (i <= fulllist.length); i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemlist=itemlist+1;
			if ((theitem==newItem) && (sOption==newOption))
			{
				var quan = 0;
				quan = parseInt(newQuantity) + parseInt(thequantity);
				retval = quan;
				removeItem(itemlist,1);
			}
			
			
		} else if (fulllist.substring(i,i+1) == ',') {
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '+') {
			itemDesc = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '%') {
			sOption = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		
	}
	if (retval==0)
	{
		retval = newQuantity;
	}
	return retval;
}


// showItems() - displays shopping basket in a table
function showItems(TargetDoc) {
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);
	totprice = 0;
	TargetDoc.writeln('<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=3>');
	
	TargetDoc.writeln('<TR><TD bgcolor="#990000"><font Face="Arial"><font color="#ffffff"><b>Item</b></TD><TD bgcolor="#990000"><font color="#ffffff"><font Face="Arial"><b>Description</b></td><TD bgcolor="#990000"><font color="#ffffff"><font Face="Arial"><b>Color</b></td><TD bgcolor="#990000"><font color="#ffffff"><font Face="Arial"><b>Quantity</b></TD><TD bgcolor="#990000"><font color="#FFFFFF"><font Face="Arial"><CENTER><b>Cost Each</b></TD><TD bgcolor="#990000"><font Face="Arial"><font color="#ffffff"><Center><b>Total</b><TD bgcolor="#990000"><font color="#ffffff"><font Face="Arial">&nbsp</TD></TR>');
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
			TargetDoc.writeln('<tr><td bgcolor="#cccccc">'+theitem+'</td><td bgcolor="#cccccc">'+itemDesc+'</td><td bgcolor="#cccccc">'+sOption+'</td><td bgcolor="#cccccc" align=right>'+thequantity+'</td><td bgcolor="#cccccc" align=right>$'+top.alterError(theprice)+'</td><td bgcolor="#cccccc" align=right>$'+top.alterError(itemtotal)+'</td><td bgcolor="#cccccc"><a href="'+top.Main.location+'" onclick="top.removeItem('+itemlist+')"><font Face="Arial"><FONT SIZE="-2"><B><font color="990000"><B>Remove</B></a></td></tr>');
		} else if (fulllist.substring(i,i+1) == ',') {
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '+') {
			itemDesc = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '%') {
			sOption = fulllist.substring(itemstart, i);
			itemstart = i+1;
			if (sOption == "")
			{sOption="N/A";
			}
		}
		
	}
	
	if (itemlist==0)
	{
		TargetDoc.writeln('<tr><td bgcolor="#cccccc" colspan=7><b><FONT COLOR="#ff0000"><font Face="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your cart is empty.</b></td></tr>');
	}
	else
	{
		TargetDoc.writeln('<tr><td bgcolor="#cccccc" colspan=7 align=center><b>The <u>Actual</U> Shipping Charges Will Be Added To This Total</b></td>');
		TargetDoc.writeln('<tr><td bgcolor="#cccccc" colspan=5 align=right><b>Total</b></td><td bgcolor="#FF8B8B" colspan=2 align=left><B>$'+top.alterError(totprice)+'</td></B>');
	}
	TargetDoc.writeln('</TABLE>');
}

function WriteTotal(TargetDoc) 
{
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);
	totprice = 0;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
		} else if (fulllist.substring(i,i+1) == '#') {
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} 
		else if ((fulllist.substring(i,i+1) == ',') || 
			(fulllist.substring(i,i+1) == '+') || 
				(fulllist.substring(i,i+1) == '%'))
		{
			itemstart = i+1;
		} 
		
	}
	TargetDoc.writeln('<FONT FACE="ARIAL" FONT COLOR="#FF0000" FONT SIZE="-1"><B>Total Spent So Far $'+top.alterError(totprice)+'</FONT></B>');
}


function removeItem(itemno, stopnav) {
	newItemList = null;
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			theitem = fulllist.substring(itemstart, itemend);
			itemlist=itemlist+1;
			if (itemlist != itemno) {
				newItemList = newItemList+'['+fulllist.substring(itemstart, itemend)+']';
			}
		}
	}
	index = document.cookie.indexOf("TheBasket");
	document.cookie="TheBasket="+newItemList;
	if (stopnav==0)
	{
		// trigger a refresh
		top.Main.location = top.Main.location;
		return true;
	}
	
	//top.frames[0].location = "bbasket.html";
}

// clearBasket() - removes all items from the basket
function clearBasket() {
	if (confirm('Are you sure you wish to clear the basket')) {
		index = document.cookie.indexOf("TheBasket");
		document.cookie="TheBasket=.";
		// trigger a refresh
		top.Main.location = top.Main.location;
		return true;
		
	}
}




function insertItemList(TargetDoc, MailForm) {
	
	index = document.cookie.indexOf("TheBasket");
	countbegin = (document.cookie.indexOf("=", index) + 1);
	countend = document.cookie.indexOf(";", index);
	if (countend == -1) {
		countend = document.cookie.length;
	}
	fulllist = document.cookie.substring(countbegin, countend);
	totprice = 0;
	
	itemlist = 0;
	var lineCount = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
			lineCount=lineCount+1;
			if (MailForm)
			{
				TargetDoc.writeln('<input type="hidden" name="quantity'+lineCount+'" value="'+thequantity+'">');
				TargetDoc.writeln('<input type="hidden" name="itemId'+lineCount+'" value="'+theitem+'">');
				TargetDoc.writeln('<input type="hidden" name="itemDesc'+lineCount+'" value="'+itemDesc+'">');
				TargetDoc.writeln('<input type="hidden" name="itemOption'+lineCount+'" value="'+sOption+'">');
				TargetDoc.writeln('<input type="hidden" name="itemPrice'+lineCount+'" value="'+top.alterError(theprice)+'">');
				TargetDoc.writeln('<input type="hidden" name="itemTotal'+lineCount+'" value="'+top.alterError(itemtotal)+'">');
			}
			else
			{
				TargetDoc.writeln('<tr><td bgcolor="#cccccc" align=right>'+thequantity+'</td>');
				TargetDoc.writeln('<td bgcolor="#cccccc" >'+theitem+'</td>');
				TargetDoc.writeln('<td bgcolor="#ccccc">'+itemDesc+'</td>');
				TargetDoc.writeln('<td bgcolor="#ccccc">'+sOption+'</td>');
				TargetDoc.writeln('<td bgcolor="#cccccc" align=right>'+top.alterError(theprice)+'</td>');
				TargetDoc.writeln('<td bgcolor="#cccccc" align=right>'+top.alterError(itemtotal)+'</td></tr>');
			}
		} else if (fulllist.substring(i,i+1) == ',') {
			theitem = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '+') {
			itemDesc = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '#') {
			theprice = fulllist.substring(itemstart, i);
			itemstart = i+1;
		} else if (fulllist.substring(i,i+1) == '%') {
			sOption = fulllist.substring(itemstart, i);
			itemstart = i+1;
		}
		
	}
	if (MailForm)
	{
		TargetDoc.writeln('<input type="hidden" name="total" value="'+top.alterError(totprice)+'">');
	}
	else
	{
		TargetDoc.writeln('<TR><td bgcolor="#cccccc" COLSPAN=5 align=right><B>Merchandise Total:</B></TD><td bgcolor="#FF8B8B" align=right><B>$'+top.alterError(totprice)+'</TD></TR>');
	}
}



