function initialise()
{
	// Initialise global variables.
	mainDefaultStatus="Distinctive designs of wall clocks, table lamps and wooden vases - plus traditional table skittles";
	top.defaultStatus=mainDefaultStatus;
	top.siteName="Cartwright";
	isNetscape=false;
	isExplorer=false;
	versionNumber=5;
	isOpera=navigator.userAgent.indexOf("Opera")>-1;
	if (!isOpera)
	{
		isExplorer=navigator.userAgent.indexOf("MSIE")>-1;
		if (!isExplorer)
		{
			if (navigator.userAgent.indexOf("Netscape")>-1) isNetscape=true; // Netscape 6
			else if (navigator.appName=="Netscape" && navigator.appCodeName=="Netscape") isNetscape=true;
		}
	}
	if (isExplorer)
	{
		var versionPos=navigator.userAgent.indexOf('MSIE')+5;
		versionNumber=parseInt(navigator.userAgent.substring(versionPos));
	}
	else if (isNetscape) versionNumber=parseInt(navigator.appVersion);

	currentWood=0;
	aDesign=new Array;
	aProduct=new Array;
	nHandClock=0;
	nFaceClock=1;
	nLamp=2;
	nVase=3;
	nSkittles=4;

	currentDesign=0;
	currentClock=-1;
	currentSkittleImage=0;
	currentProduct=-1;
	currentImage=1;
	maxImages=2;
	businessOption=0;

	backIconRangeShow=0; // range of background icons currently showing on screen
	backIconRangeFrom=-1; // range of background icons that the user has selected from.
	backIconElem=-1;

	imageBackground="";
	defaultBackground="texture3.gif";

	mainButton=0;
	// Create and fill an array to hold all available wood combinations.
	wood=new Array();
	wood[0]=new createWood("sycamore with walnut","sw");
	wood[1]=new createWood("sycamore with mahogany","sm");
	wood[2]=new createWood("ash with walnut","aw");
	wood[3]=new createWood("dark oak with sycamore","dos");
	wood[4]=new createWood("oak with sycamore","os");
	wood[5]=new createWood("oak with walnut","ow");
	wood[6]=new createWood("ash with mahogany","am");

	// Create variables to hold the 'currrently' selected wood. Initially set to 0 - sycamore with walnut

	// Put design names, design codes, sizes, wood elements and prices into arrays
	cClockSize="38 cm (15 inches)";
	prefix="clock1_"
// REMOVED WALNUT OPTIONS FROM ALL CLOCKS ON 18-10-06
	aProduct[nHandClock]= new product("wall-clocks-","wall clock"," with wooden hands",currentDesign,99,"Approximate diameter",300,300,40);
	addDesign("fan",prefix+"fan",cClockSize,"13",52.00);
	addDesign("sun",prefix+"sun",cClockSize,"13",59.00);
	addDesign("leaf",prefix+"leaf",cClockSize,"0,136",57.00);
	addDesign("flower",prefix+"flower",cClockSize,"136",52.00);
	addDesign("six-point",prefix+"6point",cClockSize,"13",50.00);
	addDesign("butterfly",prefix+"butterfly",cClockSize,"0,136",57.00);
	addDesign("umbrella",prefix+"umbrella",cClockSize,"13",52.00);
	addDesign("snowflake",prefix+"snowflake",cClockSize,"13",57.00);
	addDesign("twelve-point",prefix+"12point",cClockSize,"13",52.00);
//	addDesign("twelve-sided",prefix+"12sided",cClockSize,"03",48.00);
	aProduct[nHandClock].endElem=currentDesign-1;

	prefix="clock2_"
	aProduct[nFaceClock]= new product("wall-clocks-","wall clock"," with inset face",currentDesign,99,"Approximate diameter",300,300,40);
	addDesign("fan",prefix+"fan",cClockSize,"134",52.00);
	addDesign("flower",prefix+"flower",cClockSize,"134",52.00);
	addDesign("umbrella",prefix+"umbrella",cClockSize,"134",52.00);
	addDesign("snowflake",prefix+"snowflake",cClockSize,"134",57.00);
	addDesign("sun",prefix+"sun",cClockSize,"134",59.00);
	addDesign("twelve-point",prefix+"12point",cClockSize,"134",52.00);
	addDesign("six-point",prefix+"6point",cClockSize,"134",50.00);
//	addDesign("twelve-sided",prefix+"12sided",cClockSize,"034",48.00);
	aProduct[nFaceClock].endElem=currentDesign-1;

	prefix="lamp_";
	aProduct[nLamp]= new product("table-lamps","table lamp","",currentDesign,99,"Height",250,310,80);
	addDesign("large, straight",prefix+"large","36 cm (14 inches)","034",41.00);
	addDesign("large, grooved",prefix+"large_g","36 cm (14 inches)","034",44.00);
	addDesign("small, straight",prefix+"small","26 cm (10 inches)","34",34.00);
	addDesign("small, grooved",prefix+"small_g","26 cm 10 inches)","34",37.00);
	aProduct[nLamp].endElem=currentDesign-1;

	/* wood codes
	0 - sycamore with walnut
	1 - sycamore with mahogany
	2 - ash with walnut
	3 - dark oak with sycamore
	4 - oak with sycamore
	5 - oak with walnut
	*/

	prefix="vase_"
	aProduct[top.nVase]= new product("wooden-vases","wooden vase","",currentDesign,99,"Height",240,310,80);
	addDesign("small, straight",prefix+"small","23 cm (9 inches)","03",31.00);
	addDesign("small, grooved",prefix+"small_g","23 cm (9 inches)","0",33.00);
	addDesign("large ",prefix+"large","36 cm (14 inches)","3",38.00);
	aProduct[top.nVase].endElem=currentDesign-1;

	aProduct[nSkittles]= new product("","","",currentDesign,currentDesign,"",0,0,0);
	addDesign("Set of table skittles","skittles","63 x 43 cm (25 x 17 ins.)","",162.00);
	aProduct[nSkittles].endElem=currentDesign-1;

	// currentDesign is used in ProductTitle to determine which woods a particular design is available in.
	// Initialise to -1 so that the wood menu is not shown until a design is selected.
	currentDesign=-1;

	// Variables for order form. These are created here, rather than in order_form.html to allow global access
	customer=new Object;
	customer.title="";
	customer.firstName="";
	customer.lastName="";
	customer.address="";
	customer.postCode="";
	customer.country="UK";
	customer.email="";

	customer.delivTitle="";
	customer.delivFirstName="";
	customer.delivLastName="";
	customer.delivAddress="";
	customer.delivPostCode="";

	itemsOrdered= 0;
	orderBy=-1; // -1 for unselected, 0 for credit / debit card, 1 for print form and 2 for email.
	addressError="";
	order=new Array;
} // END of initialise function

