
au = new AreaUnitConfig();
cu = new CarbonUnitConfig();
mu = new MonetaryUnitConfig();


cc = new CarbonCalculator();

/*====================================================================*/
/* Functions for event handlers on the HTML page.                     */
/*====================================================================*/

function onChangeArea(field) {
   var f = document.forms["calc"];
   if (field.name=="landarea"  ||  cc.isAreaSet()) {
      cc.setArea(new Area(f["landarea"].value, f["areaunits"].value));
   }
   calc();
}

function onChangeCarbon(field) {
   var f = document.forms["calc"];
   if (field.name=="carbon"  ||  cc.isCarbonSet()) {
      cc.setCarbon(new Carbon(f["carbon"].value, f["carbonunits"].value));
   }
   calc();
}

function onChangeMoney(field) {
   var f = document.forms["calc"];
   if (field.name=="money"  ||  cc.isMoneySet()) {
      cc.setMoney(new Money(f["money"].value, f["moneyunits"].value));
   }
   calc();
}

function calc() {
//James edit - remove nbsp and changed label text
   f = document.forms["calc"];

   // Assuming at least one of the three values has been set, then
   // label which one is specified and which two are calculated.
   if (cc.isAreaSet()  ||  cc.isCarbonSet()  ||  cc.isMoneySet()) {

      var e = document.getElementById("calcarea");
      if (cc.isAreaSet()) {
         e.innerHTML = "You<br />Entered";
         e.className = "calclabel startwith";
         document.getElementById("calcarea_holder").style.border = "1px solid #003471";
         document.getElementById("calcmoney_holder").style.borderWidth = "0";
         document.getElementById("calccarbon_holder").style.borderWidth = "0";
         document.getElementById("calcarea_holder").style.backgroundColor = "#edf3fd";
         document.getElementById("calccarbon_holder").style.backgroundColor = "#ddd4b9";
         document.getElementById("calcmoney_holder").style.backgroundColor = "#ddd4b9";
      } else {
         e.innerHTML = "Calculated";
         e.className = "calclabel calculated";
      }

      var e = document.getElementById("calccarbon");
      if (cc.isCarbonSet()) {
         e.innerHTML = "You<br />Entered";
         e.className = "calclabel startwith";
         document.getElementById("calccarbon_holder").style.border = "1px solid #003471";
         document.getElementById("calcarea_holder").style.borderWidth = "0";
         document.getElementById("calcmoney_holder").style.borderWidth = "0";
         document.getElementById("calccarbon_holder").style.backgroundColor = "#edf3fd";
         document.getElementById("calcarea_holder").style.backgroundColor = "#ddd4b9";
         document.getElementById("calcmoney_holder").style.backgroundColor = "#ddd4b9";
      } else {
         e.innerHTML = "Calculated";
         e.className = "calclabel calculated";
     }

      var e = document.getElementById("calcmoney");
      if (cc.isMoneySet()) {
         e.innerHTML = "You<br />Entered";
         e.className = "calclabel startwith";
         document.getElementById("calcmoney_holder").style.border = "1px solid #003471";
         document.getElementById("calcarea_holder").style.borderWidth = "0";
         document.getElementById("calccarbon_holder").style.borderWidth = "0";
         document.getElementById("calcmoney_holder").style.backgroundColor = "#edf3fd";
         document.getElementById("calcarea_holder").style.backgroundColor = "#ddd4b9";
         document.getElementById("calccarbon_holder").style.backgroundColor = "#ddd4b9";
      } else {
         e.innerHTML = "Calculated";
         e.className = "calclabel calculated";
      }
   }


   cc.calcThirdRatio();

   if (!cc.isAreaSet()) {
      val = cc.getArea(f["areaunits"].value);
      f["landarea"].value = DisplayFormat(val);
   }

   if (!cc.isCarbonSet()) {
      val = cc.getCarbon(f["carbonunits"].value);
      f["carbon"].value = DisplayFormat(val);
   }

   if (!cc.isMoneySet()) {
      val = cc.getMoney(f["moneyunits"].value);
      f["money"].value = DisplayFormat(val);
   }

// James edit
 document.getElementById("calcarea").style.display = "block";
 document.getElementById("calccarbon").style.display = "block";
 document.getElementById("calcmoney").style.display = "block";
 document.getElementById("additionalMessage").style.display = "block";
}


