
function removeElements() {
  document.getElementById("rcl").style.display="none";
  document.getElementById("medipac").style.display="none";
}

function showRclQuote() {
  document.getElementById("rcl").style.display="block";
  document.getElementById("medipac").style.display="none";
}

function showMedipacQuote() {
  document.getElementById("medipac").style.display="block";
  document.getElementById("rcl").style.display="none";
}

function assignDiscountPerc(theField) {
  var province = getComboValue(theField);
  if((province == 'P.E.I.') || (province == 'Northwest Territories') || (province == 'Yukon Territories') || (province == 'Nunavut')){
    document.m.discountPerc.selectedIndex = 1;  // coresponds to 5% discount
  }else{
    document.m.discountPerc.selectedIndex = 0;  // coresponds to No discount Main Season
  }
}

function dollarCommaFormatted(amount) {
  // Add a dot if the amount is an integer
  var str = "" + Math.round(amount*100);
  var len = str.length;
  amount = (str=="0")?"":(str.substring(0,len-2)+"."+str.substring(len-2,len));

  var delimiter = ",";               // replace comma if desired 
  var a = amount.split('.',2)
  var d = a[1];
  var i = parseInt(a[0]);
  if(isNaN(i)) { return ''; }
  var minus = '';
  if(i < 0) { minus = '-'; }
  i = Math.abs(i);
  var n = new String(i);
  var a = [];
  while(n.length > 3) {
  var nn = n.substr(n.length-3);
    a.unshift(nn);
	n = n.substr(0,n.length-3);
  }
  if(n.length > 0) { a.unshift(n); }
  n = a.join(delimiter);
  if(d.length < 1) { amount = n; }
  else { amount = n + '.' + d; }
  amount = minus + amount;
  return "$" + amount;
}

function listboxItemSelected(oList1,oList2){
  if(oList2!=null){
    clearComboOrList(oList2);
    if(oList1.selectedIndex == -1){
      oList2.options[oList2.options.length] = new Option('Please make a selection from the list', '');
    } else {
      fillListbox(oList2, oList1.name + '=' + oList1.options[oList1.selectedIndex].value);
    }
  }
  calcPremium();
}

function clearComboOrList(oList){
  for (var i = oList.options.length - 1; i >= 0; i--){
    oList.options[i] = null;
  }
  oList.selectedIndex = -1;
//  if (oList.onchange) oList.onchange();
}

function fillListbox(oList, vValue) {
  if(vValue != '') {
    if(assocArray[vValue]) {
      var arrX = assocArray[vValue];
      for(var i = 0; i < arrX.length; i = i + 2){
        if(arrX[i] != 'EOF') oList.options[oList.options.length] = new Option(arrX[i + 1], arrX[i]);
	  }
//	  if(oList.options.length == 1){
	    oList.selectedIndex=0;
//	    if (oList.onchange)	oList.onchange();
//	  }
    }else {
      oList.options[0] = new Option('None found', '');
    }
  }
}

if(!assocArray) {
  var assocArray = new Object();

  assocArray["planType=Preferred Plus"] = new Array(
    "0","$ 0",
    "99","$ 99",
    "1000","$ 1,000",
    "5000","$ 5,000",
    "10000","$ 10,000",
    "EOF");
	
  assocArray["planType=Preferred"] = new Array(
    "0","$ 0",
    "250","$ 250",
    "1000","$ 1,000",
    "5000","$ 5,000",
    "10000","$ 10,000",
    "EOF");
	
  assocArray["planType=Standard"] = new Array(
    "0","$ 0",
    "250","$ 250",
    "1000","$ 1,000",
    "5000","$ 5,000",
    "10000","$ 10,000",
    "EOF");
}



// This function toggles off the pared check box for a particular question. 
// Thus only one check box can be true 
function toggleOff(clicked,uncheck) {
  if (clicked.checked) {
     uncheck.checked = false;
  }
}

// This function modify a number into two digits floating number
function Modify(value) {
  var str = "" + Math.round(value*100);
  var len = str.length;
  
  var returnValue;
  if(str == "0") {
    returnValue = '';
  }else if(str.substring(0,len-2) == ''){ // no integer part  
    returnValue = "0." + str.substring(len-2,len);
  }else{
    returnValue = str.substring(0,len-2) + "." + str.substring(len-2,len);
  }
  return returnValue;

//  return (str=="0")?"":(str.substring(0,len-2)+"."+str.substring(len-2,len));
}


