

// JavaScript Document

<!-- There is only one section of this JavaScript which needs to change when adding new products. This has been highlighted by annotation. -->

function Product(descrip, money)
{
	this.description = descrip;
	this.price = money;
}

function createArray(size)
{
	for (var i=0; i < size; i++)
	{
		this[i] = null;
	}

	return this;
}

function Category(name)
{
	this.title = name;
	this.product = new createArray(1);
	this.product[0] = new Product("No item selected", 0.00);
}

var category = new createArray(1);

<!-- LOOK HERE! To add new products change this section, from START to END. -->
<!-- START -->
<!-- Each drop down list has a section. Copy a section, changing its category name, product names, and product prices. -->

category[1] = new Category("Stairs - per flight");
category[1].product[1] = new Product("1", 25.00);
category[1].product[2] = new Product("2", 50.00);
category[1].product[3] = new Product("3", 75.00);
category[1].product[4] = new Product("4", 100.00);
category[1].product[5] = new Product("5", 125.00);

category[2] = new Category("Lounge - may depend on size");
category[2].product[1] = new Product("1", 25.00);
category[2].product[2] = new Product("2", 50.00);
category[2].product[3] = new Product("3", 75.00);
category[2].product[4] = new Product("4", 100.00);
category[2].product[5] = new Product("5", 125.00);

category[3] = new Category("Dining room - may depend on size");
category[3].product[1] = new Product("1", 25.00);
category[3].product[2] = new Product("2", 50.00);
category[3].product[3] = new Product("3", 75.00);
category[3].product[4] = new Product("4", 100.00);
category[3].product[5] = new Product("5", 125.00);

category[4] = new Category("Kitchen");
category[4].product[1] = new Product("1", 15.00);
category[4].product[2] = new Product("2", 30.00);
category[4].product[3] = new Product("3", 45.00);
category[4].product[4] = new Product("4", 60.00);
category[4].product[5] = new Product("5", 75.00);

category[5] = new Category("Bedroom - may depend on size");
category[5].product[1] = new Product("1", 25.00);
category[5].product[2] = new Product("2", 50.00);
category[5].product[3] = new Product("3", 75.00);
category[5].product[4] = new Product("4", 100.00);
category[5].product[5] = new Product("5", 125.00);

category[6] = new Category("Hallway");
category[6].product[1] = new Product("1", 15.00);
category[6].product[2] = new Product("2", 30.00);
category[6].product[3] = new Product("3", 45.00);
category[6].product[4] = new Product("4", 60.00);
category[6].product[5] = new Product("5", 75.00);

category[7] = new Category("Landing");
category[7].product[1] = new Product("1", 5.00);
category[7].product[2] = new Product("2", 10.00);
category[7].product[3] = new Product("3", 15.00);
category[7].product[4] = new Product("4", 20.00);
category[7].product[5] = new Product("5", 25.00);

category[8] = new Category("Bathroom");
category[8].product[1] = new Product("1", 10.00);
category[8].product[2] = new Product("2", 20.00);
category[8].product[3] = new Product("3", 30.00);
category[8].product[4] = new Product("4", 40.00);
category[8].product[5] = new Product("5", 50.00);

category[9] = new Category("Rugs - may depend on size");
category[9].product[1] = new Product("1", 25.00);
category[9].product[2] = new Product("2", 50.00);
category[9].product[3] = new Product("3", 75.00);
category[9].product[4] = new Product("4", 100.00);
category[9].product[5] = new Product("5", 125.00);
<!-- END -->

function SetLengths()
{
	var k=1;
	while(category[k] != null)
	{
		k++;
	}

	category.length = k;

	for (i=1; i<category.length; i++)
	{
		var j=1;

			while (category[i].product[j] != null)
			{
				j++;
			}

		category[i].product.length = j;
	}
}

SetLengths();

function writeTableRow(i)
{
	document.write('<tr bgcolor=#DEE5F7><td>' + category[i].title + '</td><td>'  + '<select size="1" name="menu' + i + '" onChange="update(' + i + ')">');
	len = category[i].product.length;

	for (j=0; j<len; j++)
	{
		if (j != 0)
		{
			document.write('<option>' + category[i].product[j].description + ' - &pound;' + fix(category[i].product[j].price) + '</option>');
		}
		else
		{
			document.write('<option selected value=" ">Qty</option>');
		}

	}

	document.write('</select></td><td>' + '<input type="text" value="0.00" name="price' + i + '" '  + 'size=3 onFocus="document.CalcForm.price' + i + '.blur()">' + '</td></tr>');
}

function writeTable()
{
	// Draws the whole table using iterative calls to writeTableRow(i) + plus the special case of the last row!
	document.write('<table cellspacing=1 cellpadding=3 border=0 bgcolor=#CCCCCC>');

	for (i=1; i<category.length; i++)
	{
		writeTableRow(i);
	}

	document.write('<tr bgcolor=#DEE5F7><td></td><td style=text-align:right><b>Total: </b></td><td><input type="text" ' + 'name="total" size=3 value="0.00"></td></tr></table>');
}

function update(num)
{
	eval('selected = document.CalcForm.menu' + num + '.selectedIndex;');
	cost = fix(category[num].product[selected].price);
	eval('document.CalcForm.price' + num + '.value = cost;');
	var grand_total = 0;
	var description = "";
	var first = 1;

	for (i=1; i<category.length; i++)
	{
		eval('grand_total += parseFloat(document.CalcForm.price' + i + '.value);');
		eval('selected = document.CalcForm.menu' + i + '.selectedIndex;');

		if(category[i].product[selected].description !=  "Please Select a Product" )
		{
			if(first == 1)
			{
				description = category[i].product[selected].description;
				first = 2;
			}
			else
			{
				description = description + " + " + category[i].product[selected].description;
			}
		}
	}

	document.CalcForm.total.value = fix(grand_total);
	document.BuyForm.amount.value = fix(grand_total);
	document.BuyForm.desc.value = description;
}

function fix(num)
{
	string = "" + num;

	if (string.indexOf('.') == -1)
	{
		return string + '.00';
	}

	seperation = string.length - string.indexOf('.');

	if (seperation > 3)
	{
 		return string.substring(0,string.length-seperation+3);
	}
	else if (seperation == 2)
	{
		return string + '0';
	}

	return string;
}

