// Copyright 2005-2011 ICWUSA.COM, Inc.
// Written by David Finch

//todo replace this
var modgroups={ //insertion of spaces affects sorting, but not display
	"EL":   "   Series",
	"UL":   "   Series",
	"T2":   "   Series",
	"TE":   "   Series",
	"TI":   "   Series",
	"TR":   "   Series",
	"RX":   "   Series",
	"EV":   "   Series",
	"GX":   "   Series",
	"RN":   "   Series",
	"VM":   "   Series",
	"PL":   "   Series",
	"PS":   "   Series",
	"EV":   "   Series",
	"MDR18":"   Series",
	"MDX18":"   Series",
	"MDP18":"   Series",
	"MD18": "   Series",

	"A":    "Arms",
	
	"C":    " Ceiling Mounts",
	"X":    " Ceiling Mounts",

	"D":    " Desk Mounts",
	"DT":   " Mounts",
	"FS":   " Desk Mounts",
	"CC":   " Desk Mounts",
	"GM":   " Desk Mounts",

	"P1":   " Pole Mounts",
	"P2":   " Pole Mounts",
	"PT":   " Mounts",
	"PH":   " Pole Mounts",
	"PM":   " Pole Mounts",
	"R":    " Pole Mounts",
	
	"TS28": " Cabinet Mounts",
	
	"F8":   " Mounts",
	
	"CPU":  " Wall Mounts",
	"FM":   " Wall Mounts",
	"T19":  " Wall Mounts",
	"T36":  " Wall Mounts",
	"TM19": " Wall Mounts",
	"TM36": " Wall Mounts",
	"W":    " Wall Mounts",
	"WT":   " Mounts",
	"T24WM":" Wall Mounts",

	"J":    "  Tray Supports",
	"K":    "  Trays",
	"LT":   "  Trays",

	"KH":   "Accessories",
	"HT":   "Accessories",

	"TB":   "Colors",
	"QG":   "Colors",
	"MW":   "Colors",
	"T":    "Colors",
	"P":    "Colors",
	"G":    "Colors",
	"B":    "Colors",
	"S":    "Colors"
};
/*
var subgroups={
	" Mount":{
		"D":"Desk Mount",
		"P":"Pole Mount",
		"R":"Radius Mount",
		"TS28":"Under-cabinet track",
		"W":"Wall Mount",
		"T19":"Wall Mount",
		"T36":"Wall Mount",
		"C":"Ceiling Mount"
	}
};*/


function getModGroup(m) {
	while(m.length>0) {
		var g=modgroups[m];
		if(g) return g;
		m=m.slice(0,-1);
	}
}

var products=[];
var mproducts={};

