function setValue(sName, sValue)
{
	document.cookie = sName + "=" + escape(sValue) + "; ";
}

function getValue(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if(sName == aCrumb[0])
		{
			if(aCrumb[1] == null)
				return "";
			else
				return unescape(aCrumb[1]);
		}
	}
	return "";
}

function writeAreaList()
{
	document.writeln('<form name="input">');
	document.writeln('<select name="area" onChange="selectArea()">');
	for( idx = 0; idx < PostalAreas.length; idx++ )
	{
		document.write('<option value="');
		document.write(idx + '">' + PostalAreas[idx]);
		document.writeln('</option>');
	}
	document.writeln('</select>');
	document.writeln('</form>');
}

function writePrice(price)
{
	document.write(price.toFixed(2));
}

function writePoundPrice(price)
{
	document.write('&pound;' + price.toFixed(2));
}

function getArea(value)
{
	var area = 0;
	var idx = value.indexOf(':');
	if(idx >= 0)
	{
		area = parseInt(value.substring(0,idx));
	}
	return area;
}

function selectArea()
{
	var list = "";
	var value = getValue("a");
	var idx = value.indexOf(':');
	if(idx >= 0)
	{
		list = value.substring(idx +1);
	}
	else
	{
		list = value;
	}
	setValue("a", document.input.area.value + ':' + list);
	location.reload(false);
}

function firstItem(value)
{
	var idx = -1;
	idx = value.indexOf(':');
	if(idx >= 0)
	{
		idx++;
	}
	else
	{
		idx = 0;
	}
	if(idx == value.length)
	{
		idx = -1;
	}
	return idx;
}

function nextItem(value, idx)
{
	return value.indexOf(',', idx);
}

function add(item)
{
	var value = getValue("a");
	value += item + ',';
	setValue("a", value);
}

function remove(item)
{
	var value = getValue("a");
	var start = firstItem(value);
	if(start >= 0)
	{
		var refresh = 0;
		var idx = nextItem(value, start);
		while(refresh == 0 && idx >= 0)
		{
			if(item == parseInt(value.substring(start,idx)))
			{
				var str = "";
				if(start > 0)
				{
					str = value.substring(0,start);
				}
				if(idx +1 < value.length)
				{
					str += value.substring(idx +1);
				}
				value = str;
				refresh = 1;
			}
			start = idx +1;
			idx = nextItem(value, start);
		}
	}
	if(refresh == 1)
	{
		setValue("a", value);
		location.reload(false);
	}
}

function removeAll()
{
	var value = getValue("a");
	var idx = firstItem(value);
	if(idx >= 0)
	{
		setValue("a", "");
		location.reload(false);
	}
}

function cookiesEnabled()
{
	var enabled = 1;
	if(document.cookie == "")
	{
		setValue("a", ":");
		if(document.cookie == "")
		{
			enabled = 0;
			alert("You must have cookies enabled - check your browser settings");
		}
		setValue("a", "");
	}
	return enabled;
}

function payPal(disabled)
{
	var value = getValue("a");
	var area = getArea(value);
	var start = firstItem(value);

	// disabled = true;
	document.write('<input type="submit" name="submit" value="Online via PayPal"');
	document.write('onClick="javascript:document.location = \'');
	document.write('https://www.paypal.com/cgi-bin/webscr');
	document.write('?cmd=_cart');
	document.write('&upload=1');
	document.write('&business=sales@gfhs.org.uk');

	if(start >= 0 && area > 0)
	{
		var itemNum = 0;
		var idx = nextItem(value, start);
		while(idx >= 0)
		{
			itemNum++;
			var item = parseInt(value.substring(start,idx));
			start = idx +1;
			idx = nextItem(value, start);
			document.write('&item_name_');
			document.write(itemNum);
			document.write('=');
			document.write(escape(Items[item +3]));

			document.write('&amount_');
			document.write(itemNum);
			document.write('=');
			writePrice(Items[item +2]);

			document.write('&shipping_');
			document.write(itemNum);
			document.write('=');
			writePrice(PostBands[Items[item +1] * PostBandFields + area -1]);
		}
	}

	document.write('&currency_code=GBP');
	document.write('\'"');
	if(disabled)
	{
		document.write(' disabled="disabled"');
	}
	document.writeln(' />');
}

function paymentMethods(disabled)
{
	document.writeln('<p></p><table align="center" border="0"><tr><th colspan="3">Choose a Payment Method</th></tr><tr>');
	document.write('<td><input type="button" name="cheque" value="Send a Cheque"');
	if(disabled)
	{
		document.write(' disabled="disabled"');
	}
	document.writeln(' onClick="javascript:document.location = \'PayByCheque.htm\'" /></td>');
	document.write('<td><input type="button" name="card" value="Credit/Debit Card by Post or Phone"');
	if(disabled)
	{
		document.write(' disabled="disabled"');
	}
	document.writeln(' onClick="javascript:document.location = \'PayByCard.htm\'" /></td>');
	document.writeln('<td>');
	payPal(disabled);
	document.writeln('</td></tr></table><p></p>');
}

