// Magic Trikes USA
// Price Calculation
// Imported by products.html, calcprice.html, and dealerprice.html

var validdate = "July 29, 2008";

// Price Calculation
var markup = 0.25;     // markup from distributor cost to retail
var trikecost = 1419;  // trike without engine
var tirecost = 94;     // 3 tires
var v1mountcost = 122; // extra cost of Victor 1 or Victor 1+ engine mount

// Distributor labor costs
var setupwing = 60;      // Setup shortpacked wing
var setuptrike = 150;    // Assemble trike
var installengine = 135; // Install engine
var installinstruments = 75; // Install Instruments
var packwing = 90;       // Build box and pack wing
var packtrike = 90;      // Build box and pack trike

discount = new Array (3);  // dealer discount from retail
  discount[0] = 0.00;  // Customer price
  discount[1] = 0.05;  // Dealer, 1st unit
  discount[2] = 0.10;  // Dealer, 2nd unit
  discount[3] = 0.15;  // Dealer, 3+ units

wingcost = new Array (2); // Wing, USD
  wingcost[0] = 0;    // Without wing
  wingcost[1] = 2828; // Spirit
  wingcost[2] = 3055; // Laser
  wingcost[3] = 3100; // Cyclone

enginecost = new Array (6);  // engine and accessories
  enginecost[0] = 0;    // without engine
  enginecost[1] = 2691; // Mini 2 Plus
  enginecost[2] = 3465; // Mini 3
  enginecost[3] = 3511; // Rotax 447
  enginecost[4] = 4703; // Victor 1
  enginecost[5] = 4973; // Victor 1 Plus

woodpropcost = new Array (6); // 2-blade wood propeller
  woodpropcost[0] = 0;   // without engine
  woodpropcost[1] = 250; // Mini 2 Plus
  woodpropcost[2] = 250; // Mini 3
  woodpropcost[3] = 295; // Rotax 447
  woodpropcost[4] = 295; // Victor 1
  woodpropcost[5] = 295; // Victor 1 Plus

carbonpropcost = new Array (6); // 3-blade carbon propeller
  carbonpropcost[0] = 0;   // without engine
  carbonpropcost[1] = 650; // Mini 2 Plus
  carbonpropcost[2] = 650; // Mini 3
  carbonpropcost[3] = 900; // Rotax 447
  carbonpropcost[4] = 900; // Victor 1
  carbonpropcost[5] = 900; // Victor 1 Plus

instrumentcost = new Array (3); // instrument prices
  instrumentcost[0] = 0; // without instruments
  instrumentcost[1] = 268; // analog
  instrumentcost[2] = 457; // digital

function calc_cost() { // Calculate distributor cost
  var c=0; // 1 for each item included, 0 otherwise

  if (pr1!=2) { // trike
    c += 1*trikecost;
    if (e1>=3){c += v1mountcost;} // extra cost for Victor 1 engine mount
    c += 1*tirecost;
    c += 1*enginecost[e1];
    if (p1==1) {c += 1*woodpropcost[e1];}   // 2-blade wood propeller
    if (p1==2) {c += 1*carbonpropcost[e1];} // 3-blade carbon propeller
    c += 1*instrumentcost[i1];
  }
  if (pr1!=1) { // wing
    c += 1*wingcost[w1];
  }

  return c;
}

function calc_setup(s) { // Calculate distributor setup labor cost
  var c=0; // 1 for each item included, 0 otherwise
  if (pr1!=2) { // trike
    if (s) {
      c += 1*setuptrike;  // Assemble trike
	  if (e1>0) {c += 1*installengine;}  // Install engine
	  if (i1>0) {c += 1*installinstruments;} // Install Instruments
	}
	c += 1*packtrike;  // Build box and pack trike
  }
  if (pr1!=1) { // wing
    if (s) {c += 1*setupwing;}  // Setup shortpacked wing
	c += 1*packwing;   // Build box and pack wing
  }
  return c;
}

function dealer_price() {
  var dcost = calc_cost();    // distributor cost
  var dsetup = calc_setup(s1==0);  // distributor setup labor cost
  return Math.round(dcost*(1+1*markup)*(1-discount[di1])+1*dsetup);
}

function customer_price() {
  var dcost = calc_cost();    // distributor cost
  var dsetup = calc_setup(s1!=2);  // distributor setup labor cost
  return Math.round(dcost*(1+1*markup)+1*dsetup);
}

function round(x,p) { // round x to precision p
  var p1 = Math.round(1/p);
  return Math.round(x*p1)/p1;
  }

// global variables
var w1;      // wing select
var e1;      // engine select
var p1;      // propeller select
var i1;      // instruments select
var s1;      // kit or assembled
var di1=0;   // discount
var pr1=0;   // product: trike, wing, both

