if (document.cookie.indexOf("Referrer") == -1)
{
	document.cookie = "Browser=" + detectBrowser();
	document.cookie = "LandingPage=" + document.location.href;
	if (!document.referrer){
		document.cookie = "Referrer=None";
	} else {
		document.cookie = "Referrer=" + document.referrer;
	}
	document.cookie = "TheBasket=.";
  document.cookie = "Postage=none";
}
	
// Browser detection ///////////////////////////////////////////////////////////////////////////////////////////////////
function checkIt(string, detect)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function detectBrowser() {
	var detect = navigator.userAgent.toLowerCase();
	var OS,browser,total,thestring;
	var version = 0;
	
	if (checkIt('konqueror', detect))
	{
		browser = "Konqueror";
		OS = "Linux";
		thestring = 'konqueror';
	}
	else if (checkIt('safari', detect)) { browser = "Safari"; thestring = 'safari'; }
	else if (checkIt('omniweb', detect)) { browser = "OmniWeb"; thestring = 'omniweb'; }
	else if (checkIt('opera', detect)) { browser = "Opera"; thestring = 'opera'; }
	else if (checkIt('webtv', detect)) { browser = "WebTV"; thestring = 'webtv'; }
	else if (checkIt('icab', detect)) { browser = "iCab"; thestring = 'icab'; }
	else if (checkIt('msie', detect)) { browser = "Internet Explorer"; thestring = 'msie'; }
	else if (!checkIt('compatible', detect))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
		thestring = 'compatible';
	}
	else browser = "An unknown browser";
	
	if (!version) version = detect.charAt(place + thestring.length);
	
	if (!OS)
	{
		if (checkIt('linux', detect)) OS = "Linux";
		else if (checkIt('x11', detect)) OS = "Unix";
		else if (checkIt('mac', detect)) OS = "Mac"
		else if (checkIt('win', detect)) OS = "Windows"
		else OS = "an unknown operating system";
	}
	
	return(browser + ' ' + version + ' on ' + OS);
}

// alterError - fixes a rounding bug in Netscape 4 /////////////////////////////////////////////////////////////////////
function alterError(value) {
	if (value<=0.99) {
  newPounds = '0';
  } else {
  newPounds = parseInt(value);
  }
  newPence = parseInt((value+.0008 - newPounds)* 100);
  if (eval(newPence) <= 9) newPence='0'+newPence;
  newString = newPounds + '.' + newPence;
  return (newString);
}

// buyItem - adds an item to the shopping basket ///////////////////////////////////////////////////////////////////////
function buyItem(newItem, newPrice, newQuantity) {
  if (newQuantity <= 0) {
  	rc = alert('The quantity entered is incorrect');
  } else {
  	if (confirm('Add '+newQuantity+' x '+newItem+' to cart?')) {
  		//index = document.cookie.indexOf("TheBasket");
	  	//countbegin = (document.cookie.indexOf("=", index) + 1);
			countbegin = (document.cookie.indexOf("TheBasket=") + 10);

	  	//countend = document.cookie.indexOf(";", index);
	  	countend = document.cookie.indexOf(";", countbegin);
	 		if (countend == -1) {
	 			countend = document.cookie.length;
	 		}
  		document.cookie="TheBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newQuantity+"]";
  	}
  }
}

