var active_items = new Object();
var oZoom0 = null;
var map = null;

function ShowItemOptions(tagid)
{
	for (var i in active_items)
	{
		if (active_items[i].tagid!=tagid)
			active_items[i].hide();
	}

	if (!active_items[tagid])
	{
		active_items[tagid]=new itemoptions(tagid);
	}
	
	var ok=false;
	if (active_items[tagid].featuresloaded)
	{
		ok = true;
		for (var i in active_items[tagid].features)
		{
			if(!active_items[tagid].features[i].optionsloaded)
			{
				ok = false;
				break;
			}
		}
	}
	
	if (ok)
	{
		active_items[tagid].display();
	}
	else
	{
		active_items[tagid]=new itemoptions(tagid);
		ServerContent(active_items[tagid].tagid,'/JS_ACTIONS/features.asp',function(key,content){active_items[tagid].setFeatures(content);},false);
	}
	return false;
}

function HideItemOptions(tagid)
{
	active_items[tagid].hide();
}

function SelectOption(tagid,featuregroup,optcode)
{
	active_items[tagid].selectoption(featuregroup,optcode);
}

function itemoptions(tagid)
{
	this.tagid=tagid;
	this.item=tagid.split('_')[0];
	this.formname=tagid.split('_')[1];;
	this.feat_string='';
	this.features=new Object();
	this.featuresloaded=false;
	this.visible=false;
	this.html='';
	this.map = null;
	
	var strHtml='';
	strHtml+='<table id="'+tagid+'_table" class=itemoptions>';
	strHtml+='<tr class=mtableheaderrow>';
	strHtml+='<td class=mtableheader>';
	strHtml+='<b class=colorred>Item #</b> <span class="conf" id="'+tagid+'_configitem">'+this.item+'</span><br><b class=colorred>Price as Configured:</b> $<span class="conf" id="'+tagid+'_configprice">0</span> <input class=mhidetext type=submit onclick="HideItemOptions(\''+this.tagid+'\'); return false;" value="Done Selecting Options">';
	strHtml+='</td>';
	strHtml+='</tr>';
	strHtml+='<tr class=mitemrow>';
	strHtml+='<td id="'+this.tagid+'_top"></td>';
	strHtml+='</tr>';
	strHtml+='<tr class=mtablebody>';
	strHtml+='<td class=mtablebody align=center></td>';
	strHtml+='</tr>';
	strHtml+='</table>'
	
	UpdateHTMLByID(this.tagid,strHtml);

	this.display();
}

itemoptions.prototype.setqty = function(qty)
{
	document[this.formname][this.item+'_QTY'].value=qty;
}

itemoptions.prototype.setFeatures = function(data)
{
	var arr_features = data.split('::');
	this.features=new Object();
	for (var i=0;i<arr_features.length;i++)
	{
		var arr_feature = arr_features[i].split('|');
		this.features[arr_feature[0]] = new feature(arr_feature[0],arr_feature[1],arr_feature[2],arr_feature[3]);
	}
	
	var strHtml='';
	for (var i in this.features)
	{
		strHtml+='<div id="'+this.tagid+'_'+this.features[i].group+'_feature" class="itemfeature" style="display:none;">';
		strHtml+='<b class=moptioncategory>'+this.features[i].description+'</b>';
		strHtml+='<div class=featureoptions id='+this.tagid+'_'+this.features[i].group+'_options></div>';
		strHtml+='</div>';
	}
	
	if (document.getElementById(this.tagid+'_top'))
	{
		UpdateHTMLByID(this.tagid+'_top',strHtml);
		this.featuresloaded = true;
	}

	for (var i in this.features)
	{
		var key=this.tagid+'|'+this.feat_string+'|'+this.features[i].group+'|'+this.features[i].item;
		var me = this;
		ServerContent(key,'/JS_ACTIONS/options.asp',function(key,content){me.setoptions(me.features[i].group,content);},true);
	}
}

