// Create the code for a single object. This doesn't necessarily have
// to be javascript, but doing so makes the shopping cart files
// much smaller

var levelArray = new Array
(
	"../",
	"../../",
	"../../../",
	"../../../../"
);

var VATRate = 20;

//------------------------------------------------------------------------------------
function createNoShippingButton (level)
{
}
//------------------------------------------------------------------------------------
function createPriceString (price)
{
	var price100 = Math.round (price * 100);
	var priceStr = "";

	if (Math.round (price) == price)
	{
//		document.writeln ('Price ' + price);
//		document.writeln ('Round ' + Math.round (price));
		priceStr = price + '.00';
	}
	else
	{
//		document.writeln ('Price100 ' + price100);
		if ((Math.round (price100 / 10)) * 10 == price100)
		{
//			document.writeln ('Round ' + Math.round (price100 / 10));
			priceStr = price100 / 100 + '0';
		}
		else
		{
			priceStr = price100 / 100 + '';
		}
	}

	return priceStr;
}
//------------------------------------------------------------------------------------
function createCartItem (code, name, price, level, button)
{
	var priceStr = "";
	var shippingStr = "";
	var taxStr = "";
	option = GetCookie ('SFSFW');
	var shippingTax = calculateShippingVATForRegion (price);

	// Shipping is calculated before any discount is applied
	shippingStr = createPriceString (calculateShippingPrice (price));

	///////////////////////////////////////////////////////////////////////////////////////////////////	//
	// Sale code - 15% discount on all orders EXCEPT accessories and non-VAT items (ie books)

	// Accessories
//	if (code.charAt (0) == 'A' && code.charAt (1) == 'C' && code.charAt (2) == 'C')
//	{
//		price = price;
//	} // if

	// Extra discount for Celtos
//	else if (code.charAt (0) == 'C' && code.charAt (1) == 'L' && code.charAt (2) == 'T')
//	{
//		price = (price * 3) / 4;		// 25%
//		price = (price * 4) / 5;		// 20%
//		price = (price * 17) / 20;		// 15%
//	} // if

	// Extra discount for Spaceships
//	if (code.charAt (0) == 'S' && code.charAt (1) == 'F' && code.charAt (2) == 'S')
//	{
//		price = (price * 17) / 20;		// 15%
//	} // if

	// Discount for Nef and Land ironclads
//	if (code.charAt (0) == 'V')
//	{
//		price = (price * 17) / 20;		// 15%
//	} // if

	// Extra discount for 6mm
//	if (code.charAt (0) == 'S' && code.charAt (1) == 'F' && code.charAt (2) == '3')
//	{
//		price = (price * 85) / 100;
//	} // if

	// Extra discount for 15mm
//	else if (code.charAt (0) == 'S' && code.charAt (1) == 'F' && code.charAt (2) == '1')
//	{
//		price = (price * 85) / 100;
//	} // if

	// Extra discount for Squadron Commander
//	else if (code.charAt (0) == 'S' && code.charAt (1) == 'C')
//	{
//		price = (price * 85) / 100;
//	} // if

	// Extra discount for Belgians
//	else if (code.charAt (0) == 'G' && code.charAt (1) == 'W' && code.charAt (2) == '2')
//	{
//		price = (price * 3) / 4;
//	} // if

	// Extra discount for Land Ironclads
//	else if (code.charAt (0) == 'V' && code.charAt (1) == 'L' && code.charAt (2) == 'I')
//	{
//		price = (price * 3) / 4;
//	} // if

	// Starmats etc
//	else if (code.charAt (0) == 'M' && code.charAt (1) == 'A' && code.charAt (2) == 'T')
//	{
//		price = price;
//	} // if

	// Xmarx buildings
//	else if (code.charAt (0) == 'X' && code.charAt (1) == 'M')
//	{
//		price = price;
//	} // if

	// Belgians - 15%
//	if (code.charAt (0) == 'G' && code.charAt (1) == 'W')
//	{
//		price = (price * 17) / 20;
//	} // else

	// Everything else - 15%
//	else
//	{
//		price = (price * 17) / 20;
//	} // else
	////////////////////////////////////////////////////////////////////////////////////////////////////////

	var tax = calculateVATForRegion (price);

//	document.writeln ('Price ' + price);
	priceStr = createPriceString (price - calculateVAT (price));
//	document.writeln ('PriceStr ' + priceStr);
	taxStr = createPriceString (tax + shippingTax);
//	document.writeln ('TaxStr ' + taxStr);

	document.writeln ('<FORM TARGET="paypal" ACTION="https://www.paypal.com/cgi-bin/webscr" METHOD="post">');
	document.writeln ('<INPUT TYPE="hidden" NAME="cmd" VALUE="_cart">');
	document.writeln ('<INPUT TYPE="hidden" NAME="business" VALUE="payment@brigademodels.co.uk">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_name" VALUE="' + name + '">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_number" VALUE=' + code + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="amount" VALUE=' + priceStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping2" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="tax" VALUE=' + taxStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="currency_code" VALUE="GBP">');
	document.writeln ('<INPUT TYPE="hidden" NAME="handling_cart" VALUE="1">');
	document.writeln ('<INPUT TYPE="hidden" NAME="image_url" VALUE="http://www.brigademodels.co.uk/Logos/BrigadePayPal.jpg">');
	if (button == "")
	{
		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + 'Graphics/Shopping/AddToCart.gif" BORDER="0" ALT="Add to cart">');
	} // if
	else
	{
		var buttonName;

		buttonName = "Graphics/Shopping/AddToCart" + button + ".gif";

		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + buttonName + '" BORDER="0" ALT="Add to cart">');
	} // else
	document.writeln ('<INPUT TYPE="hidden" NAME="add" VALUE="1">');
	document.writeln ('</FORM>');
}
//------------------------------------------------------------------------------------
function createCartItemNoVAT (code, name, price, level, button)
{
	var priceStr = "";
	var shippingStr = "";
	option = GetCookie ('SFSFW');
	var taxStr;

	priceStr = createPriceString (price);
//	document.writeln ('priceStr ' + priceStr );
	shippingStr = createPriceString (calculateShippingPrice (price));
//	document.writeln ('shippingStr ' + shippingStr );
	taxStr = createPriceString (calculateShippingVATForRegion (price));
//	document.writeln ('taxStr ' + taxStr );

	document.writeln ('<FORM TARGET="paypal" ACTION="https://www.paypal.com/cgi-bin/webscr" METHOD="post">');
	document.writeln ('<INPUT TYPE="hidden" NAME="cmd" VALUE="_cart">');
	document.writeln ('<INPUT TYPE="hidden" NAME="business" VALUE="payment@brigademodels.co.uk">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_name" VALUE="' + name + '">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_number" VALUE=' + code + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="amount" VALUE=' + priceStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping2" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="tax" VALUE=' + taxStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="currency_code" VALUE="GBP">');
	document.writeln ('<INPUT TYPE="hidden" NAME="handling_cart" VALUE="1">');
	document.writeln ('<INPUT TYPE="hidden" NAME="image_url" VALUE="http://www.brigademodels.co.uk/Logos/BrigadePayPal.jpg">');
	if (button == "")
	{
		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + 'Graphics/Shopping/AddToCart.gif" BORDER="0" ALT="Add to cart">');
	} // if
	else
	{
		var buttonName;

		buttonName = "Graphics/Shopping/AddToCart" + button + ".gif";

		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + buttonName + '" BORDER="0" ALT="Add to cart">');
	} // else
	document.writeln ('<INPUT TYPE="hidden" NAME="add" VALUE="1">');
	document.writeln ('</FORM>');
}
//------------------------------------------------------------------------------------
function createCartItemWithOption (code, name, price, level, optionStr, button)
{
	var priceStr = "";
	var shippingStr = "";
	var taxStr = "";

	priceStr = createPriceString (price - calculateVAT (price));
	shippingStr = createPriceString (calculateShippingPrice (price));
	taxStr = createPriceString (calculateVATForRegion (price));

	document.writeln ('<FORM TARGET="paypal" ACTION="https://www.paypal.com/cgi-bin/webscr" METHOD="post">');
	document.writeln ('<INPUT TYPE="hidden" NAME="cmd" VALUE="_cart">');
	document.writeln ('<INPUT TYPE="hidden" NAME="business" VALUE="payment@brigademodels.co.uk">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_name" VALUE="' + name + '">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_number" VALUE=' + code + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="amount" VALUE=' + priceStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping2" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="handling_cart" VALUE="1">');
	document.writeln ('<INPUT TYPE="hidden" NAME="tax" VALUE=' + taxStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="cn" VALUE=' + optionStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="currency_code" VALUE="GBP">');
	document.writeln ('<INPUT TYPE="hidden" NAME="image_url" VALUE="http://www.brigademodels.co.uk/Logos/BrigadePayPal.jpg">');
	if (button == "")
	{
		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + 'Graphics/Shopping/AddToCart.gif" BORDER="0" ALT="Add to cart">');
	} // if
	else
	{
		var buttonName;

		buttonName = "Graphics/Shopping/AddToCart" + button + ".gif";

		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + buttonName + '" BORDER="0" ALT="Add to cart">');
	} // else
	document.writeln ('<INPUT TYPE="hidden" NAME="add" VALUE="1">');
	document.writeln ('</FORM>');
}
//------------------------------------------------------------------------------------
function createCartItemWithTwoOptions (code, name, price, level, select1, select2, button)
{
	var priceStr = "";
	var shippingStr = "";
	var taxStr = "";

	priceStr = createPriceString (price - calculateVAT (price));
	shippingStr = createPriceString (calculateShippingPrice (price));
	taxStr = createPriceString (calculateVATForRegion (price));

	document.writeln ('<FORM TARGET="paypal" ACTION="https://www.paypal.com/cgi-bin/webscr" METHOD="post">');
	document.writeln ('<INPUT TYPE="hidden" NAME="cmd" VALUE="_cart">');
	document.writeln ('<INPUT TYPE="hidden" NAME="business" VALUE="payment@brigademodels.co.uk">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_name" VALUE="' + name + '">');
	document.writeln ('<INPUT TYPE="hidden" NAME="item_number" VALUE=' + code + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="amount" VALUE=' + priceStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="shipping2" VALUE=' + shippingStr + '>');
	document.writeln ('<INPUT TYPE="hidden" NAME="handling_cart" VALUE="1">');
	document.writeln ('<INPUT TYPE="hidden" NAME="tax" VALUE=' + taxStr + '>');

	document.writeln ('<INPUT TYPE="hidden" NAME="on1" VALUE="Pack1">');
	document.writeln ('<SELECT NAME="os1">');
	document.writeln ('<OPTION VALUE="British Fleet Pack #1">British Fleet Pack #1</OPTION>');
	document.writeln ('<OPTION VALUE="Pursuit Pack">Pursuit Pack</OPTION>');
	document.writeln ('<OPTION VALUE="Carrier Pack">Carrier Pack</OPTION>');
	document.writeln ('<OPTION VALUE="German Fleet Pack #1">German Fleet Pack #1</OPTION>');
	document.writeln ('</SELECT>');

	document.writeln ('<INPUT TYPE="hidden" NAME="on2" VALUE="Pack2">');
	document.writeln ('<SELECT NAME="os2">');
	document.writeln ('<OPTION VALUE="British Fleet Pack #1">British Fleet Pack #1</OPTION>');
	document.writeln ('<OPTION VALUE="Pursuit Pack">Pursuit Pack</OPTION>');
	document.writeln ('<OPTION VALUE="Carrier Pack">Carrier Pack</OPTION>');
	document.writeln ('<OPTION VALUE="German Fleet Pack #1">German Fleet Pack #1</OPTION>');
	document.writeln ('</SELECT>');

	document.writeln ('<INPUT TYPE="hidden" NAME="currency_code" VALUE="GBP">');
	document.writeln ('<INPUT TYPE="hidden" NAME="image_url" VALUE="http://www.brigademodels.co.uk/Logos/BrigadePayPal.jpg">');
	if (button == "")
	{
		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + 'Graphics/Shopping/AddToCart.gif" BORDER="0" ALT="Add to cart">');
	} // if
	else
	{
		var buttonName;

		buttonName = "Graphics/Shopping/AddToCart" + button + ".gif";

		document.writeln ('<INPUT TYPE="image" NAME="submit" SRC="' + levelArray [level - 1] + buttonName + '" BORDER="0" ALT="Add to cart">');
	} // else
	document.writeln ('<INPUT TYPE="hidden" NAME="add" VALUE="1">');
	document.writeln ('</FORM>');
}
//------------------------------------------------------------------------------------
function calculateShippingPrice (value)
{
	var price = 0;
	option = GetCookie ('Shipping');
	SFSFW = GetCookie ('SFSFW');

	if (option == "UK")
	{
		price = Math.round (value * (7.5 - SFSFW)) / 100;
	}
	else if (option == "Europe")
	{
		price = Math.round (value * (12.5 - SFSFW)) / 100;
	}
	else if (option == "RoWAir")
	{
		price = Math.round (value * (17.5 - SFSFW)) / 100;
	}

	return price;
}
//------------------------------------------------------------------------------------
function calculateVATForRegion (value)
{
	var price = 0;
	option = GetCookie ('Shipping');

	if (option == "RoWAir")
	{
		price = 0;
	}
	else
	{
		price = calculateVAT  (value);
	}

	return price;
}
//------------------------------------------------------------------------------------
function calculateVAT (value)
{
	var price = 0;

	price = Math.round (value * VATRate) / (100 + VATRate) - 0.00001;

	price = Math.round (price * 100) / 100;

	return price;
}
//------------------------------------------------------------------------------------
function calculateShippingVATForRegion (value)
{
	var price = 0;

	option = GetCookie ('Shipping');
	if (option == "UK")
	{
		price = (value * 1.5) / 100;
	}
	else if (option == "Europe")
	{
		price = (value * 2.5) / 100;
	}

	price = Math.round (price * 100) / 100;

	return price;
}
//------------------------------------------------------------------------------------
function displayShippingInformation (price)
{
	var shipping = 0;
	var shippingStr = "";

	option = GetCookie ('Shipping');
	shippingStr = createPriceString (calculateShippingPrice (price));

	if (option == "UK")
	{
		document.writeln (' <I>(Shipping costs to the UK are &pound;' + shippingStr + " + 20% VAT");
	}
	else if (option == "Europe")
	{
		document.writeln (' <I>(Shipping costs to Europe are &pound;' + shippingStr + " + 20% VAT");
	}
	else if (option == "RoWAir")
	{
		document.writeln (' <I>(Shipping costs outside Europe are &pound;' + shippingStr);
	}
	else
	{
		document.writeln (' <B>(You have not selected your shipping destination - click <A HREF="../../Shopping/select.html">here</A> to do so now and enable the shopping cart.)</B>');
		return;
	}

	SFSFW = GetCookie ('SFSFW');
	if (SFSFW == 5)
	{
		document.writeln (' -<BR>shipping price has your 5% SFSFW discount subtracted)</I><BR>');
	}
	else
	{
		document.writeln (')</I><BR>');
	}
}
//------------------------------------------------------------------------------------
function displayVATInformation (price)
{
	var tax = 0;
	var taxStr = "";

	option = GetCookie ('Shipping');
	taxStr = createPriceString (calculateVAT (price));

	if (option == "UK")
	{
		document.writeln (' <I>(Price includes VAT of &pound;' + taxStr + ' at ' + VATRate + '%');
	}
	else if (option == "Europe")
	{
		document.writeln (' <I>(Price includes VAT of &pound;' + taxStr + ' at ' + VATRate + '%');
	}
	else if (option == "RoWAir")
	{
		document.writeln (' <I>(' + VATRate + '% VAT will be deducted from this price at checkout');
	}
	document.writeln (')</I><BR>');
}
//------------------------------------------------------------------------------------
function zeroVATInformation ()
{
	document.writeln (' <I>(This item is zero-rated for VAT)</I><BR>');
}
//------------------------------------------------------------------------------------
function doCartButton (code, name, value, select, level, VAT, button)
{
	var option;

	option = GetCookie ('Shipping');

	if (option == "UK" ||
		option == "Europe" ||
		option == "RoWAir")
	{
		if (VAT)
		{
			createCartItem (code, name, value, level, button);
		} // if
		else
		{
			createCartItemNoVAT (code, name, value, level, button);
		} // else
	}
	else
	{
		createNoShippingButton (level);
 	}
}
//------------------------------------------------------------------------------------
function doCartButtonWithOption (code, name, value, level, VAT, optionStr, button)
{
	var option;

	option = GetCookie ('Shipping');

	if (option == "UK" ||
		option == "Europe" ||
		option == "RoWAir")
	{
		createCartItemWithOption (code, name, value, level, optionStr, button);
	}
	else
	{
		createNoShippingButton (level);
	}
}
//------------------------------------------------------------------------------------
function doCartButtonWithTwoOptions (code, name, value, level, VAT, optionStr1, optionStr2, button)
{
	var option;

	option = GetCookie ('Shipping');

	if (option == "UK" ||
		option == "Europe" ||
		option == "RoWAir")
	{
		createCartItemWithTwoOptions (code, name, value, level, optionStr1, optionStr2, button);
	}
	else
	{
		createNoShippingButton (level);
	}
}
//------------------------------------------------------------------------------------
function doViewCartButton (range)
{
	var option;

	option = GetCookie ('Shipping');

	if (option == "UK" ||
		option == "Europe" ||
		option == "RoWAir")
	{
		document.writeln ('<FORM NAME="_xclick" TARGET="paypal" ACTION="https://www.paypal.com/cgi-bin/webscr" METHOD="post">');
		document.writeln ('<INPUT TYPE="hidden" NAME="cmd" VALUE="_cart">');
		document.writeln ('<INPUT TYPE="hidden" NAME="business" VALUE="payment@brigademodels.co.uk">');
		if (range== "")
		{
			document.writeln ('<INPUT TYPE="image" SRC="../Graphics/Shopping/ViewCart.gif" BORDER="0" NAME="submit" ALT="View Cart">');
		} // if
		else
		{
			var rangeButton = "../Graphics/Shopping/ViewCart" + range + ".gif";
			document.writeln ('<INPUT TYPE="image" SRC="' + rangeButton + '" BORDER="0" NAME="submit" ALT="View Cart">');
 		} // else
		document.writeln ('<INPUT TYPE="hidden" NAME="display" VALUE="1">');
		document.writeln ('</FORM>');
	}
	else
	{
		document.writeln ('<P><B>To activate our shopping cart system, you need to select your shipping destination. Click <A HREF="../Shopping/select.html">here</A> to do so and enable on-line shopping</B></P>');
	}
}
//------------------------------------------------------------------------------------

function HoursLeft ()
{
	today  = new Date();
	todayEpoch  = today.getTime();
	target = new Date("7 January, 2010"); 
	targetEpoch = target.getTime();
	hoursLeft = Math.floor(((targetEpoch - todayEpoch) / (60*60)) / 1000);
	document.write(hoursLeft);
}

