

var PLEN = 7;
var NAMES = new Array('CODE','ITEM','PRICE','PRICINGUNIT','PRICEPERUNIT','FULLPRICEPERUNIT','QUANTITY');

var cartitems = 0;
var itemcount = 0;
var orderbox="";
var numberofproducts=0;
var cartstrings;
var cartitems;
var outputbasket = "";

var vat = 0;
var subtotal = 0;
var grandtotal = 0;

function printquote()
{
	open('printquote.php', 'pWin', 'width=600,height=500,scrollbars=yes');
}

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

function dogetquote()
{
	numberofproducts = parseInt(getCookie('MERTONnumberofitems'));
	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('MERTONbasketitem'+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 = "";

	itemcount = 0;
	subtotal = 0;
	vat =0;
	grandtotal=0;
	outputbasket += "<table cellspacing=0 width=600 cellpadding=0 border=1 bordercolor='#222222'><tr><td bordercolor='#FFFFFF'> <table cellspacing=1 width=100% cellpadding=3 border=0><tr><td valign=top><b>Code</b></td><td valign=top><b>Description</b></td><td align=center valign=top><b>Quantity</b></td><td align=right valign=top><b>Full Price Per Unit</b></td><td align=center valign=top><b>Pricing Unit</b></td><td align=right valign=top><b>Net Price Per Unit</b></td><td align=right valign=top><b>Line Price</b></td><td>&nbsp;</td></tr>";
	outputbasket += "<tr><td colspan=8><img src='images/dot_green.gif' width=100% height=1></td></tr>\n";

	for(var f=0;f<numberofproducts;f++)
	{
		if(cartitems[f]['QUANTITY']!=0)
		{
			outputbasket += "<tr>";
			outputbasket += "<td valign=top>" + cartitems[f]['CODE']	+ "<input type='hidden' name='code" + f + "' value='" + cartitems[f]['CODE'] + "'></td>\n";		
			outputbasket += "<td valign=top>" + cartitems[f]['ITEM']	+ "<input type='hidden' name='item" + f + "' value='" + cartitems[f]['ITEM'] + "'></td>\n";		
			outputbasket += "<td valign=top align=center>" + cartitems[f]['QUANTITY']	+ "<input type='hidden' name='quantity" + f + "' value='" + cartitems[f]['QUANTITY'] + "'></td>\n";		
			outputbasket += "<td valign=top align=right>" + format(cartitems[f]['FULLPRICEPERUNIT'])	+ "<input type='hidden' name='fullpriceperunit" + f + "' value='" + format(cartitems[f]['FULLPRICEPERUNIT']) + "'></td>\n";		
			outputbasket += "<td valign=top align=center>" + cartitems[f]['PRICINGUNIT']	+ "<input type='hidden' name='pricingunit" + f + "' value='" + cartitems[f]['PRICINGUNIT'] + "'></td>\n";		
			outputbasket += "<td valign=top align=right>" + format(cartitems[f]['PRICEPERUNIT'])	+ "<input type='hidden' name='priceperunit" + f + "' value='" + format(cartitems[f]['PRICEPERUNIT']) + "'></td>\n";		

			outputbasket += "<td valign=top align=right>" + format(cartitems[f]['PRICE'])	+ "<input type='hidden' name='price" + f + "' value='" + format(cartitems[f]['PRICE']) + "'></td>\n";		
			outputbasket += "<td valign=top><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=19 height=19 alt='Remove this item' border=0></a><input type='hidden' name='cookieid"+itemcount+"' value='"+f+"'>";
			outputbasket += "</tr>\n";
			outputbasket += "<tr><td colspan=8><img src='images/dot_green.gif' width=100% height=1></td></tr>\n";
			itemcount++;
			subtotal = parseFloat(subtotal) + parseFloat(cartitems[f]['PRICE']);
		}
	}
	outputbasket += "<tr><td colspan=6 align=right><b>Subtotal:</b></td><td align=right><b>"+format(subtotal)+"</b><input type='hidden' name='subtotal' value='"+format(subtotal)+"'></td><td>&nbsp</td></tr>\n";

	vat = (subtotal/100)*17.5;
	grandtotal = vat + subtotal;
	outputbasket += "<tr><td colspan=6 align=right><b>VAT:</b></td><td align=right><b>"+format(vat)+"</b><input type='hidden' name='vat' value='"+format(vat)+"'></td><td>&nbsp</td></tr>\n";
	outputbasket += "<tr><td colspan=6 align=right><b>Grand Total:</b></td><td align=right><b>"+format(grandtotal)+"</b><input type='hidden' name='grandtotal' value='"+format(grandtotal)+"'></td><td>&nbsp</td></tr>\n";
	outputbasket += "</table></td></tr></table>";

	document.write("<tr><td>You have ");
	document.write("<b>" + itemcount+ "</b> item");
	if(itemcount!=1) document.write("s"); 
	document.write(" in your current quote.<br><br>");
	document.writeln(outputbasket);
	document.writeln("<br><div align=right>You may remove an item by clicking <img src='images/delete.gif' width=19 height=19 align=absmiddle></div><p>Quotations are valid for 30 days. All prices exclude VAT.</td></tr>"); 
	document.writeln("<tr><td>");
	document.writeln("Your Email Address: <input class='quoteaddress' type='text' name='email'>");
	document.writeln("</td></tr>");
	document.writeln("<tr><td align=center><input type='hidden' name='quotedetails' value=''><input type='submit' name='submit' value='Send Quote To Your Email Address' onclick='setquotebox()'></td></tr>");
	document.writeln("<tr><td align=center><input type='button' name='printquote' value='Print Quote' onclick=\"open('printquote.php', 'pWin', 'width=600,height=500,scrolling=yes');\"></td></tr>");
	var thecust = getCookie('MERTONlogin');
}
else
{
	document.writeln("<tr><td>Your current quote is empty.</td></tr>"); 
}
}

