// links
links = document.getElementsByTagName('a');
for (i=0; i<links.length; i++)
{
	if (links[i].id.match(/cbContainer[0-9]+/))
	{
		links[i].onclick = function()
		{
			document.getElementById('cbContainer').style.display='';
			document.getElementById('resultsContainer').style.display='none';
			document.getElementById('anchorsList').style.display='none';
			document.feedbackForm.style.display='none';
			document.comparisonForm.action = "tools-comparison-chart.php";
			//return false;
		}
	}
	
	if (links[i].id.match("stylesReset"))
	{
		links[i].onclick = function()
		{
			for (j=0; j<20; j++)
			{
				if (document.getElementById('style'+j)) document.getElementById('style'+j).selectedIndex = 0;
			}
			return false;
		}
	}
	
	if (links[i].id.match("backToChart"))
	{
		links[i].onclick = function()
		{
			document.getElementById('cbContainer').style.display='none';
			document.getElementById('resultsContainer').style.display='';
			document.getElementById('anchorsList').style.display='';
			return false;
		}
	}
	
	if (links[i].id.match("downloadLink"))
	{
		links[i].onclick = function()
		{
			document.comparisonForm.action = "includes/tools/comparison-chart/comparison.csv";
			document.comparisonForm.submit();
			return false;
		}
	}
	
	if (links[i].id.match("continueToCriteria"))
	{
		links[i].onclick = function()
		{
			var valid = 0;
			for (j = 0; j < 50; j++) if (document.getElementById('line' + j)) if (document.getElementById('line' + j).checked) valid++;
			if (valid)
			{
				if (document.getElementById("cbLineList")) document.getElementById("cbLineList").style.display = "none";
				if (document.getElementById("cbCriteriaList")) document.getElementById("cbCriteriaList").style.display = "block";
			}
			else
			{
				alert('Select at least one product line');
				return false;
			}
		}
	}
	
	if (links[i].id.match("compareSubmit"))
	{
		links[i].onclick = function()
		{
			var valid = 0;
			for (j = 0; j < 50; j++) if (document.getElementById('crit' + j)) if (document.getElementById('crit' + j).checked) valid++;
			if (valid) document.comparisonForm.submit();
			else
			{
				alert('Select at least one comparison criterion');
				return false;
			}
		}
	}
	
}

// inputs
cbs = document.getElementsByTagName('input');
for (i=0; i<cbs.length; i++)
{
	if (cbs[i].type == "checkbox")
	{
		for (j=0; j<20; j++)
		{
			if (cbs[i].id.match(/line[0-9]+/))
			{
				cbs[i].onclick = function()
				{
					thatId = this.id.substr(4);
					if (this.checked) document.getElementById('style'+thatId).disabled = false;
					else
					{
						document.getElementById('style'+thatId).disabled = true;
						document.getElementById('cbLineAll').checked = false;
					}
				}
			}
			if (cbs[i].id.match(/crit[0-9]+/))
			{
				cbs[i].onclick = function()
				{
					if (!this.checked) document.getElementById('cbCritAll').checked = false;
				}
			}
		}
		if (cbs[i].id.match("cbLineAll"))
		{
			cbs[i].onclick = function()
			{
				for (j=0; j<20; j++)
				{
					if (document.getElementById('line'+j)) document.getElementById('line'+j).checked = this.checked;
					if (document.getElementById('style'+j)) document.getElementById('style'+j).disabled = !this.checked;
				}
			}
		}
		if (cbs[i].id.match("cbCritAll"))
		{
			cbs[i].onclick = function()
			{
				for (j=0; j<20; j++)
				{
					if (document.getElementById('crit'+j)) document.getElementById('crit'+j).checked = this.checked;
				}
			}
		}
	}
}