// JavaScript for pollution calculator

// See emission.comments for comments on pricing, etc.

//Set up the function which will define each state's data
function stdatel(sname,resprice,comprice,indprice,co2lbs,voclbs,noxlbs,colbs,so2lbs,p10lbs,hgmgs)
{
    this.sname = sname;
    this.resprice = resprice;
    this.comprice = comprice; 
    this.indprice = indprice;
    this.co2lbs = co2lbs;
    this.voclbs = voclbs;
    this.noxlbs = noxlbs;
    this.colbs = colbs;
    this.so2lbs = so2lbs;
    this.p10lbs = p10lbs;
    this.hgmgs = hgmgs;
}

//Set up the function which will define each supplier
function defsupplier(suplname, co2lbs, noxlbs, so2lbs)
{
    this.suplname = suplname;
    this.co2lbs = co2lbs;
    this.noxlbs = noxlbs;
    this.so2lbs = so2lbs;
}

//Create the array with the name Supplier
var Supplier = new Object();

//Define the members of the array
// Supplier name, CO2, NOx, SO2
Supplier[0] = new defsupplier("so", 0.7746,0.0018,0.0026);
Supplier[1] = new defsupplier("so", 0.7746,0.0018,0.0026);
Supplier[2] = new defsupplier("mre",0.0659,0.0011,0);
Supplier[3] = new defsupplier("mpo",0.0659,0.0011,0);

//Define the function which will calculate the pollution based on electricity usage.
//This function will be called every time there is a change to the input.
function gofigure()
{

 // Get the utility, customer account type, supplier, and tags selected from form
 var mysupplier = document.pollcalc.mysupplier.selectedIndex;

 //Get the monthly electricity usage  from form
 var nokwh = parseFloat(document.pollcalc.nokwh.value);
 if (isNaN(nokwh)) { nokwh=0; document.pollcalc.nokwh.value = "0";}

 var state = new stdatel("Maine", 0.1198, 0.1047, 0.1124, 0.7746, 0.000016, 0.0018, 0.000054, 0.0026, 0.000005, 0.000205);
 var producer = Supplier[mysupplier];

 // Calculate pollution
 var myco2 = nokwh*producer.co2lbs;
 var mynox = nokwh*producer.noxlbs;
 var myco = nokwh*state.colbs;
 var myso2 = nokwh*producer.so2lbs;
 var myhg = nokwh*state.hgmgs;

 // Calculate these in ounces
 var myvoc = nokwh*state.voclbs*16;
 var myp10 = nokwh*state.p10lbs*16;

 // Set form values
 document.pollcalc.myco2.value = Math.round(myco2*2)/2;
 document.pollcalc.myvoc.value = Math.round(myvoc*4)/4;
 document.pollcalc.mynox.value = Math.round(mynox*4)/4;
 document.pollcalc.myco.value =  Math.round(myco*4)/4;
 document.pollcalc.myso2.value = Math.round(myso2*4)/4;
 document.pollcalc.myp10.value = Math.round(myp10*4)/4;
 document.pollcalc.myhg.value =  Math.round(myhg*4)/4;

}