/////////calcPremium1()
function calcAnnualAddOnRate(planType, deductible, age) {

  var rows = new Array();
  rows[0] = 'null';
  rows[1] = '50,65,81,98,116,166,258,314';     //Preferred PLUS $99 Deductible
  rows[2] = '46,59,72,83,100,146,225,274';      //Preferred PLUS $1000
  rows[3] = '41,50,59,68,81,116,173,212';       //Preferred PLUS $5000
  rows[4] = '37,45,52,59,69,98,144,176';       //Preferred PLUS $10000
  rows[5] = '68,81,110,152,177,258,366,440';    //Preferred $250
  rows[6] = '55,65,97,123,143,203,300,383';    //Preferred $1000
  rows[7] = '45,53,78,98,113,164,237,295';     //Preferred $5000
  rows[8] = '41,49,69,86,99,142,202,251';      //Preferred $10000
  rows[9] = '118,129,138,207,281,426,607,931';  //Standard $250
  rows[10] = '100,115,120,178,243,369,525,814';  //Standard $1000
  rows[11] = '77,87,95,142,194,289,406,616';     //Standard $5000
  rows[12] = '68,77,84,124,167,248,347,524';     //Standard $10000

  var row = 0;
  if((planType == "Preferred Plus") && ((deductible == 0) || (deductible == 99))) {row = 1;}
  if((planType == "Preferred Plus") && (deductible == 1000)) {row = 2;}
  if((planType == "Preferred Plus") && (deductible == 5000)) {row = 3;}
  if((planType == "Preferred Plus") && (deductible == 10000)) {row = 4;}
  if((planType == "Preferred") && ((deductible == 0) || (deductible == 250))) {row = 5;}
  if((planType == "Preferred") && (deductible == 1000)) {row = 6;}
  if((planType == "Preferred") && (deductible == 5000)) {row = 7;}
  if((planType == "Preferred") && (deductible == 10000)) {row = 8;}
  if((planType == "Standard") && ((deductible == 0) || (deductible == 250))) {row = 9;}
  if((planType == "Standard") && (deductible == 1000)) {row = 10;}
  if((planType == "Standard") && (deductible == 5000)) {row = 11;}
  if((planType == "Standard") && (deductible == 10000)) {row = 12;}  
  
  // Get the row from rows array with the premium 
  var premiumRow = rows[row];

  // Put all premiums from a premiumRow into an array  
  var dividePremium = premiumRow.split(',');

  // Get the right Premium for the Age
  var premium = 0;
  if (age <= 55) premium = dividePremium[0];
  if ( (age >= 56) && (age <= 60) ) premium = dividePremium[1];
  if ( (age >= 61) && (age <= 65) ) premium = dividePremium[2];
  if ( (age >= 66) && (age <= 70) ) premium = dividePremium[3];
  if ( (age >= 71) && (age <= 75) ) premium = dividePremium[4];
  if ( (age >= 76) && (age <= 79) ) premium = dividePremium[5];
  if ( (age >= 80) && (age <= 85) ) premium = dividePremium[6];
  if (age >= 86) premium = dividePremium[7];

  premium = parseInt(premium);
  return premium;
}

