// JavaScript for pollution calculator

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

// Step 1. Calculate pollution from "conventional" generation.

//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;
}

// This function calculates the number of kilowatt hours
// based on utility, account type, supplier, and bill amount.
function usage(utility, account, supplier, amount)
{
    // Establish price per kwh for supply
    var myprice = 0;
    switch (supplier.suplname)
    {
	case "so":
          myprice = utility.smlso;
          break;
        case "mre":
          myprice = utility.mre;
          break;
        case "mps":
          myprice = utility.mps;
          break;
        default:
          myprice = utility.smlso;
    }

    // CMP Residential price algorithm as of June 7, 2004
    if ( (utility.utilname == "cmp") && (account.custname == "Residential") )
    {
      if (amount < 7.18)
      {
        return 0;
      }
      if ( amount < (7.18 + (100 * myprice)) )
      {
        return ( (amount - 7.18) / myprice);
      }
      return ( (amount - 0.4867) / (0.066933 + myprice) );
    }

    // CMP Small non-residential price algorithm as of June 7, 2004
    if ( utility.utilname == "cmp" )
    {
      return 100;
    }

    // BHE Residential price algorithm as of June 7, 2004
    if ( (utility.utilname == "bhe") && (account.custname == "Residential") )
    {
      var trans = 0.00968;
      if (amount < 8.87)
      {
        return 0;
      }
      if ( amount < (8.87 + (100 * trans) + (100 * myprice)) )
      {
        return ( (amount - 8.87) / (trans + myprice) );
      }
      return ( amount / (0.05623 + 0.03243 + trans + myprice) );
    }

    // MPS Residential price algorithm as of June 7, 2004
    if ( (utility.utilname == "mps") && (account.custname == "Residential") )
    {
      if (amount < 7.71)
      {
        return 0;
      }
      if ( amount < (7.71 + (100 * myprice)) )
      {
        return ( (amount - 7.71) / myprice);
      }
      return ( (amount - 0.0015) / (0.077085 + myprice) );
    }

    // Default
    return 0;

}

//Set up the function which will define each utility
// Pricing as of June 7, 2004
function defutility(utilname, smlso, medso, lrgso)
{
    this.utilname = utilname;
    this.smlso = smlso;
    this.medso = medso;
    this.lrgso = lrgso;
    this.mre = 0.065;
    this.medmpo = medso + 0.005;
    this.lrgmpo = lrgso + 0.005;
}

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

//Define the members of the array
// Utility name, small standard offer price, medium, large
Utility[0] = new defutility("cmp",0.0495,0,0);
Utility[1] = new defutility("bhe",0.05,0,0);
Utility[2] = new defutility("mps",0.05459,0,0);
Utility[3] = new defutility("kpl",0.33,0,0);

//Set up the function which will define each customer type
function custtype(custname)
{
    this.custname = custname;
}

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

//Define the members of the array
Customer[0] = new custtype("Residential");
Customer[1] = new custtype("Commercial");
Customer[2] = new custtype("Industrial");

//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 myutility  = document.pollcalc.myutility.selectedIndex;
 var mycustomer = document.pollcalc.mycustomer.value;
 var mysupplier = document.pollcalc.mysupplier.selectedIndex;

 //Get the monthly electric bill from form
 var mybill = parseFloat(document.pollcalc.mybill.value);
 if (isNaN(mybill)) { mybill=0; document.pollcalc.mybill.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 provider = Utility[myutility];
 var consumer = Customer[mycustomer];
 var producer = Supplier[mysupplier];

 nokwh=usage(provider,consumer,producer,mybill);

 // Calculate everything on an annual basis
 nokwh = nokwh*12;
 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 pollution amounts on form
 document.pollcalc.nokwh.value = Math.round(nokwh);
 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;

} // End gofigure