var modules={
	"A1":"11\" arm",
	"A2":"20\" double-arm",
	"A3":"11\" arm and a 20\" double-arm",
	"A4":"Two 20\" double-arms",
	"A5":"11\" arm",
	"AT1":"T2 arm",
	"AT12":"12 Short single arm",
	"AT1212":"1212 Short double arm",
	"AT1220":"1220 Medium double arm",
	"AT20":"20 Long single arm",
	"AT2020":"2020 Long double arm",
	"B":"Black",
	"C8X":"Ceiling mount, 8x8 plate",
	"C8X12":"8x8 ceiling plate with 12\" ext",
	"C8X24":"8x8 ceiling plate with 24\" ext",
	"C8X36":"8x8 ceiling plate with 36\" ext",
	"C8X5":"8x8 ceiling plate with  5\" ext",
	"C8X5":"8x8 ceiling plate",
	"CC":"Desk edge clamp",
	"CE":"Ceiling mount, stud",
	"CE12":"Stud/rafter ceiling plate with 12\" ext",
	"CE24":"Stud/rafter ceiling plate with 24\" ext",
	"CE36":"Stud/rafter ceiling plate with 36\" ext",
	"CE5":"Stud/rafter ceiling plate with  5\" ext",
	"CPUMINI":"Frame style, fitting 1.86\"-3.2\" CPU's",
	"CPU2.8":"Frame style, fitting 2.8\"-5\" CPU's",
	"CPU5":"Frame style, fitting 5\"-7\" CPU's",
	"CPU7":"Frame style, fitting 7\"-11\" CPU's",
	"CPUFF":"Clamp style, fitting 4.6\"-8\" CPU's",
	"CPUSF":"Clamp style, fitting 2\"-4.7\" CPU's",
	"CU":"Adjustable ceiling mount",
	"CU2":"Ceiling mount",
	"D":"Desk mount",
	"DC":"Desk edge clamp",
	"DM":"Desk mount",
	"DMX":"Desk mount with 18\" extension",
	"DT":"Desk mount",
	"DT2":"Desk mount",
	"DU1":"Desk riser, 1\"",
	"DU3":"Desk riser, 3\"",
	"DU5":"Desk riser, 5\"",
	"DU7":"Desk riser, 7.5\"",
	"D1":"Desk riser, 1\"",
	"D3":"Desk riser, 3\"",
	"D5":"Desk riser, 5\"",
	"D7":"Desk riser, 7.5\"",
	"DV1":"Desk riser, 1\"",
	"DV3":"Desk riser, 3\"",
	"DV5":"Desk riser, 5\"",
	"DV75":"Desk riser, 7.5\"",
	"EL5120":"Elite 5120 single arm laptop mount",
	"EL5120LT":"Elite 5120 single arm laptop mount",
	"EL5220":"Elite 5220 double arm laptop mount",
	"EL5220LT":"Elite 5220 double arm laptop mount",
	"ELR5120":"Elite 5120 T-Rex single arm LCD mount",
	"ELR5220":"Elite 5220 T-Rex double arm LCD mount",
	"ELX5120":"Elite 5120 Rex single arm LCD mount",
	"ELX5220":"Elite 5220 Rex double arm LCD mount",
	"EV610":"ErgoVision 610 LCD mount",
	"EV65":"ErgoVision 65 LCD mount",
	"EV65X2":"EV65 dual monitor LCD mount",
	"EV65X2H":"EV65 dual monitor horizontal LCD mount",
	"EV65X2HT24WM":"EV65 dual monitor horizontal LCD on a 24\" track",
	"F8X36":"Floor pedestal, 36\"",
	"F8X48":"Floor pedestal, 48\"",
	"FM25":"2\" fixed LCD wall mount, VESA75",
	"FM26":"1\" fixed LCD wall mount, VESA75",
	"FM30":"2\" fixed LCD wall mount, VESA100",
	"FM31":"1\" fixed LCD wall mount, VESA100",
	"FS":"Free standing desk mount",
	"G":"Gray",
	"GM":"Desk grommet mount",
	"GX150":"Galaxy compact wall mount<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<b>Limited quantities</b>",
	"GX250":"Galaxy pivoting compact wall mount arm<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<b>Limited quantities</b>",
	"GX170":"<img src='../images/n.gif'> Galaxy compact wall mount",
	"GX270":"<img src='../images/n.gif'> Galaxy pivoting compact wall mount arm",
	"HTR":"T-Rex handle",
	"K12":"Mini keyboard tray, 12\"",
	"K12F":"Mini flat keyboard tray, 12\"",
	"KB":"Bent keyboard tray",
	"KC":"Composite keyboard tray",
	"KF":"Flat keyboard tray",
	"KHANDLE":"Keyboard handle",
	"KRX12":"Mini keyboard tray, 12\"",
	"KRX12F":"Mini flat keyboard tray, 12\"",
	"KRXB":"Bent keyboard tray",
	"KRXC":"Composite keyboard tray",
	"KRXF":"Flat keyboard tray",
	"K12PL":"Paralink mini keyboard tray, 12\"",
	"K12FPL":"Paralink mini flat keyboard tray, 12\"",
	"KP12":"Paralink mini keyboard tray, 12\"",
	"KP12F":"Paralink mini flat keyboard tray, 12\"",
	"KQ12":"Quicklink mini keyboard tray, 12\"",
	"KQ12F":"Quicklink mini flat keyboard tray, 12\"",
	"KU12":"Ultra mini keyboard tray, 12\"",
	"KU12EL":"Ultra mini keyboard tray, 12\"",
	"KU12F":"Ultra mini flat keyboard tray, 12\"",
	"KU12FEL":"Ultra mini flat keyboard tray, 12\"",
	"KU12FMD":"Ultra mini flat keyboard tray, 12\"",
	"KU12FT2":"Ultra mini flat keyboard tray, 12\"",
	"KU12MD":"Ultra mini keyboard tray, 12\"",
	"KU12T2":"Ultra mini keyboard tray, 12\"",
	"KUB":"Ultra bent keyboard tray",
	"KUP":"Ultra HDPE keyboard tray",
	"KUBEL":"Ultra bent keyboard tray",
	"KUBMD":"Ultra bent keyboard tray",
	"KUBT2":"Ultra bent keyboard tray",
	"KUC":"Ultra composite keyboard tray",
	"KUCEL":"Ultra composite keyboard tray",
	"KUCMD":"Ultra composite keyboard tray",
	"KUCT2":"Ultra composite keyboard tray",
	"KUF":"Ultra flat keyboard tray",
	"KUFEL":"Ultra flat keyboard tray",
	"KUFMD":"Ultra flat keyboard tray",
	"KUFT2":"Ultra flat keyboard tray",
	"LTRX":"Rex laptop tray",
	"LTX":"Laptop tray",
	"MD18":"MD Arm with laptop tray",
	"MDR18":"MD Arm with T-Rex",
	"MDX18":"MD Arm with Rex",
	"MW":"<img  src='../images/s/white.png'> Medical White",
	"P":"<img src='../images/s/putty.png'> Putty (+$15, +2 week lead) Matches Pelton &amp; Crane, Custom: Not Returnable",
	"P1":"Pole mount, 1\"",
	"P15":"Pole mount, 1.5\"",
	"P17":"Pole mount, 1.75\"",
	"P2":"Pole mount, 2\"",
	"PM2":"Pole mount, 2\"",
	"PH2":"Horizontal pole mount, 2\"",
	"PHV2":"Horizontal pole mount, 2\"",
	"PT2":"Pole mount, 2\"",
	"PT215":"Pole mount, 1.5\"",
	"QG":"<img src='../images/s/gray.png'> Gray",
	"R2":"Radius mount, 2\"",
	"RNB1":"RNB1 double arm",
	"RNB2":"RNB2 long arm",
	"RNB3":"RNB3 short arm",
	"RNB4":"RNB4 dual monitor arms",
	"RNB5":"RNB5 LCD stand",
	"RT2":"Radius mount, 2\"",
	"RX":"Rex",
	"S":"Gray",	
	"T":"<img src='../images/s/tan.png'> Tan (+$15, +2 week lead) Matches A-dec, Custom: Not Returnable",
	"T19":"Wall track slider, 19\"",
	"T19D":"Wall track slider, 19\", separate arms",
	"T2":"T2 Laptop mount",
	"T21212":"Short T2 Laptop mount",
	"T21220":"Medium T2 Laptop mount",
	"T22020":"Long T2 Laptop mount",
	"T2E":"T2 Elite LCD mount",
	"T2ER":"T2 Elite T-Rex LCD mount",
	"T2EX":"T2 Elite Rex LCD mount",
	"T2K":"T2 keyboard mount",
	"T2K12":"Short single arm T2 keyboard mount",
	"T2K1212":"Short T2 keyboard mount",
	"T2K1220":"Medium T2 keyboard mount",
	"T2K20":"Long single arm T2 keyboard mount",
	"T2K2020":"Long T2 keyboard mount",
	"T2R":"T2 T-Rex LCD mount",
	"T2R12":"Short single arm T2 T-Rex LCD mount",
	"T2R1212":"Short T2 T-Rex LCD mount",
	"T2R1220":"Medium T2 T-Rex LCD mount",
	"T2R20":"Long  single arm T2 T-Rex LCD mount",
	"T2R2020":"Long T2 T-Rex LCD mount",
	"T2X":"T2 Rex LCD mount",
	"T2X12":"Short single arm T2 Rex LCD mount",
	"T2X1212":"Short T2 Rex LCD mount",
	"T2X1220":"Medium T2 Rex LCD mount",
	"T2X20":"Long single arm T2 Rex LCD mount",
	"T2X2020":"Long T2 Rex LCD mount",
	"T36":"Wall track slider, 36\"",
	"T36D":"Wall track slider, 36\", separate arms",
	"TS28":"Under-cabinet track slider",
	"TS28D1":"Under-cabinet track slider with a 1\" riser",
	"TS28D3":"Under-cabinet track slider with a 3\" riser",
	"TB":"<img src='../images/s/black.png'> Black Texture",
	"TM19":"Track mount w/ 19\" track",
	"TM36":"Track mount w/ 36\" track",
	"T24WM":"24\" track on a wall plate",
	"TR2M":"Double T-Rex",
	"TRM":"T-Rex",
	"TRX":"T-Rex",
	"UL100":"Ultra 100 Laptop/CRT mount",
	"UL110":"Ultra 110 LCD mount",
	"UL200":"Ultra 200 Laptop/CRT mount",
	"UL205":"Ultra 205 LCD mount",
	"UL210":"Ultra 210 LCD mount",
	"UL510":"Ultra 510 LCD mount",
	"UL510I":"Inverted Ultra 510 LCD mount",
	"UL550":"Ultra 550 Paralink LCD mount",
	"UL550IS":"Inverted Ultra 550 Swivel LCD mount",
	"UL550IRA":"Right-Angled 550 Paralink LCD mount",
	"UL550RA":"Right-Angled 550 Paralink LCD mount",
	"UL560I":"Inverted Ultra 560 LCD mount",	
	"UL500":"Ultra 500 LCD mount",
	"UL500I":"Inverted Ultra 500 LCD mount",
	"ULBV":"Ultra ball pivot LCD",
	"UL280":"Ultra 280 flipped LCD mount",
	"UL300":"Ultra 300 CRT mount",
	"UL390":"Ultra 390 LCD mount",
	"UL90":"Ultra 90 LCD mount",
	"UL95":"Ultra 95 LCD mount",
	"ULKB":"Keyboard-only mount",
	"VM300":"Viewmaster 300 LCD arm",
	"VM5518":"Viewmaster 5518 LCD arm",
	"VM5518LT":"Viewmaster 5518 laptop arm",
	"VMD24":"Viewmaster 24 LCD arm",
	"VMD30":"Viewmaster 30 LCD arm",
	"VMLT30":"Viewmaster 30 laptop arm",
	"VMPLA":"Viewmaster plasma mount",
	"VMT5518":"Viewmaster 5518 laptop arm",
	"VMT6512":"Viewmaster 6512 laptop arm",
	"W2":"Wall mount",
	"W3":"Wall mount",
	"WM":"Wall mount",
	"WMD":"wall mount",
	"WT":"Wall mount",
	"WT2":"Wall mount",
	"WT2SA":"Wall mount with rotation lock",
	"X12":"Ceiling ext, 12\"",
	"X24":"Ceiling ext, 24\"",
	"X36":"Ceiling ext, 36\"",
	"X5":"Ceiling ext,  5\"",
	"XE12":"Ceiling ext, 12\"",
	"XE24":"Ceiling ext, 24\"",
	"XE36":"Ceiling ext, 36\"",
	"XE5":"Ceiling ext,  5\"",

	"CUX12":"Adjustable with 12\" ext.",
	"CUX24":"Adjustable with 24\" ext.",
	
	
"ELP5120":"Elite 5120 Paralink single arm LCD mount",
"ELQ5120":"Elite 5120 Quicklink single arm LCD mount",
"ELP5220":"Elite 5220 Paralink double arm LCD mount",
"ELS5120":"Elite 5120 Paralink Swivel single arm LCD mount",
"ELS5280":"Inverted Elite 5280 Paralink Swivel double arm LCD mount",
"ELQ5280":"Elite 5280 Quicklink double arm LCD mount",

"KP12C":"Paralink mini composite keyboard tray, 12\"",
"KPB":"Paralink bent keyboard tray",
"KPP":"Paralink HDPE keyboard tray",
"KPC":"Paralink composite keyboard tray",
"KPF":"Paralink flat keyboard tray",

"KQ12C":"Quicklink mini composite keyboard tray, 12\"",
"KQB":"Quicklink bent keyboard tray",
"KQP":"Quicklink HDPE keyboard tray",
"KQC":"Quicklink composite keyboard tray",
"KQF":"Quicklink flat keyboard tray",

//for substitution splitting adjuster from tray
"KX12":"Mini bent keyboard tray, 12\"",
"KX12F":"Mini flat keyboard tray, 12\"",
"KX12C":"Mini composite keyboard tray, 12\"",
"KX12P":"Mini HDPE keyboard tray, 12\"",
"KXB":"Bent keyboard tray",
"KXP":"HDPE keyboard tray",
"KXC":"Composite keyboard tray",
"KXF":"Flat keyboard tray",
"LTX":"Laptop Tray",

"JP":"Paralink adjuster",
"JQ":"Quicklink adjuster",
"JU":"Ultra adjuster",


"K12CPL":"Paralink mini composite keyboard tray, 12\"",
"KBPL":"Paralink bent keyboard tray",
"KCPL":"Paralink composite keyboard tray",
"KU12C":"Ultra mini composite keyboard tray, 12\"",

"MDP18":"MD LCD Arm with Paralink",

"LTPL":"Laptop Tray with a Paralink lift and stow system",
"LTP":"Laptop Tray with a Paralink lift and stow system",
"LTU":"Laptop Tray with an Ultra flip-up stowing system",
"LTQ":"Laptop Tray with a Quicklink lift and stow system",

"T2P":"T2 Paralink",
"T2S":"T2 Paralink Swivel with no arms",
"PL":"Paralink",
"PS":"Paralink Swivel",

"T2EP":"T2 Elite Paralink LCD mount",
"T2ES":"T2 Elite Paralink Swivel LCD mount",
"T2EQ":"T2 Elite Quicklink LCD mount",

"T2P12":"Short single arm T2 Paralink LCD mount",
"T2P1212":"Short T2 Paralink LCD mount",
"T2P1220":"Medium T2 Paralink LCD mount",
"T2P20":"Long  single arm T2 Paralink LCD mount",
"T2P2020":"Long T2 Paralink LCD mount",

"T2S12":"Short single arm T2 Paralink Swivel LCD mount",
"T2S1212":"Short T2 Paralink Swivel LCD mount",
"T2S1220":"Medium T2 Paralink Swivel LCD mount",
"T2S20":"Long  single arm T2 Paralink Swivel LCD mount",
"T2S2020":"Long T2 Paralink Swivel LCD mount",

"UL115":"Ultra 115 Paralink LCD mount",
"UL215":"Ultra 215 Paralink LCD mount",
"UL285":"Ultra 285 Paralink LCD mount",
"UL395":"Ultra 395 Paralink LCD mount"	
	
};


