// DYC
// $Id: dyc.js,v 1.2 2005/07/15 15:28:00 jsan Exp $


var DYC_SELECTED        = "dyc_selected";
var DYC_DISABLED        = "dyc_disabled";
var DYC_PREVSELECTED    = "dyc_prevselected";
var DYC_PREVDISABLED    = "dyc_prevdisabled";
var DYC_PACKAGEFEATURES = "dyc_packagefeatures";
var DYC_CARID           = "dyc_carid";
var DYC_CARID_DIRECTORY = 5;
var CUR_CARID           = "carid";

if (getParam ("debug") == "yes")
{
//	debugWin("<br/><br/>Start debug...");
}
var debugMsg = "";



function setSelectedImages ()
{
	// see xsl/dyc/my06/car_js.xsl for the functions below
	setSelectedExterior ();
	setSelectedInterior ();
}



function debugWin (content)
{
	if (!top.consoleRef)
	{
		top.consoleRef = window.open("","myconsole","width=350,height=250,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1")
	}
 
	top.consoleRef.document.writeln(content + "<br>");
	//top.consoleRef.focus ();
}







// global feature list
var _all_ftrs = new Array();

// Returns the feature specified by the ID, otherwise undefined.
function getFeature(id)
{
	for(var i = 0; i < _all_ftrs.length; i++)
	{
		if (_all_ftrs[i].id == id)
		{
			return _all_ftrs[i];
		}
	}
}



// Returns the feature specified by the code, otherwise undefined.
function getFeatureByCode(code)
{
	for(var i = 0; i < _all_ftrs.length; i++)
	{
		if (_all_ftrs[i].featurecode == code)
		{
			return _all_ftrs[i];
		}
	}
}