function setquotebox()
{
	orderbox = "<table width=500 cellpadding=0 cellspacing=0 bgcolor='#000000'><tr><td><table width=100% cellpadding=3 cellspacing=1 border=0>";
	orderbox += "<tr bgcolor='#ffffff'><td valign=top><b>Code</b></td><td valign=top><b>Description</b></td><td align=center valign=top><b>Quantity</b></td><td align=right valign=top><b>Full Price per Unit</b></td><td align=center valign=top><b>Pricing Unit</b></td><td align=right valign=top><b>Net Price Per Unit</b></td><td align=center valign=top><b>Line Price</b></td></tr>";
	for(var f=0; f<itemcount; f++)
	{
		orderbox += "<tr bgcolor='#ffffff'>";
		orderbox += "<td>" + eval("document.basket.code" + f + ".value") + "</td>";		
		orderbox += "<td>" + eval("document.basket.item" + f + ".value") + "</td>";		
		orderbox += "<td align=center>" + eval("document.basket.quantity" + f + ".value") + "</td>";		
		orderbox += "<td align=right>" + eval("document.basket.fullpriceperunit" + f + ".value") + "</td>";		
		orderbox += "<td align=center>" + eval("document.basket.pricingunit" + f + ".value") + "</td>";		
		orderbox += "<td align=right>" + eval("document.basket.priceperunit" + f + ".value") + "</td>";		
		orderbox += "<td align=right>" + eval("document.basket.price" + f + ".value") + "</td>";		
		orderbox += "</tr>";
	}

	orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>Subtotal:</b></td><td align=right><b>"+format(document.basket.subtotal.value)+"</b><input type='hidden' name='subtotal' value='"+format(document.basket.subtotal.value)+"'></td></tr>\n";
	orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>VAT:</b></td><td align=right><b>"+format(document.basket.vat.value)+"</b><input type='hidden' name='subtotal' value='"+format(document.basket.subtotal.value)+"'></td></tr>\n";
	orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>Grand Total:</b></td><td align=right><b>"+format(document.basket.grandtotal.value)+"</b><input type='hidden' name='subtotal' value='"+format(document.basket.subtotal.value)+"'></td></tr>\n";
	orderbox += "</table></td></tr></table>";
	document.basket.quotedetails.value = orderbox;

	return true;
}

