Skip to content

Instantly share code, notes, and snippets.

@tdouce
Created March 2, 2010 21:40
Show Gist options
  • Save tdouce/319984 to your computer and use it in GitHub Desktop.
Save tdouce/319984 to your computer and use it in GitHub Desktop.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type='text/javascript'>
// returns true if the form is valid
// returns false if the form is invalid
function formValidator(formElements){
for (var i = 0; i < formElements.length; i++) {
var elem = formElements[i];
var msg = "Please enter a value for the " + formElements[i].id + ".";
if (!(isNumeric(elem, msg))) {
return false;
}
}
return true;
// formElements.each(function s() {
// }
// // Check each input in the order that it appears in the form!
// if(isNumeric(roof, "Please enter a value for the roof area.")){
// if(isNumeric(precip, "Please enter a precipitation value.")){
// if(isNumeric(eff, "Please enter an efficiency value.")){
// if(isNumeric(capt_area, "Please enter a capture area value.")){
// return true;
// }
// }
// }
// }
// return false;
}
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function calculate(roof, precip, eff, capt_area)
{
var eff_percent = (eff/100);
var conv_factr = 0.623;
var capt_area_percent = (capt_area/100);
var total = (roof * precip * conv_factr * capt_area_percent * eff_percent);
var total_rnd = Math.round(total);
return total_rnd;
}
function processForm()
{
var roof = document.getElementById('roof area');
var roof = document.getElementById('roof area');
var precip = document.getElementById('precipitation value');
var eff = document.getElementById('efficiency value');
var capt_area = document.getElementById('capture area');
if(formValidator([roof, precip, eff, capt_area])) {
var result = calculate(parseFloat(roof).value, parseFloat(precip).value, parseFloat(eff).value, parseFloat(capt_area).value);
document.getElementById('output').value = "Approximately " + result + " gallons can be collected off your roof annually."
}
}
function resetForm(){
document.getElementById('calculatorForm').reset();
}
</script>
<body>
<form name='formOne'>
Collection Footprint Area (square feet) <input type='text' id='roof area'/><br />
Annual precipitation in your area (inches) <input type='text' id='precipitation value'/><br />
Efficiency with which you are going to caputure rain (%)<input type='text' id='efficiency value' value='80' onFocus="if(this.value == '80') {this.value = '';}" onBlur="if (this.value == '') {this.value = '80';}" /><br />
The amount of roof area from which you are going to caputure rain (%) <input type='text' id='capture area'/><br />
Result: <textarea id='output' cols='40' rows='2'/></textarea>
<br/><input type='button' value='Calculate' onClick='processForm()'/><br/>
<input type='button' value='Reset All Fields' onClick='resetForm()'/><br/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment