
var m_compared	= false;
var m_htmlText	= "";
function Hide(id) {
	if (document.getElementsByName)
	{
		var divs = document.getElementsByName(id);
		if (divs && divs.length > 0)
		{	
			for(i=0;i<divs.length;i++)
			{
				divs[i].style.display = "none";
			}
		}		
	}
}


function Show(id) {
	if (document.getElementsByName)
	{
		var divs = document.getElementsByName(id);
		if (divs && divs.length > 0)
		{	
			for(i=0;i<divs.length;i++)
			{
				divs[i].style.display = "block";
			}
		}		
	}
}


function ShowHide(id) {
	if (document.getElementsByName)
	{
	
		var divs = document.getElementsByName(id);
		if (divs && divs.length > 0)
		{	
			for(i=0;i<divs.length;i++)
			{
			
				if (divs[i].style.display == "none")
				{
					divs[i].style.display = "block";
				}
				else
				{
					divs[i].style.display = "none";
				}
	
			}
		}		
	}
}
//This is a NewShowhide method that was created to support Good Better best
function NewShowHide(id) {
	
	if (document.getElementsByName)
	{
		
		var temp;
		var newarray= id.split(' ');
	
		for(var j= 0;  j <  newarray.length; j++)
		 {
			temp=newarray[j];
			
			var divs = document.getElementsByName(temp);
			
				if (divs && divs.length > 0)
				{	
					
						for(i=0;i<divs.length;i++)
						{
						
							if (divs[i].style.display == "none")
							{
						
							divs[i].style.display = "block";
							}
							else
							{
						
							divs[i].style.display = "none"; 	
							}
						
						
						}
					
					
				}	
				
				
		}	
	}
}

function Resize(id , cnt, isFirst) {
		
	if (cnt <= 3)
	{
		Hide ("scrolltext");
	}
	else
	{
		Show ("scrolltext");
	}
	
	var	newWidth	= "178";
	if (cnt == 3)
	{
		newWidth	= "239";
	}
	if (cnt == 2)
	{
		newWidth	= "361";
	}
	if (cnt == 1)
	{
		newWidth	= "728";
	}
	if (document.getElementsByName)
	{
		var divs = document.getElementsByName(id);
		if (divs && divs.length > 0)
		{	
			for(i=0;i<divs.length;i++)
			{
				var	chld		= divs[i].childNodes[0];
				if (chld && chld.name == "spacercol" )
				{
					continue;
				}
				divs[i].width	= newWidth;

				if (i===0)
				{
					continue;
				}

				if (isFirst)
				{
					divs[i].style.borderLeft= "medium none";
				}
				else
				{
					divs[i].style.borderLeft = "1px solid #cccccc";
				}
				
				divs[i].width	= newWidth;
			}
				
		}
		
		//impacttitles
		divs = document.getElementsByName("it_" + id);
		if (divs && divs.length > 0)
		{
			for(i=0;i<divs.length;i++)
			{
				divs[i].width = newWidth;
			}
		}		
	}
}


function RemoveItem(id) 
{
	if (document.dynamiccompare)
	{
		if (document.dynamiccompare.show.length)
		{
			for(i=0;i<document.dynamiccompare.show.length;i++)
			{
				if (document.dynamiccompare.show[i].value == id)
				{
					document.dynamiccompare.show[i].checked = false;
				}	
				else
				{
					if (!m_compared)
					{
						document.dynamiccompare.show[i].checked = true;
					}
				}
			}
			
			Reorder(false);
			
		}
		else
		{
			if (document.dynamiccompare.show.value == id)
			{
				document.dynamiccompare.show.value = "";
			}
			document.dynamiccompare.submit();	
		}
		
			
	}
}

function Validate ()
{
	
	if (m_htmlText.length=== 0)
	{
		
		if (document.getElementsByName)
		{
			
			var cTable	 = document.getElementsByName("compareTable");
			
			if (cTable && cTable.length == 1)
			{
				m_htmlText	= cTable[0].innerHTML;	
			}
		}
	
	}

	Reorder (true);
}