function expandsub(p) {
	//we want to treat the T2 families as two options.
	//so  T2P1220-WT2-KCPL-MW would become  T2P-1220-WT2-KCPL-MW
	if(p.slice(0,2)=="T2") {
		var a=p.split("-");
		var f=a[0];
		var c=f.charAt(2);
		var i;
		if("KRXPS".indexOf(c)>=0) i=3; else i=2;
		var a1=f.slice(0,i);
		var a2="AT"+f.slice(i);
		if(modules[a1]&&modules[a2]) {
			a[0]=a1+"-"+a2
			p=a.join("-");
		}
	}
	if(p.indexOf("-CU-X")>=0) {
		p=p.replace("-CU-X","-CUX");
	}
	
	if(p.indexOf("-CE")>=0 && p.indexOf("-CE-")<0) {
		p=p.replace("-CE","-CE-X");
	}
	if(p.indexOf("-C8X")>=0 && p.indexOf("-C8X-")<0) {
		p=p.replace("-C8X","-C8X-X");
	}
	if(p.slice(0,5)=="LTRX-") {
		p="RX-LTX-"+p.slice(5);
	}
	if(p.indexOf("X18-DM")>=0) {
		p=p.replace("X18-DM","DMX");
	}
	if(p.charAt(0)=="K") {
		p="ULKB-"+p;
	}
	
	if(p.indexOf("-KP")>=0) {
		p=p.replace("-KP","-JP-KX");
	}
	if(p.indexOf("-KQ")>=0) {
		p=p.replace("-KQ","-JQ-KX");
	}
	if(p.indexOf("-KU")>=0) {
		p=p.replace("-KU","-JU-KX");
	}
	if(p.indexOf("-LT")>=0) {
		p=p.replace("-LTP","-JP-LTX");
		p=p.replace("-LTU","-JU-LTX");
		p=p.replace("-LTQ","-JQ-LTX");
	}
	
	return p;
}

