function my_window_open(url, width, height, features) {
   screen_width  = screen.width;
   screen_height = screen.height;

   max_width  = 0.67 * screen_width;
   max_height = 0.84 * screen_height;

   if (width > max_width) {
      width = max_width;
   }
   if (height > max_height) {
      height = max_height;
   }

   winleft = screen_width  - width  -  50;
   wintop  = screen_height - height - 150;

   features = 'width=' + width + ',height=' + height + ',left=' + winleft + ',top=' + wintop + ',' + features;

   return window.open(url,'',features);
}

function test_AC_FI() {
   var ctrl = 0;

   if (typeof document.frm.AC_ID != "undefined" && document.frm.AC_ID.options[document.frm.AC_ID.selectedIndex].value != 0) {
      ctrl = 1;
   }
   if (typeof document.frm.FI_ID != "undefined" && document.frm.FI_ID.options[document.frm.FI_ID.selectedIndex].value != 0) {
      ctrl = 1;
   }

   if (ctrl == 0) {
      return false;
   }
   else {
      return true;
   }
}

function test_From_To() {
   var from_day, from_month, from_year, from_time;
   var to_day, to_month, to_year, to_time;
   var diff;

   if (typeof document.frm.AC_ID != "undefined" && document.frm.AC_ID.options[document.frm.AC_ID.selectedIndex].value != 0) {
      from_day   = parseInt(document.frm.from_day1.value, 10);
      from_month = parseInt(document.frm.from_month1.options[document.frm.from_month1.selectedIndex].value, 10);
      from_year  = parseInt(document.frm.from_year1.value, 10);
      from_time  = parseInt(document.frm.from_time1.options[document.frm.from_time1.selectedIndex].value, 10);

      to_day   = parseInt(document.frm.to_day1.value, 10);
      to_month = parseInt(document.frm.to_month1.options[document.frm.to_month1.selectedIndex].value, 10);
      to_year  = parseInt(document.frm.to_year1.value, 10);
      to_time  = parseInt(document.frm.to_time1.options[document.frm.to_time1.selectedIndex].value, 10);

      if (isDate(from_day, from_month, from_year) == false) {
         return false;
      }

      if (isDate(to_day, to_month, to_year) == false) {
         return false;
      }

      diff = Date.UTC(to_year, to_month-1, to_day, 0, 0, 0) - Date.UTC(from_year, from_month-1, from_day, 0, 0, 0);
      if (diff < 0 || diff == 0 && (from_time >= to_time)) {
         return false;
      }
   }

   if (typeof document.frm.FI_ID != "undefined" && document.frm.FI_ID.options[document.frm.FI_ID.selectedIndex].value != 0) {
      from_day   = parseInt(document.frm.from_day2.value, 10);
      from_month = parseInt(document.frm.from_month2.options[document.frm.from_month2.selectedIndex].value, 10);
      from_year  = parseInt(document.frm.from_year2.value, 10);
      from_time  = parseInt(document.frm.from_time2.options[document.frm.from_time2.selectedIndex].value, 10);

      to_day   = parseInt(document.frm.to_day2.value, 10);
      to_month = parseInt(document.frm.to_month2.options[document.frm.to_month2.selectedIndex].value, 10);
      to_year  = parseInt(document.frm.to_year2.value, 10);
      to_time  = parseInt(document.frm.to_time2.options[document.frm.to_time2.selectedIndex].value, 10);

      if (isDate(from_day, from_month, from_year) == false) {
         return false;
      }

      if (isDate(to_day, to_month, to_year) == false) {
         return false;
      }

      diff = Date.UTC(to_year, to_month-1, to_day, 0, 0, 0) - Date.UTC(from_year, from_month-1, from_day, 0, 0, 0);
      if (diff < 0 || diff == 0 && (from_time >= to_time)) {
         return false;
      }
   }

   return true;
}

function test_From_To_MX_VC() {
   var from_day, from_month, from_year, from_time;
   var to_day, to_month, to_year, to_time;
   var diff;

   from_day   = parseInt(document.frm.from_day.value, 10);
   from_month = parseInt(document.frm.from_month.options[document.frm.from_month.selectedIndex].value, 10);
   from_year  = parseInt(document.frm.from_year.value, 10);
   from_time  = parseInt(document.frm.from_time.options[document.frm.from_time.selectedIndex].value, 10);

   to_day   = parseInt(document.frm.to_day.value, 10);
   to_month = parseInt(document.frm.to_month.options[document.frm.to_month.selectedIndex].value, 10);
   to_year  = parseInt(document.frm.to_year.value, 10);
   to_time  = parseInt(document.frm.to_time.options[document.frm.to_time.selectedIndex].value, 10);

   if (isDate(from_day, from_month, from_year) == false) {
      return false;
   }

   if (isDate(to_day, to_month, to_year) == false) {
      return false;
   }

   diff = Date.UTC(to_year, to_month-1, to_day, 0, 0, 0) - Date.UTC(from_year, from_month-1, from_day, 0, 0, 0);
   if (diff < 0 || diff == 0 && (from_time >= to_time)) {
      return false;
   }

   return true;
}