function Reset ()
{
	if (GnReapply())
	{
		return;
	}
	
	if (document.dynamiccompare)
	{
		for(resetIdx=0;resetIdx<document.dynamiccompare.show.length;resetIdx++)
		{
			document.dynamiccompare.show[resetIdx].checked = false;
		}
		Reorder (false);
	}
}
function Reorder(validateEmpty) 
{
	if (document.dynamiccompare)
	{
		var cnt		= 0;
		for(prodIndx=0;prodIndx<document.dynamiccompare.show.length;prodIndx++)
		{
			if (document.dynamiccompare.show[prodIndx].checked )
			{
				cnt++;
			}
		}
		
	
		if (cnt === 0)
		{
			if (validateEmpty)
			{
				if (alertVal)
				{
					alert (alertVal);
				}
				else
				{
					alert ("Please Select System");
				}
				return;
			}
			else
			{
				if (readIEVer() < 5.0 || isOpera)
				{
					if (m_htmlText.length > 0 && document.getElementsByName)
					{
						var cTable	 = document.getElementsByName("compareTable");
						
						if (cTable && cTable.length == 1)
						{
							cTable[0].innerHTML= m_htmlText;	
							return;
						}
					}

					location.reload();
					return;
				}
				
			}
			
		}
		var		isFirst		= true;
		
		if (cnt > 0)
		{
			Show  ("trashcan");
			Hide ("selectors");
		
			for(prodIndx=0;prodIndx<document.dynamiccompare.show.length;prodIndx++)
			{
				if (document.dynamiccompare.show[prodIndx].checked === false)
				{
					Hide ( document.dynamiccompare.show[prodIndx].value);
				}
				else
				{
					Resize (document.dynamiccompare.show[prodIndx].value  , cnt , isFirst);
					isFirst		= false;
				
				}
			}
		}
		else
		{
			
			cnt				= document.dynamiccompare.show.length;
			
			Show  ("selectors");
			Hide ("trashcan");
			
			for(prodIndx=0;prodIndx<document.dynamiccompare.show.length;prodIndx++)
			{
				Show ( document.dynamiccompare.show[prodIndx].value);
				Resize (document.dynamiccompare.show[prodIndx].value  , cnt , prodIndx === 0);
			}
		}
		
		m_compared	= true;
				
	}
}

// =======================================================
// Guided navigation
// =======================================================

var gn;

function GnSelectAll(group)
{
	GnSelectIx(-1, group, 0);
}

function GnSelectIx(ix, group, item)
{
	if (!gn)
	{
		gn = new GuidedNav(gnShow, gnAll, gnGroups, gnRemove);
	}
	
	gn.SelectIx(ix, group, item);
}

function GnReapply()
{
	if (gn)
	{
		gn.Reapply();
		
		return true;
	}
	
	return false;
}