var moddesc={
	"A1":"with an 11\" arm",
	"A2":"with a 20\" double-arm",
	"A3":"with an 11\" arm and 20\" double-arm",
	"A4":"with two 20\" double-arms",
	"A5":"with an 11\" arm",
	"B":"Black",
	"C8X12":"ceiling mount with a 12\" ext",
	"C8X24":"ceiling mount with a 24\" ext",
	"C8X36":"ceiling mount with a 36\" ext",
	"C8X5":"ceiling mount with a 5\" ext",
	"C8X":"ceiling mount",
	"CC":"desk edge mount",
	"CE":"ceiling mount",
	"CE12":"stud ceiling mount with a 12\" ext",
	"CE24":"stud ceiling mount with a 24\" ext",
	"CE36":"stud ceiling mount with a 36\" ext",
	"CE5":"stud ceiling mount with a 5\" ext",
	"CPUMINI":"CPU wall mount, 1.86\"-3.2\"",
	"CPU2.8":"CPU wall mount, 2.8\"-5\"",
	"CPU5":"CPU wall mount, 5\"-7\"",
	"CPU7":"CPU wall mount, 7\"-11\"",
	"CPUFF":"Large EZMount CPU wall mount, 4\"-8.1\"",
	"CPUSF":"Small EZMount CPU wall mount, 2\"-4.6\"",
	"CU":"adjustable ceiling mount",
	"CUX12":"adjustable ceiling mount with a 12\" ext",
	"CUX24":"adjustable ceiling mount with a 24\" ext",
	
	"CU2":"ceiling mount",
	"D":"desk mount",	
	"DC":"desk edge clamp",
	"DM":"desk mount",
	"DT":"desk mount",
	"DT2":"desk mount",
	"DU1":"desk mount with a 1\" riser",
	"DU3":"desk mount with a 3\" riser",
	"DU5":"desk mount with a 5\" riser",
	"DU7":"desk mount with a 7.5\" riser",
	"D1":"desk mount with a 1\" riser",
	"D3":"desk mount with a 3\" riser",
	"D5":"desk mount with a 5\" riser",
	"D7":"desk mount with a 7.5\" riser",
	"DV1":"desk mount with a 1\" riser",
	"DV3":"desk mount with a 3\" riser",
	"DV5":"desk mount with a 5\" riser",
	"DV75":"desk mount with a 7.5\" riser",
	"EL5120":"Elite 5120 single arm laptop mount",
	"EL5120LT":"Elite 5120 single arm laptop mount",
	"EL5220":"Elite 5220 double arm laptop mount",
	"EL5220LT":"Elite 5220 double arm laptop mount",
	"ELR5120":"Elite 5120 T-Rex single arm LCD",
	"ELR5220":"Elite 5220 T-Rex double arm LCD",
	"ELX5120":"Elite 5120 Rex single arm LCD",
	"ELX5220":"Elite 5220 Rex double arm LCD",
	"EV610":"ErgoVision 610 LCD",
	"EV65":"ErgoVision 65 LCD",
	"EV65X2":"ErgoVision 65 dual monitor LCD",
	"EV65X2H":"ErgoVision 65 dual monitor horizontal LCD",
	"F8X36":"with a 36\" floor pedestal",
	"F8X48":"with a 48\" floor pedestal",
	"FM25":"2\" fixed VESA75 LCD wall mount",
	"FM26":"1\" fixed VESA75 LCD wall mount",
	"FM30":"2\" fixed VESA100 LCD wall mount",
	"FM31":"1\" fixed VESA100 LCD wall mount",
	"FS":"desk stand",
	"G":"Gray",
	"GM":"desk grommet mount",
	"GX150":"Galaxy compact",
	"GX250":"Galaxy pivoting compact arm",
	"GX170":"Galaxy compact",
	"GX270":"Galaxy pivoting compact arm",
	"HTR":"with a Rex handle",
	"K12":"with a 12\" keyboard tray",
	"K12F":"with a 12\" flat keyboard tray",
	"KB":"with a bent keyboard tray",
	"KC":"with a white composite keyboard tray",
	"KF":"with a flat keyboard tray",
	"KHANDLE":"with a keyboard handle",
	"KRX12":"with a 12\" keyboard tray",
	"KRX12F":"with a 12\" flat keyboard tray",
	"KRXB":"with a bent keyboard tray",
	"KRXC":"with a white composite keyboard tray",
	"KRXF":"with a flat keyboard tray",
	"KU12":"with a 12\" keyboard tray",
	"KU12EL":"with a 12\" Ultra keyboard tray",
	"KU12F":"with a 12\" flat keyboard tray",
	"KU12FEL":"with a 12\" Ultra flat keyboard tray",
	"KU12FMD":"with a 12\" Ultra flat keyboard tray",
	"KU12FT2":"with a 12\" Ultra flat keyboard tray",
	"KU12MD":"with a 12\" Ultra keyboard tray",
	"KU12T2":"with a 12\" Ultra keyboard tray",
	"KUB":"with a bent keyboard tray",
	"KUP":"with an HDPE keyboard tray",
	"KUBEL":"with a bent Ultra keyboard tray",
	"KUBMD":"with a bent Ultra keyboard tray",
	"KUBT2":"with a bent Ultra keyboard tray",
	"KUC":"with a white composite keyboard tray",
	"KUCEL":"with a white composite keyboard tray",
	"KUCMD":"with a white composite keyboard tray",
	"KUCT2":"with a white composite keyboard tray",
	"KUF":"with a flat keyboard tray",
	"KUFEL":"with a flat Ultra keyboard tray",
	"KUFMD":"with a flat Ultra keyboard tray",
	"KUFT2":"with a flat Ultra keyboard tray",
	"LTRX":"Rex laptop tray",
	"LTPL":"with a laptop tray on a Paralink",
	"LTP":"with a laptop tray on a Paralink",
	"LTUPL":"with a laptop tray on an Ultra flip-up",
	"LTU":"with a laptop tray on an Ultra flip-up",
	"LTX":"with a laptop tray",
	"MD18":"MD Arm",
	"MDR18":"MD Arm with T-Rex",
	"MDX18":"MD Arm with Rex",
	"MW":"Medical white",
	"NC":"Color%",
	"P":"Putty",
	"P1":"mount for a 1\" pole",
	"P15":"mount for a 1.5\" pole",
	"P17":"mount for a 1.75\" pole",
	"P2":"mount for a 2\" pole",
	"PH2":"mount for a 2\" horizontal pole",
	"PHV2":"mount for a 2\" horizontal pole",
	"PM2":"mount for a 2\" pole",
	"PT2":"mount for a 2\" pole",
	"PT215":"mount for a 2\" pole",
	"QG":"Gray",
	"R2":"mount for a 2\" radius",
	"RNB1":"B1 double arm",
	"RNB2":"B2 long arm",
	"RNB3":"B3 short arm",
	"RNB4":"B4 dual monitor arms",
	"RNB5":"B5 LCD stand",
	"RT2":"mount for a 2\" radius",
	"RX":"Rex",
	"S":"Silver",
	"T":"Tan",
	"T19":"mount on a 19\" wall track slider",
	"T36":"mount on a 36\" wall track slider",
	"T19D":"mount on a 19\" wall track with separate arms",
	"T36D":"mount on a 36\" wall track with separate arms",
	"T24WM":"mount on a 24\" track on a wall plate",
	"TS28":"mount on an under-cabinet track slider",
	"TS28D1":"mount on an under-cabinet track slider with a 1\" riser",
	"TS28D3":"mount on an under-cabinet track slider with a 3\" riser",
	"T21212":"Short T2 LCD mount",
	"T21220":"Medium T2 LCD mount",
	"T22020":"Long T2 LCD mount",
	"T2E":"T2 Elite LCD",
	"T2ER":"T2 Elite T-Rex LCD",
	"T2EX":"T2 Elite Rex LCD",
	"T2K12":"Short single arm T2 keyboard mount",
	"T2K1212":"Short T2 keyboard mount",
	"T2K1220":"Medium T2 keyboard mount",
	"T2K20":"Long single arm T2 keyboard mount",
	"T2K2020":"Long T2 keyboard mount",
	"T2R":"Long T2 T-Rex LCD arm",
	"T2R12":"Short single arm T2 T-Rex LCD mount",
	"T2R1212":"Short T2 T-Rex LCD arm",
	"T2R1220":"Medium T2 T-Rex LCD arm",
	"T2R20":"Long single arm T2 T-Rex LCD mount",
	"T2R2020":"Long T2 T-Rex LCD arm",
	"T2X12":"Short single arm T2 Rex LCD mount",
	"T2X1212":"Short T2 Rex LCD arm",
	"T2X1220":"Medium T2 Rex LCD arm",
	"T2X20":"Long single arm T2 Rex LCD mount",
	"T2X2020":"Long T2 Rex LCD arm",
	"TB":"Black",
	"TM19":"mount on a 19\" track slider",
	"TM36":"mount on a 36\" track slider",
	"TR2M":"double T-Rex",
	"TRM":"T-Rex",
	"TRX":"T-Rex",
	"UL100":"UL100 CRT",
	"UL110":"Ultra 110 LCD",
	"UL200":"Ultra 200 CRT",
	"LTU":"Ultra Laptop",
	"UL205":"Ultra 205 LCD",
	"UL210":"Ultra 210 LCD",
	"UL500":"Ultra 500 LCD",
	"UL510":"Ultra 510 LCD",
	"UL550":"Ultra 550 LCD",
	"ULBV":"Ultra ball pivot LCD",
	"UL500I":"Inverted Ultra 500 LCD",
	"UL510I":"Inverted Ultra 510 LCD",
	"UL550IS":"Inverted Ultra 550 LCD",
	"UL550IRA":"Right-Angled 550 Paralink LCD mount",
	"UL550RA":"Right-Angled 550 Paralink LCD mount",
	"UL560I":"Inverted Ultra 560 LCD",
	"UL280":"Ultra 280 flipped LCD",
	"UL300":"Ultra 300 CRT",
	"UL390":"Ultra 390 LCD",
	"UL90":"Ultra 90 LCD",
	"UL95":"Ultra 95 LCD",
	"ULKB":"Keyboard",
	"VM1X2":"Viewmaster dual monitor",
	"VM300":"Viewmaster 300 LCD arm",
	"VM5518":"Viewmaster LCD arm",
	"VMT5518":"Viewmaster tray arm",
	"VMT6512":"Viewmaster tray arm",
	"VM5518LT":"Viewmaster laptop arm",
	"VMD24":"Viewmaster 24 LCD arm",
	"VMD30":"Viewmaster LCD stand",
	"VMLT30":"Viewmaster laptop stand",
	"VMPLA":"Viewmaster plasma mount",
	"W2":"wall mount",
	"W3":"wall mount",
	"WM":"wall mount",
	"WMD":"wall mount",
	"WT":"wall mount",
	"WT2":"wall mount",
	"WT2SA":"wall mount with rotation lock",
	"X12":"with a 12\" ext",
	"X24":"with a 24\" ext",
	"X36":"with a 36\" ext",
	"X5":"with a 5\" ext",
	"XE12":"with a 12\" ext",
	"XE24":"with a 24\" ext",
	"XE36":"with a 36\" ext",
	"XE5":"with a 5\" ext",
	"X18":"with an 18\" ext",

"ELP5120":"Elite 5120 Paralink single arm LCD",
"ELQ5120":"Elite 5120 Quicklink single arm LCD",
"ELP5220":"Elite 5220 Paralink double arm LCD",
"ELS5120":"Elite 5120 Paralink Swivel single arm LCD",
"ELS5280":"Inverted Elite 5280 Paralink Swivel double arm LCD",
"ELQ5280":"Elite 5280 Quicklink double arm LCD",

"K12C":"with a 12\" white composite keyboard tray",
"KP12C":"with a 12\" white composite paralink keyboard tray",
"KP12":"with a 12\" bent paralink keyboard tray",
"KPB":"with a bent paralink keyboard tray",
"KPC":"with a white composite paralink keyboard tray",
"KPF":"with a flat paralink keyboard tray",
"KP12F":"with a 12\" flat paralink keyboard tray",
"K12CPL":"with a 12\" white composite paralink keyboard tray",
"KBPL":"with a bent paralink keyboard tray",
"KCPL":"with a white composite paralink keyboard tray",
"KU12C":"with a 12\" Ultra flip-up white composite keyboard tray",

"LTP":"with a laptop tray made adjustable by a Paralink lift and stow system",
"LTU":"with a laptop tray made adjustable by an Ultra flip-up stowing system",
"KPP":"with a white HDPE paralink keyboard tray",
"KQ12F":"with a 12\" flat quicklink keyboard tray",
"KQ12":"with a 12\" quicklink keyboard tray",
"KQB":"with a quicklink keyboard tray w/ wrist rest bend",
"KQF":"with a flat quicklink keyboard tray",
"KQP":"with a white HDPE quicklink keyboard tray",


//for substitution splitting adjuster from tray
"KX12":"holding a mini bent keyboard tray, 12\"",
"KX12F":"holding a mini flat keyboard tray, 12\"",
"KX12C":"holding a mini composite keyboard tray, 12\"",
"KX12P":"holding a mini HDPE keyboard tray, 12\"",
"KXB":"holding a bent keyboard tray",
"KXP":"holding an HDPE keyboard tray",
"KXC":"holding a composite keyboard tray",
"KXF":"holding a flat keyboard tray",
"LTX":"holding a laptop Tray",

"JP":"with a Paralink adjuster",
"JQ":"with a Quicklink adjuster",
"JU":"with an Ultra adjuster",



"MDP18":"MD Arm with Paralink",

"T2P":"Paralink",
"T2Q":"T2 Quicklink",
"T2S":"Paralink Swivel",
"PL":"Paralink",
"PS":"Paralink Swivel",

"T2EP":"T2 Elite Paralink LCD mount",
"T2EQ":"T2 Elite Quicklink LCD mount",
"T2ES":"T2 Elite Paralink Swivel LCD mount",

"T2P12":"Short single arm T2 Paralink LCD arm",
"T2P1212":"Short T2 Paralink LCD arm",
"T2P1220":"Medium T2 Paralink LCD arm",
"T2P20":"Long  single arm T2 Paralink LCD arm",
"T2P2020":"Long T2 Paralink LCD arm",

"AT12":"Short single arm T2 Paralink LCD arm",
"AT1212":"Short T2 Paralink LCD arm",
"AT1220":"Medium T2 Paralink LCD arm",
"AT20":"Long  single arm T2 Paralink LCD arm",
"AT2020":"Long T2 Paralink LCD arm",

"T2Q12":"Short single arm T2 Quicklink LCD arm",
"T2Q1212":"Short T2 Quicklink LCD arm",
"T2Q1220":"Medium T2 Quicklink LCD arm",
"T2Q20":"Long single arm T2 Quicklink LCD arm",
"T2Q2020":"Long T2 Quicklink LCD arm",

"T2S12":"Short single arm T2 Paralink Swivel LCD arm",
"T2S1212":"Short T2 Paralink Swivel LCD arm",
"T2S1220":"Medium T2 Paralink Swivel LCD arm",
"T2S20":"Long  single arm T2 Paralink Swivel LCD arm",
"T2S2020":"Long T2 Paralink Swivel LCD arm",

"UL115":"Ultra 115 Paralink LCD",
"UL215":"Ultra 215 Paralink LCD",
"UL285":"Ultra 285 Paralink LCD",
"UL395":"Ultra 395 Paralink LCD"

	
};