function setorderbox()
{
	if(confirm("This will create a contract for Merton Timber to supply you with the goods quoted.\n\nDo you wish to proceed?"))
	{
		orderbox = "<table width=500 cellpadding=0 cellspacing=0 bgcolor='#000000'><tr><td><table width=100% cellpadding=3 cellspacing=1 border=0>";
		orderbox += "<tr bgcolor='#ffffff'><td valign=top><b>Code</b></td><td valign=top><b>Description</b></td><td align=center valign=top><b>Quantity</b></td><td align=right valign=top><b>Full Price per Unit</b></td><td align=center valign=top><b>Unit</b></td><td align=center valign=top><b>Pricing Unit</b></td><td align=right valign=top><b>Price Per Unit</b></td><td align=center valign=top><b>Line Price</b></td></tr>";
		for(var f=0; f<itemcount; f++)
		{
			orderbox += "<tr bgcolor='#ffffff'>";
			orderbox += "<td>" + eval("document.basket.code" + f + ".value") + "</td>";		
			orderbox += "<td>" + eval("document.basket.item" + f + ".value") + "</td>";		
			orderbox += "<td align=center>" + eval("document.basket.quantity" + f + ".value") + "</td>";		
		orderbox += "<td align=right>" + eval("document.basket.fullpriceperunit" + f + ".value") + "</td>";		
			orderbox += "<td align=center>" + eval("document.basket.pricingunit" + f + ".value") + "</td>";		
			orderbox += "<td align=right>" + eval("document.basket.priceperunit" + f + ".value") + "</td>";		
			orderbox += "<td align=right>" + eval("document.basket.price" + f + ".value") + "</td>";		
			orderbox += "</tr>";
		}

		orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>Subtotal:</b></td><td align=right><b>"+format(document.basket.subtotal.value)+"</b></td></tr>\n";
		orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>VAT:</b></td><td align=right><b>"+format(document.basket.vat.value)+"</b></td></tr>\n";
		orderbox += "<tr bgcolor='#ffffff'><td colspan=6 align=right><b>Grand Total:</b></td><td align=right><b>"+format(document.basket.grandtotal.value)+"</b></td></tr>\n";
		orderbox += "</table></td></tr></table>";
		document.ordernow.theorder.value = orderbox;
		document.ordernow.submit();
	}
}


