/*
 *  calculate.js : Version 1 : 19_6_2006 : for lunchtime
 *  
 *  CHANGE : all fields passed to constructor are
 *           now given to &_variable. ie. they are
 *           made into constants
 *
 */

/*
 *  calculate.js : Version 0 : 28_7_2005
 *
 */

function _calculate ( radio )
{
  for ( var n in this.vars )
   {
    if ( this.vars[n].reset ) this.vars[n].value = this.vars[n].def;
   }

  for ( var f in this.fields )
   {
    if ( this.form[f] && ( this.form[f].value || ! /^\s*$/.test ( this.form[f].value ) )  )
     {
      var code;
      if ( this.form[f][0] && this.form[f][0].type == 'radio' )
       {
        for ( var r=0; r<this.form[f].length; r++ )
         {
          if ( this.form[f][r].checked ) code = new String ( this.form[f][r].value );
         }
       }
      else
       {
        code = new String ( this.form[f].value );
       }
      code = code.replace ( /\$([^\;\:\?=+-\/+*%'"\[\]\(\)\{\}<>]+)/g, "this.vars['$1'].value" );
      code = code.replace ( /&([^\;\:\?=+-\/+*%'"\[\]\(\)\{\}<>]+)/g,  "this.handlers['$1']" );
      //alert ( code );
      eval ( code );
     }
   }

  if ( this.oncalculate ) this.oncalculate();

  return 1;
}


// FIX :make object contain all fields config for default values to put in 'desc' etc...



function _handler ( field, callback )
{
 with ( this )
 {
  // handlers.field = new Object ();
  // handlers.field.callback = callback;
  handlers[field] = callback;
 }
}


function _variable ( n, def, reset )
{
 with ( this )
 {
  vars[n] = new Object ();
  vars[n].value = def;
  vars[n].def = def;
  vars[n].reset = reset;
 }
}


function _refresh ()
{
 with ( this )
 {
  
 }
}


function _submit ()
{
  for ( var f in this.fields )
   {
    var value;
    if ( this.form[f][0] && this.form[f][0].type == 'radio' )
     {
      for ( var r=0; r<this.form[f].length; r++ )
       {
        if ( this.form[f][r].checked && this.vars[this.form[f][r].name] )
         {
          this.form[f][r].value = this.vars[this.form[f][r].name].value;
         }
       }
     }
    else
     {
      this.form[f][this.form[f].selectedIndex].value = this.form[f][this.form[f].selectedIndex].text
     }
   }
  this.form.submit();
}



// FIX : only option and radio types of input supported do far...


// ( form_reference, [ array of pairs of form elements with code to eval to contribute to total ]

function Calculator ( form_ref, this_name )
{
 this.form = form_ref;
 this.name = this_name;
 this.missing = '';
 this.fields = new Object ();
 this.code = new Object ();
 this.code_required = new Object ();
 this.handlers = new Object ();
 this.vars = new Object ();

 this.refresh = _refresh
 this.handler = _handler;
 this.calculate = _calculate;
 this.variable = _variable;
 this.submit = _submit;

 for ( var i=2; i<Calculator.arguments.length; )
  {
   var field = Calculator.arguments[i++];
   if ( this.form[field] )
    {
     this.fields[field] = Calculator.arguments[i++];
     this.variable
      (
       field, this.fields[field], 1
      );
     if ( this.form[field][0] && this.form[field][0].type == 'radio' )
      {
       for ( var r=0; r<this.form[field].length; r++ )
        {
         this.form[field][r].onclick = new Function ( this.name + ".calculate()" );
        }
      }
     else
      {
       this.form[field].onchange = new Function ( this.name + '.calculate()' );
      }
    }
  }

}


