function whichBrowser(){
  if(navigator.appVersion<4){
     alert("You need MSIE 4.x or Netscape Navigator 4.x to view Medipac Travel Insurance Application.")
     return false;
  }
  return true;
}

function launchMedipacApp(){
  if(!whichBrowser()){return;}
  window.open("https://www.medipac.com/cgi-bin/mr/app.pl","insurance",'status=yes,toolbar=no,location=no,scrollbars=yes,resizable=yes,directories=no,menuBar=yes');
}

function launchRCLapp(){
  if(!whichBrowser()){return;}
  window.open("https://www.medipac.com/cgi-bin/rcl/app.pl","insurance",'status=yes,toolbar=no,location=no,scrollbars=yes,resizable=yes,directories=no,menuBar=yes');
}

// French Application
function launchRCLfapp(){
  if(!whichBrowser()){return;}
  window.open("https://www.medipac.com/cgi-bin/rcl/fapp.pl","insurance",'status=yes,toolbar=no,location=no,scrollbars=yes,resizable=yes,directories=no,menuBar=yes');
}

// 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 calcSingleRate(age) {
  var rate;
  if(age <= 64) {
    rate = 65;
  }else if((age >= 65) && (age <= 69)) {
    rate = 168;
  }else if((age >= 70) && (age <= 74)) {
    rate = 244;
  }else if((age >= 75) && (age <= 79)) {
    rate = 378;
  }else if((age >= 80) && (age <= 84)) {
    rate = 599;
  }else if((age >= 85) && (age <= 89)) {
    rate = 1175;
  }else if(age >= 90) {
    rate = 1478;
  }
  
  return rate;
}

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 calcSinglePremium() {
  var f = document.q;
  var age1 = getComboValue(f.age1);
  var age2 = getComboValue(f.age2);
  var appl1AnnualRate = 0;
  var appl2AnnualRate = 0;
  var legionPlus = 0;
  
  if(age1 != '') {var appl1AnnualRate = calcSingleRate(age1);}
  if(age2 != '') {var appl2AnnualRate = calcSingleRate(age2);}
  
  if(f.legionPlus1.checked == true){
    var legionPlus = 34;
    if(age2 != '') {legionPlus = 59;}
  }
  
  var totalPremium = appl1AnnualRate + appl2AnnualRate + legionPlus;
  f.total1.value = dollarCommaFormatted(totalPremium);
  
  if((age1 == '') && (age2 == '')) {
    f.total1.value = '';
  }
}

function calcFamilyPremium() {
  var f = document.q;
  var age3 = getComboValue(f.age3);

  var annualFamilyRate = calcSingleRate(age3) * 2;
  
  var legionPlus = 0; 
  if(f.legionPlus2.checked == true){
    var legionPlus = 68;
  }
  
  totalPremium = annualFamilyRate + legionPlus;
  f.total2.value = dollarCommaFormatted(totalPremium);
  
  if(age3 == '')  {
    f.total2.value = '';
  }
}