function getStaticAssumptions() {
    return "&nbsp;&nbsp;&nbsp;- " + cc.CarbonPerArea_Carbon.getValue() + " "
         + cu.getName( cc.CarbonPerArea_Carbon.getUnit() )
         + " per "
         + cc.CarbonPerArea_Area.getValue() + " "
         + au.getName( cc.CarbonPerArea_Area.getUnit() )
         + "<br />&nbsp;&nbsp;&nbsp;- "
         + cc.ValuePerCarbon_Value.getValue() + " "
         + mu.getName( cc.ValuePerCarbon_Value.getUnit() )
         + " per "
         + cc.ValuePerCarbon_Carbon.getValue() + " "
         + cu.getName( cc.ValuePerCarbon_Carbon.getUnit() )
    ;
}


function showAssumptions() {
   document.getElementById("calcsection").innerHTML = "Based On:";
   document.getElementById("calcsection").style.fontSize = "14px";

   document.getElementById("additionalMessage_assumptions").style.display = "block";
   // Copy the currently-used values into the form fields
   var f = document.getElementById("assumptions-form");
   f["CarbonPerArea_Carbon"].value = cc.CarbonPerArea_Carbon.getValue();
   f["CarbonPerArea_Carbon_Units"].selectedIndex = cc.CarbonPerArea_Carbon.getUnit();
   f["CarbonPerArea_Area"].value = cc.CarbonPerArea_Area.getValue();
   f["CarbonPerArea_Area_Units"].selectedIndex = cc.CarbonPerArea_Area.getUnit();
   f["ValuePerCarbon_Value"].value = cc.ValuePerCarbon_Value.getValue();
   f["ValuePerCarbon_Value_Units"].selectedIndex = cc.ValuePerCarbon_Value.getUnit();
   f["ValuePerCarbon_Carbon"].value = cc.ValuePerCarbon_Carbon.getValue();
   f["ValuePerCarbon_Carbon_Units"].selectedIndex = cc.ValuePerCarbon_Carbon.getUnit();

   document.getElementById("calcsection").style.paddingLeft = "88px";

   // Hide the static display of the assumptions
   document.getElementById("assumptions-static").style.display = "none";

   // Display the block containing the form
   document.getElementById("assumptions-input").style.display = "block";
}


function updateAssumptions() {

   // Copy the new values from the form fields into the objects used in calculation
   // TODO: should go through setXxxx methods on the CarbonCalculator object
   var f = document.getElementById("assumptions-form");
   cc.CarbonPerArea_Carbon = new Carbon(f["CarbonPerArea_Carbon"].value,
                                        f["CarbonPerArea_Carbon_Units"].selectedIndex
                                       );
   cc.CarbonPerArea_Area   = new Area  (f["CarbonPerArea_Area"].value,
                                        f["CarbonPerArea_Area_Units"].selectedIndex
                                       );
   cc.ValuePerCarbon_Value = new Money( f["ValuePerCarbon_Value"].value,
                                        f["ValuePerCarbon_Value_Units"].selectedIndex
                                       );
   cc.ValuePerCarbon_Carbon= new Carbon(f["ValuePerCarbon_Carbon"].value,
                                        f["ValuePerCarbon_Carbon_Units"].selectedIndex
                                       );

   cc.calcThirdRatio();   // Wouldn't be needed if setXxxx methods were used.
   calc();

   // Hide the input form
   document.getElementById("assumptions-input").style.display = "none";

   // Display the static display of assumptions
   document.getElementById("assumptions-static").innerHTML = getStaticAssumptions();
   document.getElementById("assumptions-static").style.display = "block";

  document.getElementById("calcsection").innerHTML = "Based on (<a href=\"javascript:void(showAssumptions())\">Change</a>)";
  document.getElementById("calcsection").style.fontSize = "12px";

  document.getElementById("calcsection").style.paddingLeft = "0";
}

function init() {
   document.getElementById("assumptions-static").innerHTML = getStaticAssumptions();
}
