
function Item(name) {

	this.name = name;

	this.options = new Array();

	this.selected = 0;

	this.configurator;  // set from Configurator when it adds an item

	this.addOption = function(name,cost) {
		var o = new Object;
		o.name = name;
		o.cost = cost;
		this.options.push(o);
		return true;
	}

	this.setSelected = function(index) {
		this.selected = index;
		return true;
	}


	this.getSelectElement = function() {
		this.id = this.configurator.getRandomString();
		var outHTML = '';
//		outHTML += "<div class='option'>";
		outHTML += "<tr><td>";
		outHTML += "<span class='label'>" + this.name + "</span>: ";
		outHTML += "</td><td>";
		outHTML += "<select name='" + this.name + "'";
		outHTML += " id='" + this.id + "' onchange='javascript:";
		var objname = this.configurator.objname;
		outHTML += objname + ".setSelectedOption(\"";
		outHTML += this.id + "\",this.selectedIndex";
		outHTML += ");" + objname + ".recalculate()'>";
		for ( var i = 0; i < this.options.length; i++ ) {
			var optionName = this.options[i].name;
			if ( i != this.selected ) {
				if ( this.options[i].cost > 0 ) {
					optionName += " &nbsp; (+" + this.options[i].cost + ")";
				}
				else if ( this.options[i].cost < 0 ) {
					optionName += " &nbsp; (" + this.options[i].cost + ")";
				}
			}
			outHTML += "<option value='" + optionName + "'";
			if ( i == this.selected ) { outHTML += " selected"; }
			outHTML += ">" + optionName;
			outHTML += "</option>";
		}
		outHTML += "</select>";
		outHTML += "</td></tr>";
//		outHTML += "</div>";
		outHTML += "\n";
		return outHTML;
	}

	this.getSelectedOptionCost = function() {
		var cost = this.options[this.selected].cost;
		if ( cost == null ) { cost = 0; }
		return cost;
	}

}

function Configurator(objname) {

	this.objname = objname;

	this.baseCost = null;

	this.setBaseCost = function(cost) {
		this.baseCost = cost;
	}

	this.elementName = "lamm_configurator";

	this.setElementName = function(string) {
		this.elementName = string;
	}

	this.getRandomString = function(length) {
		var randomString = '';
		var chars = "abcdefghiklmnopqrstuvwxyz";
		if ( length == null ) { length = 8; }
		for ( var i = 0; i < length; i++ ) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomString += chars.substring(rnum,rnum+1);
		}
		return randomString;
	}


	this.items = new Array();

	this.recalculate = function() {
		var cost = this.baseCost;
		for (i = 0; i < this.items.length; i++ ) {
			var item = this.items[i];
			var optionCost = item.getSelectedOptionCost();
			cost += optionCost;
		}
		var e;
		e = document.getElementById(this.elementName + "_total_cost");
		e.innerHTML = '$' + cost;
		e = document.getElementById(this.elementName + "_base_cost");
		e.innerHTML = '$' + this.baseCost;
		e = document.getElementById(this.elementName + "_total_cost_input")
		e.value = cost;
		e = document.getElementById(this.elementName + "_base_cost_input");
		e.value = this.baseCost;
		return true;
	}

	this.writeForm = function() {
		var outHTML = '';
		outHTML += "<table>\n";
		for (i = 0; i < this.items.length; i++ ) {
			var item = this.items[i];
			outHTML += item.getSelectElement();
		}
		outHTML += "<tr><td colspan='2'>";
		outHTML += "<br />";
		outHTML += "<strong>";
		outHTML += "<span class='base_cost'>";
		outHTML += "<span class='label'>Base Price:</span> ";
		outHTML += "<span id='" + this.elementName + "_base_cost'></span>";
		outHTML += "</span>";
		outHTML += "</strong>";
		outHTML += "<br />";
		outHTML += "<br />";
		outHTML += "<strong>";
		outHTML += "<span class='total_cost'>";
		outHTML += "<span class='label'>Price as Configured:</span> ";
		outHTML += "<span id='" + this.elementName + "_total_cost'></span>";
		outHTML += "</span>";
		outHTML += "</strong>";
		outHTML += "</td></tr>\n";
		outHTML += "</table>\n";
		outHTML += "<div style='display:none;'>";
		outHTML += "<input type='hidden' name='Base Price' ";
		outHTML += "id='" + this.elementName + "_base_cost_input' />\n";
		outHTML += "<input type='hidden' name='Price as Configured' ";
		outHTML += "id='" + this.elementName + "_total_cost_input' />\n";
		outHTML += "</div>\n";
		document.getElementById(this.elementName).innerHTML = outHTML;
		return true;
	}

	this.addItem = function(item) {
		this.items.push(item);
		item.configurator = this;
		return true;
	}

	this.setSelectedOption = function(id,index) {
		for (i = 0; i < this.items.length; i++ ) {
			var item = this.items[i];
			if (item.id == id) { item.setSelected(index); }
		}
		return true;
	}

	this.init = function() {
		this.writeForm();
		this.recalculate();
	}

}

