/*
 * Energy Savings Calculator
 *
 * Date: 2008/09/16
 */
$(document).ready(function()  {
	$("#newBill").hide();
	$("#calculateSavings").click(function(){
	   calculateSavings();
	});
	
	$("form").submit(function(){
	   calculateSavings();
	   return false;
	});

});
 
function calculateSavings(){
	var bill = $("#bill").val();
	var bill = stripNonNumeric(bill);
	var savings = Math.round((bill * .20 *12));
	$('#newBill').show();
	$('#newBill').html('<h3>Based on your input, you can expect to save up to <strong>$' + savings + "</strong> per year!</h3>");
}

function stripNonNumeric( str ){
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ ){
    if( rgx.test( str.charAt(i) ) ){
      if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
             ( str.charAt(i) == '-' && out.length != 0 ) ) ){
        out += str.charAt(i);
      }
    }
  }
  return out;
}
