////////////////////////////////////////////////////////////////////////////////
// Standard Code - common to ALL SVSP Shop Pages
//
////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////
// Special Offer Data - Prices for Items on Special Offer
//
var SpDoDebug=false;
var StdDoDebug=false;
var SpArrayDone=false;
var Use_Special_Offers_Page=true;
var SpOfferURL="<a href=\"special_offers.htm\">"
var VatRate = 1.175;

var SpecialOfferData=new Array();

function SetSpecialArray()
{
	if(SpArrayDone == false)
	{
		if(SpDoDebug == true)
		{
			window.alert("SpecialOfferData[]" );
		}
		// Data from Magic XL Spreadsheet - Page SpecialOffers_2...

	// ...Data from Magic XL Spreadsheet

		SpArrayDone = true;
	};
}


///////////////////////////////////////////////////////////////////////////////
// JavaScript Code
//


function pObject(ProductName,ProductRef,Price,Weight,TaxCode,VARSLOT2){
	this.ProductName = ProductName;
	this.ProductRef = ProductRef;
	this.Price = Price;
	this.Weight = Weight;
	this.TaxCode = TaxCode;
	this.VARSLOT2 = VARSLOT2;
	return(this);
}

function BoxDefnition(name, init_string, length)
{
	this.name = name;
	this.init_string = init_string;
	this.length = length;
}

function BoxLine(Ref, text, price)
{
	this.Ref = Ref;
	this.text = text;
	this.price = price;
}

function BuildSelectionX(BoxDefn, Items)
{
	BuildSelection1(BoxDefn, Items, false)
}

function BuildSelection(BoxDefn, Items)
{
	BuildSelection1(BoxDefn, Items, true)
}

function BuildSelection1(BoxDefn, Items, ShowSpText)
{
	var price = 0;
	var Ref = "";
	var AnyOffers = false;
	var ThisOffer = false;
	document.write('<select name="' + BoxDefn.name +'">');
	document.write('<option selected>' + BoxDefn.init_string +'</option>');
	for (i=0; i<BoxDefn.length; i++)
	{
		Ref = Items[i].Ref;
		text = Items[i].text;
		ThisOffer = false;

		// Check for special offers...
		price = GetOfferPrice(Ref);
		if(price > 0 && Items[i].price >= price)
		{
			ThisOffer = true;
			AnyOffers = true;
		}
		else
		{
			price = Items[i].price;
		}

		if(price > 0)
		{
			if(ThisOffer==true && ShowSpText==true)
			{
				price_text = SpecialOfferDropDownText(price);
			}
			else
			{
				price_text = StandardDropDownText(price);
			}
			document.write('<option value="' + price + '">' + Ref + ' ' + text + price_text + '</option>')
		}
		else
		{
			document.write('<option>' + Ref + ' ' + text + ' </option>');
		}
	}
	document.write('</select>');

	if(AnyOffers==true && ShowSpText==true)
	{
		DoAnyOffers();
	}
	return;
	
}

function DoMoneyP(Price)
{
	pPrice = Math.round(Price * 100);	// Price in Pence rounded to nearest penny
	
	szString = pPrice.toString();
	
	return szString;
}

function DoMoney(Price)
{
	Price = round2(Price);
	szString = Price.toString();
	
	decimal = szString.indexOf(".");
	if(decimal == -1)
	{
		szString += ".00";
	}
	else
	{
		OneDP = szString.charAt(decimal + 2);
		if(OneDP == "")
		{
			szString += "0";
		}
	}
	return szString;
}

function roundUp2(number)
{
	// Rounds Sum in pounds UP to nearest penny i.e £5.9925 = £6.00
	return (Math.round((number * 100) + 0.99)) / 100; 
}

function round2(number)
{
	// Rounds Sum in pounds to nearest penny i.e £5.9925 = £5.99
	return (Math.round(number * 100)) / 100; 
}

function DoProductPrice(Product)
{
	DoProductPrice1(Product, false, 1, true);	
}

function DoProductPriceDiv(Product, Number)
{
	DoProductPrice1(Product, false, Number, true);	
}

function DoProductPriceInc(Product)
{
	DoProductPrice1(Product, true, 1, true);	
}

function DoProductPriceIncDiv(Product, Number)
{
	DoProductPrice1(Product, true, Number, true);	
}

function DoProductPriceNoSpText(Product)
{
	DoProductPrice1(Product, false, 1, false);	
}

function DoProductPriceIncNoSpText(Product)
{
	DoProductPrice1(Product, true, 1, false);	
}

function DoProductPrice1(Product, IncVat, Divisor, ShowSpecialText)
{
	price_text = GetProductPrice1(Product, IncVat, Divisor, ShowSpecialText);
	document.write(price_text);

}

