
function displayerror ( msg ) {
 msg.innerHTML = "Please enter a valid number";
 msg.className = "msgon"; 
}

function clearerror ( msg ) {
 msg.innerHTML = "";
 msg.className = "msgoff"; 
}

function currencyformat( amount )
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	if(s.length > 6) { s=s.substr(0,s.length-6) + ',' + s.substr(s.length-6); }
	if(s.length > 10) { s=s.substr(0,s.length-10) + ',' + s.substr(s.length-10); }
	if(s.length > 14) { s=s.substr(0,s.length-14) + ',' + s.substr(s.length-14); }
	s = minus + s;
	return s;
}

function calcroi () {
 var hhscost = new Array(13900,17900,23900);
 var hhspctfuel = new Array(0.65,0.55,0.45,0.35);
 var milespermonth = eval( 'document.roi.milespermonth' );
 var truckclass = eval( 'document.roi.truckclass' );
 var avgmilespergallon = eval( 'document.roi.avgmilespergallon' );
 var costpergallon = eval( 'document.roi.costpergallon' );
 var stopandgo = eval( 'document.roi.stopandgo' );
/***
 var numbrakerepl = eval( 'document.roi.numbrakerepl' );
 var avgcostbrakerepl = eval( 'document.roi.avgcostbrakerepl' );
**/
 var roimsg = document.getElementById('roimsg');

 milespermonth.className="ok";
 truckclass.className="ok";
 avgmilespergallon.className="ok";
 costpergallon.className="ok";
 stopandgo.className="ok";
/***
 numbrakerepl.className="ok";
 avgcostbrakerepl.className="ok";
***/
 clearerror ( roimsg );

 if ( isNaN(milespermonth.value) || milespermonth.value == "" ) { displayerror(roimsg); milespermonth.className="fix"; milespermonth.focus(); return false; }
 if ( isNaN(truckclass.value) || truckclass.value == "" ) { displayerror(roimsg); truckclass.className="fix"; truckclass.focus(); return false; }
 if ( isNaN(avgmilespergallon.value) || avgmilespergallon.value == "" ) { displayerror(roimsg); avgmilespergallon.className="fix"; avgmilespergallon.focus(); return false; }
 if ( isNaN(costpergallon.value) || costpergallon.value == "" ) { displayerror(roimsg); costpergallon.className="fix"; costpergallon.focus(); return false; }
 if ( isNaN(stopandgo.value) || stopandgo.value == "" ) { displayerror(roimsg); stopandgo.className="fix"; stopandgo.focus(); return false; }
/***
 if ( isNaN(numbrakerepl.value) || numbrakerepl.value == "" ) { displayerror(roimsg); numbrakerepl.className="fix"; numbrakerepl.focus(); return false; }
 if ( isNaN(avgcostbrakerepl.value) || avgcostbrakerepl.value == "" ) { displayerror(roimsg); avgcostbrakerepl.className="fix"; avgcostbrakerepl.focus(); return false; }
***/

 var costhhs = document.getElementById('costhhs');
 var monthlyfuelcost = document.getElementById('monthlyfuelcost');
 var monthlyfuelcosthhs = document.getElementById('monthlyfuelcosthhs');
 var annualfuelcostreduction = document.getElementById('annualfuelcostreduction');
/***
 var costbrakerepl = document.getElementById('costbrakerepl');
 var costbrakereplhhs = document.getElementById('costbrakereplhhs');
***/
 var roiyears = document.getElementById('roiyears');

 var tcosthhs = hhscost[ truckclass.value ];
 var tmonthlyfuelcost = ( milespermonth.value / avgmilespergallon.value ) * costpergallon.value;
 var tmonthlyfuelcosthhs = tmonthlyfuelcost * hhspctfuel[stopandgo.value];
 var tannualfuelcostreduction = (tmonthlyfuelcost - tmonthlyfuelcosthhs) * 12;
/***
 var tcostbrakerepl = ( numbrakerepl.value * avgcostbrakerepl.value );
 var tcostbrakereplhhs = ( tcostbrakerepl * .5 );
***/
/***
 var troiyears = tcosthhs / ( tannualfuelcostreduction + tcostbrakereplhhs );
***/
 var troiyears = tcosthhs / ( tannualfuelcostreduction );

 costhhs.innerHTML = currencyformat( tcosthhs );
 monthlyfuelcost.innerHTML = currencyformat( tmonthlyfuelcost ); 
 monthlyfuelcosthhs.innerHTML = currencyformat( tmonthlyfuelcosthhs );
 annualfuelcostreduction.innerHTML = currencyformat( tannualfuelcostreduction );
/***
 costbrakerepl.innerHTML = currencyformat( tcostbrakerepl );
 costbrakereplhhs.innerHTML = currencyformat( tcostbrakereplhhs );
***/
 roiyears.innerHTML = currencyformat( troiyears );

 document.getElementById('costhhsv').value = tcosthhs;
 document.getElementById('monthlyfuelcostv').value = tmonthlyfuelcost;
 document.getElementById('monthlyfuelcosthhsv').value = tmonthlyfuelcosthhs;
 document.getElementById('annualfuelcostreductionv').value = tannualfuelcostreduction;
/***
 document.getElementById('costbrakereplv').value = tcostbrakerepl;
 document.getElementById('costbrakereplhhsv').value = tcostbrakereplhhs;
***/
 document.getElementById('roiyearsv').value = troiyears;

var calcfields = jQuery("form").serialize();

jQuery.post("/roi/roi.php",calcfields);

return false;
}

