﻿var unitCost = 0.1;
var powerDivider = 5;

function CurrencyFormatted(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'; }
	s = minus + s;
	return s;
}

function calc()
{
    
    if (!document.getElementById("txtBulbPower")) 
    { 
        alert("Sorry - cannot find the bulb power!  Your form does not contain txtBulbPower");
        return;
    }
    if (!document.getElementById("txtBulbHours"))
    { 
        alert("Sorry - cannot find the bulb hours!  Your form does not contain txtBulbHours");
        return;
    }
    if (!document.getElementById("txtBulbQty"))
    { 
        alert("Sorry - cannot find the bulb quantity!  Your form does not contain txtBulbQty");
        return;
    }
    try {
        var bulbWatts=Number(document.getElementById("txtBulbPower").value);
        var bulbHrs=Number(document.getElementById("txtBulbHours").value);
        var bulbQty=Number(document.getElementById("txtBulbQty").value);
    } catch(Error) {
        alert(Error.toString());
    }
    
    if (bulbWatts<=0)
    { 
        alert("Power of bulb is not set or is zero");
        return;
    }
    if (bulbHrs<=0)
    { 
        alert("Daily bulb hours is not set or is zero");
        return;
    }
    if (bulbQty<=0)
    { 
        alert("Bulb quantity is not set or is zero");
        return;
    }

    var estCurrentCost = ( bulbWatts/1000 ) * bulbHrs * 365 * unitCost * bulbQty;
    var newBulbPower = bulbWatts / powerDivider;
    var estNewCost = ( newBulbPower/( 1000 ) ) * bulbHrs * 365 * unitCost * bulbQty;

    document.getElementById("spNewBulbPower").innerHTML=bulbWatts/powerDivider
    document.getElementById("spEstCurrentCost").innerHTML=CurrencyFormatted(estCurrentCost);
    document.getElementById("spEstNewCost").innerHTML=CurrencyFormatted(estNewCost);
    document.getElementById("spEstSaving").innerHTML=CurrencyFormatted((estCurrentCost-estNewCost));
    
}