var questionCount = 0;
var districtCount = 0;

function hide(element)
{
	element.style.display = 'none';
}
function showHelp(helpButton)
{
	var buttonLeft = getLeftPostion(helpButton);
	var buttonTop = getTopPosition(helpButton);
	document.getElementById('helpDiv').style.left = buttonLeft;
	document.getElementById('helpDiv').style.top = buttonTop - 84;
	document.getElementById('helpDiv').style.display = 'block';
}

function showTools(toolsButton)
{
	var buttonLeft = getLeftPostion(toolsButton);
	var buttonTop = getTopPosition(toolsButton);
	document.getElementById('toolsDiv').style.left = buttonLeft;
	document.getElementById('toolsDiv').style.top = buttonTop - 130;
	document.getElementById('toolsDiv').style.display = 'block';
}

function getLeftPostion(obj)
{
	var curleft = 0;
	
    if(obj.offsetParent)
    {
		while(1) 
        {
			curleft += obj.offsetLeft;
			
			if(!obj.offsetParent)
            	break;
			
			obj = obj.offsetParent;
        }
	}
    else if(obj.x)
	{
		curleft += obj.x;
	}
	
	return curleft;
}

function getTopPosition(obj)
{
	var curtop = 0;
	
	if(obj.offsetParent)
	{
		while(1)
		{
			curtop += obj.offsetTop;
			
			if(!obj.offsetParent)
				break;
				
			obj = obj.offsetParent;
		}
	}
	else if(obj.y)
	{
		curtop += obj.y;
	}
	
	return curtop;
}

function clickQuestionCheckbox(question)
{
	if(question.className == "liBullet")
	{
		if(addQuestionToList(question.id))
			question.className = "liBulletChecked";
	}
	else
	{
		if(removeQuestionFromList(question.id))
			question.className = "liBullet";
	}				
}
function addQuestionToList(questionId)
{				
	if(document.getElementById("questionIdList").value.length>0)
		document.getElementById("questionIdList").value = document.getElementById("questionIdList").value + "," + questionId;
	else
		document.getElementById("questionIdList").value = questionId;
		
	questionCount++;
	return true;
}
function removeQuestionFromList(questionId)
{
	var questionIds = document.getElementById("questionIdList").value.split(",");
	
	if(questionIds.length<2)
	{
		document.getElementById("questionIdList").value = "";
		questionCount = 0;
	}
	else
	{
		document.getElementById("questionIdList").value = "";
		questionCount = 0;
		
		var i = 0;
		
		for(i=0; i<questionIds.length; i++)
		{
			if(questionIds[i] != questionId)
			{
				addQuestionToList(questionIds[i]);
			}
		} 		
	}
	
	return true;
}
function clearForm()
{
	document.rgForm.reset();
	
	clearQuestions();
	clearDistricts();
}
function clearQuestions()
{
	if(questionCount == 0)
	{
		return;
	}
		
	var questionIds = document.getElementById("questionIdList").value.split(",");

	var i = 0;

	for(i=0; i<questionIds.length; i++)
	{
		document.getElementById(questionIds[i]).className = "liBullet";
	}
	
	questionCount = 0;
	document.getElementById("questionIdList").value = "";
}