function test_From_To_Recurrence() {
   var from_day, from_month, from_year, from_time;
   var to_day, to_month, to_year, to_time;
   var diff;

   if (typeof document.frm.AC_ID != "undefined" && document.frm.AC_ID.options[document.frm.AC_ID.selectedIndex].value != 0) {
      from_time = parseInt(document.frm.from_time1.options[document.frm.from_time1.selectedIndex].value, 10);
      to_time   = parseInt(document.frm.to_time1.options[document.frm.to_time1.selectedIndex].value, 10);

      if (from_time >= to_time) {
         return false;
      }
   }

   if (typeof document.frm.FI_ID != "undefined" && document.frm.FI_ID.options[document.frm.FI_ID.selectedIndex].value != 0) {
      from_time = parseInt(document.frm.from_time2.options[document.frm.from_time2.selectedIndex].value, 10);
      to_time   = parseInt(document.frm.to_time2.options[document.frm.to_time2.selectedIndex].value, 10);

      if (from_time >= to_time) {
         return false;
      }
   }

   from_day   = parseInt(document.frm.from_day.value, 10);
   from_month = parseInt(document.frm.from_month.options[document.frm.from_month.selectedIndex].value, 10);
   from_year  = parseInt(document.frm.from_year.value, 10);

   to_day   = parseInt(document.frm.to_day.value, 10);
   to_month = parseInt(document.frm.to_month.options[document.frm.to_month.selectedIndex].value, 10);
   to_year  = parseInt(document.frm.to_year.value, 10);

   if (isDate(from_day, from_month, from_year) == false || isDate(to_day, to_month, to_year) == false) {
      return false;
   }

   diff = Date.UTC(to_year, to_month-1, to_day, 0, 0, 0) - Date.UTC(from_year, from_month-1, from_day, 0, 0, 0);
   if (diff < 0) {
      return false;
   }

   return true;
}

function PostValues() {
   if (typeof document.frm.AC_ID != "undefined") {
      if (document.frm.currentSelected.value == 0) {
         document.frm.from_day2.value = document.frm.from_day1.value;
         document.frm.from_month2.selectedIndex = document.frm.from_month1.selectedIndex;
         document.frm.from_year2.value = document.frm.from_year1.value;
         document.frm.from_time2.selectedIndex = document.frm.from_time1.selectedIndex;
         document.frm.to_day2.value = document.frm.to_day1.value;
         document.frm.to_month2.selectedIndex = document.frm.to_month1.selectedIndex;
         document.frm.to_year2.value = document.frm.to_year1.value;
         document.frm.to_time2.selectedIndex = document.frm.to_time1.selectedIndex;

         document.frm.currentSelected.value = 1;
      }
   }
}

function ResetSL(SL) {
   if (SL != 'PLs') document.frm.PLs.selectedIndex = 0;
   if (SL != 'FIs') document.frm.FIs.selectedIndex = 0;
   if (SL != 'MEs') document.frm.MEs.selectedIndex = 0;
   if (SL != 'IMs') document.frm.IMs.selectedIndex = 0;
   if (SL != 'ADs') document.frm.ADs.selectedIndex = 0;
}

function isEmail(Email) {
   var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

   if (Email == "" || filter.test(Email)) {
      return true;
   }
   else {
      return false;
   }
}

function isDate(day, month, year) {
   if (isWhole(day) == false) return false;
   if (day < 1) return false;

   if (isWhole(month) == false) return false;
   if (month < 1)  return false;
   if (month > 12) return false;

   if (isWhole(year) == false) return false;
   if (year < 1902) return false;
   if (year > 2037) return false;

   if (month == 4 || month == 6 || month == 9 || month == 11) {
      if (day > 30) return false;
   }

   if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
      if (day > 31) return false;
   }

   if (month == 2) {
      if (year%4 == 0 && year%100 != 0 || year%400 == 0) {
         if (day > 29) return false;
      }
      else {
         if (day > 28) return false;
      }
   }

   return true;
}

function isWhole(testInt) {
   testInt *= 1;

   if (Math.floor(testInt) == testInt) {
      return true;
   }
   else {
      return false;
   }
}

function session() {
   theDate = new Date();
   return (theDate.getTime());
}

function start_vor(company) {
   location.href = "lib/cgi-bin/login.cgi?company=" + company + "&session=" + session();
}

function start_vor_pro(company) {
   msg = "You are starting the program from the main page of www.aero-vor.ch\nPlease note that the preferred method is to start the program from the\nhomepage of your flying club / flight school.\nThe link in www.aero-vor.ch should be used only if the homepage of\nyour flying club / flight school is not accessible.\n\nYour customer support\nsupport@aero-vor.ch";

   if (company.substr(0,5) != "demo_") {
      window.alert(msg);
   }

   location.href = "pro/lib/cgi-bin/login.cgi?company=" + company + "&session=" + session();
}
