// 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 
//
// The component also keeps track of the current co2 value so as to avoid multiple
// calcs (webservice calls) of the same data.

Type.registerNamespace("CZScripts");

CZScripts.StdEditor = function(element) {    
    CZScripts.StdEditor.initializeBase(this, [element]);   
    this.ResourceUnitId = 0;
    this.Input = "";
    this.Factor = NaN;
}

CZScripts.StdEditor.prototype = {

    initialize: function() 
    {       
      CZScripts.StdEditor.callBaseMethod(this, 'initialize');

      // create delegate for input change event, so we get the right 'this' pointer
      this.delegate = Function.createDelegate(this, this.onInputUpdated);

      // Attach delegate to input change event.    
      $addHandler($get(this.Input), 'change', this.delegate);     

      $addHandler($get(this.Input), 'keydown', this.suppressEnter);
      
      // simulate a change to get our co2 updated
      this.onInputUpdated(null);
    },
    
    // Release resources before control is disposed.
    dispose: function() {   
         $removeHandler($get(this.Input), 'change', this.delegate);
         $removeHandler($get(this.Input), 'keydown', this.suppressEnter);
        delete this.delegate;

        CZScripts.StdEditor.callBaseMethod(this, 'dispose');
    },   

   suppressEnter: function(evt) 
   {
      if (evt.keyCode == Sys.UI.Key.enter)
      {
            // stop the default button firing
            evt.preventDefault();
            
            // todo generate tab                     
            // probably need to pass next field in as parameter 
      }
   },

    clearInput: function()
    {
      $get(this.Input).value = "";
      this.set_CO2(Number.NaN);
    },
    
    onInputUpdated: function(e) 
    {          
      var value = this.parseDouble($get(this.Input).value);
      if (value && !isNaN(value))
      {     
         this.set_CO2(value * this.Factor);         
      }
      else
      {
         this.set_CO2(Number.NaN);
      }

      // fire event to signal update
      this.raise_Updated();       
    },    

    get_InputValue: function()
    {
      return $get(this.Input).value;
    },
    
    set_InputValue: function(value)
    {
      $get(this.Input).value = value;
    },
    
    addToValue: function(value)
    {
      var oldvalue = this.parseDouble($get(this.Input).value);
      if (oldvalue && !isNaN(oldvalue))
      {     
         $get(this.Input).value = oldvalue + value;
      }
      else
      {
         $get(this.Input).value = value;
      }

      // simulate update
      this.onInputUpdated();       
    }

}

CZScripts.StdEditor.registerClass('CZScripts.StdEditor', CZScripts.CO2Editor);




