var MONTHS_SHORT = ["Jan", "Feb", "Már", "Ápr", "Máj", "Jún","Júl", "Aug", "Szept", "Okt", "Nov", "Dec"];
var MONTHS_DAYS = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31];
var ARR_WEEKDAYS = ["Vasárnap", "Hétfő", "Kedd", "Szerda", "Csütörtök", "Péntek", "Szombat"];



function setArrival(date, fullmonth) {
  if (date < today) {
    date = today;
  }

  year = date.charAt(0) + date.charAt(1) + date.charAt(2) + date.charAt(3);
  month = date.charAt(5) + date.charAt(6);
  day = date.charAt(8) + '' + date.charAt(9);
  if (day > MONTHS_DAYS[month-1]) day = MONTHS_DAYS[month-1];

  //nagy dátum beállítása
  myarr_date = document.getElementById("indulas");
  myarr_date.value = year + '-' + month + '-' + day;


  myarr_day = document.getElementById("arr_day");
  for (i=0;i<31;i++) {
       myarr_day.options[i]=null;
  }
  for (i=0;i<fullmonth;i++) {
       myarr_day.options[i]=new Option(1*i+1,1*i+1);
  }
  myarr_day.options[day-1].selected=true;


  myarr_month = document.getElementById("arr_month");
  j = 0;
  for (i=globalmonth;i<1*globalmonth+24;i++) {
       aktmonth = 1*i;
       aktyear = globalyear;
       if (i > 12) {
           aktmonth = 1*i-12;
           aktyear = 1*globalyear+1;
        }
        if (i > 24) {
           aktmonth = 1*i-24;
           aktyear = 1*globalyear+2;
        }
        if ((aktmonth == month) && (aktyear == year)) selectedmonth = j;
        myarr_month.options[j]=null;
        myarr_month.options[j]=new Option(aktyear + ' '+ MONTHS_SHORT[1*aktmonth-1],aktyear + ''+ aktmonth);
        j++;
  }
  myarr_month.options[selectedmonth].selected=true;
}

function setDeparture(date, fullmonth) {
  if (date < today) {
    date = today;
  }
  //alert(date);
  year = date.charAt(0) + date.charAt(1) + date.charAt(2) + date.charAt(3);
  month = date.charAt(5) + date.charAt(6);
  day = date.charAt(8) + '' + date.charAt(9);
  if (day > MONTHS_DAYS[month-1]) day = MONTHS_DAYS[month-1];

  //nagy dátum beállítása
  mydep_date = document.getElementById("tavozas");
  mydep_date.value = year + '-' + month + '-' + day;


  mydep_day = document.getElementById("dep_day");
  for (i=0;i<31;i++) {
       mydep_day.options[i]=null;
  }
  for (i=0;i<1*fullmonth;i++) {
       mydep_day.options[i]=new Option(1*i+1,1*i+1);
  }
  mydep_day.options[day-1].selected=true;


  mydep_month = document.getElementById("dep_month");

  j = 0;
  globalmonth = 06;
  globalyear = 2011;
  for (i=globalmonth;i<1*globalmonth+24;i++) {
       aktmonth = i;
       aktyear = globalyear;
       if (i > 12) {
           aktmonth = 1*i-12;
           aktyear = 1*globalyear+1;
       }
       if (i > 24) {
           aktmonth = 1*i-24;
           aktyear = 1*globalyear+2;
       }
           //alert(aktyear);
       if ((aktmonth == month) && (aktyear == year)) selectedmonth = j;
       mydep_month.options[j]=null;
       mydep_month.options[j]=new Option(aktyear + ' '+ MONTHS_SHORT[aktmonth-1],aktyear + ''+ aktmonth);
       j++;
  }
  mydep_month.options[selectedmonth].selected=true;
}

