function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function removeCookie(name)
{
	document.cookie = name + "=0; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

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 0;
}

function FixCookieDate (date) // fix date on Mac
{
  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 setCookie (name,value,expire) 
{
	  outstr = name + "=" + escape (value) + "; path=/;";
		document.cookie = outstr;
}

function thankyouWindow(item)
{
		var theThankyouWindow = open("", "mWin", "width=237,height=145");
		theThankyouWindow.document.open();
		theThankyouWindow.document.writeln("<html><head><title>Items added to shopping basket</title>");
		theThankyouWindow.document.writeln("<link rel=stylesheet type='text/css' href='thanksstyle.css' title='thanksstyle.css'>");
		theThankyouWindow.document.writeln("<script>function exit(){this.close();}</script></head>");
		theThankyouWindow.document.writeln("<body text='#000000' bgcolor='#ffffff'>Your selected items have been added to your shopping basket.<br><br><a href=\"javascript:window.opener.document.location='shopping_basket.phtml';this.close();\">View Shopping Basket <img src='../images/basket.gif' align=absmiddle border=0 width=24 height=18></a><br><br><a href='javascript:this.close();'>Continue Shopping</a><br></body></html>");
		theThankyouWindow.document.close();
		theThankyouWindow.focus();
}

function pleaseselectWindow(item)
{
		var theThankyouWindow = open("", "mWin", "width=237,height=145");
		theThankyouWindow.document.open();
		theThankyouWindow.document.writeln("<html><head><title>No items selected</title>");
		theThankyouWindow.document.writeln("<link rel=stylesheet type='text/css' href='thanksstyle.css' title='thanksstyle.css'>");
		theThankyouWindow.document.writeln("<script>function exit(){this.close();}</script></head>");
		theThankyouWindow.document.writeln("<body text='#000000' bgcolor='#ffffff'>Please select the quantity you require.<br><br><a href='javascript:this.close();'>Continue Shopping</a><br></body></html>");
		theThankyouWindow.document.close();
		theThankyouWindow.focus();
}
function additemstobasket()
{
	var numchoices = document.addtobasket.bcounter.value;
	var numberofproducts = parseInt(getCookie("MORGANMOTORSnumberofitems"));
	if(isNaN(numberofproducts)) numberofproducts = 0;
	var totalchosen = 0;
	for (f=0; f<numchoices; f++)
	{
		var thequani = eval("document.addtobasket.quantity"+f+".selectedIndex;");
		var thequan = eval("document.addtobasket.quantity"+f+".options["+thequani+"].value;");
		var thevatexempt = document.addtobasket.basketvatexempt.value;
		if(parseInt(thequan)>0)
		{
			var orderstring = eval("document.addtobasket.basketchoice"+f+".value;");

			orderstring +=  "|" + thequan + "|" + thevatexempt + "|";
			setCookie("MORGANMOTORSbasketitem" + numberofproducts,orderstring,"false");
			numberofproducts++;totalchosen+=thequan;
		}
	}
	setCookie("MORGANMOTORSnumberofitems",numberofproducts,"false");
	if(totalchosen>0) thankyouWindow();
	else pleaseselectWindow();
}


	