function updatecart()
{
	numberofproducts = parseInt(getCookie('MERTONnumberofitems'));
	if( (isNaN(numberofproducts)) || (numberofproducts<1) ) numberofproducts=0;
	for(f=0;f<numberofproducts+1;f++)
	{
		removeCookie("MERTONbasketitem" + f);	
	}
	removeCookie("MERTONnumberofitems");

	var newnumber = 0;
	for(f=0;f<itemcount;f++)
	{
		var thequantity = eval("document.basket.quantity"+f+".value");
		if(thequantity!=0)
		{
			var productcode = eval("document.basket.code"+f+".value");
			var productname = eval("document.basket.item"+f+".value");
			var thetotalprice = eval("document.basket.price"+f+".value");
			var thepricingunit = eval("document.basket.pricingunit"+f+".value");
			var priceperunit = eval("document.basket.priceperunit"+f+".value");
			var fullpriceperunit = eval("document.basket.fullpriceperunit"+f+".value");
			var thequan = eval("document.basket.quantity"+f+".value");

			var revisedorderstring = "" + productcode + "|" + productname +"|" + thetotalprice + "|" + thepricingunit+ "|" +priceperunit +"|" + fullpriceperunit + "|" + thequan + "|";


			setCookie("MERTONbasketitem" + newnumber,revisedorderstring,"false");
			newnumber++;
		}
	}

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

function checkform()
{
	if(document.basket.email.value=='' || !isemailvalid(document.basket.email.value))
	{
		alert("Please enter a valid email address.");
		document.basket.email.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function isemailvalid(anaddress)
{
	var atsym = anaddress.indexOf('@');
	var period = anaddress.lastIndexOf('.');
	var space = anaddress.indexOf(' ');
	var length = anaddress.length - 1 ;

	if((atsym < 1) || (period <= atsym+1) || (period == length ) || (space  != -1)) return false;
	else return true;
}



function formatpad(number)
{
	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 format(original_number)
{
	var result1 = parseFloat(original_number) * Math.pow(10,2);
	var result2 = Math.round(result1);
	var result3 = result2 / Math.pow(10,2);
	return formatpad(result3);
}


function dogetquoteprintable()
{
	numberofproducts = parseInt(getCookie('MERTONnumberofitems'));
	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('MERTONbasketitem'+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 = "";

	itemcount = 0;
	subtotal = 0;

	outputbasket += "<table cellspacing=0 width=510 cellpadding=0 border=1 bordercolor='#222222'><tr><td bordercolor='#FFFFFF'> <table cellspacing=1 width=100% cellpadding=5 border=0><tr><td valign=top><b>Code</b></td><td valign=top><b>Description</b></td><td align=center valign=top><b>Quantity</b></td><td align=center valign=top><b>Full Price Per Unit</b></td><td align=center valign=top><b>Pricing Unit</b></td><td align=center valign=top><b>Net Price Per Unit</b></td><td align=right valign=top><b>Line Price</b></td><td>&nbsp;</td></tr>";
	outputbasket += "<tr><td colspan=7><img src='images/dot_green.gif' width=100% height=1></td></tr>\n";

	for(var f=0;f<numberofproducts;f++)
	{
		if(cartitems[f]['QUANTITY']!=0)
		{
			outputbasket += "<tr>";
			outputbasket += "<td valign=top>" + cartitems[f]['CODE']	+ "</td>\n";		
			outputbasket += "<td valign=top>" + cartitems[f]['ITEM']	+ "</td>\n";		
			outputbasket += "<td valign=top align=center>" + cartitems[f]['QUANTITY']	+ "</td>\n";		
			outputbasket += "<td valign=top align=center>" + format(cartitems[f]['FULLPRICEPERUNIT'])	+ "</td>\n";		
			outputbasket += "<td valign=top align=center>" + cartitems[f]['PRICINGUNIT']	+ "</td>\n";		
			outputbasket += "<td valign=top align=center>" + format(cartitems[f]['PRICEPERUNIT'])	+ "</td>\n";		

			outputbasket += "<td valign=top align=right>" + format(cartitems[f]['PRICE'])	+ "</td>\n";		
			outputbasket += "</tr>\n";
			outputbasket += "<tr><td colspan=7><img src='images/dot_green.gif' width=100% height=1></td></tr>\n";
			itemcount++;
			subtotal = parseFloat(subtotal) + parseFloat(cartitems[f]['PRICE']);
		}
	}
	vat = (subtotal/100)*17.5;
	grandtotal = vat + subtotal;
	outputbasket += "<tr><td colspan=6 align=right><b>Subtotal:</b></td><td align=right><b>"+format(subtotal)+"</b><input type='hidden' name='subtotal' value='"+format(subtotal)+"'></td></tr>\n";
	outputbasket += "<tr><td colspan=6 align=right><b>VAT:</b></td><td align=right><b>"+format(vat)+"</b><input type='hidden' name='subtotal' value='"+format(subtotal)+"'></td></tr>\n";
	outputbasket += "<tr><td colspan=6 align=right><b>Grand Total:</b></td><td align=right><b>"+format(grandtotal)+"</b><input type='hidden' name='subtotal' value='"+format(subtotal)+"'></td></tr>\n";
	outputbasket += "</table></td></tr></table>";
	document.writeln(outputbasket);


}
}