// zero padding function
function PadDigits(n, totalDigits)
{
	n = n.toString();
	var pd = '';
	if (totalDigits > n.length)
	{
		for (i=0; i < (totalDigits-n.length); i++)
		{
			pd += '0';
		}
	}
	return pd + n.toString();
}

// captures when the Enter Key is pressed and submits
// the benchmark calculation
function acceptEnterKey(btn, evt) {
	var key = (evt.keyCode) ? evt.keyCode : evt.which;
	if (key == 13) btn.onclick();
	return true;
}

// creates the benchmarking score and 
// swaps the score image.
function bm()
{
	// create a shorthand refernce to the benchmarking form
	var form
	form = document.benchmark;

	// grab the area value
	var area
	area =	parseFloat(form.elements['area'].value);
	// make sure that the submitted value is a number
	if(isNaN(area)){
		errorMessage('Area must be a number', form.elements['area'])
		return
	}

	// grab the cost value
	var cost
	cost =	parseFloat(form.elements['cost'].value);
	// make sure that the submitted value is a number
	if(isNaN(cost)){
		errorMessage('Cost must be a number', form.elements['cost'])
		return
	}

	var score
	var roundedScore
	score = Math.round(cost/area*100)/100  // rounds to hundreds
	roundedScore = parseInt(score)
	if(roundedScore > 75){
		roundedScore = 75
	}
	if(roundedScore < 5){
		roundedScore = 5
	}
	if(roundedScore < 10){
		roundedScore = PadDigits(roundedScore, 2);
	}

	// these variable represent the scale images
	var graph
	graph = document.getElementById("graph")
	//		graph.setAttribute("src", "./dc_scores/"+roundedScore+".png")
	graph.src = "images/bm_scores/"+roundedScore+".png";

	// now write the score into the html
	var benchScore
	var benchScoreElement = document.createTextNode("$ "+score)
	benchScore = document.getElementById("result-score")

	benchScore.replaceChild(benchScoreElement, benchScore.firstChild)
	return true;
}

// simple error alert message
function errorMessage(message, element)
{
	alert(message)
	element.focus()
}