// showItems() - displays shopping basket in a table ///////////////////////////////////////////////////////////////////
function showItems() {
	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;
	document.writeln('<FORM NAME="updateform">');
	document.writeln('<TABLE WIDTH="100%">');
	
	document.writeln('<TR BGCOLOR="#DCDDC1"><TD><b><font FACE="Arial" size="-1">Item</font></b></TD><TD width="10%"><b><font FACE="Arial" size="-2">Quantity</font></b></TD><TD width="6%"><b><FONT FACE="Arial" size="-2">Cost Each</font></b></TD><td width="6%"><b><font FACE="Arial" size="-2">Total Cost</font></b></td><TD COLSPAN="2"><center><b><font FACE="Arial" size="-2">Revise Order</font></b></center></TD></TR>');
	itemlist = 0;
	for (var i = 0; i <= fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			itemstart = i+1;
			thisitem = 1;
			} else if (fulllist.substring(i,i+1) == ']') {
			itemend = i;
			thequantity = fulllist.substring(itemstart, itemend);
			itemtotal = 0;
			itemtotal = (eval(theprice*thequantity));
			temptotal = itemtotal * 100;
			totprice = totprice + itemtotal;
			itemlist=itemlist+1;
			document.write('<tr BGCOLOR="#DCDDC1"><td><font FACE="Arial" size="-1">'+theitem+'</font></td>');	
			document.write('<td align=right><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=2></td><td align=right>'+theprice+'</td><td align=right>'+alterError(itemtotal)+'</td><td ALIGN="CENTER"><a href="javascript:removeItem('+itemlist+')"><FONT FACE="Arial" SIZE="-2">Delete</FONT></a></td><td  ALIGN="CENTER"><a href="javascript:amendItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><FONT FACE="Arial" SIZE="-2">Update Order</FONT></a></td></tr>');
			
			} else if (fulllist.substring(i,i+1) == '|') {
			if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			thisitem++;
			itemstart=i+1;
		}
	}
	
	document.writeln('<tr BGCOLOR="#DCDDC1"><td colspan=3><b><FONT FACE="Arial" size="-1">Total</font></b></td><td align=right>'+alterError(totprice)+'</td><td COLSPAN="2"></td></tr>');
	document.writeln('</TABLE>');
	document.writeln('</FORM>');
}

// amendItem() - amends item in shopping basket ////////////////////////////////////////////////////////////////////////
function amendItem(itemno, newquant) {
	if (newquant != 0) {
		newItemList = "";
		itemlist = 0;
		for (var i = 0; i <= fulllist.length; i++) {
	  	if (fulllist.substring(i,i+1) == '[') {
			  thisitem = 1;
			  itemstart = i+1;
			  fullstart = i+1;
		  } else if (fulllist.substring(i,i+1) == ']') {
			  itemend = i;
			  itemlist=itemlist+1;
			  if (itemlist != itemno) {
				  newItemList = newItemList+'['+fulllist.substring(fullstart, itemend)+']';
			  } else {
				  newItemList = newItemList + '['+theitem+'|'+theprice+'|'+newquant+']';
			  }
	  	} else if (fulllist.substring(i,i+1) == '|') {
	  		if (thisitem==1) theitem = fulllist.substring(itemstart, i);
			  if (thisitem==2) theprice = fulllist.substring(itemstart, i);
			  thisitem++;
			  itemstart=i+1;
		  }
	  }
	  index = document.cookie.indexOf("TheBasket");
	  document.cookie="TheBasket=."+newItemList;
	  self.location = "thebasket.html";
	} else {
		removeItem(itemno);
	}
}

// removeItem() - removes an item from the shopping basket /////////////////////////////////////////////////////////////
function removeItem(itemno) {
  newItemList = "";
  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;
  self.location = "thebasket.html";
}

// clearBasket() - removes all items from the basket ///////////////////////////////////////////////////////////////////
function clearBasket() {
  if (confirm('Are you sure you wish to clear your shopping basket?')) {
	  //index = document.cookie.indexOf("TheBasket");
	  document.cookie="TheBasket=.";
	  document.cookie="Postage=none";
	  self.location = document.location.href;
  }
}

// resetShoppingBasket - resets to shopping basket to empty
//function resetShoppingBasket() {
  //index = document.cookie.indexOf("TheBasket");
  //document.cookie="TheBasket=";
//}

// Return a cookie value ///////////////////////////////////////////////////////////////////////////////////////////////
function getCookieVal(what) {
	var start, end, val
	
	if (document.cookie.indexOf(what + '=') == -1) {
		val = 'null';
	} else {
		start = document.cookie.indexOf(what + '=') + what.length + 1;
		val = document.cookie;
		val = val.substring(start);
		if (val.indexOf(";") == -1) {
			end = val.length;
		} else {
			end = val.indexOf(";");
		}
		val = val.substring(0, end);
	}

	return(val);
}