function clickDistrictCheckbox(district)
{
	var districtObj = eval(district);
	var districtIdParts = districtObj.id.split("-");
	var districtId = districtIdParts[1];
	
	if(district.className == "liBullet")
	{
		if(addDistrictToList(districtId))
			district.className = "liBulletChecked";
	}
	else
	{
		if(removeDistrictFromList(districtId))
			district.className = "liBullet";
	}				
}
function addDistrictToList(districtId)
{				
	if(document.getElementById("districtIdList").value.length>0)
		document.getElementById("districtIdList").value = document.getElementById("districtIdList").value + "," + districtId;
	else
		document.getElementById("districtIdList").value = districtId;
		
	districtCount++;
	return true;
}
function removeDistrictFromList(districtId)
{
	var districtIds = document.getElementById("districtIdList").value.split(",");
	
	if(districtIds.length<2)
	{
		document.getElementById("districtIdList").value = "";
		districtCount = 0;
	}
	else
	{
		document.getElementById("districtIdList").value = "";
		districtCount = 0;
		
		var i = 0;
		
		for(i=0; i<districtIds.length; i++)
		{
			if(districtIds[i] != districtId)
			{
				addDistrictToList(districtIds[i]);
			}
		} 		
	}
	
	return true;
}
function clearDistricts()
{
	if(districtCount == 0)
	{
		return;
	}
		
	var districtIds = document.getElementById("districtIdList").value.split(",");

	var i = 0;

	for(i=0; i<districtIds.length; i++)
	{
		document.getElementById(districtIds[i]).className = "liBullet";
	}
	
	districtCount = 0;
	document.getElementById("districtIdList").value = "";
}
function showDemographicColumn(checkbox, showCol, formVar)
{
	var style = "none";
	var tbl  = document.getElementById('rgTable');
	var currentTableWidth = tbl.style.width.substring(0, tbl.style.width.indexOf("px"));

	if(checkbox.checked)
	{
		style = "";
		document.getElementById(formVar).value = "TRUE";
		var newWidth = parseInt(parseInt(currentTableWidth) + 150);
		tbl.style.width = newWidth + "px";		
	}
	else
	{
		style = "none";
		document.getElementById(formVar).value = "";
		var newWidth = parseInt(parseInt(currentTableWidth) - 150);
		tbl.style.width = newWidth + "px";
	}
	
	showHideDemoColumn((showCol-1), style);
	showHideDemoColumn(showCol, style);
}
function showHideDemoColumn(col_no, stl)
{
	var tbl  = document.getElementById('rgTable');
	var rows = tbl.getElementsByTagName('tr');
	
	for (var row=0; row<rows.length; row++) 
	{
		var cels = rows[row].getElementsByTagName('td');
		cels[col_no].style.display=stl;
	}
}
function show_hide_column(col_no, do_show) 
{
	var stl;
	
	if (do_show) 
		stl = 'block'
	else
		stl = 'none';

	var tbl  = document.getElementById('rgTable');
	var rows = tbl.getElementsByTagName('tr');
	
	//now for each row, look for the td element at col_no
	for (var row=0; row<rows.length; row++) 
	{
		var cels = rows[row].getElementsByTagName('td');
		cels[col_no].style.display=stl;
		cels[(col_no-1)].style.display=stl;
	}
	
	var currentTableWidth = tbl.style.width.substring(0, tbl.style.width.indexOf("px"));
	var cellWidth = 200;
	var newWidth = 0 + currentTableWidth - cellWidth;
	
	tbl.style.width = newWidth + "px";	
}
function showHideAllStates()
{		
	var stateAnswerCount = document.getElementById("stateAnswerCount").value;
	var i = 0;
	
	showHideAnswer("stateFootnotes");
	
	for(i=0; i<stateAnswerCount; i++)
	{
		showHideAnswer("stateAnswer" + i);
	}			
}
function showHideAllDistricts()
{
	var answerCount = document.getElementById("stateAnswerCount").value;
	var i = 0;
	
	showHideAnswer("districtFootnotes");
	
	for(i=0; i<answerCount; i++)
	{
		showHideAnswer("districtAnswer" + i);
	}		
}
function showHideAnswer(id)
{

	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).style.display == 'none')
		{
			document.getElementById(id).style.display = 'block';
			return;
		}
		else
		{
			document.getElementById(id).style.display = 'none';
			return;
		}
	}
	else {
		if (document.layers) { // Netscape 4
			if(document.id.display == 'none')
			{
				document.id.display = 'block';
				return;
			}
			else
			{
				document.id.display = 'none';
				return;
			}
		}
		else { // IE 4
			if(document.all.id.style.display == 'none')
			{
				document.all.id.style.display = 'block';
				return;
			}
			else
			{
				document.all.id.style.display = 'none';
				return;
			}
		}
	}
}
function bookmark(url,title)
{
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) 
  {
  	window.external.AddFavorite(url,title);
  }  
  else 
  {
    alert("Press CTRL-D (Firefox) or CTRL-T (Opera) to bookmark");
  }
}

function printReport(params)
{
	if(document.getElementById('viewStateData'))
		openWindow('/tr3/reports/customPrint.jsp?' + params + '&showState=' + document.getElementById('viewStateData').checked, 'Print', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=485');
	else
		openWindow('/tr3/reports/customPrint.jsp?' + params + '&showState=false', 'Print', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=485');
}
function printpage() 
{
	window.print();  
}
function buildMessageAndSendEmail(url)
{
	var body = "Thought you might be interested in this report I generated at NCTQ's Teacher Rules, Roles and Rights website: " + url;		
	sendEmail("", "NCTQ Teacher Rules, Roles and Rights Custom Report", body);
}
function sendEmail(toAddress, subject, body) 
{
  window.location = "mailto:" + toAddress + "?subject=" + subject + "&body=" + body;
}
function checkEmailAddress(field) 
{
	// the following expression must be all on one line...
	var goodEmail = field.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  
	if (goodEmail) 
		return true;
	else
		return false;		
}
function showDistrictSection()
{
	document.getElementById("stateSection").style.display = 'none';	
	document.getElementById("districtSection").style.display = 'block';
	document.getElementById("districtStateChoice").value = "D";
}
function showStateSection()
{
	document.getElementById("districtSection").style.display = 'none';
	document.getElementById("stateSection").style.display = 'block';	
	document.getElementById("districtStateChoice").value = "S";
}
function selectAllStates()
{
	document.getElementById("districtIdList").value = "";
	districtCount = 0;
	
	var i = 200;
	var currentDistrict;
	
	for(i=200; i<251; i++)
	{
		currentDistrict = document.getElementById("districtId-" + i);
		clickDistrictCheckbox(currentDistrict);
	}
}
function unSelectAllStates()
{
	document.getElementById("districtIdList").value = "";
	districtCount = 0;
	
	var i = 200;
	var currentDistrict;
	
	for(i=200; i<251; i++)
	{
		currentDistrict = document.getElementById("districtId-" + i);
		currentDistrict.className = "liBullet";
	}
}
function doTooltip(e, msg) 
{
	if ( typeof Tooltip == "undefined" || !Tooltip.ready )
	{
		return;
	}
	
	Tooltip.show(e, msg);
}
function hideTip() 
{
	if ( typeof Tooltip == "undefined" || !Tooltip.ready ) 
	{
		return;
	}
	
	Tooltip.hide();
}
function openWindow(URL,name,attributes) 
{	
	popupWin = window.open(URL,name,attributes);
	popupWin.focus();
}
function listQuestions(restriction)
{
	popupWin = window.open('/tr3/ListQuestions.jsp?restriction=' + restriction, 'List', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=660');
	popupWin.focus();
}
function showAdvancedSelection()
{
	if(document.getElementById('advancedSelectionDiv'))
		document.getElementById('advancedSelectionDiv').style.display = 'block';
}
function hideAdvancedSelection()
{
	if(document.getElementById('advancedSelectionDiv'))
		document.getElementById('advancedSelectionDiv').style.display = 'none';
}
function getRadioCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