itemoptions.prototype.setoptions = function(group,data)
{
	var strHtml='';
	var arr_options = data.split('::');
	var feature = this.features[group];
	feature.options = new Array();
	var ok=false;
	if (arr_options.length>1)
	{
		for (var i=0;i<arr_options.length;i++)
		{
			var arr_option = arr_options[i].split('|');
			feature.options[i] = new option(arr_option[0],arr_option[1],arr_option[2],arr_option[3]);
			if (feature.optcode==feature.options[i].code)
				ok=true;
		}

		if (!ok)
		{
			this.selectoption(feature.group,feature.options[0].code);
			return;
		}
		strHtml='<table class=moptionsrow><tr>';
		for (var i=0;i<feature.options.length;i++)
		{
			if (i%5==0)
				strHtml+='</tr><tr>';

			strHtml+='<td'+((feature.optcode==feature.options[i].code && feature.optcode.indexOf('.')<0)?' class=selected':'')+'>';
			if (feature.options[i].code.indexOf('.')<0)
			{
				strHtml+='<a class=itemoptions href=\"javascript:SelectOption(\''+this.tagid+'\',\''+feature.group+'\',\''+feature.options[i].code+'\');\">';
				if(feature.group!='MAP')
					strHtml+='<img class="itemoptions" src="/img/options/'+feature.group+'/'+feature.options[i].code.replace(/^\s+|\s+$/g,"")+'.jpg" border=0>';
				strHtml+='</a>';
			}
			strHtml+='<p style="padding-left:15px; text-indent:-15px;">';
			strHtml+='<input type=radio onclick="SelectOption(\''+this.tagid+'\',\''+feature.group+'\',\''+feature.options[i].code+'\');" name="'+this.tagid+'_'+feature.group+'_radiobtn"'+((feature.optcode==feature.options[i].code)?' checked':'')+'>';
			strHtml+=feature.options[i].description+'<br>';
			if (feature.group=='BRDOPT')
				strHtml+='$'+feature.options[i].price + ' (total)';
			else if(feature.group.substring(0,3)=='HRZ')
				;//do nothing
			else
				strHtml+=(feature.options[i].price>0)?'(add $'+feature.options[i].price+')':'';
			strHtml+='</p>';
			strHtml+='</td>';
		}
		strHtml+='</tr></table>';
	}
	else
	{
		if (arr_options[0])
		{
			var arr_option = arr_options[0].split('|');
			feature.options[0] = new option(arr_option[0],arr_option[1],arr_option[2]);
			if (feature.optcode != feature.options[0].code)
				this.selectoption(feature.group,feature.options[0].code);
		}
	}

	if (document.getElementById(this.tagid+'_'+feature.group+'_options'))
	{
		UpdateHTMLByID(this.tagid+'_'+feature.group+'_options',strHtml);
		SetStyleVis(this.tagid+'_'+feature.group+'_feature',(strHtml && (feature.group!='MAP' || map==null))?1:0);
		feature.optionsloaded = true;
	}
}

itemoptions.prototype.selectoption = function(group,code)
{
	var key=this.tagid+'|'+this.feat_string+'|';
	var x = 0;
	for (var i in this.features)
	{
		if (x>0)
		{
			key+='/';
		}
		if (this.features[i].group==group)
		{
			this.features[i].optcode=code;
			//google.maps.Log.write(map.frameCode);
			if(map && group=='FRAME')
			{
				map.setFrameCode(code.replace('ZV',''));
			}
		}
		key+=this.features[i].group+':'+this.features[i].optcode
		x++;
	}
	
	var me = this;
	ServerContent(key,'/JS_ACTIONS/configstring.asp',function(key,content){me.setconfigstring(content);},true);
}


itemoptions.prototype.setconfigstring = function(data)
{
	var i=0;
	var arr_temp = data.split('|');
	var key='';

	this.feat_string=arr_temp[0];
	
	if (arr_temp.length==1)
	{
		document[this.formname][this.item+'_FEATURESTRING'].value = this.feat_string;
		if(oZoom0!=null)
		{
			var tempConfig = this.hyphenatedConifgString();
			tempConfig = tempConfig.replace('-ZV-ZV','-FRM1-');
			tempConfig = tempConfig.replace(/-CC|-PGL|-FRM5/,'-FRM1');
			tempConfig = tempConfig.replace(/-TYA(K)?|-AK|-LHB|-ESL/g,'');
			oZoom0.config=tempConfig;
			oZoom0.setImage();
		}
		var key = this.tagid+'|'+this.feat_string;
		var me = this;
		ServerContent(key,'/JS_ACTIONS/prices.asp',function(key,content){me.setprices(content);});
	}
	
	for (i=1;i<arr_temp.length;i++)
	{
		for (var j in this.features)
		{
			if (arr_temp[i]==this.features[j].group)
				this.features[j].optcode='';
		}
	}
	
	for (var i in this.features)
	{
		this.features[i].optionsloaded=false;
		key = this.tagid+'|'+this.feat_string+'|'+this.features[i].group+'|'+this.features[i].item;
		var me = this;
		ServerContent(key,'/JS_ACTIONS/options.asp',function(key,content){me.setoptions(me.features[i].group,content);},true);
		//setTimeout('ServerContent(\''+key+'\',\'/JS_ACTIONS/options.asp\',function(key,content){active_items[\''+this.tagid+'\'].setoptions(\''+this.features[i].group+'\',content);});',i*200);
	}
}