// ******** Start of Feature Object definition
// id = feature ID
// type = optional: RIM, INT_CLR, EXT_CLR
// code = O = option, P = package, F = standard, E = engine
function Feature (id, type, code)
{
	this.id = id;
	this.type = type;
	this.code = code;
	this.price = 0.0;
	this.name;
	this.desc; // description
	this.featurecode;
	
	// setting of the following MUST be done via the enable & select functions
	this.isSelected = false;
	this.isEnabled = true;
	
	// The following are optional arrays of feature IDs.
	// These define the feature dependencies.
	this.incl;
	this.inclBy;
	this.excl;
	this.req;
	this.reqBy;
	this.prevSelected = false;
	this.prevDisabled = false;
	
	// listener arrays
	this.listener; // listener object reference
	
	// optional associated graphical component
	this.component; 


	// -------> functions

	// Updates the enabled state.
	// Updates the associated graphical component, if any.
	this.enable = function(state)
	{
		if ((this.code != 'F'  ||  this.type == 'RIM'  ||  this.type == 'INT_CLR')  &&  state != this.isEnabled)
		{
			this.isEnabled = state;
			// inform the graphical component of the new state
			if (this.component)
				this.component.enable (state);
		}
	} // end enable(state)


	// Updates the selected state, if enabled.
	// Updates the associated graphical component, if any.
	// Updates all the dependencies according to this features selection state.
	// Updates the summary panel.
	// parameters:
	//	selected = true | false, the new selection state
	//  mediated = true | false, if true then dependencies are not checked
	this.select = function (selected, mediated) 
	{
		if ((this.code != 'F'  ||  this.type == 'RIM'  ||  this.type == 'INT_CLR')  
			&&  this.isEnabled  
			&&  selected != this.isSelected)
		{
			// update the affected features accordingly, i.e. the dependencies
			if (mediated)
			{
				// save state, but don't inform dependencies, handled by mediator
				this.isSelected = selected;
			}
			else if (selected)
			{
				debugMsg += "\n";
				if (getParam ("debug") == "yes")
				{
					debugMsg += "1a - selected: " + this.name + "<BR/>\n";
				}
				// deselect package members before selecting the package itself.
				// otherwise the prices for selected features will double.
				if (this.featurecode == 'PACKAGE')
				{
					if (getParam ("debug") == "yes")
					{
						debugMsg += "2a - selected: " + this.name + "<BR/>\n";
					}
					this.disableSelectedPackageMembers ();
					if (getParam ("debug") == "yes")
					{
						debugMsg += "3a - selected: " + this.name + "<BR/>\n";
					}
				}

				// this is done here because dependencies have to know if they were modified due to a package
				this.isSelected = true;

				// deselecting all features that are excluded by this feature
				if (getParam ("debug") == "yes")
				{
					debugMsg += "4a - selected: " + this.name + "<BR/>\n";
				}
				if (getParam ("debug") == "yes")
				{
					debugMsg += "5a - selected: " + this.name + "<BR/>\n";
				}
				this.deselectExcludedFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "6a - selected: " + this.name + "<BR/>\n";
				}

				// select and disable included features
				this.selectIncludedFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "7a - selected: " + this.name + "<BR/>\n";
				}
				
				// enabling all features that are required by this feature
				this.enableSubFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "8a - selected: " + this.name + "<BR/>\n";
				}
			}
			else   // not selected
			{
				debugMsg += "\n";
				if (getParam ("debug") == "yes")
				{
					debugMsg += "1b - deselected: " + this.name + "<BR/>\n";
				}
				this.deselectSubFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "2b - deselected: " + this.name + "<BR/>\n";
				}
				this.disableSubFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "3b - deselected: " + this.name + "<BR/>\n";
				}
				this.deselectIncludedFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "4b - deselected: " + this.name + "<BR/>\n";
				}
				this.enableIncludedFeatures ();
				if (getParam ("debug") == "yes")
				{
					debugMsg += "5b - deselected: " + this.name + "<BR/>\n";
				}
				
				// this is done here because dependencies have to know if the were modified due to a package
				this.isSelected = false;
				
				if (this.featurecode == 'PACKAGE')
				{
					if (getParam ("debug") == "yes")
					{
						debugMsg += "6b - deselected: " + this.name + "<BR/>\n";
					}
					this.enableSelectedPackageMembers ();
					if (getParam ("debug") == "yes")
					{
						debugMsg += "7b - deselected: " + this.name + "<BR/>\n";
					}
				}
			}
			if (debugMsg != ""  &&  getParam ("debug") == "yes")
			{
				debugWin (debugMsg.replace ("\n", "<BR/>\n"));
			}

			// inform all listeners that this feature's selection state has changed
			if (this.listener)
			{
				for (var i = 0; i < this.listener.length; i++)
				{
					this.listener[i].featureListener (this);
				}
			}

			// inform the component of the new state
			if (this.component)
				this.component.select(selected);
			setSelectedImages ();
		}
	} // end select(isSelected)
	
	
	
	this.enableSubFeatures = function ()
	{
		if (this.reqBy)
		{
			for (var i = 0; i < this.reqBy.length; ++i)
			{
				var tmpFtr = getFeature (this.reqBy[i]);
				if (tmpFtr)
				{
					tmpFtr.enable (true);
				}
			}
		}
	}
	
	
	
	this.disableSubFeatures = function ()
	{
		if (this.reqBy)
		{
			for (var i = 0; i < this.reqBy.length; ++i)
			{
				var tmpFtr = getFeature (this.reqBy[i]);
				if (tmpFtr)
				{
					tmpFtr.enable (false);
				}
			}
		}
	}
	
	
	
	this.deselectSubFeatures = function ()
	{
		if (this.reqBy)
		{
			for (var i = 0; i < this.reqBy.length; ++i)
			{
				var tmpFtr = getFeature (this.reqBy[i]);
				if (tmpFtr  &&  !tmpFtr.featureAlreadyIncluded (this.id))
				{
					tmpFtr.select (false);
				}
			}
		}
	}
	
	
	
	this.featureAlreadyIncluded = function (id)
	{
		var selected = false;
		
		if (this.req)
		{
			for (var i = 0; i < this.req.length; ++i)
			{
				var tmpFtr = getFeature (this.req[i]);
				if (tmpFtr  &&  tmpFtr.id != id  &&  tmpFtr.isSelected)
				{
					selected = true;
				}
			}
		}
		
		return selected;
	}
	
	
	
	this.deselectExcludedFeatures = function ()
	{
		if (this.excl)
		{
			for (var i = 0; i < this.excl.length; ++i)
			{
				var tmpFtr = getFeature (this.excl[i]);
				if (tmpFtr)
				{
					if (this.featurecode == 'PACKAGE'  &&  tmpFtr.isSelected)
					{
						tmpFtr.prevSelected = true;
					}

					tmpFtr.select (false);

					// does the feature have requirements?
					if (!tmpFtr.req)
					{
						ftrEnabled = this.isEnabled;
					}
					else
					{
						// enable the feature if at least one requirement is met
						ftrEnabled = false;
						for(r = 0; r < tmpFtr.req.length; ++r)
						{
							var ftr = getFeature(tmpFtr.req[r]);

							if (ftr  &&  ftr.isSelected)
							{
								ftrEnabled = this.isEnabled;
								break;
							}
						}
					}
					tmpFtr.enable (ftrEnabled);
				}
			}
		}
	}
	
	
	
	this.disableExcludedFeatures = function ()
	{
		if (this.excl)
		{
			for (var i = 0; i < this.excl.length; ++i)
			{
				var tmpFtr = getFeature (this.excl[i]);
				if (tmpFtr)
				{
					tmpFtr.enable (false)
				}
			}
		}
	}
	
	
	
	this.selectIncludedFeatures = function ()
	{
		if (this.incl)
		{
			for (var i = 0; i < this.incl.length; ++i)
			{
				var tmpFtr = getFeature (this.incl[i]);
				if (tmpFtr)
				{
					if (!tmpFtr.inclBy)
						tmpFtr.inclBy = new Array ();
					if (arrayIndexOf (tmpFtr.inclBy, String (this.id)) == -1)
						tmpFtr.inclBy.push (this.id);
					
					// save previous rim
					if ((tmpFtr.type == "RIM"  ||  tmpFtr.type == "EXT_CLR"  ||  tmpFtr.type == "INT_CLR")
						&&  (tmpFtr.excl  ||  tmpFtr.isSelected))
					{
						if (tmpFtr.isSelected  ||  tmpFtr.prevSelected)
							tmpFtr.prevSelected = true;

						if (tmpFtr.excl)
						{
							var tmpFtr2;
							for (var j = 0; j < tmpFtr.excl.length; ++j)
							{
								tmpFtr2 = getFeature (tmpFtr.excl[j]);
								if (tmpFtr2)
								{
									tmpFtr2.prevDisabled = !tmpFtr2.isEnabled;
									tmpFtr2.prevSelected = tmpFtr2.isSelected || tmpFtr2.prevSelected;
									tmpFtr2.select (false);
									tmpFtr2.enable (false);
								}
							}
						}
					}

					tmpFtr.select (true);
					tmpFtr.enable (false);
				}
			}
		}
	}
	
	
	
	this.deselectIncludedFeatures = function ()
	{
		if (this.incl)
		{
			for (var i = 0; i < this.incl.length; ++i)
			{
				var tmpFtr = getFeature (this.incl[i]);
				if (tmpFtr)
				{
					if (tmpFtr.inclBy)
					{
						var index = arrayIndexOf (tmpFtr.inclBy, String (this.id));
						if (index != -1)
						{
							tmpFtr.inclBy.splice (index, 1);
						}
						
						tmpFtr.enable (true);
						tmpFtr.select (false);
						
						// restore previous rim
						if ((tmpFtr.type == "RIM"  ||  tmpFtr.type == "EXT_CLR"  ||  tmpFtr.type == "INT_CLR")
							&&  tmpFtr.excl)
						{
							for (var j = 0; j < tmpFtr.excl.length; ++j)
							{
								var tmpFtr2 = getFeature (tmpFtr.excl[j]);
								if (tmpFtr2  &&  tmpFtr2.prevSelected)
								{
									tmpFtr2.enable (true);
									tmpFtr2.select (true);
									tmpFtr2.prevSelected = false;
									break;
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	
	this.enableIncludedFeatures = function ()
	{
		if (this.incl)
		{
			for (var i = 0; i < this.incl.length; ++i)
			{
				var tmpFtr = getFeature (this.incl[i]);
				if (tmpFtr)
				{
					tmpFtr.prevDisabled = false;
					tmpFtr.enable (!tmpFtr.prevDisabled);
				}
			}
		}
	}
	
	
	
	this.enableSelectedPackageMembers = function ()
	{
		if (this.incl)
		{
			for (var i = 0; i < this.incl.length; ++i)
			{
				var tmpFtr = getFeature (this.incl[i]);

				if (tmpFtr  &&  ((tmpFtr.inclBy  &&  tmpFtr.inclBy.length > 0)  ||  tmpFtr.prevSelected))
				{
					tmpFtr.select (true);
					tmpFtr.prevSelected = false;
					if (tmpFtr.inclBy  &&  tmpFtr.inclBy.length > 0)
					{
						tmpFtr.enable (false);
						tmpFtr.disableExcludedFeatures ();
					}
				}
				else if (tmpFtr  &&  tmpFtr.prevDisabled)
				{
					tmpFtr.enable (false);
					tmpFtr.prevDisabled = false;
				}
			}
		}
	}
	
	
	
	this.disableSelectedPackageMembers = function ()
	{
		if (this.incl)
		{
			for (var i = 0; i < this.incl.length; ++i)
			{
				var tmpFtr = getFeature (this.incl[i]);
				if (tmpFtr)
				{
					if (tmpFtr.isSelected  ||  (tmpFtr.inclBy  &&  tmpFtr.inclBy.length <= 0))
						tmpFtr.prevSelected = tmpFtr.isSelected;

					tmpFtr.prevDisabled = !tmpFtr.isEnabled;
					tmpFtr.enable (true);
					tmpFtr.select (false);
				}
			}
		}
	}
	
	
	
	// Updates all the features in the dependency list according to the epxression.
	// Checks if the dependency list is defined, otherwise does nothing.
	//   id = id of current feature, to avoid depending on itself
	//   list = the dependency list to be updated
	//   expr = the expression to perform on each dependency
	//          NOTE: the expr should refer to the feature as ftr
	//          EXAMPLE: expr = 'ftr.select(true);'
	this.updateDependencies = function(id, list, expr)
	{
		if (list)
		{
			for(var i = 0; i < list.length; i++)
			{
				if (list[i] != id)
				{
					var ftr = getFeature(list[i]);
					if (ftr)
					{
						eval(expr);
					}
					else
					{
						debug('Data error: unknown feature: '+list[i], 'ERROR');
					}
				}
				else
				{
					debug('Data Error: Skipping self dependency: '+id, 'ERROR');
				}
			}
		}
	}
	
	
	// toggles the features selection state via the select function
	this.click = function ()
	{
		if (this.isEnabled)
		{
			// features in groups can't be deselected
			if ((this.type == 'EXT_CLR') || (this.type == 'INT_CLR') || (this.type == 'RIM'))
			{
				if (!this.isSelected)
				{
					this.select(true);
				}
			}
			else
			{
				this.select(!this.isSelected);
			}
		}
		
		this.showConflictInformation ();
	}
	
	
	
	this.showConflictInformation = function ()
	{
		if (!this.isEnabled)
		{
			var msg1 = disabledGeneral + "\n";
			var msg2 = "";

			if (this.req)
			{
				reqSelected = false;
				total = 0;
				msg2 = "";
				
				for (var j = 0; j < this.req.length; ++j)
				{
					var ftr = getFeature (this.req[j]);
					
					if (ftr)
					{
						if (ftr.isSelected)
						{
							reqSelected = true;
						}
						++total;
						msg2 += '<IMG src="images/dyc/conflict_bullet.gif" vspace="2" width="3" height="3"/>' 
							+ '<IMG src="images/spacer.gif" width="5" height="1"/>'
							+ ftr.name + "<BR/>\n";
					}
				}

				if (!reqSelected)
				{
					msg1 += disabledRequired1 + " "
						+ (total > 1 ? disabledRequired2 + " " : "")
						+ disabledRequired3 + ":"
						+ "\n";
				}
			}
			else if (this.inclBy)
			{
				total = 0;
				msg2 = "";
				
				for (var j = 0; j  < this.inclBy.length; ++j)
				{
					var ftr = getFeature (this.inclBy[j]);
					if (ftr)
					{
						msg2 += '<IMG src=images/dyc/conflict_bullet.gif" vspace="2" width="3" height="3"/>' 
							+ '<IMG src="../../spacer.gif" width="5" height="1"/>'
							+ ftr.name + "<BR/>\n";
						++total;
					}
						
					if (total > 0)
					{
						msg1 = disabledIncluded + ":\n";
					}
				}
			}
			else if (this.excl)
			{
				exclSelected = false;
				total = 0;
				msg2 = "";
				
				for (var j = 0; j  < this.excl.length; ++j)
				{
					var ftr = getFeature (this.excl[j]);

					if (ftr  &&  ftr.inclBy  &&  ftr.inclBy.length > 0)
					{
						for (var k = 0; k < ftr.inclBy.length; ++k)
						{
							var ftr2 = getFeature (ftr.inclBy[k]);
							if (ftr2)
							{
								msg2 += '<IMG src=images/dyc/conflict_bullet.gif" vspace="2" width="3" height="3"/>' 
									+ '<IMG src="../../spacer.gif" width="5" height="1"/>'
									+ ftr2.name + "<BR/>\n";
								++total;
							}
						}
						
						if (total > 0)
						{
							msg1 += disabledBlocking + "\n";
						}
					}
				}
			}
			
			if (msg2)
			{
				htmlstart = '<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">';
				htmlstart += '<TR>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="24" bgcolor="#424242"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="338" bgcolor="#424242"><IMG src="images/spacer.gif" width="338" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="25" bgcolor="#424242"><IMG src="images/spacer.gif" width="25" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="2"><IMG src="images/spacer.gif" width="2" height="1" alt="" border="0"</TD>';
				htmlstart += '</TR>';
				htmlstart += '<TR>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD colspan="4" bgcolor="#CCCCCC"><IMG src="images/spacer.gif" width="388" height="2" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="2"><IMG src="images/spacer.gif" width="2" height="1" alt="" border="0"</TD>';
				htmlstart += '</TR>';
				htmlstart += '<TR>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD colspan="4" bgcolor="#CCCCCC"><IMG src="images/spacer.gif" width="388" height="23" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlstart += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlstart += '</TR>';


				htmlmiddle = '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="1" height="14" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"></TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';
				htmlmiddle += '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"><IMG src=images/dyc/dependency_warning.gif" width="36" height="29" alt="" border="0"/><BR/>';
				htmlmiddle += '    <IMG src="images/spacer.gif" width="1" height="11" alt="" border="0"/><BR/>';
				htmlmiddle += '  </TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"></TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';
				htmlmiddle += '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><SPAN class="smallheader">' + msg1 + '</SPAN><BR/>';
				htmlmiddle += '    <IMG src="images/spacer.gif" width="1" height="9" alt="" border="0"/><BR/>';
				htmlmiddle += '    <SPAN class="smalltext">' + msg2 + '</SPAN>';
				htmlmiddle += '  </TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';
				htmlmiddle += '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="1" height="15" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"></TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';
				htmlmiddle += '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD align="right" bgcolor="#FFFFFF">';
				
				closelabel = disabledClose;
				htmlmiddle += '    <TABLE border="0" cellpadding="0" cellspacing="0"><TR>'
							+ '<TD bgcolor="#FF9933"><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '<TD bgcolor="#FF9933" colspan="4"><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '<TD><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '</TR>'
							+ '<TR>'
							+ '<TD bgcolor="#FF9933" width="2"><IMG alt border="0" height="15" src="images/spacer.gif" width="2"></TD>'
							+ '<TD bgcolor="#FF9933" width="5"><IMG alt border="0" height="15" src="images/spacer.gif" width="5"></TD>'
							+ '<TD bgcolor="#FF9933" valign="TOP" width="1"><NOBR><A class="whitelink" href="javascript:closeDependencyMessage()">' + closelabel + '</A></NOBR></TD>'
							+ '<TD bgcolor="#FF9933" valign="TOP"><IMG alt border="0" height="1" src="images/spacer.gif" width="3" align="middle"><IMG alt border="0" height="15" src=images/arrow_white.gif" width="15"></TD>'
							+ '<TD align="RIGHT" bgcolor="#FF9933" valign="TOP" width="1"><IMG alt border="0" height="1" src="images/spacer.gif" width="1" align="middle"></TD>'
							+ '<TD bgcolor="#434343" width="2"><IMG alt border="0" height="15" src="images/spacer.gif" width="2"></TD>'
							+ '</TR>'
							+ '<TR>'
							+ '<TD width="2"><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '<TD bgcolor="#434343" colspan="4" width="NaN"><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '<TD bgcolor="#434343" width="2"><IMG alt border="0" height="2" src="images/spacer.gif" width="2"></TD>'
							+ '</TR>'
							+ '</TABLE>';
				htmlmiddle += '  </TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"></TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';
				htmlmiddle += '<TR>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD colspan="2" bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"><IMG src="images/spacer.gif" width="1" height="25" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD bgcolor="#FFFFFF"></TD>';
				htmlmiddle += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlmiddle += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlmiddle += '</TR>';

				
				htmlend = '<TR>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD colspan="4" bgcolor="#CCCCCC"><IMG src="images/spacer.gif" width="388" height="23" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="23" alt="" border="0"/></TD>';
				htmlend += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="23" alt="" border="0"/></TD>';
				htmlend += '</TR>';
				htmlend += '<TR>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD colspan="4" bgcolor="#CCCCCC"><IMG src="images/spacer.gif" width="388" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="1" alt="" border="0"</TD>';
				htmlend += '</TR>';
				htmlend += '<TR>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="24" bgcolor="#424242"><IMG src="images/spacer.gif" width="24" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="338" bgcolor="#424242"><IMG src="images/spacer.gif" width="338" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="25" bgcolor="#424242"><IMG src="images/spacer.gif" width="25" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="1" alt="" border="0"/></TD>';
				htmlend += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="1" alt="" border="0"</TD>';
				htmlend += '</TR>';
				htmlend += '<TR>';
				htmlend += '  <TD width="1"><IMG src="images/spacer.gif" width="1" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1"><IMG src="images/spacer.gif" width="1" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="24" bgcolor="#424242"><IMG src="images/spacer.gif" width="24" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="338" bgcolor="#424242"><IMG src="images/spacer.gif" width="338" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="25" bgcolor="#424242"><IMG src="images/spacer.gif" width="25" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="1" bgcolor="#424242"><IMG src="images/spacer.gif" width="1" height="2" alt="" border="0"/></TD>';
				htmlend += '  <TD width="2" bgcolor="#424242"><IMG src="images/spacer.gif" width="2" height="2" alt="" border="0"</TD>';
				htmlend += '</TR>';
				htmlend += "</TABLE>";
			
				reWriteLayer ("dependencymessage", htmlstart + htmlmiddle + htmlend);
				placeMessageWindow ();
				setVisibility ("dependencybackground", "visible");
				setVisibility ("dependencymessage", "visible");
			}
		}
	}
	
	
	// Adds a listener function to this feature.
	// When this feature's selection state changes, the listening objects will
	// be informed via their supplied listener function, ex: obj.listener(feature)
	//   obj = listener object
	this.addListener = function(obj)
	{
		if (!this.listener) this.listener = new Array();
		this.listener[this.listener.length] = obj;
	}
	
	// register this feature
	_all_ftrs[_all_ftrs.length] = this;
} // end Feature(id, type, code)


					
var myns4 = (document.layers)? true:false
var myie4 = (document.all)? true:false



function createConflictBackgroundLayer ()
{
	var winW = (myns4)? window.innerWidth  : document.body.scrollWidth;
	var winH = (myns4)? window.innerHeight : document.body.scrollHeight;
	var f = getObject ("dependencybackground");
	
	f.width = winW;
	f.height = winH;
	reWriteLayer ('dependencybackground', '<IMG src="images/spacer.gif" height="' + winH + '" width="' + winW + '">');
}



function placeMessageWindow ()
{
	var msg = getObject ("dependencymessage");
	var w = msg.width;
	if (w.indexOf ("px"))
	{
		w = w.substring (0, w.indexOf ("px"));
	}
	var h = msg.height;
	if (h.indexOf ("px"))
	{
		h = h.substring (0, h.indexOf ("px"));
	}
	var winW = document.body.clientWidth;
	var winH = document.body.clientHeight;
	var midX = (winW - w) / 2;
	var midY = (winH - h) / 2;

	msg.top = Number (getScrollHeight () + midY) + "px";
	msg.left = midX + "px";
}



function getScrollHeight ()
{
	var deltaY = 0;
	
	if( typeof (window.pageYOffset) == 'number' ) 
	{
		//Netscape compliant
		deltaY = window.pageYOffset;
	} 
	else if (document.body  
		&&  document.body.scrollTop)
	{
		//DOM compliant
		deltaY = document.body.scrollTop;
	}
	else if (document.documentElement 
		&&  document.documentElement.scrollTop)
	{
		//IE6 standards compliant mode
		deltaY = document.documentElement.scrollTop;
	}

	return deltaY;
}
	
	


function closeDependencyMessage ()
{
	setVisibility ("dependencymessage", "hidden");
	setVisibility ("dependencybackground", "hidden");
}



// ******** Start Summary function definitions.
// Expects a global feature list called: _all_ftrs
// Expects an HTML form called DYC_SUMMARY with the following input fields:
//   EXTERIOR, INTERIOR, WHEELS, MET_PAINT, PACKAGES, OPTIONS, TOTAL
function Summary()
{
	this.form;

	// listens for selection and deselection of features
	this.featureListener = function (ftr)
	{
		if (!this.form)
			this.form = document.forms['DYC_SUMMARY'];
		
		// check if feature is included in a selected package
		var partOfPkg = false;
		for (var i = 0; i < _all_ftrs.length; ++i)
		{
			tmpFtr = _all_ftrs[i];
			// if this is a selected package, check it
			if (tmpFtr.featurecode == 'PACKAGE'  
				&&  tmpFtr.incl
				&&  (tmpFtr.isSelected  ||  tmpFtr.code == 'F'))
			{
				for (var j = 0; j < tmpFtr.incl.length; ++j)
				{
					if (ftr.id == tmpFtr.incl[j])
					{
						partOfPkg = true;
						debug ('Not counting ftr ' + ftr.id + ', included in pkg ' + tmpFtr.id, 'DEBUG');
						break;
					}
				}
			}
		}


//		alert (ftr.type  + ", " + ftr.code + ", " + ftr.featurecode);
		// adjust the names and prices for interior, exterior, and wheels
		if (ftr.type == 'EXT_CLR')
		{
			if (ftr.isSelected)
			{
				if (!partOfPkg)
					this.adjustValue (this.form.elements['MET_PAINT'], ftr);
			}
			else
			{
				this.form.elements['MET_PAINT'].value = '';
			}
		}
		else if (ftr.type == 'INT_CLR')
		{
			if (!partOfPkg)
				this.adjustValue(this.form.elements['OPTIONS'], ftr);
		}
		else if (ftr.type == 'RIM')
		{
			if (!partOfPkg)
				this.adjustValue (this.form.elements['OPTIONS'], ftr);
		}
		else if (ftr.code == 'O'  &&  ftr.featurecode != 'PACKAGE')   // options and packages
		{
			if (!partOfPkg)
				this.adjustValue (this.form.elements['OPTIONS'], ftr);
		}
		else if (ftr.featurecode == 'PACKAGE')
		{
			if (!partOfPkg)
				this.adjustValue (this.form.elements['PACKAGES'], ftr);
		}
	
		if (!partOfPkg)
			this.adjustValue (this.form.elements['TOTAL'], ftr);
	} // end featureListener(ftr)


	
	// goes through all the selected features and updates the summary panel
	this.refresh = function ()
	{
	  
		//alert("in refresh()...")
		if (! this.form) this.form = document.forms['DYC_SUMMARY'];
		if (this.form)
		{
			// clear all summary values
			this.form.elements['MET_PAINT'].value = "";
			this.form.elements['OPTIONS'].value = "";

			// special case for summary page
			if (this.form.elements['PACKAGES']) 
				this.form.elements['PACKAGES'].value = "";
		
			// set base car price
			if (this.form.elements['PRICE'].value > 0)
			{
				this.form.elements['PRICE'].value = localizePrice (
					this.form.elements['PRICE'].value, 
					thousanddelimiter, 
					decimaldelimiter, 
					true);
				this.form.elements['TOTAL'].value = this.form.elements['PRICE'].value;
			}
			else
			{
				this.form.elements['TOTAL'].value = this.form.elements['PRICE'].value;
			}
	
			// update all selected features
			for(var i=0; i < _all_ftrs.length; i++)
			{
				if (_all_ftrs[i].isSelected)
				{
					this.featureListener(_all_ftrs[i]);
				}
			}
		}
	} // refresh()


	
	// *** private function
	// Adds 2 strings together as numbers, result is 0 if not a number
	// element = form element to start value in
	// ftr = Feature object
	// adjustment = + or -, an override for ftr.selected
	this.adjustValue = function(element, ftr, adjustment)
	{
		if (!element) 
			return "";

		var a = parseFloat (delocalizePrice (element.value, thousanddelimiter, decimaldelimiter));
		var b = parseFloat (ftr.price);
		if (isNaN(a)) 
			a = 0;
		if (isNaN(b)) 
			b = 0;
		var result = (ftr.isSelected) ? (a + b) : (a - b);
		result = localizePrice (result, thousanddelimiter, decimaldelimiter, true);
		
		element.value = (result == 0) ?  "" : result;
	} // adjustValue(element,ftr,adjustment)


	// *** private function
	// Adjusts the total price of options included in a package 
	// ftr = Feature object
	this.adjustPackage = function(ftr)
	{
		if (ftr.incl)
		{
			for(var i = 0; i < ftr.incl.length; i++)
			{
			}
		}
	} // adjustPackage(ftr)

} // Summary()



var summaryPanel = new Summary();
// ******** End of Summary function definitions.



// imgObjName = name of the HTML image object
// src = the source to be swapped into thh image object's source property.
function ImageSwapper(imgObjName, src)
{
	this.img;
	this.imgObjName = imgObjName;
	this.src = src;
	
	// functions
	this.featureListener = function()
	{
		if (!this.img) this.img = getImage(this.imgObjName);
		
		if (this.img)
		{
			this.img.src = this.src;
		}
		else
		{
			debug('ImageSwapper image object missing: '+this.imgObjName, 'ERROR');
		}
	} // end featureListener()
} // end ImageSwapper(imgObjName, src)



function FormBinding(formName, elemName)
{
	this.elem;
	this.formName = formName;
	this.elemName = elemName;

	this.featureListener = function(ftr)
	{
		debug( "updating binding: document.forms['"+this.formName+"'].elements['"+this.elemName+"']", 'DEBUG' );
		if (!this.elem) this.elem = eval("document.forms['"+this.formName+"'].elements['"+this.elemName+"']");
		this.elem.value = ftr.name;
	}
} // FormBinding(formName,elemName)



// sets the default for this feature type
function setDefaults(type)
{
	var def;
	
	for(var i = 0; i < _all_ftrs.length; i++)
	{
		if (_all_ftrs[i].type == type)
		{
			// if a feature is selected, use that
			if (_all_ftrs[i].isSelected) 
				return;
			
			// get the first feature in the group
			if (!def) 
				def = _all_ftrs[i];
			
			// if a feature is standard, use that instead of the first feature
			if (_all_ftrs[i].code == 'F') 
				def = _all_ftrs[i]; 
		}
	}
	if (def) 
		def.select(true);
} // end setDefaults(type)



// Expects the following:
//   - a global feature array called: _all_ftrs
//   - a selected feature array called: selected
//   - a disabled feature array called: disabled
//   - a form to exist called: DYC
//   - all form elements tied to features have feature IDs as names
//   - a summary panel javascript object must be defined, called summaryPanel
// Does the following:
//   - Sets the selected & enabled state of each feature as per the global lists.
//   - Disables features that don't have their required features.
//   - Creates the Component objects for all features.
function dycInit()
{
	// this information is saved in cookies as a comma separated list of IDs
	var dyc_selected     = getCookie (DYC_SELECTED);
	var dyc_disabled     = getCookie (DYC_DISABLED);
	var dyc_prevselected = getCookie (DYC_PREVSELECTED);
	var dyc_prevdisabled = getCookie (DYC_PREVDISABLED);
	var dyc_carid        = getCookie (DYC_CARID);
	var cur_carid        = getDirectoryFromUrl (DYC_CARID_DIRECTORY);

	if (getParam ("debug") == "yes")
	{
		alert ("dyc_selected:" + dyc_selected + "\n"
			+ "dyc_disabled:" + dyc_disabled + "\n"
			+ "dyc_prevselected:" + dyc_prevselected + "\n"
			+ "dyc_prevdisabled:" + dyc_prevdisabled + "\n"
			+ "dyc_carid:" + dyc_carid + "\n"
			+ "cur_carid:" + cur_carid);
	}
	
	if (!cur_carid || (dyc_carid && (dyc_carid == cur_carid)))
	{
		// set the selected state of each feature
		initStandardPackages ();
		initSelectedFeatures (dyc_selected);
		initSelectedSubFeatures (dyc_selected);
	
		// set the enabled state of each feature
		initDisabledFeatures (dyc_disabled);
		initPreviouslySelected (dyc_prevselected);
		initPreviouslyDisabled (dyc_prevdisabled);
	}
	else
	{
		// not the same car in the cookies, so remove those
		deleteCookie (DYC_SELECTED);
		deleteCookie (DYC_DISABLED);
		deleteCookie (DYC_PACKAGEFEATURES);
		deleteCookie (DYC_PREVSELECTED);
		deleteCookie (DYC_PREVDISABLED);
		deleteCookie (DYC_CARID);
	}
	
	// - Disable features if any of their required features are not selected.
	// - Add the Summary panel listener to each feature.
	// - Add the interior, exterior, and rim listeners
	disableRequiredSubFeatures ();
	
	// adjust the spec group selection dropdown, if it is available
	// this is only in step 2
	setSpecgroupDropdownDefault ();
	
	setDefaults ('EXT_CLR');
	setDefaults ('INT_CLR');
	setDefaults ('RIM');
	refreshCheckboxes ();
	summaryPanel.refresh ();
	setSelectedImages ();
} // end dycInit()



function initSelectedFeatures (dyc_selected)
{
	if (dyc_selected)
	{
		selected = eval('new Array (' + dyc_selected + ');');
		for(var j = 0; j < selected.length; ++j)
		{
			var ftr = getFeature (selected[j]);
			
			if (ftr)
			{
				ftr.select (true, true);
			}
			else 
			{
				debug('init:data error: invalid feature: '+selected[j], 'ERROR');
			}
		}
	}
}



function initSelectedSubFeatures (dyc_selected)
{
	if (dyc_selected)
	{
		selected = eval('new Array (' + dyc_selected + ');');
		for(var j = 0; j < selected.length; ++j)
		{
			var ftr = getFeature (selected[j]);
			
			if (ftr)
			{
				ftr.selectIncludedFeatures ();
			}
			else 
			{
				debug('init:data error: invalid feature: '+selected[j], 'ERROR');
			}
		}
	}
}



function initStandardPackages ()
{
	for(var j = 0; j < _all_ftrs.length; ++j)
	{
		var ftr = _all_ftrs[j];

		if (ftr)
		{
			if (ftr.featurecode == 'PACKAGE'  &&  ftr.code == 'F')
			{
				ftr.selectIncludedFeatures ();
			}
		}
		else 
		{
			debug('init:data error: invalid feature: '+selected[j], 'ERROR');
		}
	}
}



function initDisabledFeatures (dyc_disabled)
{
	if (dyc_disabled)
	{
		disabled = eval('new Array('+dyc_disabled+');');

		for (j = 0; j < disabled.length; ++j)
		{
			var ftr = getFeature (disabled[j]);
			if (ftr)
			{
				ftr.enable (false);
//				ftr.deselectExcludedFeatures ();
//				ftr.disableExcludedFeatures ();
			}
		}
	}
}



function initPreviouslySelected (dyc_prevselected)
{
	if (dyc_prevselected)
	{
		var prevSelected = eval('new Array(' + dyc_prevselected + ');');

		for (j = 0; j < prevSelected.length; ++j)
		{
			var ftr = getFeature (prevSelected[j]);
			if (ftr)
			{
				ftr.prevSelected = "true";
			}
		}
	}
}



function initPreviouslyDisabled (dyc_prevdisabled)
{
	if (dyc_prevdisabled)
	{
		var prevDisabled = eval('new Array(' + dyc_prevdisabled + ');');

		for (j = 0; j < prevDisabled.length; ++j)
		{
			var ftr = getFeature (prevDisabled[j]);
			if (ftr)
			{
				ftr.prevDisabled = "true";
			}
		}
	}
}



function disableRequiredSubFeatures ()
{
	for(j = 0; j < _all_ftrs.length; ++j)
	{
		// add the summary panel listener
		_all_ftrs[j].addListener (summaryPanel);
		
		// does the feature have requirements?
		if (_all_ftrs[j].req)
		{
			// enable the feature if at least one requirement is met
			ftrEnabled = false;
			for(r = 0; r < _all_ftrs[j].req.length; ++r)
			{
				var ftr = getFeature(_all_ftrs[j].req[r]);
				
				if (ftr  &&  ftr.isSelected)
				{
					ftrEnabled = true;
					break;
				}
			}
			_all_ftrs[j].enable (ftrEnabled);
		}
	}
}



function setSpecgroupDropdownDefault ()
{
	var specgroup = getParam ('specgroup');
	var formSpecGroup = getForm ('specgroup');
	
	if (formSpecGroup  &&  specgroup)
	{
		var dropdown = eval ("document.forms['specgroup'].specgroup");
		var idx = 0;
    	for(var i = 0; i < dropdown.options.length; ++i)
		{
	    	if (dropdown.options[i].value == specgroup)
			{
				idx = i;
				break;
			}
    	}
		dropdown.selectedIndex = idx;
	}
}



// Saves the selected and disabled states in cookies
function saveState(carid)
{
	if (!carid  ||  carid == -1) carid = getDirectoryFromUrl (DYC_CARID_DIRECTORY);
	var dyc_selected = '';
	var dyc_disabled = '';

	for(var i=0; i < _all_ftrs.length; i++)
	{
		if (_all_ftrs[i].code != 'F'  ||  _all_ftrs[i].type == 'RIM'  ||  _all_ftrs[i].type == 'INT_CLR')
		{
			if (getParam ("debug") == "yes" && _all_ftrs[i].isSelected   &&  (!_all_ftrs[i].inclBy  ||  _all_ftrs[i].inclBy.length <= 0))
			{
				alert ("selected: " + _all_ftrs[i].name);
			}
			if (_all_ftrs[i].isSelected  &&  (!_all_ftrs[i].inclBy  || _all_ftrs[i].inclBy.length <= 0))
			{
				dyc_selected = (dyc_selected) ? dyc_selected+",'"+_all_ftrs[i].id+"'" : "'"+_all_ftrs[i].id+"'";
			}
			if (!_all_ftrs[i].isEnabled)
			{ 
				dyc_disabled = (dyc_disabled) ? dyc_disabled+",'"+_all_ftrs[i].id+"'" : "'"+_all_ftrs[i].id+"'";
			}
		}
	}
	
	setCookie (DYC_SELECTED, dyc_selected);
	setCookie (DYC_DISABLED, dyc_disabled);
	setCookie (DYC_PACKAGEFEATURES, getPackageFeatures ());
	setCookie (DYC_PREVSELECTED, getPreviouslySelected ());
	setCookie (DYC_PREVDISABLED, getPreviouslyDisabled ());
	setCookie (DYC_CARID, carid);
//	alert (getCookie (DYC_SELECTED));
} // end saveState(carid)



/* 
 * get a string of all included feature for selected packages.
 * these features are not supposed to be included in the SQL
 * query to get all prices for selected features.
 *
 */
function getPackageFeatures ()
{
	var s = "";
	
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = getFeature (_all_ftrs[i].id);
		if (ftr  &&  ftr.featurecode == 'PACKAGE'  &&  ftr.isSelected) 
		{
			for (var j = 0; ftr.incl  &&  j < ftr.incl.length; ++j)
			{
				var tmpFtr = getFeature (ftr.incl[j]);
				if (tmpFtr)
				{
					if (s != '')
					{
						s += ',';
					}
					s += "'" + tmpFtr.id + "'";
				}
			} 
		}
	}
	
	return s;
}



function getPreviouslySelected ()
{
	var s = "";
	
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = _all_ftrs[i];
		if (ftr  &&  ftr.prevSelected
			&&  (ftr.type == "RIM"  ||  ftr.type == "EXT_CLR"  ||  ftr.type == "INT_CLR"))
		{
			if (s != '')
			{
				s += ',';
			}
			s += "'" + ftr.id + "'";
		}
		else if (ftr  &&  ftr.featurecode == 'PACKAGE'  &&  ftr.isSelected) 
		{
			for (var j = 0; ftr.incl  &&  j < ftr.incl.length; ++j)
			{
				var tmpFtr = getFeature (ftr.incl[j]);
				if (tmpFtr  &&  tmpFtr.prevSelected)
				{
					if (s != '')
					{
						s += ',';
					}
					s += "'" + tmpFtr.id + "'";
				}
			} 
		}
	}
	
	return s;
}



function getPreviouslyDisabled ()
{
	var s = "";
	
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = _all_ftrs[i];
		if (ftr  &&  ftr.prevDisabled
			&&  (ftr.type == "RIM"  ||  ftr.type == "EXT_CLR"  ||  ftr.type == "INT_CLR"))
		{
			if (s != '')
			{
				s += ',';
			}
			s += "'" + ftr.id + "'";
		}
		else if (ftr  &&  ftr.featurecode == 'PACKAGE'  &&  ftr.isSelected) 
		{
			for (var j = 0; ftr.incl  &&  j < ftr.incl.length; ++j)
			{
				var tmpFtr = getFeature (ftr.incl[j]);
				if (tmpFtr  &&  tmpFtr.prevDisabled)
				{
					if (s != '')
					{
						s += ',';
					}
					s += "'" + tmpFtr.id + "'";
				}
			} 
		}
	}
	
	return s;
}



function arrayIndexOf (arr, val)
{
	var index = -1;
	
	if (arr)
	{
		for (var i = 0; i < arr.length; ++i)
		{
			if (arr[i] == val)
			{
				index = i;
				break;
			}
		}
	}
	
	return index;
}



function showDisabled ()
{
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = _all_ftrs[i];
		if (ftr  &&  !ftr.isEnabled)
		{
			debugWin ("disabled feature: " + ftr.name + " (" + ftr.id + ")");
		}
	}
}



function arrPush (item)
{
        this[this.length] = item;
}
if (typeof Array.prototype.push == "undefined")
{
        Array.prototype.push = arrPush;
}



function arrSplice (index, delTotal)
{
        var arr = new Array ();
        var ret = new Array ();
        var i = 0;

        // copy the elements before the first element to splice away
        for (i = 0; i < index; ++i)
        {
                arr[i] = this[i];
        }

        // copy the elements after the last element to splice away
        for (i = index + delTotal; i < this.length; ++i)
        {
                arr[arr.length] = this[i]
        }

        // copy the elements that are spliced away
        for (i = 0; i < delTotal; ++i)
        {
                ret[i] = this[index + i];
        }

        // copy the temp array to the current
        this.length = 0
        for (i = 0; i < arr.length; ++i)
        {
                this[this.length] = arr[i]
        }

        return ret;
}
if (typeof Array.prototype.splice == "undefined")
{
        Array.prototype.splice = arrSplice;
}



function getSelectedOfType (ftype)
{
	var ftr;
	var s = "";
	var i = 0;
	for (i = 0; i < _all_ftrs.length; ++i)
	{
		ftr = _all_ftrs[i];
		if (ftr  &&  ftr.type == ftype)
			s += "Feature: " + ftr.name + "\n";
		
		if (ftr  &&  ftr.type == ftype  
			&&  (ftr.isSelected  
				||  (ftype == "ENG"  &&  ftr.code == "E")))
		{
			break;
		}
	}
	
	return (ftr) ? ftr : "";
	
}



function setSaveForm ()
{
	var selected = "";
	var total = 0;
	var form = getForm ("DYC_SUMMARY");
	
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = _all_ftrs[i];
		if (ftr  &&  ftr.isSelected)
		{
			if (selected != "")
				selected += ",";
			selected += ftr.id;
			total += Number (ftr.price);
		}
	}
	
	form.FEATURE.value = selected;
	form.TOTAL.value = total;
	form.CARID.value = carid;
}



function openExtended(id, modelcode)
{
	upDir = (location.href.indexOf ("step2.shtml") > 0)
		? "../../"
		: "../../../";
	newWindow = window.open(upDir + "model/" + modelcode + "/" + id + "/fc_extended.shtml","Saab","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=no,width=403,height=492");
	newWindow.focus ();
}



function updateFeatureName (featureType)
{
	reWriteLayer ("name" + featureType, getSelectedOfType (featureType).name);
}



function initFeatureNames()
{
	updateFeatureName ("EXT_CLR");
	updateFeatureName ("INT_CLR");
	updateFeatureName ("RIM");
}



function createSummary ()
{
//	getSelectedOfType ("EXT_CLR");
	var html = '';
	var ftr = '';
	var total = 0;
	var engine = getSelectedOfType ("ENG");

	// General options and features
	html = '<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">';
	html += tableHeader (summarytitle + ' ' + summarymodelname + ' ' + summarycarname)
	html += tableSpacer (10);
	html += tableHeader (summarymodeltrimengine)
	html += lightGrayLine ();
	html += tableSpacer (4);
	html += tableRowStart (3);
	html += tableCell (summarymodelname + ' ' + '<A href="javascript:openExtended(' + engine.id + ',\'' + summarymodelcode + '\');">' + summarycarname + '</A>', 'smalltext');
	if (summarycarprice > 0)
		html += tableCell (getPrice (summarycarprice), 'smalltext', 'right');
	html += tableRowEnd (8);
	html += tableSpacer (5);
	html += lightGrayLine ();
	html += tableSpacer (25)
	total += Number (summarycarprice);

	// Exterior and interior
	html += tableHeader (summaryextint)
	html += lightGrayLine ();
	
	ftr = getSelectedOfType ("EXT_CLR");
	html += tableSpacer (4);
	html += tableRowStart (3);
	html += tableCell (ftr.name, 'smalltext');
	if (ftr.price > 0)
		html += tableCell (getPrice (ftr.price), 'smalltext', 'right');
	html += tableRowEnd (8);
	html += tableSpacer (5);
	html += lightGrayLine ();
	total += Number (ftr.price);

	ftr = getSelectedOfType ("INT_CLR");
	html += tableSpacer (4);
	html += tableRowStart (3);
	html += tableCell (ftr.name, 'smalltext');
	if (ftr.price > 0  &&  !inPackage (ftr))
	{
		html += tableCell (getPrice (ftr.price), 'smalltext', 'right');
		total += Number (ftr.price);
	}
	html += tableRowEnd (8);
	html += tableSpacer (5);
	html += lightGrayLine ();

	ftr = getSelectedOfType ("RIM");
	html += tableSpacer (4);
	html += tableRowStart (3);
	html += tableCell (ftr.name, 'smalltext');
	if (ftr.price > 0  &&  !inPackage (ftr))
	{
		html += tableCell (getPrice (ftr.price), 'smalltext', 'right');
		total += Number (ftr.price);
	}
	html += tableRowEnd (8);
	html += tableSpacer (5);
	html += lightGrayLine ();
	html += tableSpacer (25)


	// All other options
	html += tableHeader (summaryoptions);
	html += lightGrayLine ();

	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		var ftr = _all_ftrs[i];
		if (ftr  &&  ftr.isSelected
			&&  (ftr.type != 'EXT_CLR'  &&  ftr.type != 'INT_CLR'  &&  ftr.type != 'RIM')
			&&  !ftr.inclBy)

		{
			html += tableSpacer (4);
			html += tableRowStart (3);
			html += tableCell (ftr.name, 'smalltext');
			if (ftr.price > 0)
				html += tableCell (getPrice (ftr.price), 'smalltext', 'right');
			html += tableRowEnd (8);
			html += tableSpacer (5);
			html += lightGrayLine ();
			
			total += Number (ftr.price);
		}
	}

	html += tableSpacer (25)
	
	if (total > 0)
	{
		// Total cost
		html += lightGrayLine ();
		html += tableSpacer (4);
		html += tableRowStart (3);
		html += tableCell (summarytotal, 'smallheader');
		html += tableCell (getPrice (total), 'smallheader', 'right');
//		html += tableCellNoWrap (getPrice (total), 'smallheader', 'right');
		html += tableRowEnd (8);
		html += tableSpacer (5);
		html += lightGrayLine ();
		html += tableSpacer (25)
	}
	html += tableRowStart (3);
	html += tableCell ('', 'smallheader', '', 250);
	html += tableCell ('', 'smallheader', 'right', 89);
	html += tableRowEnd (8);
	html += '</TABLE>';
	
	
	reWriteLayer ("summarylayer", html);
}



function getPrice (msrp)
{
	var s = "";
	
	if (Number (msrp) > 0)
	{
		s = localizePrice (msrp, thousanddelimiter, decimaldelimiter, true);
	}
	
	return s;
}



function localizePrice (val, sepThousands, sepDecimal, intOnly)
{
	var s = String (Number (val));
	var i = s.indexOf (".");
	var s1 = "0";
	var s2 = "00";
	var ret = "";

	if (val == ""  ||  isNaN (val)  ||  val == "0")
	{
		ret = s1 + (intOnly ? "" : sepDecimal + s2);
	}
	else
	{
		if (i > 0)
		{
			s1 = s.substring (0, i);
			s2 = s.substring (i + 1, s.length);
		}
		else
		{
			s1 = s;
		}


		if (sepThousands == "")
		{
			ret = s1;
		}
		else
		{
			while (s1.length > 3)
			{
				ret = s1.substring (s1.length - 3, s1.length)
					+ (ret.length > 0 ? sepThousands : "")
					+ ret;
				s1 = s1.substring (0, s1.length - 3);
			}
			ret = (ret.length > 0) ? s1 + sepThousands + ret : s1;
		}
		
		ret += (intOnly ? "" : sepDecimal + s2);

		if (currencyprefix == "&#128;")
		{
			currencyprefix = 'euro';
		}
		if (currencyprefix != "")
		{
			ret = currencyprefix + " " + ret;
		}
		if (currencysuffix != "")
		{
			ret += " " + currencysuffix;
		}
	}
	
	return ret;
}



function delocalizePrice (val, sepThousands, sepDecimal)
{
	var ret = String (val);
	var regexp;
	
	if (sepThousands == ".")
		sepThousands = "\\" + sepThousands;

	regexp = new RegExp (sepThousands, "gi");
	ret = ret.replace (regexp, "");
	
	if (sepDecimal != ".")
	{
		regexp = new RegExp (sepDecimal, "gi");
		ret = ret.replace (regexp, ".");
	}

	if (currencyprefix != ""  &&  ret != "")
	{
		var prefix = currencyprefix;
		regexp = new RegExp ("\\$", "gi");
		prefix = prefix.replace (regexp, "\\$");
		
		regexp = new RegExp (prefix, "gi");
		ret = ret.replace (regexp, "");
	}
	if (currencysuffix != ""  &&  ret != "")
	{
		var suffix = currencysuffix;
		regexp = new RegExp ("\\$", "gi");
		suffix = suffix.replace (regexp, "\\$");
		
		regexp = new RegExp (suffix, "gi");
		ret = ret.replace (regexp, "");
	}
	
	return ret;
}



function tableHeader (s)
{
	return ''
		+ '<TR>'
		+ '<TD colspan="4">'
		+ '<B><SPAN class="featuretext">' + s + '</SPAN><B><BR/>'
		+ '<IMG src="images/spacer.gif" width="1" height="2"/>'
		+ '</TD>'
		+ '</TR>';
}




function lightGrayLine ()
{
	return ''
		+ '<TR>'
		+ '<TD colspan="4" height="1" bgcolor="#CCCCCC"><IMG src="images/spacer.gif" width="1" height="1"/></TD>'
		+ '</TR>'
		+ '<TR>';
}



function tableSpacer (height)
{
	return ''
		+ '<TR>'
		+ '<TD colspan="4" height="' + height + '"><IMG src="images/spacer.gif" width="1" height="' + height + '"/></TD>'
		+ '</TR>';
}



function tableRowStart (width)
{
	return ''
		+ '<TR>'
		+ '<TD width="' + width + '"><IMG src="images/spacer.gif" width="' + width + '" height="1"/></TD>';
}



function tableRowEnd (width)
{
	return ''
		+ '<TD width="' + width + '"><IMG src="images/spacer.gif" width="' + width + '" height="1"/></TD>'
		+ '</TR>';
}



function tableCell (s, style, align, cellWidth)
{
	var alignment = align ? align : "left";

	return ''
		+ '<TD class="' + style + '" align="' + alignment + '"'
		+ ((cellWidth  &&  cellWidth != ''  &&  cellWidth != 0) ? ' width="' + cellWidth + '"' : '')
		+ '>'
		+ s
		+ ((cellWidth  &&  cellWidth != ''  &&  cellWidth != 0) ? '<img src="images/spacer.gif" width="' + cellWidth + '" height="1" border="0"/>' : '')
		+ '</TD>';
}



function tableCellNoWrap (s, style, align)
{
	var alignment = align ? align : "left";

	return ''
		+ '<TD class="' + style + '" align="' + alignment + '" nowrap="">'
		+ s
		+ '</TD>';
}



function inPackage (ftr)
{
	var partOfPkg = false;
	for (var i = 0; i < _all_ftrs.length; ++i)
	{
		// if this is a selected package, check it
		if (_all_ftrs[i].isSelected  &&  _all_ftrs[i].featurecode == 'PACKAGE'  &&  _all_ftrs[i].incl)
		{
			for (var j = 0; j < _all_ftrs[i].incl.length; ++j)
			{
				if (ftr.id == _all_ftrs[i].incl[j])
				{
					partOfPkg = true;
					debug ('Not counting ftr ' + ftr.id + ', included in pkg ' + _all_ftrs[i].id, 'DEBUG');
					break;
				}
			}
		}
	}
	
	return partOfPkg;
}
