// This type is the client side ui logic for standard editor.
// It's function is to detect changes to the text field and 
// 1) update the co2 output label
// 2) fire event used to trigger update of section and page totals 


Type.registerNamespace("CZScripts");

CZScripts.CarEditor = function(element) {    
    CZScripts.CarEditor.initializeBase(this, [element]);   
    
   this.delegates = [];

   this.eFuelType = { Petrol : 0, Diesel : 1, LPG : 2 };
   this.eInputType = { Litres : 0, Dollars : 1, Kms : 2 };
   this.eEngineSize = { Small : 0, Medium : 1, Large: 2 }; 
   
   this.NonKmRateTable = "";
   this.KmRateTable = "";
   this.Litres=null;
   this.Dollars=null;
   this.Kms=null;   
   this.FuelType = null;
   this.EngineSize = null; 
}

CZScripts.CarEditor.prototype = {

    initialize: function() 
    {       
      CZScripts.CarEditor.callBaseMethod(this, 'initialize');

      this.EvalNonKmRateTable = eval(this.NonKmRateTable);
      this.EvalKmRateTable = eval(this.KmRateTable);
   
      //TODO: Fix this hack
      this.Petrol = this.FuelType +'_0';
      this.Diesel = this.FuelType +'_1';
      this.LPG = this.FuelType +'_2';

      //TODO: Fix this hack
      this.Small = this.EngineSize +'_0';
      this.Medium = this.EngineSize +'_1';
      this.Large = this.EngineSize +'_2';
      
      // Hook input fields
      this.delegates.push(new CZScripts.Delegate($get(this.Litres), 'change', this.onLitres, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Litres), 'keydown', this.suppressEnter, this));

      this.delegates.push(new CZScripts.Delegate($get(this.Dollars), 'change', this.onDollars, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Dollars), 'keydown', this.suppressEnter, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Kms), 'change', this.onKms, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Kms), 'keydown', this.suppressEnter, this));

      // Hook fuel type rb's
      this.delegates.push(new CZScripts.Delegate($get(this.Petrol), 'click', this.onPetrol, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Diesel), 'click', this.onDiesel, this));
      this.delegates.push(new CZScripts.Delegate($get(this.LPG), 'click', this.onLPG, this));

      // Hook car size rb's
      this.delegates.push(new CZScripts.Delegate($get(this.Large), 'click', this.UpdateCO2, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Medium), 'click', this.UpdateCO2, this));
      this.delegates.push(new CZScripts.Delegate($get(this.Small), 'click', this.UpdateCO2, this));
      
      this.UpdateCO2();
    },
    
    // Release resources before control is disposed.
    dispose: function() {   

         for (var i in this.delegates)
         {
            this.delegates[i].dispose();
         }        

        CZScripts.CarEditor.callBaseMethod(this, 'dispose');
    },   

   suppressEnter: function() 
   {
      if (window.event.keyCode == 13)
      {
         window.event.keyCode = 9;
      }
   },

   GetFuelType: function()
   {
      if ($get(this.Petrol).checked)
      {
         return (this.eFuelType.Petrol);
      }
      if ($get(this.Diesel).checked)
      {
         return (this.eFuelType.Diesel);
      }
      if ($get(this.LPG).checked)
      {
         return (this.eFuelType.LPG);
      }
   },
   
   GetEngineSize: function()
   {
      if ($get(this.Large).checked)
      {
         return (this.eEngineSize.Large);
      }
      if ($get(this.Medium).checked)
      {
         return (this.eEngineSize.Medium);
      }
      if ($get(this.Small).checked)
      {
         return (this.eEngineSize.Small);
      }
   },

   GetInputType: function()
   {
      if (this.HasValue(this.Dollars))
      {
         return (this.eInputType.Dollars);
      }
      if (this.HasValue(this.Kms))
      {
         return (this.eInputType.Kms);
      }
      if (this.HasValue(this.Litres))
      {
         return (this.eInputType.Litres);
      }
      return -1;
   },
   
   HasValue: function(tbid)
   {
      var element = $get(tbid);
      return !(element.disabled || element.value == "");
   },
   
   GetReading: function(inputType)
   {
      switch (inputType)
      {
      case this.eInputType.Dollars:
         return this.parseCurrency($get(this.Dollars).value);
      case this.eInputType.Kms:
         return this.parseDouble($get(this.Kms).value);
      case this.eInputType.Litres:
         return this.parseDouble($get(this.Litres).value);      
      }
      return NaN;   
   },
      
   onLitres: function(e)
   {
      // clear other text fields
      // Litres, Dollars and Kms are exclusive
      $get(this.Dollars).value = "";
      ValidatorValidate($get(this.Dollars + "Validator"));
      $get(this.Kms).value = "";
      ValidatorValidate($get(this.Kms + "Validator"));

      this.UpdateCO2();
   },

   onDollars: function(e)
   {
      // clear other text fields
      // Litres, Dollars and Kms are exclusive

      $get(this.Litres).value = "";
      ValidatorValidate($get(this.Litres + "Validator"));

      $get(this.Kms).value = "";
      ValidatorValidate($get(this.Kms + "Validator"));

      this.UpdateCO2();
   },
   
   onKms: function(e)
   {
      // clear other text fields
      // Litres, Dollars and Kms are exclusive
      $get(this.Dollars).value = "";
      ValidatorValidate($get(this.Dollars + "Validator"));
      $get(this.Litres).value = "";
      ValidatorValidate($get(this.Litres + "Validator"));

      this.UpdateCO2();
   }, 

   onDiesel: function(e)
   {
      // all enabled :o)
      enableValidatedTextBox($get(this.Dollars), true, "N/A");
      enableValidatedTextBox($get(this.Litres), true, "N/A");
      enableValidatedTextBox($get(this.Kms), true, "N/A");
      this.UpdateCO2();
   }, 

   onLPG: function(e)
   {
      // we have $ and litres but no kms
      enableValidatedTextBox($get(this.Dollars), true, "N/A");
      enableValidatedTextBox($get(this.Litres), true, "N/A");
      enableValidatedTextBox($get(this.Kms), false, "N/A");
      this.UpdateCO2();
   }, 

   onPetrol: function(e)
   {
      // all enabled :o)
      enableValidatedTextBox($get(this.Dollars), true, "N/A");
      enableValidatedTextBox($get(this.Litres), true, "N/A");
      enableValidatedTextBox($get(this.Kms), true, "N/A");
      this.UpdateCO2();
   }, 
   
   UpdateCO2: function()
   {
      // Calc New CO2
      var inputType = this.GetInputType();
      if (inputType == this.eInputType.Kms)
      {
         var factor = this.EvalKmRateTable[this.GetFuelType()][this.GetEngineSize()];
      }
      else
      {
         var factor = this.EvalNonKmRateTable[this.GetFuelType()][inputType];
      }

      var reading = this.GetReading(inputType);
      if (reading && !isNaN(reading))
      {
         this.set_CO2(reading * factor);
      }
      else
      {
         this.set_CO2(NaN);
      }            
   }
}

CZScripts.CarEditor.registerClass('CZScripts.CarEditor', CZScripts.CO2Editor);