function GetProductPrice(Product)
{
	strReturn = GetProductPrice1(Product, false, 1, true);
	return strReturn;
}

function GetProductPrice1(Product, IncVat, Divisor, ShowSpecialText)
{

	// Check For Special Offer Prices
	var SOPrice =0;
	var SpecialOffer = false;
	var price_text = "";

	setProductArrayData();
	SOPrice = GetOfferPrice(productData[Product].ProductRef);

	if(SOPrice > 0 && productData[Product].Price >= SOPrice)
	{
		ProductPrice = SOPrice;
		SpecialOffer = true;

		// Need to update the value that "Shop Assistant" Uses...
		productData[Product].Price = SOPrice;
	}
	else
	{
		ProductPrice = productData[Product].Price
	}

	if(IncVat == true)
	{
		ProductPrice = roundUp2(ProductPrice * VatRate);
	}

	if(Divisor > 1)
	{
		ProductPrice = ProductPrice / Divisor;
	}

	if(SpecialOffer == true && (Divisor <= 1) && ShowSpecialText)
	{
		price_text = SpecialOfferItemText(ProductPrice);
	}
	else
	{
		price_text = StandardItemText(ProductPrice);
	}

	return price_text;
}

function GoToMainIndex()
{
	window.location="main_index.htm";
}

function GoToShopEntrance()
{
	window.location="Cover page.htm";
}

function TrimString(InString)
{
	// Removes Leading & Trailing Spaces
	var StartIndex;
	var EndIndex =0;
	for(StartIndex = 0;
		InString.charAt(StartIndex) == " ";
		StartIndex ++);

	EndIndex = InString.indexOf(" ",StartIndex);
	if(EndIndex == -1)
	{
		// Trailing Space not found
		EndIndex=InString.length
	}

	OutString=InString.substring(StartIndex,EndIndex)

//	if(StdDoDebug == true)
//	{
//		window.alert("TrimString String= -" + InString + "- Start=" + StartIndex + " End=" + EndIndex);
//		window.alert("TrimString InString= -" + InString + "- OutString= -" + OutString + "-");
//	}
	
	return OutString;
}

/////////////////////////////////////////////////////////////////////////////
// Special Offer Processing
//
function GetOfferPrice(InRef)
{
	SetSpecialArray();
	found = false;
	SpPrice = 0;

	ProductRef=TrimString(InRef);

	for(index=0; 
	    (index < SpecialOfferData.length) && !found; 
		index++)
	{
//		if(SpDoDebug == true)
//		{
//			window.alert("Index = " + index );
//		}
		if(SpecialOfferData[index].Ref==ProductRef)
		{
			found=true;
			SpPrice=SpecialOfferData[index].SpPrice;
		}
	}
//	if(SpDoDebug == true)
//	{
//		window.alert("GetOfferPrice(" + ProductRef + ") = " + SpPrice );
//	}
	return SpPrice;
}

function SpOfferItem(ProductRef, Price)
{
	this.Ref=TrimString(ProductRef);
	this.SpPrice=Price;
}

//var SpecialOfferText1 = "Only ** ";
//var SpecialOfferText2 = " **";

function SpecialOfferDropDownText(price)
{
	var price_text= "";
	if(price < 1.0)
	{
		price_text = ', Special Offer @ ' + DoMoneyP(price) + 'p';
	}
	else
	{
		price_text = ', Special Offer @' + '£' + DoMoney(price);
	}
	return price_text;
}

function SpecialOfferItemText(price)
{
	if(ProductPrice < 1.0)
	{
		price_text = "Special Offer Price, Only " + DoMoneyP(ProductPrice) + "p";
	}
	else
	{
		price_text = "Special Offer Price, Only " + "£" + DoMoney(ProductPrice);
	}
	return price_text;
}

function StandardDropDownText(price)
{
	if(price < 1.0)
	{
		price_text = ' @ ' + DoMoneyP(price) + 'p';
	}
	else
	{
		price_text = ' @ ' + '£' + DoMoney(price);
	}
	return price_text;
}

function StandardItemText(price)
{
	if(price < 1.0)
	{
		price_text = ' ' + DoMoneyP(price) + 'p';
	}
	else
	{
		price_text = ' ' + '£' + DoMoney(price);
	}
	return price_text;
}

function DoAnyOffers()
{
	if(Use_Special_Offers_Page == true)
	{
	document.write("<p align=center>");
	document.write(SpOfferURL);
	document.write("<b><i>(See Special Offers Page)</i></b>");
	document.write("</a>");		
	document.write("</p>");		
	}
}

