var printReady = false
var sBorderColor = "white"

function openWindow(myLink) {

  if (!window.focus)return true;
  var href;
  if (typeof(myLink) == 'string')
    href=myLink;
  else
    href=myLink.href;
  var myWindow = window.open(href, 'MGWindow','height=400,width=500,directories=1,fullscreen=0,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1');
  myWindow.focus();
  return false;

}

function openPopup(myLink) {

  if (!window.focus)return true;
  var href;
  if (typeof(myLink) == 'string')
    href=myLink;
  else
    href=myLink.href;
  var myPopup = window.open(href, 'MGPopup','height=400,width=500,directories=0,fullscreen=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,toolbar=0');
  myPopup.focus();
  return false;

}

function isEmpty(sValue) {

	return ((sValue.search(/\S/) == -1) || (sValue == null))

}

function expressionOk( oInput ) {

//	if ((window.document.activeElement == window.document.frmEvent.btnCancel) ||
//		(window.document.activeElement == window.document.frmEvent.btnReset))
//		return true;
	var sTemp = oInput.value
	if (isEmpty(oInput.value)) return true
	sTemp = sTemp.replace(/exp/gi, "")
	sTemp = sTemp.replace(/e\^/gi, "")
	sTemp = sTemp.replace(/ln/gi, "")
	sTemp = sTemp.replace(/asin|arcsin/gi, "")
	sTemp = sTemp.replace(/acos|arccos/gi, "")
	sTemp = sTemp.replace(/atan|arctan/gi, "")
	sTemp = sTemp.replace(/sin/gi, "")
	sTemp = sTemp.replace(/cos/gi, "")
	sTemp = sTemp.replace(/tan/gi, "")
	sTemp = sTemp.replace(/sqrt/gi, "")
	sTemp = sTemp.replace(/y/gi, "")
	sTemp = sTemp.replace(/t/gi, "")
	sTemp = sTemp.replace(/x/gi, "")
	sTemp = sTemp.replace(/h/gi, "")
	sTemp = sTemp.replace(/i/gi, "")
	var reLetter = /[a-zA-Z]/
	if (reLetter.test(sTemp)) {
		oInput.select();
		alert("Please enter a valid expression for the function.");
		oInput.focus();
		return false
		}
	return true

}


function floatOk ( oInput ) {

	if (isEmpty(oInput.value)) return true;
	oInput.value = oInput.value.replace(/^\s*([-+]?)\s*[.]/,"$10.")
	var sTemp = oInput.value
	var reReal = /^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$/
	if (!reReal.test(sTemp)) {
		oInput.select();
		alert("Please enter a valid number for the approximation.");
		oInput.focus();
		return false
		}
	return true
}

function integerOk( oInput, iMin, iMax ) {

	if (oInput.value == "") return true;
	var sTemp = oInput.value
	var reDigit = /^[-+]?\d+$/
	if (!reDigit.test(sTemp)) {
		oInput.select();
		alert("Please enter a valid integer.");
		oInput.focus();
		return false
		}
	var iNum = parseInt(sTemp)
	if (iNum < iMin) {
		oInput.select();
		alert("Please enter an integer greater than or equal to  "+iMin+".");
		oInput.focus();
		return false
		}
	if (iNum > iMax) {
		oInput.select();
		alert("Please enter an integer less than or equal to "+iMax+".");
		oInput.focus();
		return false
		}
	return true
}