function calcLongTripRate(planType, deductible, tripApplied, age) {

var rows = new Array();
rows[0] = 'null';
//Preferred PLUS $99 Plan Type
rows [1] = '74,94,124,171,231,338,601,1014';
rows [2] = '87,124,143,202,274,405,728,1198';
rows [3] = '110,141,163,241,317,482,820,1364';
rows [4] = '144,186,217,324,401,583,1038,1535';
rows [5] = '156,194,231,345,449,655,1150,1656';
rows [6] = '173,199,237,360,462,699,1241,1798';
rows [7] = '184,219,245,392,503,720,1330,1984';
rows [8] = '202,265,290,445,566,865,1554,2240';
rows [9] = '222,286,303,478,616,964,1662,2393';
rows [10] = '237,291,322,507,672,998,1761,2522';
rows [11] = '250,326,368,541,719,1027,1845,2690';
rows [12] = '273,369,404,593,799,1159,2099,2934';
rows [13] = '289,388,444,631,852,1232,2266,3157';
rows [14] = '299,399,467,675,912,1299,2364,3237';
rows [15] = '321,438,507,710,963,1358,2463,3367';
rows [16] = '355,485,607,763,1029,1462,2634,3846';
rows [17] = '388,529,654,808,1098,1619,2799,4062';
rows [18] = '424,552,673,824,1153,1646,2849,4139';
rows [19] = '507,642,740,907,1236,1775,3212,4555';
rows [20] = '544,689,830,977,1375,1942,3378,4799';
rows [21] = '579,740,909,1058,1542,2079,3577,5102';
//Preferred PLUS $1000 Plan Type
rows [22] = '70,92,116,169,221,324,544,930';
rows [23] = '83,120,142,199,265,399,654,1034';
rows [24] = '107,137,161,237,290,444,725,1148';
rows [25] = '126,163,189,284,352,510,908,1344';
rows [26] = '135,170,202,302,393,574,998,1450';
rows [27] = '152,180,208,319,399,612,1038,1576';
rows [28] = '161,191,214,344,440,630,1164,1737';
rows [29] = '181,232,254,389,495,757,1350,1960';
rows [30] = '194,250,267,418,538,844,1454,2094';
rows [31] = '208,255,282,444,588,877,1540,2199';
rows [32] = '219,285,322,473,630,898,1614,2350';
rows [33] = '238,323,354,519,699,1014,1846,2567';
rows [34] = '253,340,388,552,746,1078,1974,2756';
rows [35] = '270,354,399,591,798,1142,2069,2833';
rows [36] = '281,383,445,622,843,1188,2155,2947';
rows [37] = '311,425,534,667,898,1279,2299,3364';
rows [38] = '340,463,573,708,964,1417,2453,3552';
rows [39] = '371,483,584,721,998,1461,2493,3625';
rows [40] = '449,562,648,793,1081,1553,2810,3986';
rows [41] = '476,599,725,855,1203,1698,2956,4201';
rows [42] = '507,647,795,926,1349,1819,3119,4464';
//Preferred PLUS $5000 Plan Type
rows [43] = '64,78,89,131,170,260,415,708';
rows [44] = '78,97,110,160,199,307,499,798';
rows [45] = '86,110,124,187,224,342,560,890';
rows [46] = '97,126,146,219,272,393,698,1037';
rows [47] = '105,131,157,233,303,442,777,1118';
rows [48] = '116,139,160,246,312,472,831,1216';
rows [49] = '125,148,165,265,340,486,898,1340';
rows [50] = '139,180,194,299,382,584,1048,1512';
rows [51] = '150,187,207,322,416,651,1122,1603';
rows [52] = '160,194,218,343,454,677,1189,1698';
rows [53] = '169,220,248,366,486,693,1245,1799';
rows [54] = '183,249,274,399,539,782,1424,1981';
rows [55] = '194,262,299,426,575,831,1530,2130';
rows [56] = '208,274,315,456,607,881,1595,2184';
rows [57] = '217,296,344,480,650,917,1662,2361';
rows [58] = '240,327,405,506,695,987,1766,2592';
rows [59] = '262,358,442,546,743,1093,1893,2747';
rows [60] = '286,373,454,556,778,1127,1923,2796';
rows [61] = '347,433,499,613,834,1198,2167,3074';
rows [62] = '367,465,560,659,928,1310,2280,3240';
rows [63] = '389,499,614,714,1041,1399,2414,3444';
//Preferred Plus $10000 Plan Type
rows [64] = '60,73,84,121,156,239,387,641';
rows [65] = '73,88,99,147,188,285,457,731';
rows [66] = '79,101,111,172,202,315,514,814';
rows [67] = '89,116,134,201,249,363,645,948';
rows [68] = '97,120,143,215,280,407,716,1023';
rows [69] = '107,128,149,223,288,435,771,1113';
rows [70] = '115,137,153,244,313,448,827,1224';
rows [71] = '128,165,180,277,353,539,965,1379';
rows [72] = '138,179,192,298,382,599,1029,1478';
rows [73] = '148,183,199,315,418,622,1095,1552';
rows [74] = '157,199,225,333,445,638,1142,1661';
rows [75] = '170,230,251,368,498,721,1311,1808';
rows [76] = '180,242,277,392,529,766,1408,1940';
rows [77] = '192,251,291,421,557,812,1468,1999';
rows [78] = '198,273,315,442,599,845,1531,2070';
rows [79] = '214,302,379,473,639,905,1637,2375';
rows [80] = '242,329,404,499,684,999,1742,2512';
rows [81] = '263,344,420,512,716,1038,1771,2558';
rows [82] = '319,399,459,565,769,1099,1997,2812';
rows [83] = '338,428,516,607,855,1207,2099,2965';
rows [84] = '361,460,566,658,959,1292,2222,3139';
//Preferred $250
rows [85] = '144,180,197,304,363,553,830,1232';
rows [86] = '175,219,240,380,442,664,1009,1379';
rows [87] = '186,254,273,424,484,719,1159,1521';
rows [88] = '217,286,319,471,544,787,1396,1786';
rows [89] = '244,295,334,501,605,881,1552,1919';
rows [90] = '269,321,349,530,635,948,1662,2028';
rows [91] = '286,351,366,572,672,972,1798,2291';
rows [92] = '320,429,438,654,770,1167,2123,2598';
rows [93] = '334,440,454,703,849,1299,2246,2810';
rows [94] = '365,445,488,747,912,1337,2390,2916';
rows [95] = '388,515,536,786,986,1398,2512,3043';
rows [96] = '421,572,615,893,1087,1560,2855,3404';
rows [97] = '453,608,663,939,1160,1632,3099,3652';
rows [98] = '494,630,699,995,1248,1747,3176,3754';
rows [99] = '505,694,766,1052,1312,1875,3328,3907';
rows [100] = '557,762,913,1090,1441,1984,3564,4484';
rows [101] = '608,831,982,1177,1513,2191,3785,4727';
rows [102] = '652,869,999,1217,1590,2263,3846,4807';
rows [103] = '804,999,1098,1349,1698,2428,4357,5284';
rows [104] = '847,1082,1228,1446,1899,2667,4583,5571';
rows [105] = '913,1147,1346,1568,2131,2858,4856,5902';
//Preferred $1000 Plan Type
rows [106] = '125,156,189,272,317,483,726,1077';
rows [107] = '154,191,210,325,387,581,883,1216';
rows [108] = '163,222,239,370,423,629,1014,1331';
rows [109] = '189,249,279,404,476,688,1221,1563';
rows [110] = '214,258,293,436,530,770,1350,1679';
rows [111] = '236,281,304,464,556,822,1453,1775';
rows [112] = '249,308,320,499,588,851,1574,2005';
rows [113] = '278,376,383,572,673,1021,1857,2273';
rows [114] = '293,386,398,616,742,1138,1966,2459';
rows [115] = '319,394,425,653,798,1170,2072,2551';
rows [116] = '334,445,469,687,861,1227,2198,2663';
rows [117] = '368,501,537,782,951,1365,2494,2979';
rows [118] = '395,533,580,822,1014,1428,2719,3196';
rows [119] = '433,551,612,871,1093,1529,2779,3285';
rows [120] = '445,606,669,920,1147,1641,2910,3419';
rows [121] = '487,667,799,953,1260,1736,3119,3923';
rows [122] = '534,723,858,1030,1317,1917,3311,4137';
rows [123] = '558,759,877,1065,1389,1981,3365,4206';
rows [124] = '704,878,962,1181,1488,2124,3812,4623';
rows [125] = '740,947,1065,1265,1661,2333,3999,4875';
rows [126] = '799,1004,1171,1372,1867,2499,4249,5164';
//Preferred $5000 Plan Type
rows [127] = '97,121,133,211,245,373,561,832';
rows [128] = '118,148,162,257,299,448,681,931';
rows [129] = '125,171,184,287,326,485,782,1013';
rows [130] = '147,192,217,319,367,530,942,1205';
rows [131] = '165,199,223,338,404,594,1038,1296';
rows [132] = '182,217,236,359,429,640,1122,1370';
rows [133] = '192,237,248,387,454,656,1214,1546';
rows [134] = '216,290,297,441,519,788,1433,1753';
rows [135] = '223,298,307,476,573,878,1516,1898';
rows [136] = '245,304,329,505,608,898,1613,1969';
rows [137] = '261,348,362,530,665,946,1695,2055';
rows [138] = '284,387,415,599,733,1053,1927,2297';
rows [139] = '302,412,444,635,783,1099,2098,2466';
rows [140] = '333,426,471,671,843,1179,2143,2534';
rows [141] = '344,468,517,708,886,1266,2246,2637';
rows [142] = '376,514,620,736,973,1337,2399,3026';
rows [143] = '413,557,663,795,1014,1479,2554,3192';
rows [144] = '440,587,670,822,1073,1527,2596,3244';
rows [145] = '544,678,742,911,1148,1639,2941,3566';
rows [146] = '571,730,829,976,1281,1799,3094,3760';
rows [147] = '618,774,910,1058,1440,1929,3278,3983';
//Preferred $10000 Plan Type
rows [148] = '89,111,122,192,223,338,516,766';
rows [149] = '109,135,149,234,276,399,620,857';
rows [150] = '115,158,169,261,301,446,720,945';
rows [151] = '134,177,197,292,334,488,867,1110';
rows [152] = '152,183,207,309,377,547,964,1193';
rows [153] = '168,199,213,327,394,589,1033,1261';
rows [154] = '177,219,228,354,418,599,1118,1425';
rows [155] = '199,267,273,403,479,725,1319,1615';
rows [156] = '209,275,281,435,527,808,1396,1746';
rows [157] = '226,282,299,455,557,830,1485,1812';
rows [158] = '242,321,333,477,614,871,1558,1891';
rows [159] = '260,356,381,552,669,970,1767,2117';
rows [160] = '282,379,411,580,721,1015,1931,2270';
rows [161] = '304,392,433,615,777,1086,1974,2333';
rows [162] = '316,432,473,649,816,1165,2057,2429';
rows [163] = '346,473,568,673,896,1233,2216,2787';
rows [164] = '380,517,606,727,940,1355,2352,2938';
rows [165] = '403,538,621,752,989,1399,2390,2988';
rows [166] = '499,624,681,833,1058,1508,2708,3284';
rows [167] = '526,672,760,893,1180,1657,2849,3463';
rows [168] = '569,713,833,968,1326,1777,3013,3669';
//Standard $250 Plan Type
rows [169] = '267,316,333,495,639,962,1295,2198';
rows [170] = '315,395,405,598,768,1028,1454,2535';
rows [171] = '360,424,460,679,840,1070,1513,2813';
rows [172] = '381,479,557,797,959,1296,2038,3202';
rows [173] = '418,498,597,852,1113,1512,2299,3551';
rows [174] = '466,515,606,899,1196,1684,2441,3846';
rows [175] = '488,590,652,968,1254,1745,2589,4056';
rows [176] = '557,710,779,1143,1437,1955,2889,4726';
rows [177] = '598,748,840,1253,1598,2160,3116,5073';
rows [178] = '644,781,893,1318,1724,2302,3333,5341';
rows [179] = '668,870,971,1390,1776,2411,3431,5692';
rows [180] = '732,961,1099,1572,2028,2631,3843,6088';
rows [181] = '780,1014,1163,1622,2130,2772,4230,6682';
rows [182] = '859,1056,1227,1773,2385,3022,4539,6868';
rows [183] = '882,1104,1292,1846,2537,3088,4633,7102';
rows [184] = '931,1216,1593,1925,2790,3384,5189,8116';
rows [185] = '1031,1350,1766,2160,2932,3851,5673,8489';
rows [186] = '1115,1420,1845,2231,3043,3998,5999,8788';
rows [187] = '1356,1633,2064,2509,3467,4413,6936,9654';
rows [188] = '1425,1725,2329,2728,3899,4889,7275,10146';
rows [189] = '1532,1873,2568,2985,4437,5286,7994,10814';
//Standard $1000 Plan Type
rows [190] = '223,278,293,433,558,842,1133,1923';
rows [191] = '277,347,358,523,672,899,1351,2218';
rows [192] = '315,371,402,607,735,936,1499,2462';
rows [193] = '333,420,489,698,839,1131,1784,2802';
rows [194] = '366,432,523,746,974,1324,2013,3107';
rows [195] = '405,446,537,790,1047,1474,2078,3365';
rows [196] = '427,516,558,847,1098,1540,2130,3550';
rows [197] = '488,622,693,998,1257,1711,2528,4136';
rows [198] = '523,654,735,1097,1398,1890,2727,4439';
rows [199] = '557,684,781,1154,1520,1985,2908,4674';
rows [200] = '584,762,850,1216,1582,2058,2999,4980';
rows [201] = '640,841,964,1376,1775,2298,3362,5327';
rows [202] = '683,891,1017,1419,1864,2426,3699,5847';
rows [203] = '752,924,1074,1551,2086,2644,3972,5999';
rows [204] = '770,967,1116,1615,2220,2699,4054,6214';
rows [205] = '808,1067,1393,1684,2434,2960,4540,7102';
rows [206] = '893,1182,1546,1890,2565,3370,4964,7428';
rows [207] = '975,1248,1614,1952,2663,3512,5198,7690';
rows [208] = '1186,1420,1805,2195,3033,3861,6068,8447';
rows [209] = '1247,1528,2027,2387,3419,4279,6366,8879';
rows [210] = '1336,1639,2232,2612,3883,4626,6995,9462';
//Standard $5000 Plan Type
rows [211] = '178,212,223,334,432,648,874,1483';
rows [212] = '212,267,278,404,518,694,982,1711';
rows [213] = '243,287,310,458,567,719,1021,1899';
rows [214] = '256,323,376,538,647,874,1376,2161';
rows [215] = '282,335,399,575,752,1021,1553,2397';
rows [216] = '314,348,416,606,807,1131,1647,2597';
rows [217] = '328,398,440,653,847,1186,1738,2738';
rows [218] = '376,480,516,772,970,1319,1950,3191';
rows [219] = '403,505,568,846,1078,1457,2098,3425';
rows [220] = '435,527,599,890,1164,1543,2250,3598';
rows [221] = '457,588,669,938,1199,1627,2316,3842';
rows [222] = '494,649,743,1061,1370,1776,2596,4058';
rows [223] = '526,688,785,1096,1438,1870,2855,4511';
rows [224] = '580,709,829,1197,1610,2039,3064,4636';
rows [225] = '595,746,871,1246,1713,2085,3119,4794';
rows [226] = '629,823,1075,1298,1883,2284,3503,5478';
rows [227] = '696,912,1193,1458,1958,2598,3829,5730';
rows [228] = '753,963,1245,1507,2028,2709,4054,5932';
rows [229] = '913,1103,1392,1694,2334,2979,4682,6517';
rows [230] = '962,1179,1559,1842,2637,3299,4911,6849';
rows [231] = '1035,1264,1734,2015,2995,3568,5397,7299';
//Standard $10000 Plan Type
rows [232] = '171,196,202,304,397,598,799,1366';
rows [233] = '196,245,254,372,478,639,899,1576';
rows [234] = '221,263,285,422,522,665,940,1673';
rows [235] = '237,298,344,496,595,805,1267,1990';
rows [236] = '259,309,365,529,692,939,1430,2207';
rows [237] = '291,320,379,557,746,1046,1517,2391';
rows [238] = '303,367,403,599,780,1079,1637,2522';
rows [239] = '346,442,482,695,893,1214,1796,2938';
rows [240] = '368,465,518,779,993,1343,1936,3146';
rows [241] = '398,487,552,811,1060,1431,2072,3320';
rows [242] = '416,540,607,863,1115,1499,2132,3538';
rows [243] = '454,598,680,992,1261,1635,2389,3784';
rows [244] = '484,634,718,1008,1338,1697,2629,4153';
rows [245] = '533,656,759,1098,1482,1879,2822,4261';
rows [246] = '549,686,794,1147,1577,1919,2880,4414';
rows [247] = '579,758,977,1196,1712,2099,3222,5023';
rows [248] = '640,839,1091,1338,1822,2394,3526,5276';
rows [249] = '693,887,1139,1395,1891,2495,3739,5462';
rows [250] = '844,1014,1274,1560,2154,2742,4311,5997';
rows [251] = '885,1085,1439,1696,2429,3039,4521,6307';
rows [252] = '953,1165,1586,1855,2758,3284,4968,6697';

  if (planType == "Preferred Plus") var planValue = 0;
  else if (planType == "Preferred") var planValue = 1;
  else if (planType == "Standard") var planValue = 2;
  
  if ((deductible == 0) || (deductible == 99) || (deductible == 250)) var deductibleValue = 0;
  if (deductible == 1000) var deductibleValue = 1;
  if (deductible == 5000) var deductibleValue = 2;
  if (deductible == 10000) var deductibleValue = 3;
  
     // Get Trip Length Applied for in Days
  if (tripApplied == 50) var termValue = 1;
  if (tripApplied == 60) var termValue = 2;
  if (tripApplied == 66) var termValue = 3;
  if (tripApplied == 75) var termValue = 4;
  if (tripApplied == 82) var termValue = 5;
  if (tripApplied == 90) var termValue = 6;
  if (tripApplied == 96) var termValue = 7;
  if (tripApplied == 105) var termValue = 8;
  if (tripApplied == 112) var termValue = 9;
  if (tripApplied == 120) var termValue = 10;
  if (tripApplied == 126) var termValue = 11;
  if (tripApplied == 135) var termValue = 12;
  if (tripApplied == 142) var termValue = 13;
  if (tripApplied == 150) var termValue = 14;
  if (tripApplied == 156) var termValue = 15;
  if (tripApplied == 165) var termValue = 16;
  if (tripApplied == 175) var termValue = 17;
  if (tripApplied == 183) var termValue = 18;
  if (tripApplied == 190) var termValue = 19;
  if (tripApplied == 200) var termValue = 20;
  if (tripApplied == 212) var termValue = 21;

  var row = planValue * 84 + deductibleValue * 21 + termValue;

  // Get the row from rows array with the premium 
  var premiumRow = rows[row];

  // Put all premiums from a premiumRow into an array  
  var dividePremium = premiumRow.split(',');

  // Get the right Premium for the Age
  var premium = 0;
  if (age <= 55) premium = dividePremium[0];
  if ( (age >= 56) && (age <= 60) ) premium = dividePremium[1];
  if ( (age >= 61) && (age <= 65) ) premium = dividePremium[2];
  if ( (age >= 66) && (age <= 70) ) premium = dividePremium[3];
  if ( (age >= 71) && (age <= 75) ) premium = dividePremium[4];
  if ( (age >= 76) && (age <= 79) ) premium = dividePremium[5];
  if ( (age >= 80) && (age <= 85) ) premium = dividePremium[6];
  if (age >= 86) premium = dividePremium[7];
 
  premium = parseFloat(premium);
  return premium;
}