var comments={
	"CPUFF-QG":"New! Easy to install",
	"CPUSF-QG":"New! Easy to install",
	"CPUFF-TB":"New! Easy to install",
	"CPUSF-TB":"New! Easy to install",
	"T2EP-CE5-QG":"Ideal for dental offices",
	"ELP5220-WT-QG":"Ideal for dental offices",
	"ELP5220-WT-KUB-QG":"Very popular",
	"UL215-W3-QG":"Ideal for dental offices",
	"UL215-W3-KUB-QG":"Very popular",
	"UL95-CU-QG":"Popular",
	"T2P2020-WT2-QG":"Popular",
	"T2P1212-WT2-QG":"Great for small space requirements",
	"UL115-P2-A5-QG":"Popular",
	"UL215-P2-QG":"Popular",
	"UL215-W3-KUB-TB":"Popular",
	"UL90-W2-QG":"Popular",
	"UL395-T36-KB-A4-QG":"Ideal for hospitals",
	"UL395-T36-KB-QG":"Ideal for hospitals"
};


var subnotes={
	"U":"an Ultra monitor adjuster instead of a Paralink monitor adjuster",
	"P":"a T-Rex monitor adjuster instead of a Paralink monitor adjuster",
	"P2":"a Rex monitor adjuster instead of a Paralink monitor adjuster",
	"P3":"a T-Rex monitor adjuster instead of a Paralink Swivel monitor adjuster",
	"PLT":"a Rex tray adjuster instead of a Paralink tray adjuster",
	"ULT":"a Rex tray adjuster instead of an Ultra tray adjuster",
	"C":"a flat aluminum keyboard tray instead of a composite keyboard tray",
	"R":"keyboard tray on a Rex adjuster instead of an Ultra flip-up",
	"RC":"a flat metal keyboard tray on a Rex adjuster instead of a composite keyboard tray on an Ultra flip-up"
}
var oldmodsubUL={
	"K12CPL":"KU12F,C",
	"K12PL":"KU12",
	"KBPL":"KUB",
	"KCPL":"KUF,C",
	"KU12C":"KU12F,C",
	"KUC":"KUF,C"
};
var oldmodsubNotUL={
	"KUB":"KRXB,R",
	"KUC":"KRXF,RC",
	"KU12":"KRX12,R",
	"KU12C":"KRX12F,RC"
};
var oldmodsub={
	"K12C":"K12F,C",
	"K12CPL":"KRX12F,C",
	"K12FPL":"KRX12F",
	"K12PL":"KRX12",
	"KBPL":"KRXB",
	"KCPL":"KRXF,C",

	"ELP5120":"ELR5120,P",
	"ELP5220":"ELR5220,P",
	"ELS5120":"ELR5120,P3",
	"ELS5280":"ELX5220,P3",
	"MDP18":"MDR18,P",
	"PL":"TRX,P",
	"PS":"RX,P2",
	"T2P":"TRX,P",
	"T2S":"RX,P2",
	"T2EP":"T2ER,P",
	"T2ES":"T2ER,P",
	"T2P12":"T2R12,P",
	"T2P1212":"T2R1212,P",
	"T2P1220":"T2R1220,P",
	"T2P20":"T2R20,P",
	"T2P2020":"T2R2020,P",
	"T2S12":"T2R12,P3",
	"T2S1212":"T2R1212,P3",
	"T2S1220":"T2R1220,P3",
	"T2S20":"T2R20,P3",
	"T2S2020":"T2R2020,P3",
	"UL115":"UL110,U",
	"UL215":"UL210,U",
	"UL285":"UL280,U",
	"UL395":"UL390,U",
	"LTPL":"LTRX,PLT",
	"LTP":"LTRX,PLT",
	"LTUPL":"LTRX,ULT",
	"LTU":"LTRX,ULT",
	"KC":"KF,C"
};