function checkList(modify)
{
	var value = getValue("a");
	var area = getArea(value);
	var start = firstItem(value);
	var total = 0;

	if(start >= 0)
	{
		document.writeln('<table align="center" border="1">');
		document.write('<tr class="grey"><th>Title</th><th>Price</th>');
		document.writeln('<th>P&amp;P');
		if(modify == 1)
		{
			writeAreaList();
			document.input.area.selectedIndex = area;
		}
		else
		{
			document.write(' to the<br />');
			document.write(PostalAreas[area]);
		}
		document.write('</th><th>Total Price</th>');
		if(modify == 1)
		{
			document.write('<th>Remove from Order</th>');
		}
		document.writeln('</tr>');
		document.writeln('<colgroup><colgroup align="right"><colgroup align="right"><colgroup align="right"><colgroup align="center">');
		var idx = nextItem(value, start);
		while(idx >= 0)
		{
			var item = parseInt(value.substring(start,idx));
			start = idx +1;
			idx = nextItem(value, start);
			document.write('<tr><td>');
			document.write(Items[item +3]);
			document.write('</td><td>');
			var price = Items[item +2]
			writePoundPrice(price);
			document.write('</td><td>');
			if(area > 0)
			{
				if(PostBands[Items[item +1] * PostBandFields + area -1] == 0)
				{
					document.write('Included');
				}
				else
				{
					writePoundPrice(PostBands[Items[item +1] * PostBandFields + area -1]);
				}
			}
			document.write('</td><td>');
			if(area > 0)
			{
				price += PostBands[Items[item +1] * PostBandFields + area -1];
			}
			writePoundPrice(price);
			if(modify == 1)
			{
				document.writeln('</td><td>');
				document.write('<input type="submit" name="remove" value="Remove" onClick="remove(');
				document.write(item);
				document.write(')"></input>');
			}
			document.writeln('</td></tr>');
			total += price;
		}
		document.writeln('<tr><td colspan="3"><b>Total</b></td><td><b>');
		writePoundPrice(total);
		document.writeln('</b>');
		if(modify == 1)
		{
			document.writeln('</td><td>');
			document.write('<input type="button" name="removeAll" value="Remove All" onClick="removeAll()" />');
		}
		document.writeln('</td></tr>');
		document.writeln('</table>');

		if(area <= 0)
		{
			total = 0;
		}
	}
	else
	{
		document.writeln('<h3>No items have been selected for purchase.</h3>');
	}
	return total;
}

function sales(cat)
{
	var value = getValue("a");
	var area = getArea(value);

	document.write('<center><p>Add the items you wish to buy, then press <em>Place Order</em>.</p>');
	document.write('You will then get a choice of payment method (cheque, credit card or PayPal).</p>');
	document.write('<input style="clear:both" type="button" value="Place Order"');
	document.writeln(' onClick="document.location = \'CheckOut.htm\'" />');
	document.write('<p></p></center>');

	document.writeln('<table align="center" border="1">');
	document.writeln('<tr class="grey"><th>Title</th><th>Price</th><th>P&amp;P ');
	writeAreaList();
	document.input.area.selectedIndex = area;
	document.writeln('</th><th>Total Price</th><th>Add to Order</th></tr>');
	document.writeln('<colgroup><colgroup align="right"><colgroup align="right"><colgroup align="right"><colgroup align="center">');
	for(var item = 0; item < Items.length; item += ItemFields)
	{
		if(Items[item] == cat)
		{
			document.write('<tr><td>');
			if(Items[item + 4].length > 0)
			{
				document.write('<a name="');
				document.write(Items[item +4]);
				document.write('"></a>');
			}
			if(Items[item + 6].length > 0)
			{
				document.write('<em>');
				document.write(Items[item +6]);
				document.write('</em> ');
			}
			document.write('<b>');
			document.write(Items[item +3]);
			document.write('</b>');
			if(Items[item + 5].length > 0)
			{
				document.write('<br />');
				document.write(Items[item +5]);
			}
			if(Items[item + 7].length > 0)
			{
				document.write('<br />');
				document.write('<a href="Sales/');
				document.write(Items[item +7]);
				document.write('.jpg" target="_new">');

				document.write('<img src="Sales/Thumbs/');
				document.write(Items[item +7]);
				document.write('.jpg"> ');

				document.write('</a> ');

			}
			document.write('</td><td>');
			var price = Items[item +2]
			writePoundPrice(price);
			document.write('</td><td>');
			if(area > 0)
			{
				if(PostBands[Items[item +1] * PostBandFields + area -1] == 0)
				{
					document.write('Included');
				}
				else
				{
					writePoundPrice(PostBands[Items[item +1] * PostBandFields + area -1]);
				}
			}
			document.write('</td><td>');
			if(area > 0)
			{
				price += PostBands[Items[item +1] * PostBandFields + area -1];
			}
			writePoundPrice(price);
			document.writeln('</td><td>');
			document.write('<input type="submit" name="buy" value="Add" onClick="add(');
			document.write(item);
			document.write(')"></input>');
			document.writeln('</td></tr>');
		}
	}
	document.writeln('</table>');

	document.write('<center><p></p>');
	document.write('<input type="button" value="Place Order"');
	document.writeln(' onClick="document.location = \'CheckOut.htm\'" />');
	document.write('<p></p></center>');
}