function changeArrDate() {

  myarr_day = document.getElementById("arr_day");
  myarr_month = document.getElementById("arr_month");
  myarr_month_text = myarr_month.options[myarr_month.selectedIndex].value;
  day = myarr_day.options[myarr_day.selectedIndex].value;
  year = myarr_month_text.charAt(0) + myarr_month_text.charAt(1) + myarr_month_text.charAt(2) + myarr_month_text.charAt(3);
  month = myarr_month_text.charAt(4) + myarr_month_text.charAt(5);
  //biztosan frissiteni kell az érkezést

  arrdate = year;
  if (month < 10) {
    arrdate = arrdate + '-0' + month;
  } else {
    arrdate = arrdate + '-' + month;
  }
  if (day < 10) {
    arrdate = arrdate + '-0' + day;
  } else {
    arrdate = arrdate + '-' + day;
  }
  //alert(arrdate);

  globaldate = '2012-05-31';
  globaldate2 = '2012-05-30';
  globalday2 = '30';
  if (globaldate == arrdate) {
     arrdate = globaldate2;
         arrday = globalday2;
  }


  setArrival(arrdate, MONTHS_DAYS[month-1]);


  day = myarr_day.options[myarr_day.selectedIndex].value;
  year = myarr_month_text.charAt(0) + myarr_month_text.charAt(1) + myarr_month_text.charAt(2) + myarr_month_text.charAt(3);
  month = myarr_month_text.charAt(4) + myarr_month_text.charAt(5);

  //csak akkor módosítjuk a távozás dátumát, ha az megelőzi az érkezést
  mydep_day = document.getElementById("dep_day");
  mydep_month = document.getElementById("dep_month");
  mydep_month_text = mydep_month.options[mydep_month.selectedIndex].value;
  depnowday = mydep_day.options[mydep_day.selectedIndex].value;
  depnowyear = mydep_month_text.charAt(0) + mydep_month_text.charAt(1) + mydep_month_text.charAt(2) + mydep_month_text.charAt(3);
  depnowmonth = mydep_month_text.charAt(4) + mydep_month_text.charAt(5);
//  alert(day +'<'+ depnowday);
  if (1*year < 1*depnowyear) return;
  if ((1*year == 1*depnowyear) && (1*month < 1*depnowmonth)) return;
  if ( (1*year == 1*depnowyear) && (1*month==1*depnowmonth) && (1*day < 1*depnowday)) return;

  //alert(year + '-' + month + '-' + day);
  depday = 1*day + 1;
  depmonth = month;
  depyear = year;
  if ( (month<12) && (MONTHS_DAYS[month-1] == day)) { //nem december és hónap vége van
    depday = 1;
        depmonth = 1*month+1;
        depyear = 1*year;
  }
  if ((month == 12) && (MONTHS_DAYS[month-1] == day)) { //december és hónap vége van
    depday = 1;
        depmonth = 1;
        depyear = 1*year + 1;
  }
  depdate = depyear;
  if (depmonth < 10) {
    depdate = depdate + '-0' + depmonth;
  } else {
    depdate = depdate + '-' + depmonth;
  }
  if (depday < 10) {
    depdate = depdate + '-0' + depday;
  } else {
    depdate = depdate + '-' + depday;
  }
  //alert(depdate);
  setDeparture(depdate, MONTHS_DAYS[depmonth-1]);
}

function changeDepDate() {

  myarr_day = document.getElementById("arr_day");
  myarr_month = document.getElementById("arr_month");
  myarr_month_text = myarr_month.options[myarr_month.selectedIndex].value;
  arrday = myarr_day.options[myarr_day.selectedIndex].value;
  arryear = myarr_month_text.charAt(0) + myarr_month_text.charAt(1) + myarr_month_text.charAt(2) + myarr_month_text.charAt(3);
  arrmonth = myarr_month_text.charAt(4) + myarr_month_text.charAt(5);

  mydep_day = document.getElementById("dep_day");
  mydep_month = document.getElementById("dep_month");
  mydep_month_text = mydep_month.options[mydep_month.selectedIndex].value;
  depday = mydep_day.options[mydep_day.selectedIndex].value;
  depyear = mydep_month_text.charAt(0) + mydep_month_text.charAt(1) + mydep_month_text.charAt(2) + mydep_month_text.charAt(3);
  depmonth = mydep_month_text.charAt(4) + mydep_month_text.charAt(5);
  //biztosan frissiteni kell az távozást
  depdate = depyear;
  if (depmonth < 10) {
    depdate = depdate + '-0' + 1*depmonth;
  } else {
    depdate = depdate + '-' + 1*depmonth;
  }
  if (depday < 10) {
    depdate = depdate + '-0' + 1*depday;
  } else {
    depdate = depdate + '-' + 1*depday;
  }
  //alert(depdate);

  globaldate = '2011-08-01';
  globaldate2 = '2011-08-02';
  if (globaldate == depdate) {
     depdate = globaldate2;
         depday = 2;
  }

  arrdate = arryear;
  if (arrmonth < 10) {
    arrdate = arrdate + '-0' + 1*arrmonth;
  } else {
    arrdate = arrdate + '-' + 1*arrmonth;
  }
  if (arrday < 10) {
    arrdate = arrdate + '-0' + 1*arrday;
  } else {
    arrdate = arrdate + '-' + 1*arrday;
  }

  setDeparture(depdate, MONTHS_DAYS[1*depmonth-1]);
  return;
  if (arrdate  < depdate) return; //minden ok

  //változtatni kell az érkezést is sajna
  arrday = 1*depday - 1;
  arrmonth = depmonth;
  arryear = depyear;
  if ( (depmonth>0) && (depday == 1)) { //nem január és hónap eleje
    arrday = 31; //a beállíátsnál úgyis a hónap végére módosítja a függvény
        arrmonth = 1*depmonth-1;
        arryear = 1*depyear;
  }
  if ((month == 0) && (depday == 1)) { //január és hónap eleje van
    arrday = 31;
        arrmonth = 12;
        arryear = 1*depyear - 1;
  }
  arrdate = arryear;
  if (arrmonth < 10) {
    arrdate = arrdate + '-0' + 1*arrmonth;
  } else {
    arrdate = arrdate + '-' + 1*arrmonth;
  }
  if (arrday < 10) {
    arrdate = arrdate + '-0' + 1*arrday;
  } else {
    arrdate = arrdate + '-' + 1*arrday;
  }
  //alert(arrdate);
  setArrival(arrdate, MONTHS_DAYS[arrmonth-1]);
}






//*******************************************
//INICIALIZÁLÁS

