// knihovna Javascript funkci pro podporu stranky s vyhledavanim certifikatu uzivatelu

// funkce pro kontrolu, zda je zadane cislo v povolenem intervalu
function checkMinMax(num,min,max) {
// num ... cislo, ktere se bude kontrolovat
// min ... minimalni mozna hodnota cisla
// max ... maximalni mozna hodnota cisla

  var i = num-0;
  if (isNaN(i)) return false;
    else return ((min<=i)&&(i<=max));
}

// funkce vraci maximalni pripustny den pro zadany mesic
function maxDayMonth(month,year) {
// month ... cislo mesice v roce (0-11)
// year  ... odpovidajici rok

  var monthdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31); // pocet dni v jednotlivych mesicich
  // vyreseni prestupnych roku
  if (year % 100 == 0) {
    monthdays[1] = (year % 400 == 0) ? 29 : 28;
  } else {
    monthdays[1] = (year % 4 == 0) ? 29 : 28;
  }
  return monthdays[month];  
}

// funkce pro kontrolu vyplnenych casovych udaju pro hledani v historii CRL
function checkDates(from_d,from_m,from_y,to_d,to_m,to_y) {
  var err_day = "The value entered in the field \"Day\" is invalid.\nIt is necessary to enter the valid number of the day in the selected month.";
  var err_year = "The value entered in the field \"Year\" is invalid.\nIt is necessary to enter the valid number between 2005 and 2030.";
  var err_daymonth = "Selected month does not have so many days.\nPlease, correct the value in the field \"Day\".";
  var err_equaltimes = "The date \"FROM\" is the same as the date \"TO\".\nPlease, change the date \"FROM\" so that it is before the date \"TO\".";
  var err_revtimes = "The date \"FROM\" is bigger than the date \"TO\".\nPlease, change the date \"FROM\" so that it is before the date \"TO\".";
  // ziskani ciselneho vyjadreni mesicu v rozsahu 0..11
  from_lm = from_m.options[from_m.selectedIndex].value-1;
  to_lm = to_m.options[to_m.selectedIndex].value-1;
  // kontrola dni
  if (!checkMinMax(from_d.value,1,31)) {
    window.alert(err_day);
    from_d.focus();
    return false;
  }
  if (!checkMinMax(to_d.value,1,31)) {
    window.alert(err_day);
    to_d.focus();
    return false;
  }
  // kontrola roku
  if (!checkMinMax(from_y.value,2005,2030)) {
    window.alert(err_year);
    from_y.focus();
    return false;
  }
  if (!checkMinMax(to_y.value,2005,2030)) {
    window.alert(err_year);
    to_y.focus();
    return false;
  }
  // kontrola, zda jsou zadany dny pripustne v ramci zadaneho mesice (kontrola stavu 31.2.)
  if (from_d.value>maxDayMonth(from_lm,from_y)) {
    window.alert(err_daymonth);
    from_d.focus();
    return false;
  }
  if (to_d.value>maxDayMonth(to_lm,to_y)) {
    window.alert(err_daymonth);
    to_d.focus();
    return false;
  }
  // kontrola na stejne zadane hodnoty
  if ((from_d.value == to_d.value)&&(from_lm == to_lm)&&(from_y.value == to_y.value)) {
    window.alert(err_equaltimes);
    from_d.focus();
    return false;
  }
  // kontrola, zda datum "od" neni vetsi nez datum "do"
  from = new Date(from_y.value,from_lm,from_d.value,0,0,0,0);
  to = new Date(to_y.value,to_lm,to_d.value,0,0,0,0);
  if (from > to) {
    window.alert(err_revtimes);
    from_d.focus();
    return false;
  }
  return true;
}
