
function generate_contourObject()
{var gridSpacingInput=null;var contourIntervalInput=null;var firstElevationInput=null;var secondElevationInput=null;var slopePara=null;var numContourPara=null;var contourTableDiv=null;var contourPrecision=0;var newItem=null;var newText=null;var ContourTable={};function colorizeRows(theTable)
{var rows=theTable.getElementsByTagName('tr');for(var i=2;i<rows.length;i+=2)
{var style=rows[i].style;style.backgroundColor='#cfc';style.color='black';}}
function formatVal(val,precision)
{var p=Math.pow(10,precision);var v=Math.round(val*p)/p;var str=v.toString();if(precision<0)return str;var decIndex=str.indexOf('.');if((precision>0)&&decIndex<0)
{decIndex=str.length;str+='.';}
while(decIndex+precision+1>str.length)
{str+='0';}
return str;}
function eventHandler(evt)
{evt=evt?evt:window.event;var gridVal,contourInterval,firstVal,secondVal;if((gridSpacingInput.value!=null)&&(gridSpacingInput.textLength!=0))
{gridSpacingInput.style.backgroundColor="#9f9";gridVal=parseFloat(gridSpacingInput.value);if(isNaN(gridVal)||(Math.abs(gridVal)<1e-4))
{gridSpacingInput.style.backgroundColor="#f99";return;}
gridSpacingInput.value=gridVal;}
else
{return;}
if((contourIntervalInput.value!=null)&&(contourIntervalInput.textLength!=0))
{contourIntervalInput.style.backgroundColor="#9f9";contourInterval=parseFloat(contourIntervalInput.value);if(contourInterval<0)contourInterval=-contourInterval;if(isNaN(contourInterval))
{contourIntervalInput.style.backgroundColor="#f99";return;}
contourIntervalInput.value=contourInterval;var decIndex=contourIntervalInput.value.indexOf('.');if(decIndex<0)
{contourPrecision=0;}
else
{contourPrecision=contourIntervalInput.value.length-decIndex-1;}}
else
{return;}
if((firstElevationInput.value!=null)&&(firstElevationInput.textLength!=0))
{firstElevationInput.style.backgroundColor="#9f9";firstVal=parseFloat(firstElevationInput.value);if(isNaN(firstVal))
{firstElevationInput.style.backgroundColor="#f99";return;}
firstElevationInput.value=firstVal;}
else
{return;}
if((secondElevationInput.value!=null)&&(secondElevationInput.textLength!=0))
{secondElevationInput.style.backgroundColor="#9f9";secondVal=parseFloat(secondElevationInput.value);if(isNaN(secondVal))
{secondElevationInput.style.backgroundColor="#f99";return;}
secondElevationInput.value=secondVal;}
else
{return;}
var low=firstVal,high=secondVal;if(low>high)
{low=secondVal;high=firstVal;}
var slopeVal=(high-low)/gridVal;var slopeStr=formatVal(slopeVal,3);var str="The slope is (";str=str+high+" - "+low+") / "+gridVal+" = "+
slopeStr+".";slopePara.nodeValue=str;while(contourTableDiv.firstChild!=null)
{contourTableDiv.removeChild(contourTableDiv.firstChild);}
var contourTable=document.createElement('table');var contourTableBody=document.createElement('tbody');newRow=document.createElement('tr');newCol=document.createElement('th');newCol.appendChild(document.createTextNode('Elevation'));newRow.appendChild(newCol);newCol=document.createElement('th');newCol.appendChild(document.createTextNode('Distance From Lower Endpoint'));newRow.appendChild(newCol);newCol=document.createElement('th');newCol.appendChild(document.createTextNode('Percentage of Distance Between Endpoints'));newRow.appendChild(newCol);contourTableBody.appendChild(newRow);var numContours=0;var thisContour=Math.ceil(low/contourInterval)*contourInterval;if(high>low)
{while(thisContour<=high)
{var proportion=(thisContour-low)/(high-low);var contourStr=formatVal(thisContour,contourPrecision);var proportionStr=formatVal(proportion,3);var distanceStr=formatVal(proportion*gridVal,3);newRow=document.createElement('tr');newCol=document.createElement('td');newText=document.createTextNode(contourStr);newCol.appendChild(newText);newRow.appendChild(newCol);var proportionVal=(thisContour-low)/(high-low);var distanceStr=formatVal(gridVal*proportionVal,3);newCol=document.createElement('td');newText=document.createTextNode(distanceStr);newCol.appendChild(newText);newRow.appendChild(newCol);var proportionStr=formatVal(100*proportionVal,1);newCol=document.createElement('td');newText=document.createTextNode(proportionStr);newCol.appendChild(newText);newRow.appendChild(newCol);contourTableBody.appendChild(newRow);thisContour+=contourInterval;numContours++;}
contourTable.appendChild(contourTableBody);contourTableDiv.appendChild(contourTable);colorizeRows(contourTable);}
switch(numContours)
{case 0:numContourPara.nodeValue="There are no contour lines.";return;case 1:numContourPara.nodeValue="There is one contour line:";break;default:{var str="There are "+numContours+" contour lines:";numContourPara.nodeValue=str;}}
document.getElementById('slope').style.display='block';document.getElementById('num_contours').style.display='block';document.getElementById('contours').style.display='block';}
function keyReleased(evt)
{evt=evt?evt:window.event;if(evt.keyCode==13)
{eventHandler(evt);}}
return{init:function()
{gridSpacingInput=document.getElementById('grid_spacing');contourIntervalInput=document.getElementById('contour_interval');firstElevationInput=document.getElementById('first_elevation');secondElevationInput=document.getElementById('second_elevation');slopePara=document.getElementById('slope').firstChild;numContourPara=document.getElementById('num_contours').firstChild;contourTableDiv=document.getElementById('contours');Core.addEventListener(gridSpacingInput,'change',eventHandler);Core.addEventListener(contourIntervalInput,'change',eventHandler);Core.addEventListener(firstElevationInput,'change',eventHandler);Core.addEventListener(secondElevationInput,'change',eventHandler);Core.addEventListener(document.getElementsByTagName('body')[0],'keyup',keyReleased);}}}
Core.start(generate_contourObject());