function mouseOver(thisButton)
{
	var buttonNumber=thisButton.id.substring(thisButton.id.length-1)
	if (buttonNumber!=top.mainButton) thisButton.className="buttonHover";
}

function mouseOut(thisButton)
{
	var buttonNumber=thisButton.id.substring(thisButton.id.length-1)
	if (buttonNumber!=top.mainButton) thisButton.className="buttonNormal";
}

function mouseDown(thisButton)
{
	var buttonNumber=thisButton.id.substring(thisButton.id.length-1)
	if (buttonNumber!=top.mainButton) thisButton.className="buttonInset";
}

function setButton(newButton)
{
	if (top.mainButton>0) // Another button is inset - 'raise' it.
	{
		if (top.mainButton>5) var thisButton=top.Utility.document.getElementById("button"+top.mainButton);
		else var thisButton=top.Contents.document.getElementById("button"+top.mainButton);
		thisButton.className="buttonNormal";
	}
	if (newButton>0) // newButton will be set to zero if home page selected.
	{
		if (newButton>5) var thisButton=top.Utility.document.getElementById("button"+newButton);
		else var thisButton=top.Contents.document.getElementById("button"+newButton);
		thisButton.className="buttonInset";
		self.focus(); // remove selection box from around button
		if (newButton-1<=top.nFaceClock) top.currentClock=newButton-1;

	}
	top.mainButton=newButton;
}