//return [part,notes]
function FindSubstitute(part) {
	var ma=part.split("-");
	var isUL=part.charAt(0)=="U";
	var sub;
	var notes=[];
	var info="";
	var color=ma[ma.length-1];
	if(color=="P"||color=="T"||color=="TB"||color=="QG") {
		//simple color change
		ma[ma.length-1]="MW";
		notes.push("white or gray coating");
	} else {
		for(var i=0;i<ma.length;i++) {
			var m=ma[i];
			var sub=(isUL?oldmodsubUL:oldmodsubNotUL)[m] || oldmodsub[m];
			if(sub) {
				sub=sub.split(",");
				if(sub.length>1) notes.push(subnotes[sub[1]]);
				ma[i]=sub[0];
			}
		}
		info="<a href='../paralink.html' target='_blank' style='text-decoration:underline;color:blue;'>More info and images</a>"
	}
	var newpart=ma.join("-");
	if(newpart==part) return false;
	
	if(notes.length>0) {
		if(notes.length>2) {
			notes[notes.length-1]="and "+notes[notes.length-1];
			notes=notes.join(", ");
		} else {
			notes=notes.join(" and ")
		}
		notes="Exact image not yet available. Shown with "+notes+". "+info;
	} else {
		notes=""; //this shouldn't happen
	}
	return [newpart,notes];
}


var rtrim=/(^\s*)|(\s*$)/g;
function trim(s) {
	return s.replace(rtrim,"");
}



function AutoPartDesc(n,light) {
	try {
		var m=n.split("-");
		var d="";
		m=m.slice(-1).concat(m.slice(0,-1));
		var rare=false;
		for(var j=0;j<m.length;j++) {
			var mod=moddesc[m[j]];
			if(mod) d+=" "+mod; else d+=" ["+m[j]+"]";
		}

		m=d.split("with");
		m=[m.slice(0,2).join("with")].concat(m.slice(2));
		if(m.length>2) {
			d=[m.slice(0,-1).join(", ")].concat(m.slice(-1)).join(", and");
		} else {
			d=m.join("and");
		}
		d=d.split(" , ").join(", ");
		d=d.split("  ").join(" ");
		d=trim(d);
		
		if(d.slice(0,7)=="with a ") d=d.slice(7);
		if(d.slice(0,8)=="with an ") d=d.slice(8);
		
		d=d.slice(0,1).toUpperCase()+d.slice(1)+".";
		if(!light) {
			var c=comments[n];
			if(c) d+=" "+c+".";
		}
		
		return d;
	} catch(e) {
		return "["+(e.message||e.Description)+"]";
	}
}

function repl(a,b,c) {
	if(a.indexOf(b)>=0) return a.split(b).join(c);
	return a;
}

function colorsubst(text,suffix) {
	var a,b,c,d;
	switch(suffix) {
	case "-TB":
		a="black"; b="Black"; c="textured black"; d="Textured black";
		break;
	case "-QG":
		a="gray"; b="Gray"; c="quartz gray"; d="Quartz gray";
		break;
	case "-MW":
		a="white"; b="White"; c="gloss white"; d="Gloss white";
		break;
	case "-T":
		a="tan"; b="Tan"; c="tan"; d="Tan";
		break;
	case "-P":
		a="putty"; b="Putty"; c="putty"; d="Putty";
		break;
	}
	text=repl(text,"color%",a);
	text=repl(text,"Color%",b);
	text=repl(text,"colour%",c);
	text=repl(text,"Colour%",d);
	return text;
}
function fullProduct(p) {
	if(typeof(p)=="string") p=products[p];
	if(!p) return false;
	if(!p.desc) { //assume that anything without description is an empty colored version of a "-NC" base
		var temp=p.stockcode.split("-");
		var suffix="-"+temp.pop();
		var ncname=temp.join("-");
		var nc=mproducts[ncname];
		if(nc) {
			for(var n in nc) {
				if(!p[n]) {
					var v=nc[n];
					if(typeof(v)=="string") v=colorsubst(v,suffix);
					p[n]=v;
				}
			}
		}
		
	}
	if(!p.expanded) p.expanded=expandsub(p.stockcode);
	return p;
}


var mods={}; //lists of products per option, with option keyed by "group : optionDescription"
var choices={}; //currently selected options
var mlist=[]; //list of options sorted by group, description