function calcShortTripRate(planType, deductible, tripApplied, age) {   
  
var rows = new Array();
rows[0] = 'null';
rows [1] = '24,27,39,42,44,48,68,85';
rows [2] = '27,30,42,45,52,64,88,139';
rows [3] = '29,36,45,48,62,94,134,196';
rows [4] = '31,42,48,52,72,114,178,245';
rows [5] = '33,48,53,60,79,126,214,298';
rows [6] = '35,52,62,71,96,149,247,349';
rows [7] = '38,56,72,83,120,173,284,397';
rows [8] = '41,59,82,92,133,198,336,455';
rows [9] = '44,64,91,104,141,222,379,532';
rows [10] = '47,68,98,116,156,248,422,586';
rows [11] = '49,72,106,128,174,272,463,620';
rows [12] = '54,79,112,139,191,296,497,698';
rows [13] = '58,88,118,152,212,328,552,787';
//Preferred Plus $1,000
rows [14] = '22,23,37,38,40,45,59,69';
rows [15] = '23,27,38,43,46,59,83,100';
rows [16] = '24,34,44,46,60,85,128,159';
rows [17] = '29,39,47,50,70,104,160,202';
rows [18] = '31,44,52,58,77,125,197,250';
rows [19] = '32,48,61,69,94,138,221,282';
rows [20] = '35,55,71,81,114,166,269,345';
rows [21] = '36,58,81,90,128,187,305,393';
rows [22] = '38,63,89,102,139,206,338,436';
rows [23] = '42,67,96,114,153,227,375,483';
rows [24] = '44,71,101,126,157,233,385,498';
rows [25] = '49,78,109,137,174,264,441,572';
rows [26] = '52,87,112,149,183,280,473,615';
//Preferred Plus $5,000
rows [27] = '21,22,33,34,38,41,50,59';
rows [28] = '22,23,34,39,42,50,68,83';
rows [29] = '23,30,43,45,54,70,101,127';
rows [30] = '24,34,46,49,64,85,127,159';
rows [31] = '28,38,51,57,74,101,153,195';
rows [32] = '29,40,59,68,82,111,171,219';
rows [33] = '31,47,66,80,94,133,207,267';
rows [34] = '32,49,71,89,106,149,233,303';
rows [35] = '33,53,76,96,114,163,258,335';
rows [36] = '36,58,84,106,125,179,284,372';
rows [37] = '37,59,85,108,128,184,293,382';
rows [38] = '40,69,89,117,140,205,333,437';
rows [39] = '45,73,91,120,145,217,354,469';
//Preferred Plus $10,000
rows [40] = '20,21,31,33,35,38,46,53';
rows [41] = '21,22,33,37,39,46,61,72';
rows [42] = '22,29,42,44,52,68,99,121';
rows [43] = '23,33,45,48,63,83,121,150';
rows [44] = '26,37,50,56,72,97,145,183';
rows [45] = '27,39,56,67,78,106,162,205';
rows [46] = '30,44,63,79,90,124,195,249';
rows [47] = '31,45,67,87,100,139,219,279';
rows [48] = '32,50,73,95,109,153,242,310';
rows [49] = '34,54,77,102,118,166,266,342';
rows [50] = '36,55,78,103,120,171,275,352';
rows [51] = '39,62,81,112,130,189,309,402';
rows [52] = '42,67,83,114,134,198,329,429';
//Preferred $250
rows [53] = '0,0,58,64,67,81,102,122';
rows [54] = '0,0,66,78,84,107,144,178';
rows [55] = '0,0,83,105,113,153,219,282';
rows [56] = '0,0,94,123,135,189,277,359';
rows [57] = '0,0,108,143,159,227,340,443';
rows [58] = '0,0,116,157,176,252,380,499';
rows [59] = '0,0,133,184,208,302,463,610';
rows [60] = '0,0,147,204,232,342,523,695';
rows [61] = '0,0,159,224,253,376,581,771';
rows [62] = '0,0,167,238,273,410,637,849';
rows [63] = '0,0,171,245,279,421,657,877';
rows [64] = '0,0,183,268,310,474,751,1007';
rows [65] = '0,0,191,284,330,509,807,1084';
//Preferred $1,000
rows [66] = '0,0,57,63,66,78,96,116';
rows [67] = '0,0,65,75,81,100,134,168';
rows [68] = '0,0,80,99,110,145,205,264';
rows [69] = '0,0,91,116,129,175,256,335';
rows [70] = '0,0,102,135,151,211,313,414';
rows [71] = '0,0,111,148,166,234,350,464';
rows [72] = '0,0,127,173,195,279,424,567';
rows [73] = '0,0,140,192,218,314,480,644';
rows [74] = '0,0,150,209,239,346,531,714';
rows [75] = '0,0,158,222,256,375,583,788';
rows [76] = '0,0,161,229,262,387,600,812';
rows [77] = '0,0,171,249,288,434,685,931';
rows [78] = '0,0,178,263,307,465,736,1003';
//Preferred $5,000
rows [79] = '0,0,56,60,62,69,85,99';
rows [80] = '0,0,61,68,73,89,114,138';
rows [81] = '0,0,73,88,95,123,168,211';
rows [82] = '0,0,81,100,111,147,209,264';
rows [83] = '0,0,91,115,128,174,250,322';
rows [84] = '0,0,96,125,140,192,279,362';
rows [85] = '0,0,110,145,164,228,337,440';
rows [86] = '0,0,119,159,178,256,381,498';
rows [87] = '0,0,127,173,195,280,420,550';
rows [88] = '0,0,131,184,208,303,459,606';
rows [89] = '0,0,135,187,213,311,473,624';
rows [90] = '0,0,140,202,232,347,535,711';
rows [91] = '0,0,145,211,244,369,574,763';
//Preferred $10,000
rows [92] = '0,0,55,59,61,68,83,95';
rows [93] = '0,0,60,67,72,85,106,125';
rows [94] = '0,0,72,83,90,112,150,186';
rows [95] = '0,0,78,95,101,132,184,229';
rows [96] = '0,0,87,105,116,154,218,277';
rows [97] = '0,0,91,114,124,168,241,309';
rows [98] = '0,0,100,129,143,197,288,372';
rows [99] = '0,0,109,141,157,219,323,420';
rows [100] = '0,0,116,151,169,239,355,464';
rows [101] = '0,0,117,158,178,255,386,506';
rows [102] = '0,0,119,162,182,262,397,523';
rows [103] = '0,0,121,171,194,288,446,591';
rows [104] = '0,0,123,176,202,304,474,633';
//Standard $250
rows [105] = '0,0,66,77,86,102,126,171';
rows [106] = '0,0,81,101,114,141,184,261';
rows [107] = '0,0,108,145,169,217,294,432';
rows [108] = '0,0,128,176,208,272,375,556';
rows [109] = '0,0,150,211,251,333,462,693';
rows [110] = '0,0,164,234,280,374,521,784';
rows [111] = '0,0,193,280,338,455,637,966';
rows [112] = '0,0,215,315,381,514,725,1101';
rows [113] = '0,0,235,346,421,570,804,1227';
rows [114] = '0,0,252,376,459,626,887,1358';
rows [115] = '0,0,258,388,474,645,917,1403';
rows [116] = '0,0,285,435,536,737,1053,1620';
rows [117] = '0,0,302,465,574,793,1135,1751';
//Standard $1,000
rows [118] = '0,0,63,72,80,93,114,154';
rows [119] = '0,0,74,92,105,128,165,234';
rows [120] = '0,0,98,129,151,193,259,382';
rows [121] = '0,0,114,156,184,240,329,490';
rows [122] = '0,0,133,185,221,292,403,610';
rows [123] = '0,0,146,205,246,327,455,689';
rows [124] = '0,0,171,245,295,396,554,846';
rows [125] = '0,0,190,273,333,448,631,966';
rows [126] = '0,0,206,301,367,495,699,1075';
rows [127] = '0,0,220,326,398,543,770,1189';
rows [128] = '0,0,225,334,411,560,794,1228';
rows [129] = '0,0,246,374,463,637,910,1416';
rows [130] = '0,0,260,399,494,683,981,1530';
//Standard $5,000
rows [131] = '0,0,59,66,71,83,98,127';
rows [132] = '0,0,67,82,91,109,136,186';
rows [133] = '0,0,85,109,127,158,208,297';
rows [134] = '0,0,98,130,153,196,261,379';
rows [135] = '0,0,111,152,182,236,318,468';
rows [136] = '0,0,121,168,201,262,357,528';
rows [137] = '0,0,140,197,240,316,435,646';
rows [138] = '0,0,153,220,269,356,491,735';
rows [139] = '0,0,167,240,295,393,545,817';
rows [140] = '0,0,174,258,319,427,597,901';
rows [141] = '0,0,179,265,328,440,615,929';
rows [142] = '0,0,192,293,366,497,701,1068';
rows [143] = '0,0,201,311,390,532,755,1153';
//Standard $10,000
rows [144] = '0,0,58,65,70,82,97,126';
rows [145] = '0,0,66,81,90,108,134,182';
rows [146] = '0,0,84,108,125,155,202,286';
rows [147] = '0,0,97,129,149,189,251,361';
rows [148] = '0,0,110,150,176,226,305,444';
rows [149] = '0,0,120,165,193,251,339,499';
rows [150] = '0,0,139,193,229,299,411,611';
rows [151] = '0,0,152,215,254,336,464,693';
rows [152] = '0,0,166,234,278,370,513,770';
rows [153] = '0,0,173,249,300,402,561,847';
rows [154] = '0,0,177,256,308,413,579,875';
rows [155] = '0,0,188,281,342,463,657,1002';
rows [156] = '0,0,196,296,362,496,704,1080';

  if (planType == "Preferred Plus") var planValue = 0;
  else if (planType == "Preferred") var planValue = 1;
  else if (planType == "Standard") var planValue = 2;

  if ((deductible == 0) || (deductible == 99) || (deductible == 250)) var deductibleValue = 0;
  if (deductible == 1000) var deductibleValue = 1;
  if (deductible == 5000) var deductibleValue = 2;
  if (deductible == 10000) var deductibleValue = 3;

  // Get Trip Length Applied for in Days
  if (tripApplied == 3) var termValue = 1;
  if (tripApplied == 6) var termValue = 2;
  if (tripApplied == 9) var termValue = 3;
  if (tripApplied == 12) var termValue = 4;
  if (tripApplied == 15) var termValue = 5;
  if (tripApplied == 18) var termValue = 6;
  if (tripApplied == 21) var termValue = 7;
  if (tripApplied == 24) var termValue = 8;
  if (tripApplied == 27) var termValue = 9;
  if (tripApplied == 30) var termValue = 10;
  if (tripApplied == 33) var termValue = 11;
  if (tripApplied == 36) var termValue = 12;
  if (tripApplied == 40) var termValue = 13;

  var row = planValue * 52 + deductibleValue * 13 + termValue;  
  
  // Get the row from rows array with the premium 
  var premiumRow = rows[row];

  // Put all premiums from a premiumRow into an array  
  var dividePremium = premiumRow.split(',');

  // Get the right Premium for the Age
  var premium;
  if (age <= 55) premium = dividePremium[0];
  if ( (age >= 56) && (age <= 60) ) premium = dividePremium[1];
  if ( (age >= 61) && (age <= 65) ) premium = dividePremium[2];
  if ( (age >= 66) && (age <= 70) ) premium = dividePremium[3];
  if ( (age >= 71) && (age <= 75) ) premium = dividePremium[4];
  if ( (age >= 76) && (age <= 79) ) premium = dividePremium[5];
  if ( (age >= 80) && (age <= 85) ) premium = dividePremium[6];
  if (age >= 86) premium = dividePremium[7];

  premium = parseInt(premium);
  return premium;
}


