function doKeyUp(e) 
{
	recalc();
}
document.onclick = doKeyUp;
document.onkeyup = doKeyUp;
if(document.layers)
{
	document.captureEvents(Event.KEYUP);
}

var PLEN = 5;
var NAMES = new Array('CODE','ITEM','PRICE','QUANTITY','VATEXEMPT');
var cartitems = 0;
var itemcount = 0;
var orderbox="";
var numberofproducts=0;
var cartstrings;
var cartitems;
var outputbasket = "";
var vattotal = 0;
var subtotal = 0;

function remove(cookieid,formid)
{
	var theid = parseInt(cookieid);
	eval("document.basket.quantity"+formid+".value=0");
	updateCart();
	document.location.reload(true);
}

function doGetBasket()
{
	numberofproducts = parseInt(getCookie('MORGANMOTORSnumberofitems'));
	if( (isNaN(numberofproducts)) || (numberofproducts<1) ) numberofproducts=0;

if(numberofproducts>0)
{
	cartstrings = new Array(numberofproducts+1);
	for(var f=0;f<numberofproducts;f++)
	{
		cartstrings[f] = getCookie('MORGANMOTORSbasketitem'+f);
	}
	
	cartitems = new Array(numberofproducts);
	for(var f=0;f<numberofproducts;f++)
	{
		cartitems[f] = new Object();
	}
	
	for(var f=0;f<numberofproducts;f++)
	{
		for(var g=0;g<PLEN;g++)
		{
			cartitems[f][NAMES[g]] = cartstrings[f].substring(0,cartstrings[f].indexOf('|'));
			cartstrings[f] = cartstrings[f].substring((cartstrings[f].indexOf('|')) + 1);
		}
	}
	outputbasket += "<br><br><table width=500 cellpadding=4 cellspacing=0 border=0>\n";
	outputbasket += "<tr><td width=250><nobr><b>Item</b><img src='../images/idot.gif' width=200 height=1></nobr></td><td><b>Qty</b></td><td align=right><b>Price</b></td><td><b>&nbsp;&nbsp;Subtotal</b></td><td width=20>&nbsp;</td></tr>\n";
	outputbasket += "<tr height=1><td height=1 colspan=6><img src='../images/dot_white.gif' width=500 height=1></td><tr>";
	itemcount = 0;
	subtotal=0;
	grandtotal=0;

	heavyused = 0;
	for(var f=0;f<numberofproducts;f++)
	{
		if(cartitems[f]['QUANTITY']!=0)
		{
			outputbasket += "<tr>";
			outputbasket += "<td><input type='hidden' name='code" + f + "' value='" + cartitems[f]['CODE']+"'><input type='hidden' name='vatexempt" + f + "' value='" + cartitems[f]['VATEXEMPT']+"'>"+ cartitems[f]['ITEM']	+ "<input type='hidden' name='item" + f + "' value='" + cartitems[f]['ITEM'] + "'></td>\n";		
			outputbasket += "<td><input type='text' class='quantitybox' size=2 name='quantity" + f + "' value='" + cartitems[f]['QUANTITY'] + "' onChange='recalc()' onKeyUp='recalc()'></td>\n";		
			outputbasket += "<td align=right><nobr><span class='small'>&pound;" + cartitems[f]['PRICE']	+ "</span></nobr><input type='hidden' name='price" + f + "' value='" + format(cartitems[f]['PRICE']) + "'></td>\n";

			lineprice = parseFloat(cartitems[f]['PRICE'])  * parseInt(cartitems[f]['QUANTITY']);
			outputbasket += "<td><nobr><span class='small'>&pound;</span><input type='text' class='pricebox' size=5 name='lineprice" + f + "' value='" + format(lineprice) + "' onFocus='this.blur();'></nobr></td>\n";		
			outputbasket += "<td><a href=\"javascript:remove('" + f  + "','"+itemcount+"')\" onMouseOver=\"self.status='Remove this item'; return true;\" onMouseOut=\"self.status='';return true;\"><img src='../images/delete.gif' width=14 height=14 alt='Remove this item' border=0></a><input type='hidden' name='cookieid"+itemcount+"' value='"+f+"'>";
			outputbasket += "</tr>\n";
			itemcount++;
			subtotal = parseFloat(subtotal) + parseFloat(lineprice);
		}
	}

	outputbasket += "<tr height=1><td height=1 colspan=6><img src='../images/dot_white.gif' width=500 height=1></td><tr>";

	outputbasket += "<tr><td colspan=3>Goods net price total</td>\n";
	outputbasket += "<td><nobr>&pound;<input type='text' class='pricebox' size=5 name='nettotal' value='' onFocus='this.blur();'></nobr></td>";
	outputbasket += "<td>&nbsp;</td></tr>";

	outputbasket += "<tr><td right><nobr>Delivery</nobr></td>";
	outputbasket += "<td colspan=2>";
	outputbasket += "<select name='countrypostage' class='countrydropdown' onChange='setvat()'>";
        outputbasket += "<option value='0'>-- Please Select --</option>";
	outputbasket += "<option value='10'>United Kingdom</option>";
	outputbasket += "<option value='25'>Europe</option>";
	outputbasket += "<option value='50'>REST OF WORLD</option>";
	outputbasket += "</select><input type='hidden' name='country'>";
	outputbasket += "</td><td><nobr>&pound;<input type='text' name='postage' size=5 class='pricebox' value='' onFocus='this.blur();'></nobr></td><td>&nbsp;</td></tr>\n";

	outputbasket += "<tr><td colspan=3>Subtotal</td>\n";
	outputbasket += "<td><nobr>&pound;<input type='text' class='pricebox' size=5 name='subtotal' value='' onFocus='this.blur();'></nobr></td>";
	outputbasket += "<td>&nbsp;</td></tr>";
	
	outputbasket += "<td><font color=red>* VAT will be deducted automatically at payment stage if you can provide a valid VAT exemption number or if your order is being sent to a country outside of the EU.</font></td></tr>";
	

	
	outputbasket += "<tr><td><nobr>VAT &nbsp;&nbsp;<span class='vatnumtext'>(VAT No. if exempt: <input type='text' name='vatnum' class='vatnumbox'>&nbsp;)</span></nobr></td>";
	outputbasket += "<td colspan=2>";
	outputbasket += "<input type='hidden' name='vatcheck' onChange='recalc()'>";
	outputbasket += "</td><td><nobr>&pound;<input type='text' name='vat' size=5 class='pricebox' value='' onFocus='this.blur();'></nobr></td><td><font color=red>VAT</font></td></tr>\n";

	outputbasket += "<tr height=1><td height=1 colspan=6><img src='../images/dot_white.gif' width=500 height=1></td><tr>";

	outputbasket += "<tr><td colspan=3 align=right><b>Grand Total</b></td>\n";
	outputbasket += "<td><nobr>&pound;<input type='text' class='priceboxB' size=5 name='grandtotal' value='' onFocus='this.blur();'></nobr></td>";
	outputbasket += "<td>&nbsp;</td></tr>";

	outputbasket += "</table>";
	document.writeln('<form name="basket">');
	document.write("You have ");
	document.write("<b>" + itemcount+ "</b> item");
	if(itemcount!=1) document.write("s"); 
	document.write(" in your Shopping Basket");

	document.writeln(outputbasket);
	document.writeln("<br>You may edit your order by changing quantities, or remove an item by clicking <img src='../images/delete.gif' width=14 height=14 align=absmiddle><br>"); 
	
	document.writeln("</form>");

	document.writeln('<form name="orderdetails"  action="confirm_order.phtml" method="POST" onsubmit = "return checkShipping()">');
	document.writeln('<input type="hidden" name="logbox" value=""><input type="hidden" name="orderbox" value=""><input type="hidden" name="total" value="">');
	document.writeln('<input type="submit" name="submit" value="Order On-line" onclick="setorderbox()">');
	document.writeln('</form>');

	document.writeln("<table width=600 cellpadding=0 cellspacing=0 border=0><tr><td><span class='small'><font color=red><b>Please Note:</b></font> Your order will be despatched once all items are IN STOCK.  If you have ordered an item that is NOT IN STOCK then you will be contacted by the Morgan Motor Company via telephone or email to advise you of this.</font><br><br><b>Payment Methods</b><br><br>We accept Visa and Mastercard. We do not charge for any item until it is ready to ship. You may send your credit card information via phone, fax, snail mail or over the Internet.");
	document.writeln("<br><br>If you are shopping from North America or anywhere else, place your order and your credit card company will convert the transaction to US Dollars or your own currency.");
	document.writeln("<br><br><b>Delivery, VAT</b><br><br>Delivery will be charged at the following percentages in addition to the prices listed.<br>UK - 10%<br>Europe - 25%<br>Rest of the World - 50%<br><br>For your information, VAT is charged on delivery, books are exempt from VAT under current legislation. <br><br>If you are exempt from paying UK VAT, the final charge will be adjusted before taking payment from your card.");
	document.writeln("<br><br>Telephone +44 (0) 1684 573104. Fax +44 (0) 1684 892295. Alternatively write to us at Morgan Motor Company, Pickersleigh Road, Malvern Link, Worcestershire, WR14 2LL.</span></td></tr></table>");

	recalc();
}
else
{
	document.writeln("<br><br>Your shopping basket is empty<br>"); 
}
}