function createWood(cName,cCode) // Create wood objects
{
	this.name=cName;
	this.code=cCode;
}

function design(cName,cCode,cSize,cWood,nPrice) // Create design objects.
{
	this.name=cName;	// eg 'Butterfly'
	this.code=cCode;		// eg 'clock1_butterfly_'
	this.size=cSize;		// eg '38 cm (15 inches)'
	this.wood=cWood;		// eg '2' - (ash)
	this.price=nPrice;		// eg '57.00'
}

function addDesign(cName,cCode,cSize,cWood,nPrice)
// Create a new design object and increment the currentDesign counter.
{
	top.aDesign[top.currentDesign]= new design(cName,cCode,cSize,cWood,nPrice);
	++top.currentDesign;
}

function product(cName,cFullName,cDescript,nStartElem,nDsgnElem,cSizeText,nImageWidth,nImageHeight,nIconHeight)
// Create product objects.
{
	this.name=cName;
	this.fullName=cFullName;
	this.description=cDescript;
	this.startElem=nStartElem;
	this.endElem=0;
	this.selected=false;
	this.designSelected=false;
	this.dsgnElem=nDsgnElem;
	this.sizeText=cSizeText;
	this.imageWidth=nImageWidth;
	this.imageHeight=nImageHeight;
	this.iconHeight=nIconHeight;
}

function fillOrderLine(nElem,cCode,cName,cWood,nQuantity,nPrice)
{
	if (nElem>=top.order.length) top.order[nElem]=new Object();
	top.order[nElem].code=cCode;
	top.order[nElem].name=cName;
	top.order[nElem].wood=cWood;
	top.order[nElem].quantity=nQuantity;
	top.order[nElem].price=nPrice;
}

function proper(string)
// returns 'string' with the leading character converted to uppercase
{
	if (isNaN(string)) var properString=string.charAt(0).toUpperCase()+string.substring(1,string.length);
	else var properString=string; // string begins with a number - return 'as is'.
	return properString;
}

function numToText(nVal)
{
	// Don't need to test if nVal is a number - this has already been checked.
	var numTextList = new Array("one","two","three","four","five","six","seven","eight","nine","ten");
	if (nVal-1<numTextList.length) var numText=numTextList[nVal-1];
	else var numText=nVal;
	return numText;
}

function confirmOrder(cWindow,orderQuan,sameProduct)
{
// alert("in confirmOrder");
// alert(cWindow);

	var prevFile=cWindow.document.location.href;
// alert("prevFile = "+prevFile);

	// Create a dummy window in the same frame as the order button
	cWindow.document.open();
	var docWrite="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
	docWrite+="<HTML><HEAD><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>";
	docWrite+="<TITLE><\/TITLE><LINK REL='StyleSheet' HREF='style.css' TYPE='text/css'><\/HEAD>";
	
	docWrite+="<BODY CLASS='main' onLoad='self.focus();' onBlur='self.focus();'>";
	docWrite+="<DIV CLASS='dummyWindow'>";
	docWrite+="<DIV CLASS='dummyWindowTitle'>&nbsp;&nbsp;Your order<\/DIV>";
	docWrite+="<DIV CLASS='dummyWindowText'>";
	if (top.currentProduct==top.nSkittles) var message="Set of table skittles";
	else var message=top.order[top.itemsOrdered-1].name+" - in "+top.wood[top.currentWood].name+" inlays";
	
	docWrite+=top.proper(message)+"<DIV CLASS='insetBox'> "+proper(numToText(orderQuan));
	if (sameProduct) docWrite+=" now on";
	else docWrite+=" added to";
	
	docWrite+=" the order form<FORM NAME='orderButtons' STYLE='margin:0;'>";
	docWrite+="<INPUT CLASS='button' STYLE='width:150px;' TYPE='button' VALUE='Continue shopping' ";

//	alert("onClick='self.document.location.href= \""+prevFile+"\" '><BR>");
	docWrite+="onClick='self.document.location.href= \""+prevFile+"\" '><BR>";

	docWrite+="<INPUT CLASS='button' STYLE='width:150px; margin-top:4px;' TYPE='button' VALUE='View order form' ";
	docWrite+="onClick='top.DisplayData.location.href=\"order_form_frame.html\" '><\/FORM>";
	
	docWrite+="<\/DIV><\/DIV><\/DIV><\/BODY><\/HTML>";
	cWindow.document.write(docWrite);

	cWindow.document.close();
}

