// Where Detail links point to
var SalesLink = 'http://www.walkeasy.com/shop/product_details.asp?';

// The URL of the "Notify Me" link.
var NotifyLink = 'http://www.walkeasy.com/interact/notify.asp';

var ChildAge = 13;                // The child/adult cutoff

var AdultMaxWeight = 250;         // Adult maximum weight
var ChildMaxWeight = 160;         // Child maximum weight

var MaxHeight = 76;               // Maximum height, in inches
var MinHeight = 36;               // Minimum height, in inches

var AdultForearmRatio = 5.15;
var ChildForearmRatio = 5.24;

var AdultGripToFloorRatio = 2.17;
var ChildGripToFloorRatio = 2.29;

function CrutchCalc() {

// In the Deduct, FullModel and HalfModel tables, the indices correspond to the person's height, 
// in inches. In the model tables, multiple model numbers for a single entry must be separated
// with the | character, with no spaces.

  Deduct = new Array();
  Deduct[36] = 2.4;   Deduct[46] = 2.7;   Deduct[56] = 3.1;   Deduct[66] = 3.4;   Deduct[76] = 3.8;
  Deduct[37] = 2.4;   Deduct[47] = 2.7;   Deduct[57] = 3.1;   Deduct[67] = 3.5;
  Deduct[38] = 2.4;   Deduct[48] = 2.8;   Deduct[58] = 3.2;   Deduct[68] = 3.5;
  Deduct[39] = 2.5;   Deduct[49] = 2.8;   Deduct[59] = 3.2;   Deduct[69] = 3.5;
  Deduct[40] = 2.5;   Deduct[50] = 2.9;   Deduct[60] = 3.2;   Deduct[70] = 3.6;
  Deduct[41] = 2.5;   Deduct[51] = 2.9;   Deduct[61] = 3.3;   Deduct[71] = 3.6;
  Deduct[42] = 2.6;   Deduct[52] = 2.9;   Deduct[62] = 3.3;   Deduct[72] = 3.6;
  Deduct[43] = 2.6;   Deduct[53] = 3.0;   Deduct[63] = 3.3;   Deduct[73] = 3.7;
  Deduct[44] = 2.6;   Deduct[54] = 3.0;   Deduct[64] = 3.4;   Deduct[74] = 3.7;
  Deduct[45] = 2.7;   Deduct[55] = 3.1;   Deduct[65] = 3.4;   Deduct[75] = 3.8;

  FullModels = new Array();
  FullModels[36] = "562";       FullModels[56] = "582";        FullModels[76] = "490";
  FullModels[37] = "562";       FullModels[57] = "582";
  FullModels[38] = "562";       FullModels[58] = "582";
  FullModels[39] = "562";       FullModels[59] = "582";
  FullModels[40] = "562";       FullModels[60] = "582|495";
  FullModels[41] = "562";       FullModels[61] = "471|495";
  FullModels[42] = "562|572";   FullModels[62] = "471|495";
  FullModels[43] = "562|572";   FullModels[63] = "471|495";
  FullModels[44] = "562|572";   FullModels[64] = "471|495";
  FullModels[45] = "572";       FullModels[65] = "471|495";
  FullModels[46] = "572";       FullModels[66] = "471|495";
  FullModels[47] = "572";       FullModels[67] = "471|490";
  FullModels[48] = "572";       FullModels[68] = "471|490";
  FullModels[49] = "572";       FullModels[69] = "471|490";
  FullModels[50] = "572|582";   FullModels[70] = "471|490";
  FullModels[51] = "572|582";   FullModels[71] = "471|490";
  FullModels[52] = "572|582";   FullModels[72] = "471|490";
  FullModels[53] = "582";       FullModels[73] = "471|490";
  FullModels[54] = "582";       FullModels[74] = "490";
  FullModels[55] = "582";       FullModels[75] = "490";

  HalfModels = new Array();
  HalfModels[36] = "564";       HalfModels[56] = "584";         HalfModels[76] = "494";
  HalfModels[37] = "564";       HalfModels[57] = "584";
  HalfModels[38] = "564";       HalfModels[58] = "584";
  HalfModels[39] = "564";       HalfModels[59] = "584";
  HalfModels[40] = "564";       HalfModels[60] = "584";
  HalfModels[41] = "564";       HalfModels[61] = "584";
  HalfModels[42] = "564|574";   HalfModels[62] = "584|450";
  HalfModels[43] = "564|574";   HalfModels[63] = "584|450";
  HalfModels[44] = "564|574";   HalfModels[64] = "450";
  HalfModels[45] = "574";       HalfModels[65] = "450";
  HalfModels[46] = "574";       HalfModels[66] = "450";
  HalfModels[47] = "574";       HalfModels[67] = "450";
  HalfModels[48] = "574";       HalfModels[68] = "450";
  HalfModels[49] = "574";       HalfModels[69] = "450";
  HalfModels[50] = "574|584";   HalfModels[70] = "450";
  HalfModels[51] = "574|584";   HalfModels[71] = "450";
  HalfModels[52] = "574|584";   HalfModels[72] = "450";
  HalfModels[53] = "584";       HalfModels[73] = "450|494";
  HalfModels[54] = "584";       HalfModels[74] = "450|494";
  HalfModels[55] = "584";       HalfModels[75] = "494";

  CForm = document.CrutchForm;

  var AgeLen    = CForm.age_field.value.length;
  var WeightLen = CForm.weight_field.value.length;
  var FeetLen   = CForm.height_feet_field.value.length;
  var InchesLen = CForm.height_inches_field.value.length;

  var Age    = CForm.age_field.value * 1;
  var Weight = CForm.weight_field.value * 1;
  var Feet   = CForm.height_feet_field.value * 1;
  var Inches = CForm.height_inches_field.value * 1;
  var Height = (Feet * 12) + Inches;

  var MaxWeight = AdultMaxWeight;
  var ForearmRatio = AdultForearmRatio;
  var GripToFloorRatio = AdultGripToFloorRatio;
  var CrutchAgeType = "";

  if (Age <= ChildAge) {
    MaxWeight = ChildMaxWeight;
    ForearmRatio = ChildForearmRatio;
    GripToFloorRatio = ChildGripToFloorRatio;
    CrutchAgeType = " pediatric";
  }

  CuffType = "half";
  if (CForm.cuff_preference[0].checked
  ||  CForm.cuff_preference[2].checked)
  {
    CuffType = "full";
  }

  var TooHeavy =
  "Unfortunately, our" + CrutchAgeType + " crutches have only been tested to support " +
  "up to " + MaxWeight + " lbs. We are currently evaluating the feasibility of developing a " +
  "heavier-duty crutch. If you are interested in being notified when such a product becomes " +
  "available, please click the \"Notify Me\" link below.";

  var TooTall =
  "Walk Easy does not carry " + CuffType + " cuff forearm crutches for adults taller than "+
  "6' 4\" (193 cm.). We are evaluating the feasibility of developing a more rigid " +
  CuffType + " cuff forearm crutch suitable for taller individuals. If you are interested in being " +
  "notified when such a product becomes available, please click the \"Notify Me\" link below.";

  var TooShort =
  "Walk Easy does not carry forearm crutches for children 3' 0\" (92 cm.) or shorter. It is our " +
  "opinion that such a child should learn proper gait with a specially-designed pediatric walker. We " +
  "are currently evaluating the feasibility of developing such a product. If you are interested in " +
  "being notified when such a product becomes available, please click the \"Notify Me\" link below.";


// ------------------------------------------------------------------------------------- //

// Get the indices of the notification and sales links on the page.
  ModelIndices = CForm.ModelIndices.value;
  ModelIndex   = ModelIndices.split("|");
  NotifyIndex  = CForm.NotifyIndex.value;

// Initialize the result message, links and description boxes.
  CForm.ResultMsg.value = "";
  document.NotifyTag.src = "../assets/interact/notifyoff.gif";
  document.links[NotifyIndex].href = NotifyLink;
  for (i=0; i < ModelIndex.length; i++) {
    document.links[ModelIndex[i]].href = SalesLink;
    eval('CForm.Description' + i + '.value=""');
    eval('document.Pic' + i + '.src="../assets/products/nopic.jpg"');
  }

// If all the boxes are filled in, perform a calculation
  if (AgeLen    > 0
  &&  WeightLen > 0
  &&  FeetLen   > 0
  &&  InchesLen > 0) {

    if (CForm.weight_field.value > MaxWeight) {
      CForm.ResultMsg.value = TooHeavy;
      document.links[NotifyIndex].href =
      NotifyLink + 
      "?prob=1&age=" + Age + "&height=" + Height + "&weight=" + Weight + "&cuff=" + CuffType;
      document.NotifyTag.src = "../assets/interact/notifyon.gif";
      return;
    }

    if (Height > MaxHeight) {
      CForm.ResultMsg.value = TooTall;
      document.links[NotifyIndex].href =
      NotifyLink + 
      "?prob=2&age=" + Age + "&height=" + Height + "&weight=" + Weight + "&cuff=" + CuffType;
      document.NotifyTag.src = "../assets/interact/notifyon.gif";
      return;
    }

    if (Height < MinHeight) {
      CForm.ResultMsg.value = TooShort;
      document.links[NotifyIndex].href =
      NotifyLink + 
      "?prob=3&age=" + Age + "&height=" + Height + "&weight=" + Weight + "&cuff=" + CuffType;
      document.NotifyTag.src = "../assets/interact/notifyon.gif";
      return;
    }

    var Forearm     = ((Height / ForearmRatio) - Deduct[Height]);
    var GripToFloor = (Height / GripToFloorRatio);
    Forearm         = Math.round(Forearm * 10) / 10;
    GripToFloor     = Math.round(GripToFloor * 10) / 10;

    if (CuffType == "half") Models = HalfModels[Height].split("|");
    else                    Models = FullModels[Height].split("|");

    var s = "";
    if (Models.length > 1) s = "s";

    Message =
    "We have estimated your ideal cuff-to-grip measurement at " + Forearm + " inches and your " +
    "actual grip-to-floor length, without shoes, at " + GripToFloor + " inches (depending on your " +
    "gait and footwear, add 2 to 3 inches to this length to determine usable length).\n\n" +

    "Based on the information submitted above, we recommend the following model" + s + ":";

    CForm.ResultMsg.value = Message;

    for (i=0; i < Models.length; i++) {
      document.links[ModelIndex[i]].href = SalesLink + "ProductCode=" + Models[i];
      eval('CForm.Description' + i + '.value="Model #' + Models[i] + '"');
      eval('document.Pic' + i + '.src="../assets/products/' + Models[i] + '_small.jpg"');
    }
  }
}