function checkShipping()
{
        if(document.basket.countrypostage.value == 0)
        {
                alert('Please select a delivery location');
                return false;
        }
        return true;
}

function setorderbox()
{
	recalc();
	subtotal = 0 ;
	logbox = "";
	orderbox = "<table width=600 cellpadding=3 cellspacing=1 border=0>";
	orderbox += "<tr bgcolor=#c5c6c8><td><b>Item</b></td><td><b>Qty</b></td><td align=center><b>Price</b></td><td align=right><b>Subtotal</b></td></tr>";
	for(var f=0; f<itemcount; f++)
	{
		orderbox += "<tr bgcolor=#c5c6c8>";
		orderbox += "<td>" + eval("document.basket.item" + f + ".value") + "</td>";		
		orderbox += "<td>" + eval("document.basket.quantity" + f + ".value") + "</td>";		
		orderbox += "<td align=center>&pound;" + eval("document.basket.price" + f + ".value") + "</td>";		
		orderbox += "<td align=right>&pound;" + eval("document.basket.lineprice" + f + ".value") + "</td>";		
		orderbox += "</tr>";
	
	if(f!=0){logbox+="~";}
	logbox += eval("document.basket.code" + f + ".value") + "#" + eval("document.basket.price" + f + ".value")+ "#" + eval("document.basket.quantity" + f + ".value");

	}
	orderbox += "<tr bgcolor=#c5c6c8><td colspan=3><b>Goods net price total</b></td>";
	orderbox += "<td align=right><b>&pound;"+document.basket.nettotal.value+"</b></td>";
	orderbox += "</tr>\n";

	orderbox += "<tr bgcolor=#c5c6c8><td colspan=2><b>Delivery</b></td>";
	orderbox += "<td align=right><b>"+document.basket.countrypostage.options[document.basket.countrypostage.selectedIndex].text+"</b></td>";
	orderbox += "<td align=right><b>&pound;"+document.basket.postage.value+"</b></td>";
	orderbox += "</tr>\n";

	orderbox += "<tr bgcolor=#c5c6c8><td colspan=3><b>Subtotal</b></td>";
	orderbox += "<td align=right><b>&pound;"+document.basket.subtotal.value+"</b></td>";
	orderbox += "</tr>\n";

	orderbox += "<tr bgcolor=#c5c6c8><td colspan=3><b>VAT</b> (VAT exemption number: "+document.basket.vatnum.value+")</td>";
	orderbox += "<td align=right><b>&pound;"+document.basket.vat.value+"</b></td>";
	orderbox += "</tr>\n";

	orderbox += "<tr bgcolor=#c5c6c8><td colspan=3><b>GRAND TOTAL</b></td>";
	orderbox += "<td align=right><b>&pound;"+document.basket.grandtotal.value+"</b></td>";
	orderbox += "</tr>\n";

	orderbox += "</table>"; 

	document.orderdetails.logbox.value = logbox;
	document.orderdetails.orderbox.value = orderbox;
	document.orderdetails.total.value = document.basket.grandtotal.value;
	return true;
}