itemoptions.prototype.hyphenatedConifgString = function()
{
	var config = '';
	for (var i in this.features)
	{
		var temp = this.features[i].optcode.replace(/\s|\./g,"");
		if(temp!='')
		{
			config+='-'+temp;
		}
	}
	return config;
}

itemoptions.prototype.setprices = function(data)
{
	var arr_qty = data.split('|')[0].split(',');
	var arr_price = data.split('|')[1].split(',');
	if(arr_price.length)
	{
		UpdateHTMLByID(this.tagid+'_configitem',this.item+this.hyphenatedConifgString());
		UpdateHTMLByID(this.tagid+'_configprice',arr_price[0]);
	}
	
	for (var i=0;i<arr_qty.length && arr_qty[i];i++)
	{
		if (document.getElementById(this.tagid+'_PRICE'+i))
		{
			if (GetHTMLByID(this.tagid+'_PRICE'+i).indexOf('$')>=0)
				UpdateHTMLByID(this.tagid+'_PRICE'+i,'$'+arr_price[i]);
			else
				UpdateHTMLByID(this.tagid+'_PRICE'+i,arr_price[i]);
		}
		else
			break;
	}
}


itemoptions.prototype.display = function()
{
	if (this.html && !this.visible)
	{
		UpdateHTMLByID(this.tagid,this.html);
	}
	//var key = this.tagid+'|';
	//var me = this;
	//ServerContent(key,'/JS_ACTIONS/prices.asp',function(key,content){me.setprices(content);});
	this.visible = true;
}

itemoptions.prototype.hide = function()
{
	if (this.visible)
	{
		this.html = GetHTMLByID(this.tagid);
		var strHtml = this.featurehtml();
		if (strHtml=='')
		{
			this.feat_string = '';
		}
		document[this.formname][this.item+'_FEATURESTRING'].value = this.feat_string;
		var key = this.tagid+'|'+this.feat_string;
		var me = this;
		ServerContent(key,'/JS_ACTIONS/prices.asp',function(key,content){me.setprices(content);});

		UpdateHTMLByID(this.tagid,strHtml);
	}
	this.visible = false;
}

itemoptions.prototype.featurehtml = function()
{
	var i=0;
	var j=0;
	var strHtml='';
	if (this.featuresloaded)
	{
		strHtml+='<ul>';
		for (var i in this.features)
		{
			if (this.features[i].options.length>1 && this.features[i].optionsloaded)
			{
				strHtml+='<li>';
				strHtml+=this.features[i].description+': ';
				for (j=0;j<this.features[i].options.length;j++)
				{
					if (this.features[i].options[j].code==this.features[i].optcode)
					{
						strHtml+=this.features[i].options[j].description;
						break;
					}
				}
				strHtml+='</li>';
			}
			else if (!this.features[i].optionsloaded)
			{
				strHtml = '';
				return strHtml;
			}
		}
		strHtml+='</ul>';
	}

	return strHtml;
}

function feature(group,item,description,required)
{
	this.group=group;
	this.item=item;
	this.description=description;
	this.required=required;
	this.optcode='';
	this.options=new Array();
	this.optionsloaded=false;
}

function option(item,code,description,price)
{
	this.item=item;
	this.code=code;
	this.description=description;
	this.price=price;
}

/*** hack for FFB ***/

function featureStringSelect(form,chartitem,offset,length,code)
{
	var featstr = form[chartitem+'_FEATURESTRING'].value;
	featstr = featstr.substring(0,offset-1) + code+Array(length+1-code.length).join(' ') + featstr.substring(offset+length-1,featstr.length);
	form[chartitem+'_FEATURESTRING'].value = featstr;
}