function confirmDuplicate(cWindow,orderQuan,nDuplicate)
{
	var prevWindow=cWindow;
	var prevLoc=cWindow.document.location.href;
	// Create a dummy window in the same frame as the order button
	cWindow.document.open();
	var docWrite="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
	docWrite+="<HTML><HEAD><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>";
	docWrite+="<TITLE><\/TITLE><LINK REL='StyleSheet' HREF='style.css' TYPE='text/css'><\/HEAD>";
	docWrite+="<BODY CLASS='main' onLoad='self.focus();' onBlur='self.focus();'>";
	docWrite+="<DIV CLASS='dummyWindow'><DIV CLASS='dummyWindowTitle'>&nbsp;&nbsp;Your order<\/DIV>";
	docWrite+="<DIV CLASS='dummyWindowText'>You have "+top.numToText(top.order[nDuplicate].quantity);
	docWrite+=" of this item on your order form.<DIV CLASS='insetBox'>Add another "+top.numToText(orderQuan)+"?";
	
	docWrite+="<FORM STYLE='margin-top:5; margin-bottom:0;'>";
	docWrite+="<INPUT CLASS='button' TYPE='button' STYLE='width:40px;' VALUE='Yes' ";
// alert("prevWindow = "+prevWindow);
// alert("prevLoc = "+prevLoc);

	docWrite+="onClick='top.addAnother(\""+cWindow+"\","+orderQuan+","+nDuplicate+")';>";

	docWrite+="&nbsp;&nbsp;";
	docWrite+="<INPUT CLASS='button' TYPE='button' STYLE='width:40px;' ";
	docWrite+="VALUE='No' onClick='self.document.location.href=\""+prevLoc+"\"'><\/FORM>";
	
	docWrite+="<\/DIV><\/DIV><\/DIV><\/BODY><\/HTML>";
// alert(docWrite);
	cWindow.document.write(docWrite);
	cWindow.document.close();
}

function addAnother(cWindow,orderQuan,nDuplicate)
{
// alert("in addAnother");

// alert("orderQuan = "+orderQuan);
// alert("nDuplicate = "+nDuplicate);
// alert("in addAnother - cWindow = "+cWindow);

	top.order[nDuplicate].quantity+=parseInt(orderQuan);
	top.confirmOrder(cWindow,top.order[nDuplicate].quantity,true);
}

function invalidQuantity(cWindow,orderQuan)
{
	// Create a dummy window in the same frame as the order button
	cWindow.document.open();
	var docWrite="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
	docWrite+="<HTML><HEAD><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1'>";
	docWrite+="<TITLE><\/TITLE><LINK REL='StyleSheet' HREF='style.css' TYPE='text/css'><\/HEAD>";
	var prevFile='"'+cWindow.document.location.href+'"';
	docWrite+="<BODY CLASS='main' onBlur='self.document.location.href="+prevFile+";'><DIV CLASS='dummyWindow'>";
	docWrite+="<DIV CLASS='dummyWindowTitle'>&nbsp;&nbsp;Order error<\/DIV>";
	docWrite+="<DIV CLASS='dummyWindowText'>"+orderQuan+" is not a valid quantity";
	docWrite+="<FORM>";
	docWrite+="<INPUT CLASS='button' TYPE='button' VALUE=' OK ' onClick='self.document.location.href="+prevFile+"'>";
	docWrite+="<\/FORM>";
	docWrite+="<\/DIV><\/BODY><\/HTML>";
	cWindow.document.write(docWrite);
	cWindow.document.close();
}