function modinit(rep,base) {
	//rep is default/initial selection.
	//todo should rep handling be replaced by a call to setProduct?

	var count=0;
	/*var bc=0,rc=0;*/
	
	var debugging=(""+document.location.href).indexOf("universeman")>=0;
	
	var groups;
	//for each product
	for(var i in products) {
		var p=products[i];
		var def=p.stockcode==rep; //if this product is the default
		p.expanded=expandsub(p.stockcode);
		var m=p.expanded.split("-"); //expandsub turns dashed module list into dashed options list, then we split into array.
		
		if(def) {
			current=p;
		}

		count++;

		groups={};
		p.mods={};
		
		var compat=""; //prefix which module is compatible with
		
		for(var j=0;j<m.length;j++) {
			var mm=m[j]; //module name
			
			//patches
			//todo is this obsolete? ignore ceiling mount option when already followed by another ceiling option
			if(mm=="CE" && m[j+1].slice(0,2)=="XE") continue;
			if(mm=="CU2" && m[j+1].slice(0,2)=="XE") continue;
			
			//DEBUG
			if(debugging) {
				//report obvious errors
				if(!modules[mm]) document.write("Missing module["+mm+"]<br>");
				if(!getModGroup(mm)) document.write("Missing getModGroup("+mm+")<br>");
				if(!moddesc[mm]) document.write("Missing moddesc["+mm+"]<br>");
			}
			
			//get which option group this module falls over
			var g=getModGroup(mm);
			groups[g]=1; //indicate that the group needs to be visible on the page
			
			var o=g+" : "+modules[mm]; //key of group and module descriptive name. Note that two modules with same meaning can collapse into one option
			
			//add this product to the list of products for that module
			if(!mods[o]) {
				mods[o]=[];
				modcompat[o]={};
			}
			mods[o].push(p);
			
			modcompat[o][compat]=1;
			
			//add module to product's list of modules
			p.mods[o]=1;
			
			//if default product, choose the option
			if(def) choices[o]=1;
			
			if(compat=="") compat=mm; else compat+="-"+mm;
		}
		for(var n in modgroups) {
			var g=modgroups[n];
			
			//for every group that this product doesn't appear under, it needs to appear under a "None" option, creating one if necessary
			if(!groups[g]) {
				//ugly duplication of code from previous loop
				var o=g+" :  None";
				if(!mods[o]) mods[o]=[];
				mods[o].push(p);
				p.mods[o]=1;
				if(def) choices[o]=1;
				groups[g]=1;
			}
		}
	}
	
	//drop mandatory modules (that match all products and cannot be unselected) from mods list
	var temp={};
	for(var n in mods) {
		var m=mods[n];
		if(m.length<count) {
			temp[n]=m;
		} else {
			//document.write("dropping "+n+"<br>");
		}
	}
	mods=temp;

	//turn into array sorted by group, option
	for(var n in mods) {
		mlist.push(n);
	}
	mlist.sort();
	
	//generate the html for the options
	var pg="";
	var ct=0,mct=0,ct2=0;
	var boxtype=window.opera?"checkbox":"radio"; //radio buttons are glitchy on opera
	var eg;
	for(var i=0;i<mlist.length;i++) {
		var m=mlist[i];
		var ma=m.split(" : "); //split into array [group, optionDescription]
		var g=ma[0]; //group
		var o=ma[1]; //option
		if(g!=pg) { //if group doesn't match previous, it's a new group so we add a new group header
			if(i>0) { //if we need to close previous group
				document.write("</span></div>");
			}
			var link=g;
			eg=escape(g);
			var hide=""
			if(o.indexOf("None")>=0) {
				var scr="toggleHeader(escape(\""+eg+"\"))"
			
				link="<a href='javascript:"+scr+"'>"+link+" <span id='"+eg+"_toggle'>[+]</span></a>";
				hide=" style='display:none'";
			}
			document.write("<div class='optionheader' id='"+eg+"'>"+link+"<br><span id='"+eg+"_sub'"+hide+">");
			ct=0;
		}
		modheaders[m]=eg;
		var ms=escape(m); //escape to produce usable id
		document.write("<label id='lbl"+ms+"' class='optionitem' for='"+ms+"'><input class='optionbox' type='"+boxtype+"' id='"+ms+"' onclick='choose(unescape(\""+ms+"\"))'>&nbsp;"+trim(o) /*.replace(/[ ]/g," ")*/ +"</label>");
		ct++;
		pg=g;
	}
	document.write("</span></div>");
	document.write("<br><center><small><a href='#"+rep+"' id='linktothis'><u>Link to selected product</u></a><br><font color='gray'>(right click, \"Copy "+(document.all?"Shortcut":"Link")+"\")</font></small></center>");
}	

var modcompat={};
var modheaders={};

function toggleHeader(id,desired) {
	//alert("["+id+"]");
	var sub=document.getElementById(id+"_sub");
	if(!sub) return;
	var current=sub.style.display||"inline";
	if(!desired) desired=(current=="none")?"inline":"none";
	
	if(current!=desired) {
		sub.style.display=desired;
		var link=document.getElementById(id+"_toggle");
		link.innerHTML=(desired=="none")?"[+]":"[-]";
	}
}

function showHeader(id) {
	toggleHeader(id,"inline");
}

function hideHeader(id) {
	toggleHeader(id,"none");
}
	
//function el(n) { return document.all?document.all[n]:document.getElementById(n); }
function el(n,f) {
	if(f) return document.forms[n][f];
	else return document.all?document.all[n]:document.getElementById(n);
}
	
function updateboxes() {
	for(var m in mods) {
		var ms=escape(m);
		var eg=modheaders[m];
		var e=el(ms);
		var L=el("lbl"+ms);
		var isNone=m.indexOf("None")>=0;
		
		if(choices[m]) {
			if(!e.checked) { //only if previously unchecked
				if(eg) {
					if(isNone) {
						hideHeader(eg);
					} else {
						showHeader(eg);
					}
				}
			}
			e.checked=true;
			L.setAttribute("class", "optionitemselected");
		}else {
			e.checked=false;
			L.setAttribute("class", "optionitem");
		}
		
		var isCompat=false;
		var testcompat=modcompat[m];
		//modcompat for a module has a list of full part prefixes leading up to that module.
		
		if(testcompat) {
			for(var n in testcompat) {
				if(current.expanded.indexOf(n)==0) {
					isCompat=true;
					break;
				}
			}
		}
		//todo optimize
		if(isCompat || isNone) {
			L.style.color="black";
			L.style.fontStyle="normal";
			L.setAttribute("title", m);
		} else {
			L.style.color="#777";
			L.style.fontStyle="italic";
			L.setAttribute("title", "This option is incompatible with the series and/or mount currently selected.");
		}
	}
}

var current;

function addOne() {
	current=fullProduct(current.stockcode);
	addCart(current.stockcode,current.msrp,1);
}

function choose(m) {
	choices[m]=choices[m]?0:1;
	var ch=choices[m];
	var mc=mods[m];
	var pr=ch?mc:products;
	if(ch) m="!"+m;
	for(var i in pr) {
		var p=pr[i];
		p.choice=m;
		p.score=-p.rank/20000;
		if(p.basecode==lastbase) p.score+=0.5;
	}
	if(!ch) {
		for(var i in mc) {
			var p=mc[i];
			p.score-=10000;
		}
	}
	
	var count;
	for(var c in choices) {
		if(choices[c]) {
			count=0;
			var mod=mods[c];
			for(var i in mod) {
				var p=mod[i];
				if(p.choice==m) {
					p.score++;
					count++;
				}
			}
			//if(count==0) choices[c]=0;
		}
	}
	var ms=-1;
	var best;
	for(var i in pr) {
		var p=pr[i];
		if(p.score>ms || !best) {
			ms=p.score;
			best=p;
		}
	}
	
	current=best;
	setproduct(best.stockcode);
}

var AllowFull=true;

