var contactUrl = 'http://competition.ca/cinew-contact.htm';

var quotes = {
  // quote floors, in thousands
  'custom': {
      '1': 100,
      '2': 112,
      '3': 126,
      '4': 140,
      '5': 154,
      '6': 168,
      '7': 182,
      '8': 196,
      '9': 210,
     '10': 224,
     '11': 248,
     '12': 252,
     '13': 266,
     '14': 280,
     '15': 294,
     '16': 308,
     '17': 322,
     '18': 336,
     '19': 350,
     '20': 364,
     '21': 378,
     '22': 392,
     '23': 406,
     '24': 420,
     '25': 434,
     '30': 504,
     '35': 574,
     '40': 644,
     '45': 714,
     '50': 784,
     '55': 854,
     '60': 924,
     '65': 994,
     '70': 1064,
     '75': 1134,
     '80': 1204,
     '85': 1274,
     '90': 1344,
     '95': 1414,
    '100': 1484
  },

  'silver-antique': {
     '1': 100,
     '2': 110,
     '3': 110,
     '4': 110,
     '5': 110,
     '6': 110,
     '7': 110,
     '8': 110,
     '9': 116,
    '10': 124,
    '11': 132,
    '12': 140,
    '13': 148,
    '14': 156,
    '15': 164,
    '16': 172,
    '17': 180,
    '18': 188,
    '19': 196,
    '20': 204,
    '21': 212,
    '22': 220,
    '23': 228,
    '24': 236,
    '25': 244,
    '30': 284,
    '35': 324,
    '40': 364,
    '45': 404,
    '50': 444,
    '55': 484,
    '60': 524,
    '65': 564,
    '70': 604,
    '75': 644,
    '80': 684,
    '85': 724,
    '90': 764,
    '95': 804,
   '100': 844
  },

  'silver-special': {
     '1': 100,
     '2': 100,
     '3': 100,
     '4': 111,
     '5': 123,
     '6': 135,
     '7': 147,
     '8': 159,
     '9': 171,
    '10': 183,
    '11': 195,
    '12': 207,
    '13': 219,
    '14': 231,
    '15': 243,
    '16': 255,
    '17': 267,
    '18': 279,
    '19': 291,
    '20': 303,
    '21': 315,
    '22': 327,
    '23': 339,
    '24': 351,
    '25': 363,
    '30': 423,
    '35': 483,
    '40': 543,
    '45': 603,
    '50': 663,
    '55': 723,
    '60': 783,
    '65': 843,
    '70': 903,
    '75': 963,
    '80': 1023,
    '85': 1083,
    '90': 1143,
    '95': 1203,
   '100': 1263
  }
};

function setupQQ() {
  var qq = $('#quick-quote');

  var form = $('<strong>Quick Quote</strong><form action="#">' +
    '<label>Coverage Plan:<br> <select name="plan">' +
      '<optgroup label="Silver Wheel Plan™"><option value="silver-special">Special Interest (15-19 years old)</option><option value="silver-antique">Antique (20 years and older)</option></optgroup>' +
      '<option value="custom">Custom Wheel Plan™</option>' +
    '</select></label><br>' +
    '<label>Vehicle Value:<br> <input name="value"></label><br>' +
    '<input type="submit" value="Fetch Quote">' +
  '</form>');

  form.submit(fetchQuote);

  qq.html(form);
}

function fetchQuote() {
  var qq   = $('#quick-quote');
  var form = $(this);

  var plan    = form.find('select[name=plan]').val();
  // the desired coverage
  var desired = form.find('input[name=value]').val();

  // the result HTML
  var result;

  if (!desired) {
    result = $('<p>You must enter a coverage value.</p>');
  } else if(desired > 100000) {
    result = $('<p>Please <a id="qq-contact">contact us</a> for quotes on coverage of more than $100,000.</p>');
    result.children('#qq-contact').attr('href', contactUrl);
  } else {
    var premium;
    var coverage = desired;
    var coverages = quotes[plan];

    if(desired < 1000) {
      premium = coverages[1];
      coverage = 1000;
    } else if(coverages[desired / 1000]) {
      // we have an exact value
      premium = coverages[desired / 1000];
    } else {
      // interpolate between the two coverage possibilities bracketing the desired value

      // find the premium for the coverage level above the value requested
      var covCeil = Math.ceil(desired / 1000);
      var premiumCeil = coverages[covCeil];
      while(!premiumCeil) {
        covCeil++;
        premiumCeil = coverages[covCeil];
      }

      // find the premium for the coverage level below the value requested
      var covFloor = Math.floor(desired / 1000);
      var premiumFloor = coverages[covFloor];
      while(!premiumFloor) {
        covFloor--;
        premiumFloor = coverages[covFloor];
      }

      var slope      = (premiumCeil - premiumFloor) / (covCeil - covFloor);
      var yIntercept = premiumFloor - slope * covFloor;

      premium = slope * (coverage / 1000) + yIntercept;
    }

    result = $('<strong>Quote</strong><table>' +
          '<tr><th>Plan</th><td id="qq-plan"></td></tr>' +
          '<tr><th>Coverage</th><td id="qq-coverage">$</td></tr>' +
          '<tr><th>Annual Premium</th><td id="qq-premium"></td></tr>' +
        '</table>' +
        '<p>Please <a id="qq-contact">contact us</a> for more information.</p>'
        );

    var planName;

    if (plan == 'custom') {
      planName = 'Custom Wheel Plan™';
    } else if (plan == 'silver-special') {
      planName = 'Silver Wheel Plan™ (Special Interest)';
    } else if (plan == 'silver-antique') {
      planName = 'Silver Wheel Plan™ (Antique)';
    }

    result.find('#qq-plan').text(planName);
    result.find('#qq-coverage').append(coverage);
    result.find('#qq-premium').text(premium);
    result.find('#qq-contact').attr('href', contactUrl);
  }

  qq.html(result);

  var another = $('<input type="submit" id="qq-another" value="Another Quote">');
  another.click(setupQQ);
  qq.append(another);

  return false;
}

$(document).ready(function() {
  setupQQ();
});