/////////checkAnnualPlanAddOn()
function checkAnnualPlanAddOn(theField) {
  var f = document.m;
  var tripLength = getComboValue(f.tripLength);

  if(tripLength < 23) {
    theField.checked = false;
    f.tripLength.focus()
    alert ('Annual Add-on is available only if you purchased a single trip plan of 23 days or longer.')
  }
}

// This function gets the value of a Combo Box and returns it.
function getComboValue(theField) {
  var index = theField.selectedIndex;
  if(index == -1) {return true;} // avoids an error message from succesive calls to unselected combo boxes from calcPremium() 
  var choice = theField.options[index].value;
  return choice;
}

function roundOff(value, precision) {
   value = "" + value //convert value to string
   precision = parseInt(precision);
   var whole = "" + Math.round(value * Math.pow(10, precision));
   var decPoint = whole.length - precision;
   if(decPoint != 0) {
      result = whole.substring(0, decPoint);
      result += ".";
      result += whole.substring(decPoint, whole.length);
   }else {
      result = "0." + whole;
   }
   result = parseFloat(result);
   return result;
}

function calcPremium() {
  var f = document.m;
  var tripLength = getComboValue(f.tripLength);
  var planType = getComboValue(f.planType);
  var deductible = getComboValue(f.deductible);
  var age = getComboValue(f.age);
  var province = getComboValue(f.province);

  if( (tripLength != '') && (planType != '') && (deductible != '') && (age != '') ) {
    // Single Trip Rate
    var singleTripRate;
    if((tripLength >= 41) && (tripLength <= 212)) {
   	    singleTripRate = calcLongTripRate(planType, deductible, tripLength, age);
    }
    if((tripLength >= 3) && (tripLength <= 40)) {
   	  singleTripRate = calcShortTripRate(planType, deductible, tripLength, age);
		
		  if(singleTripRate == 0) {
		    alert('Short Term Rates of 40 days or less for Applicants under age 61 and a Plan Type Preferred or Standard are not available.')
		    f.totalDiscount.value = 'Not Available';
		    f.totalPremium.value = 'Not Available';
		    return false;
		  }
    }

	// SUBTRACT Total discount
	var totalDiscount = 0;

	var discountPerc = getComboValue(f.discountPerc);
  if(discountPerc != '') {
    discountPerc = parseInt(discountPerc);
	  totalDiscount = singleTripRate * (discountPerc/100);
	  totalDiscount = roundOff(totalDiscount, 2); // return is a float noumber
	  f.totalDiscount.value = '$' + totalDiscount;
  }else{
	  f.totalDiscount.value = '';  
  }
 
	var subtotalA = singleTripRate - totalDiscount;
	subtotalA = roundOff(subtotalA, 2);
	
  // Annual Add-on Rate
  if(f.annualAddOn.checked == true) {
     var annualAddOnRate = calcAnnualAddOnRate(planType, deductible, age);
	   subtotalA = subtotalA + annualAddOnRate;
	}

	// Rate Subtotal
  var rateSubtotal = subtotalA;
	
	// Add 10% to the $99 deductible rate if taking a $0 deductible (Preferred Plus only)
	var tenPercent = 0;
  if(deductible == 0) {
	   tenPercent = (rateSubtotal / 10);
	}

	// Subtotal
  var subtotalB = subtotalA + tenPercent;

	// ADD 15% if you have smoked cigarettes
	var smokerFee = 0;
  if(f.smoker.checked == true) {
	  smokerFee = subtotalB * (15/100);
	  subtotalB = subtotalB + smokerFee;
	}

	// Total Premium Per Applicant
	var totalPremium = subtotalB;
	f.totalPremium.value = '$' + Modify(totalPremium);

  }
  // Clear the fields 
  else {
    f.totalDiscount.value = '';
    f.totalPremium.value = '';
  }
  
  // Validate Plan Type & Deductible Fields 
  if( ((planType == 'Preferred Plus') && (deductible == 250)) || ((planType == 'Preferred') && (deductible == 99)) || ((planType == 'Standard') && (deductible == 99)) ) {
    f.totalDiscount.value = 'Not Available';
    f.totalPremium.value = 'Not Available';  
  }    
}