function GuidedNav(showVar, all, groups, remove)
{
	var gnCompareTable;
	var gnAll;
	var gnHide;
	var gnShow;
	var gnShowDict;
	var gnGroups;
	var gnSelText;
	var gnSelections;
	var gnElemId;
	var gnVisible;
	var gnHidden;
	var gnGrouping;
	var gnGroupTitle;
	var gnRemove;
	var gnImage;
	var gnRepl1 = new RegExp("\\/content\\/.*\\?", "gi");
	
	gnRemove = remove;
	
	Init(showVar, all, groups);

	function SelectIx(ix, group, item)
	{
		var i = 0;

		var ids = HiddenProducts(ix, group, item);
		var showIds = ShownProducts(ids);
		
		if (showIds.length === 0)
		{
			gnCompareTable.innerHTML = "<div class=\"validationsummary_error\">No products match these criteria.</div>";
		}
		else
		{
			gnVisible = showIds;
			gnHidden = ids;
			
			ApplyFilter();
		}

		Highlight(group, item);
	}
	
	this.SelectIx = SelectIx;
	this.Reapply = ApplyFilter;
	
	function ApplyFilter()
	{
		var filterText = BuildFilter();		
	
		gnImage = BuildMetrics();
		gnCompareTable.innerHTML = filterText + m_htmlText + gnImage;
	
		if (filterText.length > 0)
		{
			for(var key in gnHidden)
			{
				Hide(key);
			}
			
			var first = true;
			var cnt = gnVisible.length;
			
			for(prodIndx = 0; prodIndx < cnt; prodIndx++)
			{
				Resize(gnVisible[prodIndx], cnt, first);
				first = false;
			}
		}
	}
	
	function BuildFilter()
	{
		var i = 0;
		var filter = null;
		
		while(i < gnSelText.length)
		{
			var sel = gnSelText[i];
			
			if (sel)
			{
				if (!filter)
				{
					filter = "<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\" bgcolor=\"#f5f5f5\" style=\"border:1px solid #cccccc\"><tr><td><div style=\"border-left:3px solid #666666; line-height:20px; padding:0px 5px; margin:4px 0px\">";
				}
				
				filter = filter + "<div class=\"para\"><b>" + gnGroupTitle[i] + "</b> : " + sel + " (<a href=\"JavaScript:GnSelectAll(" + i + ");\" class=\"pnlLink\">" + gnRemove + "</a>)</div>";
			}
			
			i++;
		}
		
		if (filter)
		{
			filter = filter + "</div></tr></td></table>";
		}
		
		return filter ? filter : "";
	}

	function BuildMetrics()
	{
		var i = 0;
		var tag = null;
		
		while(i < gnSelText.length)
		{
			var sel = gnSelText[i];
			
			if (sel)
			{
				if (tag)
				{
					tag += "^" + escape(sel);
				}
				else
				{
					tag = escape(sel);
				}
			}
			
			i++;
		}

		var myHref = document.location.href;
		
		href = myHref.replace(gnRepl1, "/content/products/guided/filter.htm?");
		var ix = href.indexOf("#");
		if (ix > 0)
		{
			href = href.substr(0, ix);
		}

		var gnRepl2 = new RegExp("\\.aspx\\/(.*)\\?", "gi");
		var idr = gnRepl2.exec(myHref);
		href += "&~cat=" + RegExp.$1;
		
		if (tag)
		{
			href += "&~assist=" + tag;
		}

		var guid = Math.random().toString().substr(2);
		href += "&~guid=" + guid;

		return "<img src=\"" + href + "\" width=\"1\" height=\"1\" />";
	}
	
	function HiddenProducts(ix, group, item)
	{
		if (ix >= 0)
		{
			gnSelections[group] = gnHide[ix];
			
			elem = document.getElementById("gn:" + group + ":" + item);
			gnSelText[group] = elem.innerHTML;			
		}
		else
		{
			gnSelections[group] = null;
			gnSelText[group] = null;
		}

		var i = 0;
		var hideVar = new Array();
		
		while(i < gnSelections.length)
		{
			var h = gnSelections[i];
			
			if (h)
			{
				for(var key in h)
				{
					id = h[key];
					
					hideVar[id] = id;
				}
			}
			
			i++;
		}
		
		return hideVar;
	}

	function ShownProducts(hidden)
	{
		var src = 0;
		var dst = 0;
		var showVar = new Array();
		
		while(src < gnAll.length)
		{
			var showId = gnAll[src];
			
			if (!hidden[showId])
			{
				showVar[dst++] = showId;
			}
			
			src++;
		}
		
		return showVar;
	}

	function Init(showVar, all, groups)
	{
		gnAll = all.split(",");
		gnSelections = new Array(groups.length);
		gnSelText = new Array(groups.length);
		gnGroupTitle = new Array(groups.length);
		gnGroups = groups;
		
		gnShow = new Array();
		gnHide = new Array();
		gnShowDict = new Array();
		gnElemId = new Array();
		gnGrouping = new Array();
		gnVisible = gnAll;
		
		i = 0;

		while(i < groups.length)
		{
			gnSelText[i] = null;
			gnSelections[i] = null;
			gnGroupTitle[i] = document.getElementById("gn:" + i + ":title").innerHTML;
			i++;
		}
		
		i = 0;
		
		index = 1;
		group = 0;

		while(i < showVar.length)
		{
			if (index >= gnGroups[group])
			{
				group++;
				index = 1;
			}
			
			gnElemId[i] = "gn:" + group + ":" + index;
			gnGrouping[i] = new Array(group, index++);
		
			id = showVar[i].split(",");
			gnShow[i] = id;
			
			showDict = new Array();
		
			j = 0;
		
			while(j < id.length)
			{
				showDict[id[j]] = true;
				
				j++;
			}
			
			hideVar = new Array();
			
			j = 0;
		
			while(j < gnAll.length)
			{
				id = gnAll[j];

				if (!showDict[id])
				{
					hideVar[hideVar.length] = id;				
				}
				
				j++;
			}
			
			gnHide[i] = hideVar;
			gnShowDict[i] = showDict;
			
			i++;
		}
		
		var tdElem = document.getElementById(gnAll[0]);
		
		gnCompareTable = parentNode(parentNode(parentNode(parentNode(tdElem))));
		m_htmlText	= gnCompareTable.innerHTML;
	}
	
	function CalculateCount(ix, group, item)
	{
		var mySelections = new Array();
		var s = 0;
		
		while(s < gnSelections.length)
		{
			if (s == group)
			{
				mySelections[s] = gnHide[ix];
			}
			else
			{
				mySelections[s] = gnSelections[s];
			}
			
			s++;
		}
		
		var i = 0;
		var hideVar = new Array();
		var hideCount = 0;
		
		while(i < mySelections.length)
		{
			var h = mySelections[i];
			
			if (h)
			{
				for(var key in h)
				{
					id = h[key];
					
					if (!hideVar[id])
					{
						hideVar[id] = id;
						hideCount++;
					}
				}
			}
			
			i++;
		}
		
		return gnAll.length - hideCount;
	}

	function Highlight(group, item)
	{
		var base = "gn:" + group + ":";
		
		for(i = 1; i < gnGroups[group]; i++)
		{
			var elem = document.getElementById(base + i);

			elem.className = (i == item) ? "fnavsel" : "fnavnor";
		}
		
		i = 0;
		
		var vizDict = new Array();
		
		while(i < gnVisible.length)
		{
			vizDict[gnVisible[i++]] = true;
		}
		
		i = 0;
		count = 1;

		while(i < showVar.length)
		{
			var viz = false;
			var showIds = gnShow[i];
			
			var g = gnGrouping[i];
			
			group = g[0];
			index = g[1];
			
			var resultCount = CalculateCount(i, group, index);
			var elemName = "gn:" + group + ":" + index + ":count";
			var countElem = document.getElementById(elemName);
			countElem.innerHTML = "(" + resultCount + ")";
			
			ix = 0;

			while(ix < showIds.length)
			{
				key = showIds[ix++];

				if (vizDict[key])
				{
					viz = true;
					count++;
					break;
				}
			}
			
			var elem = document.getElementById(gnElemId[i]);
			parentNode(elem).style.display = viz ? "block" : "none";
			
			if (index == gnGroups[group] - 1)
			{
				var container = parentNode(parentNode(elem));
			
				container.style.display = (count > 2) ? "block" : "none";
				count = 1;
			}
			
			i++;
		}
	}
}
