function getMotorTorque (f, f1, speed, gearRatio)
{
  var drag;
  drag = getTotalDrag (f, f1, speed);
  return (drag / (gearRatio.value * f.drive_efficiency.value));
}

function getMotorRpm (f1, speed, gearRatio)
{
  return ((speed * gearRatio.value * f1.revPerMile.value) / 60);
}

function getBatteryVolts (batteryAmps, power)
{
  return (power / batteryAmps);
}

function getBatteryAmps (f, f1, power)
{
  with (Math)
  {
    var current = 5;
    var tmp;
    var tmp1;
    var tmp2;

// -f.totalResistance * current^2 + f.voltage.value * current = power
// (-f.totalResistance.value/power) * current^2 +  (f.voltage.value/power) * current = 0;
//      x = (-b +- root (b^2 - 4ac))/2a

    tmp  = sqrt (pow (f.voltage.value, 2) - (4 * f1.totalResistance.value * power));
    tmp1 = -f.voltage.value + tmp;
    tmp2 = 2 * -f1.totalResistance.value;

    return ((tmp1/tmp2) / f.numString.value);
  }
}
  
function calcTableRow (f, f1, drag, gearRatio, speed, torque, rpm,
                    motorAmps, motorVolts, batteryAmps, batteryVolts, range, whpermi,costmi,costtrip)
{
  with (Math)
  {
      var power, tqVal, rpVal, mvVal, maVal, baVal, bvVal, rgVal, whVal, costVal,tripVal;
      
      tqVal = getMotorTorque (f, f1, speed, gearRatio);
      rpVal = getMotorRpm (f1, speed, gearRatio);
      mvVal = getMotorVoltage (f, rpVal, tqVal);
      maVal = getMotorCurrent (f, tqVal);
      power = mvVal * maVal / (f.controllerEfficiency.value /100);
      baVal = getBatteryAmps (f, f1, power);
      bvVal = getBatteryVolts (baVal, power);
      rgVal = ((f.puekertNum.value/ pow (baVal, f.puekertExp.value)) * speed);
      if (f.dodPercent.value > 0 && f.dodPercent.value < 100)
        rgVal = (f.dodPercent.value/100) * rgVal;
      whVal = (power / speed);
     if (whVal!=0 && f.kwh.value!=0)
        costVal = f.kwh.value / (1000.0/whVal) ;
     else
        costVal = 0;
     if (costVal!=0 && f.miles.value!=0)
        tripVal = costVal * f.miles.value;
     else
        tripVal = 0;
      var totRes        = f1.totalResistance.value * 1;      
  }
  
  torque.value      = TrimDecimals(tqVal,2);
  rpm.value         = TrimDecimals(rpVal,0);
  motorVolts.value  = TrimDecimals(mvVal,0);
  motorAmps.value   = TrimDecimals(maVal,0);
  batteryAmps.value = TrimDecimals(baVal,2);
  batteryVolts.value= TrimDecimals(bvVal,1);
  range.value       = TrimDecimals(rgVal,1);
  whpermi.value     = TrimDecimals(whVal,1);
  f1.totalResistance.value = TrimDecimals(totRes, 4);
  costmi.value      = TrimDecimals(costVal,3);
  costtrip.value    = TrimDecimals(tripVal,2);
}

function calcTable (f, f6, gearRatio, f1)
{
  calcTableRow (f, f6, f.totalDrag10, gearRatio, 10, f1.torque10, f1.rpm10, f1.motorAmps10, f1.motorVolts10, f1.batteryAmps10, f1.batteryVolts10, f1.range10, f1.whpermi10, f1.costmi10, f1.commute10);
  calcTableRow (f, f6, f.totalDrag20, gearRatio, 20, f1.torque20, f1.rpm20, f1.motorAmps20, f1.motorVolts20, f1.batteryAmps20, f1.batteryVolts20, f1.range20, f1.whpermi20, f1.costmi20, f1.commute20);
  calcTableRow (f, f6, f.totalDrag30, gearRatio, 30, f1.torque30, f1.rpm30, f1.motorAmps30, f1.motorVolts30, f1.batteryAmps30, f1.batteryVolts30, f1.range30, f1.whpermi30, f1.costmi30, f1.commute30);
  calcTableRow (f, f6, f.totalDrag40, gearRatio, 40, f1.torque40, f1.rpm40, f1.motorAmps40, f1.motorVolts40, f1.batteryAmps40, f1.batteryVolts40, f1.range40, f1.whpermi40, f1.costmi40, f1.commute40);
  calcTableRow (f, f6, f.totalDrag50, gearRatio, 50, f1.torque50, f1.rpm50, f1.motorAmps50, f1.motorVolts50, f1.batteryAmps50, f1.batteryVolts50, f1.range50, f1.whpermi50, f1.costmi50, f1.commute50);
  calcTableRow (f, f6, f.totalDrag60, gearRatio, 60, f1.torque60, f1.rpm60, f1.motorAmps60, f1.motorVolts60, f1.batteryAmps60, f1.batteryVolts60, f1.range60, f1.whpermi60, f1.costmi60, f1.commute60);
  calcTableRow (f, f6, f.totalDrag70, gearRatio, 70, f1.torque70, f1.rpm70, f1.motorAmps70, f1.motorVolts70, f1.batteryAmps70, f1.batteryVolts70, f1.range70, f1.whpermi70, f1.costmi70, f1.commute70);
  calcTableRow (f, f6, f.totalDrag80, gearRatio, 80, f1.torque80, f1.rpm80, f1.motorAmps80, f1.motorVolts80, f1.batteryAmps80, f1.batteryVolts80, f1.range80, f1.whpermi80, f1.costmi80, f1.commute80);
  calcTableRow (f, f6, f.totalDrag90, gearRatio, 90, f1.torque90, f1.rpm90, f1.motorAmps90, f1.motorVolts90, f1.batteryAmps90, f1.batteryVolts90, f1.range90, f1.whpermi90, f1.costmi90, f1.commute90);
}