function listOk( oInput ) {

	if (isEmpty(oInput.value)) return true;
	oInput.value = oInput.value.replace(/\s/g,"")
	oInput.value = oInput.value.replace(/(^|[,])([-+]?)[.]/g,"$1$20.")
	var sTemp = oInput.value
	var reList = /^(([-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?)[,])+([-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?)$/
	if (!reList.test(sTemp)) {
		oInput.select();
		alert("Please enter a valid list of numbers separated by commas.");
		oInput.focus();
		return false
		}
	return true

}

function checkHeight() {

	var iHeight = 0
	
	if (document.getElementById('divContent2')) {
		for (i=0;i<document.getElementById('divContent2').children.length;i++) {
			if (document.getElementById('divContent2').children[i].offsetHeight > iHeight) {
				iHeight = document.getElementById('divContent2').children[i].offsetHeight
				}
			}
		if (iHeight+65 > 440) {
			document.getElementById('divContent1').style.pixelHeight=iHeight + 65
			document.getElementById('divLeftArea').style.pixelHeight=iHeight + 65
			}
		else {
			document.getElementById('divContent1').style.pixelHeight=440
			document.getElementById('divLeftArea').style.pixelHeight=440
			}	
	}
/*	if (document.all.divContent2) {	
		for (i=0;i<document.all.divContent2.children.length;i++) {
			if (document.all.divContent2.children[i].offsetHeight > iHeight) {
				iHeight = document.all.divContent2.children[i].offsetHeight
				}
			}
		if (iHeight+65 > 440) {
			document.all.divContent1.style.pixelHeight=iHeight + 65
			document.all.divLeftArea.style.pixelHeight=iHeight + 65
			}
		else {
			document.all.divContent1.style.pixelHeight=440
			document.all.divLeftArea.style.pixelHeight=440
			}	
	}
*/
	}

function hideIcons() {

//	document.all.refreshIcon.style.display='none'
	document.getElementById('refreshIcon').style.display='none'

}

function showIcons() {

//	document.all.refreshIcon.style.display='block'
	document.getElementById('refreshIcon').style.display='block'
}

function toggleColumn(oLabel,oHeading,oColumn) {

	var oHeadings = tableHeadings.getElementsByTagName("th")
	var oRows = tableResults.getElementsByTagName("tr")
	var iCol = parseInt(oColumn.id.replace(/col/,""))
	if (oColumn.style.display == 'none') {
		oHeading.style.display = 'block'
		oHeadings.item(parseInt(oHeading.id.replace(/hdr/,""))).style.borderRight = '1px solid '+sBorderColor
		oColumn.style.display = 'block'
		for (i=0;i<=oRows.length-1;i++) {
			oCells = oRows.item(i).getElementsByTagName("td")
			oCells.item(iCol).style.borderRight = '1px solid '+sBorderColor
			}
//		oLabel.style.color = 'blue'
		oLabel.checked = true
		}
	else {
		oHeading.style.display = 'none'
		oHeadings.item(parseInt(oHeading.id.replace(/hdr/,""))).style.borderRight = ''
		oColumn.style.display = 'none'
		for (i=0;i<=oRows.length-1;i++) {
			var oCells = oRows.item(i).getElementsByTagName("td")
			oCells.item(iCol).style.borderRight = ''
			}
//		oLabel.style.color = 'steelblue'
		oLabel.checked = false

		}
}

function scrollHeadings() {

	//divHeadings.scrollTo(divResults.scrollLeft,0)
	divHeadings.scrollLeft = divResults.scrollLeft

}

function showWhatsThis(oWhat) {

	alert(oWhat.title)
	
}

function toggleInstructions() {

	if (divInstructions.style.display == 'none') {
		document.all.divInstructions.style.display = 'block'
		document.all.btnInstructions.value = 'Hide Instructions'
		}
	else {
		document.all.divInstructions.style.display = 'none'
		document.all.btnInstructions.value = 'Show Instructions'
		}
}

function togglePrint() {

	if (!printReady) {
		document.bgColor = 'white'
		document.all.divTopArea.style.display='none'
		document.all.divLeftArea.style.display='none'
		document.all.divTopArea.style.display='none'
		document.all.divCorner.style.display='none'
		document.all.divTitle1.style.display='none'
		document.all.divTitle2.style.display='block'
		document.all.divCopyright.style.display='none'
		if (document.all.divEditor) {
			document.all.divEditor.style.display = 'none'
			}
		document.all.divContent1.style.top = '0px'
		document.all.divContent1.style.left = '0px'
		sBorderColor = 'black'
		if (document.all.styleTools) {
//			document.all.styleTools.href = "../../Styles/PrintTools.css"			
			styleTools.disabled = true
			stylePrintTools.disabled = false
			}
		if (document.all.divShowForm) {
			divShowForm.style.backgroundColor = 'white'
			}
		if (document.all.divShowResults) {
			divShowResults.style.backgroundColor = 'white'
			divShowResults.style.overflow = 'visible'
			divShowResults.style.borderColor= ""
			divShowResults.style.borderWidth = ""
			divShowResults.style.borderStyle = ""
			}
		if (document.all.tableHeadings) {
			divHeadings.style.overflow = 'visible'
			divResults.style.overflow = 'visible'
			tableHeadings.style.borderColor = 'white'
			tableResults.style.borderColor = 'white'
			var oHeadings = tableHeadings.getElementsByTagName("th")
			for (i=0;i<=oHeadings.length-1;i++) {
				oHeadings.item(i).style.borderColor = 'black'
				}
			var oCells = tableResults.getElementsByTagName("td")
			for (i=0;i<=oCells.length-1;i++) {
				oCells.item(i).style.borderColor = 'black'
				}
			}
		if (document.all.divCatGrades) {
		alert('here')
			divCatGrades.style.overflow = 'visible'
			divDateGrades.style.overflow = 'visible'
			}
		printReady = true
		var iWidth = 0
		for (i=0;i<document.all.divContent2.children.length;i++) {
			if (document.all.divContent2.children[i].offsetWidth > iWidth) {
				iWidth = document.all.divContent2.children[i].offsetWidth
				}
			}
		if (iWidth > document.all.spanActualTitle.offsetWidth) {
			document.all.divTitle2.style.pixelWidth=iWidth
			}
//		window.print()
		}
	else {
		document.bgColor = 'black'
		document.all.divTopArea.style.display='block'
		document.all.divLeftArea.style.display='block'
		document.all.divTopArea.style.display='block'
		document.all.divCorner.style.display='block'
		document.all.divTitle1.style.display='block'
		document.all.divTitle2.style.display='none'
		document.all.divCopyright.style.display='block'
		if (document.all.divEditor) {
			document.all.divEditor.style.display = 'block'
			}
		document.all.divContent1.style.top = '150px'
		document.all.divContent1.style.left = '130px'
		sBorderColor = 'white'
		if (document.all.styleTools) {
//			document.all.styleTools.href = "../../Styles/Tools.css"			
			styleTools.disabled = false
			stylePrintTools.disabled = true
			}
		if (document.all.divShowForm) {
			divShowForm.style.backgroundColor = '#cccccc'
			}
		if (document.all.divShowResults) {
			divShowResults.style.backgroundColor = '#cccccc'
			divShowResults.style.overflow = 'auto'
			divShowResults.style.borderColor= "black"
			divShowResults.style.borderWidth = "2pt"
			divShowResults.style.borderStyle = "solid"
			}
		if (document.all.tableHeadings) {
			divHeadings.style.overflow = 'hidden'
			divResults.style.overflow = 'auto'
			tableHeadings.style.borderColor = '#cccccc'
			tableResults.style.borderColor = '#cccccc'
			var oHeadings = tableHeadings.getElementsByTagName("th")
			for (i=0;i<=oHeadings.length-1;i++) {
				oHeadings.item(i).style.borderColor = 'white'
				}
			var oCells = tableResults.getElementsByTagName("td")
			for (i=0;i<=oCells.length-1;i++) {
				oCells.item(i).style.borderColor = 'white'
				}
			}
		if (document.all.divCatGrades) {
			divCatGrades.style.overflow = 'auto'
			divDateGrades.style.overflow = 'auto'
			}
		printReady = false
		}

}

window.onbeforeprint = hideIcons
window.onafterprint = showIcons