var lastbase="";
function setproduct(s) {
	var p=fullProduct(s);
	if(!p) return false;
	
	try {
		document.location.hash="#"+p.stockcode;
	} catch(ee){}
	
	current=p;
	el("imgnotes").innerHTML="";
	AllowFull=true;
	
	badAngle=false;
	noPlaceHolder=false;
	imageAngle=nextAngle="";
	
	el("bigphoto").src=cache("../a/w/"+p.stockcode+".jpg");
	el("stockcode").innerHTML=p.stockcode;
	try {
		el("msrp").innerHTML=CurrencyStr(p.msrp);
	} catch(ex) {
		el("msrp").innerHTML=p.msrp; //in case old cart.js is cached or it's somehow not loaded
	}
	el("title").innerHTML=p.title;
	el("desc").innerHTML=p.desc;
	el("specs").innerHTML=p.specs;
	
	var linky=el("linktothis");
	if(linky) {
		var u=""+document.location.href;
		if(u.indexOf("#")) u=u.split("#")[0];
		linky.href=u+"#"+p.stockcode;
	}

	//for each option
	for(var c in mods) {
		if(p.mods[c]) { //if this product has this option
			choices[c]=1;
		} else {
			choices[c]=0;
		}
	}
	
	updateboxes(); //update selected options to match selected part (choices[]) (needed if a selection forced another to change)
	
	
	if(p.basecode!=lastbase) {
		try { //update side bar to match new selection
			lastbase=p.basecode;
			loadopt(p.basecode);
			updatesidebar("blah");
		} catch(e) {}
	}
	
	return true;
}

var noPlaceHolder;
var ErrBeforeLoad;
function ImageError() {
	if(imageAngle!="" && !badAngle) {
		updateImageAngle(true);
		return;
	}
	if(!loaded) {
		ErrBeforeLoad=true;
		//alert("delaying");
		return;
	}
	if(noPlaceHolder) return; //prevent interference from mouseover script
	
	AllowFull=false; //don't expect there to be a full image, so don't bother opening
	var img=el("bigphoto");
	if(!img) return;
	var oldsrc=""+img.src;
	
	var newsrc='../a/w/placeholder225.gif';
	var notes="";
	if(oldsrc.indexOf(current.stockcode)>=0) {
		/* substitutes disabled for now. conflicting with angled images on occasion when switching quickly.
		var sub=FindSubstitute(current.stockcode);
		if(sub) {
			//alert(sub[0]);
			newsrc='../images/product_images/320x240/'+sub[0]+'.jpg';
			notes=sub[1];
		} else {
			//notes="no substitute";
		}
		*/
	}
	if(oldsrc!=newsrc) {
		img.src=cache(newsrc);
		//alert(newsrc);
	} else {
		notes="";
	}
	el("imgnotes").innerHTML=notes;
}

var loaded;
var initran;
var noscrollimage=false;
function pageinit() {
	if(initran) return;
	initran=true;
	
	var eb=el("bigphoto");

	if(eb) {
		var url=(""+document.location.href).split("#");
		if(url.length>1 && url[1].length>0) {
			var part=url[1];
			if(setproduct(part)) {
				ErrBeforeLoad=false;
			} else {
				updateboxes(); //todo ugly
			}
		} else {
			updateboxes();
		}
	}
	displaycart();
	loaded=true;
	if(ErrBeforeLoad) ImageError();
	
	
	if(eb) {
		if(eb.width==225) normalSize="225x225f"; //todo remove when confident nobody has the old pages cached.
		
		
		eb.onclick=function(e) {
			if(AllowFull) {
				open("http://www.icwdental.com/a/w/"+getImageName(),"_blank");
			} else {
				el("imgnotes").innerHTML="Larger image unavailable";
			}
			
			return false;		
		}
		eb.onmousemove=function(e) {
			e=e||window.event;
			var x,y;
			if(document.all) {
				x=e.offsetX;
				y=e.offsetY;
			} else {
				var p=getPosition(el("bigphoto"));
				x = e.clientX+document.body.scrollLeft - p.x;
				y = e.clientY+document.body.scrollTop - p.y;
			}
			//el("imgnotes").innerHTML=x+", "+y;
			x/=320;
			y/=240;
			
			if(x<.33) {
				nextAngle="Right";
			} else if(x>0.67) {
				nextAngle="Left";
			} else if((y>0.33 && y<0.67) || (imageAngle=="Left" || imageAngle=="Right")) {
				nextAngle="Front";
			} else if(y<0.2 || y>0.8) {
				nextAngle="";
			}
			//updateImageAngle();
		}
		eb.onmouseout=function() {
			nextAngle="";
			//updateImageAngle();
		}
		window.onscroll=function() {
			if (noscrollimage) return;
			var e=el("bigphoto");
			var x=el("close");
			var p=getPosition(e);
			var y=p.y-(parseInt(e.style.top)||0);
			var scroll=document.body.scrollTop||0;
			if(!scroll && document.documentElement) scroll=document.documentElement.scrollTop||0; //Internet Explorer fix
			//el("imgnotes").innerHTML=""+y+", "+scroll;
			//if(y<scroll) {
				if(e.style.position!="relative") {
					e.style.position="relative";
					e.style.left="0px";
				}
				var t=scroll-y;
				if(t<0) t=0;
				if(t>400) t=400;
				if(t>0) {x.style.visibility="visible"}else{x.style.visibility="hidden"};
				e.style.top=t+"px";
				x.style.top=(t+5)+"px";
				var newborder;
				if(t) newborder="1px solid silver";
				else newborder="none"
				if(newborder!=e.style.border) e.style.border=newborder;
				
			//}
		}
	}
	
}

function unscroll(){
	var e=el("bigphoto");
	e.style.top="0px";
	e.style.border="none";
	noscrollimage=true;
	el("close").style.visibility="hidden";
}

function getImageName(folder) {
	var name=current.stockcode+".jpg";
	if(imageAngle!="" && !badAngle) {
		noPlaceHolder=true;
		name=folder+"_"+imageAngle+"/"+imageAngle+"_"+name;
	} else if (folder) {
		name=folder+"/"+name;
	} 
	//el("imgnotes").innerHTML=name; //testing
	return name;
}

var badAngle=false;
var nextAngle="";
var imageAngle="";

var normalSize="320x240";

function updateImageAngle(bad) {
	if(badAngle || !AllowFull) return;

	if(nextAngle!=imageAngle || bad)  {
		badAngle=bad||false;
		imageAngle=nextAngle;
		var e=el("bigphoto");
		if(!e) return;
		e.src=cache('../a/w/'+getImageName(normalSize));
		//todo reduce duplication between this and other image setting
	}
}

function getPosition(e) {
	var x=0,y=0;
	while(e) {
		x+=e.offsetLeft;
		y+=e.offsetTop;
		e=e.offsetParent;
	}
	var ret={};
	ret.x=x;
	ret.y=y;
	return ret;
}

// from http://dean.edwards.name/weblog/2006/06/again/
if (document.addEventListener) {
	//Mozilla and Opera
	document.addEventListener("DOMContentLoaded", pageinit, false);
} else if (/WebKit/i.test(navigator.userAgent)) { // sniff
	//Webkit
	var _timer = setInterval(function() {
	if (/loaded|complete/.test(document.readyState)) {
		clearInterval(_timer);
		pageinit(); // call the onload handler
	}
	}, 10);
} else if(document.location.protocol!="https:") {
	//IE
	/*@cc_on @*/
	/*@if (@_win32)
	document.write("<sc"+"ript id=__ie_onload defer src=javasc"+"ript:void(0)><\/scri"+"pt>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			pageinit(); // call the onload handler
		}
	};
	/*@end @*/
}