function calcTotalResistance (f,f1)
{
  with (Math)
  {
    var i;
    var resistance;

    resistance = 0;

    for (i = 0; i < f.numString.value; i++)
    {
      resistance += 1 / (f.batteryResistance.value * f1.numBatteries.value);
    }

    f1.totalResistance.value = ((1 / resistance)).toFixed(4);
  }
}

function calcResults (d, setcookie)
{
  var cnt = 0;
  var f;
  var errors = '';
  
  f = d.getElementsByName("vehicle_type");
  if (f[0].selectedIndex==0) {
    errors = 'Vehicle';
    cnt++;
  }

  f = d.getElementsByName("motor");
  if (f[0].selectedIndex==0) {
    if (errors!='')
        errors += ', ';
    errors += 'Motor';
    cnt++;
  }

  f = d.getElementsByName("battery");
  if (f[0].selectedIndex==0) {
    if (errors!='')
        errors += ', ';
    errors += 'Battery';
    cnt++;
  }

  f = d.getElementsByName("controller");
    if (f[0].selectedIndex==0) {
    if (errors!='')
        errors += ', ';
    errors += 'Controller';
    cnt++;
  }
  
  if (cnt > 0) {
    alert('First select: ' + errors);
    return;
  }
  
  setTire (d.forms[0], d.forms[6]);
  calcWeight (d.forms[0], d.forms[6]);
  calcTotalResistance (d.forms[0], d.forms[6]);
  calcAllDrag (d.forms[0], d.forms[6]);
  
  calcTable (d.forms[0], d.forms[6], d.forms[0].gearRatio1, d.forms[1]);
  calcTable (d.forms[0], d.forms[6], d.forms[0].gearRatio2, d.forms[2]);
  calcTable (d.forms[0], d.forms[6], d.forms[0].gearRatio3, d.forms[3]);
  calcTable (d.forms[0], d.forms[6], d.forms[0].gearRatio4, d.forms[4]);
  calcTable (d.forms[0], d.forms[6], d.forms[0].gearRatio5, d.forms[5]);
  calcTopSpeed (d);
  if (setcookie)
      SetCookie(d.forms[0]);
}



function getMotorVoltage (f, rpm, torque)
{
  var volts;

  volts = 0;

  with (Math)
  {
    volts = (rpm * f.motorD.value) /
        ((f.motorA.value/(pow (torque, f.motorB.value))) +
         parseFloat (f.motorC.value));
  }

  return volts;
}

function getMotorCurrent (f, torque)
{
  var amps;

  amps = 0;

  with (Math)
  {
    amps = pow ((torque / f.motorK.value), (1/f.motorN.value));
  }

  return amps;
}

function TrimDecimals(value, decimals)
{
    if (isNaN(value))      
        return '---';

  return value.toFixed(decimals);
}


function calcWeight(f,f1)
{
  f1.numBatteries.value = (f.voltage.value / f.batteryVoltage.value) * f.numString.value;

  f1.totalBatteryWeight.value = (f1.numBatteries.value * f.batteryWeight.value).toFixed(0);

  f1.totalChargerWeight.value = f.chargerWeight.value;

  f1.totalControllerWeight.value = f.controllerWeight.value;

  f1.totalMotorWeight.value = f.motorWeight.value;

  f1.weightAdded.value = parseFloat (f1.totalBatteryWeight.value) +
                        parseFloat (f1.totalChargerWeight.value) +
                        parseFloat (f1.totalControllerWeight.value) +
                        parseFloat (f1.totalMotorWeight.value) +
                        parseFloat (f.miscWeight.value);
 
  f1.totalWeight.value = parseFloat (f1.weightAdded.value) +
                        parseFloat (f.curb_weight.value) -
                        parseFloat (f.weightRemoved.value);
}



function setTire (f,f1)
{
  with (Math)
  {
    var true_diameter;
    var height;
    var inches;

    inches             = parseFloat (f.section.value) / 25.4;
    height             = inches * parseFloat (f.aspect.value) * 0.01;
    true_diameter      = (height * 2) + parseFloat (f.rim.value);
    f1.revPerMile.value = ((1 / true_diameter) * 20933).toFixed(0);
  }
}