// DELETE getId, closeRedBox and AddToOrder functions to remove red box
function getId(name)
{
	if (document.getElementById) return document.getElementById(name).style;
	else if (document.all) return document.all[name].style;
	else if (document.layers) return document.layers[name];
}

function closeRedBox()
{
	var curObject=getId("redBox");
	curObject.visibility="hidden";
}

function addToOrder(cWindow,orderQuan)
// Called from the 'Add to order' button in product_title.html and table-skittles.html. The required details are added to a new element in the
// array that appears on the order form, or the quantity of an existing order is incremented as required.
{
	// Validate orderQuan. If zero or less than zero has been entered '<1' will trap it.
	// If a non-number value has been entered dividing orderQuan by itself will result in NaN instead of 1
	if (orderQuan<1 || orderQuan/orderQuan!=1) invalidQuantity(cWindow,orderQuan);
	else // quantity is valid - next check if the design already exists on the order form.
	{
		if (top.currentProduct==top.nSkittles) var cWoodReq = "";
		else var cWoodReq = top.wood[top.currentWood].name+" inlays"; // all others
		// Check if item has already been ordered
		var nDuplicate=checkExstgOrder(cWoodReq);
		if (nDuplicate<top.itemsOrdered) confirmDuplicate(cWindow,orderQuan,nDuplicate);
		else
		{
			var orderText="";
			if (top.aProduct[top.currentProduct].fullName>"") orderText+=top.proper(top.aProduct[top.currentProduct].fullName)+" - ";
			orderText+=top.aDesign[top.currentDesign].name;
			if (top.currentProduct<=top.nFaceClock) orderText+=" design";
			if (top.aProduct[top.currentProduct].description>"") orderText+=" "+top.aProduct[top.currentProduct].description;
			fillOrderLine(top.itemsOrdered++,top.aDesign[top.currentDesign].code,orderText,cWoodReq,parseInt(orderQuan),top.aDesign[top.currentDesign].price);
			confirmOrder(cWindow,orderQuan);
		}
	}
}

function checkExstgOrder(cWoodReq) // Check if a design is already on order form.
{
	var nDuplicate=(top.itemsOrdered+1);
	var i=0;
	while (i<top.itemsOrdered && nDuplicate>top.itemsOrdered)
	{
		if (order[i].code==aDesign[top.currentDesign].code && order[i].wood==cWoodReq) nDuplicate=i;
		i++;
	}
	return nDuplicate;
}

function formatCost(nCost)
{
	if (nCost==0) var formatCost="";
	else
	{
		var nPounds=Math.floor(nCost);
		var nPence= Math.round((nCost-nPounds)*100,2);
		if (nPounds > 999)
		{
			nPounds = "" + nPounds;
			nPounds=nPounds.substring(0,nPounds.length-3)+ ","+nPounds.substring(nPounds.length-3,nPounds.length);
		}
		if (nPence<10) nPence+="0";
		var formatCost=nPounds+"."+nPence;
	}
	return formatCost;
}

function orderDiscount()
{
	// check if any discount is due on the order. Calculate discount based on pairs of similar size items.
	var discount=0;
	var numClocks=0;
	var numLampsOrVases=0;
	for (i=0; i+1<=top.itemsOrdered; i++)
	{
		if (top.order[i].code=="skittles" && top.order[i].quantity >1) discount+=(Math.floor(top.order[i].quantity/2)*5);
		else if (top.order[i].code.substring(0,5)=="clock") numClocks+=top.order[i].quantity;
		else numLampsOrVases+=top.order[i].quantity;
	}
	if (numClocks>1) discount+=(Math.floor(numClocks/2)*2); 
	if (numLampsOrVases>1) discount+=(Math.floor(numLampsOrVases/2)*2); 
	return discount;
}