function updateCart()
{
	numberofproducts = parseInt(getCookie('MORGANMOTORSnumberofitems'));
	if( (isNaN(numberofproducts)) || (numberofproducts<1) ) numberofproducts=0;
	for(f=0;f<numberofproducts+1;f++)
	{
		removeCookie("MORGANMOTORSbasketitem" + f);	
	}
	removeCookie("MORGANMOTORSnumberofitems");

	var newnumber = 0;
	for(f=0;f<itemcount;f++)
	{
		var thequantity = eval("document.basket.quantity"+f+".value");
		if(thequantity!=0)
		{
			var thecode = eval("document.basket.code"+f+".value");
			var theitem = eval("document.basket.item"+f+".value");
			var theprice = eval("document.basket.price"+f+".value");
			var thequantity = eval("document.basket.quantity"+f+".value");
			var thevatexempt = eval("document.basket.vatexempt"+f+".value");
			var revisedorderstring = "" + thecode + "|"+ theitem + "|" + theprice + "|" + thequantity + "|" + thevatexempt + "|"; 
			setCookie("MORGANMOTORSbasketitem" + newnumber,revisedorderstring,"false");
			newnumber++;
		}
	}

	setCookie("MORGANMOTORSnumberofitems",newnumber,"false");
}

function recalc()
{
	subtotal = 0;
	grandtotal = 0;
	vattotal = 0;

	productotherthantickets = 0;


		for(var f=0; f<itemcount; f++)
		{
			aquantity = eval("document.basket.quantity" + f + ".value");
			if(isNaN(aquantity)||aquantity < 0||aquantity == '') aquantity = '0.0';
			aprice = eval("document.basket.price" + f + ".value");
			atotal = parseInt(aquantity) * parseFloat(aprice);
			eval("document.basket.lineprice"+f+".value = '" + format(atotal) + "'");
			subtotal = subtotal + atotal;

			isvatexempt = eval("document.basket.vatexempt" + f + ".value");
			if(isvatexempt!='1'){vattotal = vattotal + atotal;}

			thecode = eval("document.basket.code" + f + ".value");	
			if(thecode!='PROefec218'){productotherthantickets=1;}

		}

		document.basket.nettotal.value = format(subtotal);
	
		selectedcountryI = document.basket.countrypostage.selectedIndex;
		postagerate = document.basket.countrypostage.options[selectedcountryI].value;


		if(productotherthantickets==0)
		{
			postagecost = 0;
		}
		else
		{
			postagecost = round_decimals((subtotal/100) * postagerate,2);
		}
	
		document.basket.postage.value = format(postagecost);

		subtotal = subtotal + parseFloat(postagecost);

		vattotal = vattotal + parseFloat(postagecost);

		document.basket.subtotal.value = format(subtotal);

		if(document.basket.vatcheck.checked)
		{
			vatcost = 0; 
			document.basket.vat.value = format(0);
			grandtotal = subtotal;
		}
		else
		{
			vatcost = round_decimals((parseFloat(vattotal)/100) * 17.5,2);
			document.basket.vat.value = format(vatcost);
			grandtotal = subtotal + vatcost;					
		}
		document.basket.grandtotal.value = "" + format(grandtotal);

}

function setvat()
{
	selectedcountryI = document.basket.countrypostage.selectedIndex;
	if(selectedcountryI ==3 )
	{
		document.basket.vatcheck.checked=1;
	}else{
        document.basket.vatcheck.checked=0;
    }
	recalc();
}

function format(number)
{
	if(number=='CALL')return 'CALL';
	if(number<1 && number>0) string = " 0" + number;
	else string = " " + number;
	if (string.indexOf('.') == -1)
	{
		return string + '.00';
	}
	point = string.length - string.indexOf('.');
	if (point > 3)
	{
		return string.substring(0, string.length - point + 3);
	}
	else if (point == 2)
	{
		return string + '0';
	}
	return string;
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return result3;
}

