/*------------------------------------------------------------------
---  global vars
------------------------------------------------------------------*/
var ucal_oncolor="#d2202a";
var ucal_offcolor="#d2202a";
var ucal_isopen=0;
var ucal_isclosing=0;
var ucal_dow=0;
var ucal_dtc=0;
var ucal_dtype = 3;  //1=year,2=month,3=week,4=day,5=dow,10=custom
var ucal_comp=0;
var ucal_which=0;
var ucal_trigger=0;

var ucal2_dtype = 3;  //1=year,2=month,3=week,4=day,5=dow,10=custom
var ucal2_dow=0;

var ucal_timer;
var ucal_cpwidth=0;
var ucal_cpheight=0;

var ucal_cdate = new ucalDate();

var ucal_bdate = new ucalDate();
var ucal_edate = new ucalDate();
var ucal_pbdate = new ucalDate();
var ucal_pedate = new ucalDate();
var ucal_pbsdate = new ucalDate();
var ucal_pesdate = new ucalDate();

var ucal2_bdate = new ucalDate();
var ucal2_edate = new ucalDate();
var ucal2_pbdate = new ucalDate();
var ucal2_pedate = new ucalDate();
var ucal2_pbsdate = new ucalDate();
var ucal2_pesdate = new ucalDate();

var ucal_smonths = new Array();
var ucal_sdows = new Array();
var ucal_start_dow = 1;
var ucal_flipym = 0;

/*------------------------------------------------------------------
---  date class
------------------------------------------------------------------*/
function ucalDate(mdate) {
   if (!mdate || mdate == "") {
      var cx = new Date();
      var cy = cx.getFullYear();
      var cm = cx.getMonth()+1;
      var cd = cx.getDate();
      if (cm < 10) cm = "0"+cm;
      if (cd < 10) cd = "0"+cd;
      mdate = ""+cy+cm+cd;
   }

   mdate += "";
   this.date = mdate;
   this.year = mdate.substring(0,4);
   this.month = mdate.substring(4,6);
   this.day = mdate.substring(6,8);

   this.date  *= 1;
   this.year  *= 1;
   this.month *= 1;
   this.day   *= 1;

   this.setFullDate = function() {
      var cy = this.year;
      var cm = this.month;
      var cd = this.day;
      if (cm < 10) cm = "0"+cm;
      if (cd < 10) cd = "0"+cd;
      this.date = ""+cy+cm+cd;
      this.date *= 1;
   }
}

/*------------------------------------------------------------------
---
---  Main Calendar Functions
---
------------------------------------------------------------------*/

/*------------------------------------------------------------------
---  build the calendar
------------------------------------------------------------------*/
function ucalBuild(which) {

   /*-------------------------------------------------------------------
   --- local vars
   --------------------------------------------------------------------*/
   var calname = "ucal_";
   var dtype = ucal_dtype; 
   var bdate = ucal_bdate;
   var edate = ucal_edate2;
   var true_edate = ucal_edate;
   var cdate = ucal_cdate;
   var cdow  = ucal_dow;

   if (which) {
      calname = "ucal2_";
      bdate = ucal2_bdate;
      edate = ucal2_edate;
      dtype = ucal2_dtype; 
      cdow  = ucal2_dow; 
   }

//	alert('from:'+bdate.year+bdate.month+bdate.day+' til:'+true_edate.year+true_edate.month+true_edate.day+' shown:'+edate.year+edate.month+edate.day);

   /*-------------------------------------------------------------------
   --- set the popup color
   --------------------------------------------------------------------*/
   var mye3;
   if ((mye3 = document.getElementById("ucal_pop"))   != null) mye3.style.borderColor = ucal_offcolor;
   if ((mye3 = document.getElementById("ucalpb_yr"))  != null) mye3.style.backgroundColor = ucal_oncolor;
   if ((mye3 = document.getElementById("ucalpe_yr"))  != null) mye3.style.backgroundColor = ucal_oncolor;

   var dtype_name = term309;
   if      (dtype == 1) dtype_name = term311;
   else if (dtype == 2) dtype_name = term310;
   else if (dtype == 3) dtype_name = term309;
   else if (dtype == 4) dtype_name = term308;
   else if (dtype == 5) dtype_name = term583;
   else if (dtype == 10) dtype_name = term584;

   if (which) {
      if ((mye3 = document.getElementById("ucal_dtype2")) != null) { mye3.innerHTML = dtype_name; }
   } else {
      if ((mye3 = document.getElementById("ucal_dtype")) != null) { mye3.innerHTML = dtype_name; }
   }

   if (ucal_comp) {
      if ((mye3 = document.getElementById("ucal_calt1")) != null) { mye3.style.display = ""; }
      if ((mye3 = document.getElementById("ucal_calt2")) != null) { mye3.style.display = ""; }
   } else {
      if ((mye3 = document.getElementById("ucal_calt1")) != null) { mye3.style.display = "none"; }
      if ((mye3 = document.getElementById("ucal_calt2")) != null) { mye3.style.display = "none"; }
   }

   /*-------------------------------------------------------------------
   --- set the dow string
   --------------------------------------------------------------------*/
   for (var ii=0;ii<7;ii++) {
      var mydow = ii+ucal_start_dow;
      var id = "";
      if (which) id = "2";
      if (mydow > 6) mydow = mydow%7;
      if ((mye3 = document.getElementById("ucal"+id+"_dow_"+(ii+1))) != null) {
         mye3.innerHTML = ucal_sdows[mydow];
      }
   }

   /*-------------------------------------------------------------------
   --- Set the year
   --------------------------------------------------------------------*/
   var mye,mye2;
   if ((mye = document.getElementById(calname+"year")) == null) return ;
   mye.innerHTML = edate.year;
   if (dtype == 1) mye.style.backgroundColor = ucal_oncolor; 
   else            mye.style.backgroundColor = "";
   
   if (edate.year == cdate.year) {
      if ((mye=document.getElementById(calname+"rarrow")) != null) {
         mye.src = "http://itthon.hu/site/img/right_grey_arrow.gif";
         if (!isIE5) mye.style.cursor = "auto";
      }
   } else {
      if ((mye=document.getElementById(calname+"rarrow")) != null)  {
         mye.src = "http://itthon.hu/site/img/right_arrow.gif";
         if (!isIE5) mye.style.cursor = "pointer";
      }
   }
   if ((mye=document.getElementById(calname+"larrow")) != null) {
      if (!isIE5) mye.style.cursor = "pointer";
   }

   /*-------------------------------------------------------------------
   --- Set the Month
   --------------------------------------------------------------------*/
   for (var ii=1;ii<=12;ii++) {
      var bd = (100*bdate.year)+bdate.month;
      var ed = (100*edate.year)+edate.month;
      var nd = (100*edate.year)+ii;
      var cd = (100*cdate.year)+cdate.month;

      if ((mye=document.getElementById(calname+"month_"+ii)) == null) continue;
      if (nd >= bd && nd <= ed) {
         mye.style.backgroundColor = ucal_oncolor;
         if (!isIE5) mye.style.cursor = "pointer";
         if (nd <= cd) {
            mye.style.color = "";
         } else {
            mye.style.color = "#999999";
         }
      } else if ((ii <= cdate.month && edate.year == cdate.year) || edate.year < cdate.year) {
         mye.style.backgroundColor = "#e7e7e8";
         if (!isIE5) mye.style.cursor = "pointer";
         mye.style.color = "#ffffff";
      } else {
         mye.style.backgroundColor = "#e7e7e8";
         if (!isIE5) mye.style.cursor = "auto";
         mye.style.color = "#999999";
      }
	  // aktuális hónap border
	  //alert(edate.year+':'+bdate.year+':'+ucal_todaydate.year);
	  if ((edate.year==ucal_todaydate.year) && (ii==ucal_todaydate.month)) {
	     mye.style.border='1px solid black';
	  } else {
	     mye.style.border='';
	  }
	  if ((edate.year==ucal_todaydate.year) && (ii==edate.month)) {
		mye.style.color = "#999999";
	  } else {
		mye.style.color = "#FFFFFF";
	  }
   }

   /*-------------------------------------------------------------------
   --- Set the Day
   --------------------------------------------------------------------*/
   var md = new Date(edate.year,edate.month-1,1);
   var dow = md.getDay();
   var ef1=0;
   var eci=0;
   while (dow != ucal_start_dow) {
      md.setDate(md.getDate()-1);
      dow--;
      if (dow < 0) dow = 6;
   }
   for (var ii=1;ii<=42;ii++) {
      if ((mye=document.getElementById(calname+"day_"+ii)) == null) continue;
      mye.innerHTML = md.getDate();

      var mdate = (100*md.getFullYear())+(md.getMonth()+1);
      mdate = (100*mdate)+md.getDate();

      if (dtype == 5 && cdow && cdow == md.getDay()+1 && 
         mdate <= cdate.date && mdate >= bdate.date && mdate <= edate.date) { 
         mye.style.backgroundColor = ucal_oncolor; 
         mye.style.color           = "#ffffff";
         if (!isIE5) mye.style.cursor = "pointer";
      } else if (dtype != 5 && mdate >= bdate.date && mdate <= true_edate.date) {
         mye.style.backgroundColor = ucal_oncolor; 
         if (mdate <= cdate.date) {
            mye.style.color = "#ffffff";
            if (!isIE5) mye.style.cursor = "pointer";
         } else {
            mye.style.color = "#999999";
            if (!isIE5) mye.style.cursor = "auto";
         }
      } else if (mdate <=  cdate.date) {
         mye.style.backgroundColor = "#e7e7e8";
         mye.style.color           = "";
         if (!isIE5) mye.style.cursor = "pointer";
      } else  {
         if (eci == 0) eci = ii;
         mye.style.backgroundColor = ""; 
         mye.style.backgroundColor = "#e7e7e8";
         mye.style.color           = "#999999";
         if (!isIE5) mye.style.cursor = "auto";
      }
      
      md.setDate(md.getDate()+1);
      if (md.getMonth()+1 > edate.month && (edate.year == md.getFullYear())) ef1=1;
      if (edate.year < md.getFullYear()) ef1=1;
      if (ef1 && md.getDay() == ucal_start_dow) break;
	  if ((edate.year==ucal_todaydate.year) && (edate.month==ucal_todaydate.month) && mye.innerHTML==ucal_todaydate.day && (md.getMonth()+1)==ucal_todaydate.month) {
	     mye.style.border='1px solid black';
	  } else {
	     mye.style.border='';
	  }
   }
   if (eci == 0) eci = ii;

   /*-------------------------------------------------------------------
   --- Show/hide weeks
   --------------------------------------------------------------------*/
   for (var yy=1;yy<=6;yy++) {
      if ((mye=document.getElementById(calname+"week_"+yy)) == null) continue;
      if (yy < ii/6)  mye.style.display = "";
      else mye.style.display = "none";

      if ((mye2=document.getElementById(calname+"warrow_"+yy)) == null) continue;
      if (((yy-1)*7)+1 <= (eci-1)) {
         mye2.src = "http://itthon.hu/site/img/week_arrow.gif";
         if (!isIE5) mye2.style.cursor = "pointer";
      } else {
         mye2.src = "http://itthon.hu/site/img/week_arrow2.gif";
         if (!isIE5) mye2.style.cursor = "auto";
      }
   }

   /*-------------------------------------------------------------------
   --- set the bgcolor of Date Control
   --------------------------------------------------------------------*/

   if ((mye=document.getElementById("ucal_dtselect")) == null) return;
   if (urdsets[0] && urdsets[0].rtype != 1 && urdsets[0].rtype != 20) {
      if ((mye2=document.getElementById("ucal2_dtselect")) == null) return;
      mye2.innerHTML = mye.options[mye.selectedIndex].text;
      ucal_dtc = 0;
      mye.selectedIndex = 0;
      mye.disabled = true;
   } else {
      mye.disabled = false;
   }

   /*-------------------------------------------------------------------
   --- based on report type disable
   --------------------------------------------------------------------*/
   if ((mye=document.getElementById("ucal_comp")) == null) return;
   if (urdsets[0] && urdsets[0].rtype == 3) {
       mye.style.display = "none";
   } else {
       mye.style.display = "";
   }

   return;
}

/*------------------------------------------------------------------
---  toggle the year
------------------------------------------------------------------*/
function ucalYearToggle(cmd,which) {
   var bdate = ucal_bdate;
   var edate = ucal_edate2;
   var cdate = ucal_cdate;
   var dtype = ucal_dtype;

   if (cmd == 1 && edate.year <= cdate.year) { 
//      bdate.year++;
      edate.year++;
   }
   if (cmd == -1) {
//      bdate.year--;
      edate.year--;
   }
   if (cmd == 0 || (cmd == -1 && dtype == 1)) {
      bdate.month = 1;
      bdate.day   = 1;
      ucal_edate.month = 12;
      ucal_edate.day   = 31;
	  ucal_edate.year=edate.year;
      bdate.year  = ucal_edate.year;
	  ucal_dtype = 1; 
	  bdate.setFullDate();
	  ucal_edate.setFullDate();
	  setCalDate(bdate.date,ucal_edate.date);
   }

   edate.setFullDate();

   ucalBuild(which);
   ucal_trigger=1;
   
   return;
}

/*------------------------------------------------------------------
---  toggle the month
------------------------------------------------------------------*/
function ucalMonthToggle(mon,which,norebuild) {
	/*
   var bdate = ucal_bdate;
   var edate = ucal_edate;
   var cdate = ucal_cdate;
   var dtype = ucal_dtype;
   if (which) {
      bdate = ucal2_bdate;
      edate = ucal2_edate;
      dtype = ucal2_dtype;
   }

   if (!mon) mon = bdate.month;
   if (mon > cdate.month && edate.year == cdate.year) { 
      mon = cdate.month;
   }

   if (!norebuild) {
      if (which) ucal2_dtype = 2;
      else       ucal_dtype = 2;
   }

   bdate.month = mon*1;
   edate.month = mon*1;

   bdate.day = 1;
   bdate.year = edate.year;
   edate.day = ucalGetDay(mon);
   bdate.setFullDate();
   edate.setFullDate();
	setCalDate(bdate.date,edate.date);
*/
   var edate = ucal_edate2;
   var cdate = ucal_cdate;
   if (mon > cdate.month && edate.year == cdate.year) { 
      mon = cdate.month;
   }
   edate.month = mon*1;
   edate.day = ucalGetDay(mon);
   edate.setFullDate();

   ucalBuild(which);
   ucal_trigger=1;
   
   return;
}

/*------------------------------------------------------------------
---  toggle the day
------------------------------------------------------------------*/
function ucalDayToggle(day,which,shift) {
   var calname = "ucal_";
   var bdate = ucal_bdate;
   var edate = ucal_edate;
   var edate2 = ucal_edate2;
   var cdate = ucal_cdate;
   var dtype = ucal_dtype;
   if (which) {
      calname = "ucal2_";
      bdate = ucal2_bdate;
      edate = ucal2_edate;
      dtype = ucal2_dtype;
   }

   var mye;
   var cmon=edate2.month;
   var cyear=edate2.year;
   var cday=0;

   if (!day) { 
      cday = bdate.day;
   } else {
      if ((mye=document.getElementById(calname+"day_"+day)) == null) return;
      cday = 1*ucalStripTag(mye.innerHTML);
   }

   if (day <= 15 && cday > 15) {
      cmon = cmon-1;
      if (cmon <= 0) { cyear--; cmon=12; }
   }
   if (day >= 22 && cday < 15) {
      cmon = cmon+1;
      if (cmon > 12) { cyear++; cmon=1; }
   }

   var mdate = 100*((100*cyear)+cmon)+cday;
   if (mdate > cdate.date) return;

   if (which) ucal2_dtype = 4;
   else       ucal_dtype = 4;

   if (!shift) {
	   bdate.year = cyear; bdate.month = cmon; bdate.day = cday;
	   bdate.setFullDate();
   }

   edate.year = cyear; edate.month = cmon; edate.day = cday;
   edate.setFullDate();

	if (shift && (edate.date<bdate.date)) {
	   edate.year = bdate.year; edate.month = bdate.month; edate.day = bdate.day;
	   edate.setFullDate();
	   bdate.year = cyear; bdate.month = cmon; bdate.day = cday;
	   bdate.setFullDate();
	}

	setCalDate(bdate.date,edate.date);

   if (ucal_comp && ucal_dtype != ucal2_dtype) {
      ucalToggleComp(0,1,which,1);
      return;
   }

   ucalBuild(which);
   ucal_trigger=1;
   
   return;
}

/*------------------------------------------------------------------
---  toggle the week
------------------------------------------------------------------*/
function ucalWeekToggle(week,which) {
   var calname = "ucal_";
   var bdate = ucal_bdate;
   var edate = ucal_edate;
   var cdate = ucal_cdate;
   var dtype = ucal_dtype;
   if (which) {
      calname = "ucal2_";
      bdate = ucal2_bdate;
      edate = ucal2_edate;
      dtype = ucal2_dtype;
   }

   var mye;
   var cmon=edate.month;
   var cyear=edate.year;
   if (!week) week = 1;
   if ((mye=document.getElementById(calname+"day_"+week*7)) == null) return;
   var cday = 1*ucalStripTag(mye.innerHTML);

   if (week <= 2 && cday > 15) {
      cmon = cmon-1;
      if (cmon <= 0) { cyear--; cmon=12; }
   }
   if (week >= 3 && cday < 15) {
      cmon = cmon+1;
      if (cmon > 12) { cyear++; cmon=1; }
   }

   var mydate = new Date(cyear, cmon-1, cday);
   mydate.setDate(mydate.getDate()-6);

   var bd = 100*((100*mydate.getFullYear())+mydate.getMonth()+1)+mydate.getDate();
   var ed = 100*((100*cyear)+cmon)+cday;
   if (bd > cdate.date && ed > cdate.date) return;


   if (which) ucal2_dtype = 3;
   else       ucal_dtype = 3;
   bdate.year = mydate.getFullYear(); bdate.month = mydate.getMonth()+1; bdate.day = mydate.getDate();
   edate.year = cyear; edate.month = cmon; edate.day = cday;
   bdate.setFullDate();
   edate.setFullDate();
	setCalDate(bdate.date,edate.date);

   if (ucal_comp && ucal_dtype != ucal2_dtype) {
      ucalToggleComp(0,1,which,1);
      return;
   }

   ucalBuild(which);
   ucal_trigger=1;
   
   return;
}

/*------------------------------------------------------------------
---  toggle the day of the week
------------------------------------------------------------------*/
function ucalDOWToggle(dow,which) {
   var edate = ucal_edate;
   var mydow = dow + ucal_start_dow;
   if (which) edate = ucal2_edate;
   if (mydow > 7) mydow = mydow%7;

   ucalMonthToggle(edate.month,which,1);

   if (which) { ucal2_dtype = 5; ucal2_dow = mydow; }
   else       { ucal_dtype = 5; ucal_dow = mydow; }

   if (ucal_comp && ucal_dtype != ucal2_dtype) {
      ucalToggleComp(0,1,which,1);
      return;
   }

   ucalBuild(which);
   ucal_trigger=1;
   return;
}

/*------------------------------------------------------------------
---  toggle the next and previous
------------------------------------------------------------------*/
function ucalDateToggle(cmd,which) {
   var bdate = ucal_bdate;
   var edate = ucal_edate;
   var cdate = ucal_cdate;
   var dtype = ucal_dtype;
   var dow   = ucal_dow;

   if (which) {
      bdate = ucal2_bdate;
      edate = ucal2_edate;
      dtype = ucal2_dtype;
      dow   = ucal2_dow;
   }

   var mbd = new Date(bdate.year,bdate.month-1,bdate.day);
   var med = new Date(edate.year,edate.month-1,edate.day);
   if (edate.date >= cdate.date && cmd == 1 && dtype != 5) return;

   if (dtype == 1) { 
      var syear = 1;
      if (cmd == -1) syear = -1;
      mbd.setFullYear(mbd.getFullYear()+syear);
      med.setFullYear(med.getFullYear()+syear);
   } else if (dtype == 2) { 
      var smonth = 1;
      if (cmd == -1) smonth = -1;
      med.setDate(1);

      if (isSaf) {
         if (smonth == -1 && mbd.getMonth() == 0) {
            mbd.setMonth(11); mbd.setFullYear(mbd.getFullYear()-1);
            med.setMonth(11); med.setFullYear(med.getFullYear()-1);
         } else {
            mbd.setMonth(mbd.getMonth()+smonth);
            med.setMonth(med.getMonth()+smonth);
         }
      } else {
         mbd.setMonth(mbd.getMonth()+smonth);
         med.setMonth(med.getMonth()+smonth);
      }
      med.setDate(ucalGetDay(med.getMonth()+1));
   } else if (dtype == 3) { 
      var sdays = 7;
      if (cmd == -1) sdays = -7;
      mbd.setDate(mbd.getDate()+sdays);
      med.setDate(med.getDate()+sdays);
   } else if (dtype == 4) {
      var sdays = 1;
      if (cmd == -1) sdays = -1;
      mbd.setDate(mbd.getDate()+sdays);
      med.setDate(med.getDate()+sdays);
   } else if (dtype == 5) {
      if (cmd == -1) dow--;
      else dow++;
      if (dow < 1) dow = 7;
      if (dow > 7) dow = 1;
      if (which) ucal2_dow = dow;
      else       ucal_dow = dow;
   } else if (dtype == 10) { 
      var ndt = new Date(bdate.year,bdate.month-1,bdate.day);
      var nsd = ""+ndt.getFullYear()+(ndt.getMonth()+1)+ndt.getDate();
      var ned = ""+med.getFullYear()+(med.getMonth()+1)+med.getDate();
      var sdays = 1;
      while(nsd != ned) {
         ndt.setDate(ndt.getDate()+1);
         nsd = ""+ndt.getFullYear()+(ndt.getMonth()+1)+ndt.getDate();
         sdays++;
         if (sdays > 365) break;
      }
      if (cmd == -1) sdays = (sdays*-1);
      mbd.setDate(mbd.getDate()+sdays);
      med.setDate(med.getDate()+sdays);
   }

   bdate.year = mbd.getFullYear(); bdate.month = mbd.getMonth()+1; bdate.day = mbd.getDate();
   edate.year = med.getFullYear(); edate.month = med.getMonth()+1; edate.day = med.getDate();
   bdate.setFullDate();
   edate.setFullDate();
	setCalDate(bdate.date,edate.date);

   if (ucal_comp && ucal_dtype != ucal2_dtype) {
      ucalToggleComp(0,1,which,1);
      return;
   }

   ucalBuild(which);
   ucal_trigger=1;
   
   return;
}

/*------------------------------------------------------------------
---  toggle date control view
------------------------------------------------------------------*/
function ucalCToggle(mye) {
   var mye2=null;
   if (!mye) return;
   if ((mye2 = document.getElementById("ucal2_dtselect")) == null) return;
   mye2.innerHTML = mye.options[mye.selectedIndex].text;
   ucal_dtc = (mye.options[mye.selectedIndex].value)*1;
   ucal_trigger=1;
}

/*------------------------------------------------------------------
---
---  Popup Calendar Functions
---
------------------------------------------------------------------*/

/*------------------------------------------------------------------
---  open the popup calendar
------------------------------------------------------------------*/
function ucalOpen(which) {
   var bdate = ucal_bdate;
   var edate = ucal_edate;
   if (which) {
      bdate = ucal2_bdate;
      edate = ucal2_edate;
   }
   ucal_pbdate = new ucalDate(bdate.date);
   ucal_pedate = new ucalDate(edate.date);
   ucal_pbsdate = new ucalDate(bdate.date);
   ucal_pesdate = new ucalDate(edate.date);
   ucalBuildPopup(1);
   ucalBuildPopup(2);

   var pname = "ucal_crange";
   if (which) pname = "ucal_crange2";

   if (ucal_isclosing) return;
   if (ucal_isopen) { ucalClose(); return; }

   var mye1,mye2,mye3;
   if ((mye1 = document.getElementById("ucal_pop")) == null) return ;
   if ((mye2 = document.getElementById(pname)) == null) return ;

   var startx = 0;
   var starty = 0;

   for(var p = mye2; p && p.tagName!='BODY'; p = p.offsetParent){
      startx += p.offsetLeft;
      starty += p.offsetTop;
   }

   ucal_isopen=1;

   mye1.style.position = "absolute";
   mye1.style.left = (startx)+"px";
   mye1.style.top = (starty+18)+"px";

   mye1.style.width = 0;
   mye1.style.height = 0;
   mye1.style.display = "";
   ucal_cpwidth = 0;
   ucal_cpheight = 0;
   ucal_timer = setInterval("ucalAnimateOpen()",10);

   ucal_which = which;
   return;
}

/*------------------------------------------------------------------
---  close the calendar
------------------------------------------------------------------*/
function ucalClose() {
   if (!ucal_isopen) return;
   ucal_isclosing=1;
   var mye;
   if ((mye = document.getElementById("ucal_pop")) == null) reutrn ;

   ucal_cpwidth = 270;
   ucal_cpheight = 150;
   ucal_timer = setInterval("ucalAnimateClose()",10);

   ucal_isopen=0;
}

function ucalCloseHandler(event) {
  var submenuId = 'ucal_pop';
  var toElement = document.getElementById("ucal_pop");

  if (event.relatedTarget) {
    toElement = event.relatedTarget;
  }
  else if (event.toElement) {
    toElement = event.toElement;
  }

  while (toElement && toElement.id != submenuId) {
    toElement = toElement.parentNode;
  }

  if (!toElement) {
    ucalClose(); 
  }
}

/*------------------------------------------------------------------
---  animate the calendar opening
------------------------------------------------------------------*/
function ucalAnimateOpen() {
   ucal_cpwidth += 20;
   ucal_cpheight += 10;

   if (ucal_cpwidth >= 260) { clearInterval(ucal_timer); ucal_timer=0; }

   if ((mye = document.getElementById("ucal_pop")) == null) return;
   if (ucal_timer==0) { 
      if (isns6 && !isSaf) {
         mye.style.width = "";
         mye.style.height = "auto";
      } else if (isOp) {
         mye.style.width = "";
         mye.style.height = "auto";
      } else {
         mye.style.width = "auto";
         mye.style.height = "auto";
      }
   } else {
      mye.style.width = ucal_cpwidth+"px";
      mye.style.height = ucal_cpheight+"px";
   }
}


/*------------------------------------------------------------------
---  animate the calendar closing
------------------------------------------------------------------*/
function ucalAnimateClose() {
   ucal_cpwidth -= 20;
   ucal_cpheight -= 10;

   if (ucal_cpwidth <= 0) { clearInterval(ucal_timer); ucal_timer=0; }

   if ((mye = document.getElementById("ucal_pop")) == null) return;
   if (ucal_timer==0) { 
      mye.style.display = "none";
      mye.style.width = "auto";
      mye.style.height = "auto";
   } else {
      mye.style.width = ucal_cpwidth+"px";
      mye.style.height = ucal_cpheight+"px";
   }
   ucal_isclosing=0;
}


/*------------------------------------------------------------------
---  set the event to close the calendar
------------------------------------------------------------------*/
function ucalSetClose(off) {
   var mye;
   if ((mye = document.getElementById("ubody")) == null) reutrn ;
   if (off) mye.onclick = null;
   else mye.onclick = ucalClose;
}

/*------------------------------------------------------------------
---  build the popup calendar
------------------------------------------------------------------*/
function ucalBuildPopup(which) {
   if (!which) return;
   var id = "";
   var pdate;
   var sdate;

   if (which == 1) { 
      pdate = ucal_pbdate;
      sdate = ucal_pbsdate;
      id    = "pb";
   } else {
      pdate = ucal_pedate;
      sdate = ucal_pesdate;
      id    = "pe";
   }

   var mym = (100*pdate.year)+pdate.month;
   var cym = (100*ucal_cdate.year)+ucal_cdate.month;

   /*-------------------------------------------------------------------
   --- set the dow string
   --------------------------------------------------------------------*/
   for (var ii=0;ii<7;ii++) {
      var mydow = ii+ucal_start_dow;
      if (mydow > 6) mydow = mydow%7;
      if ((mye3 = document.getElementById("ucal"+id+"_dow_"+(ii+1))) != null) {
         mye3.innerHTML = ucal_sdows[mydow];
      }
   }

   /*-------------------------------------------------------------------
   --- Set the year month
   --------------------------------------------------------------------*/
   var mye,mye2;
   if ((mye = document.getElementById("ucal"+id+"_yearmonth")) == null) return;
   if (ucal_flipym) {
      mye.innerHTML = pdate.year + " " + ucal_smonths[pdate.month-1];
   } else {
      mye.innerHTML = ucal_smonths[pdate.month-1] + " " + pdate.year;
   }

   if (mym >= cym) {
      if ((mye=document.getElementById("ucal"+id+"_rarrow")) != null) 
         mye.src = "http://itthon.hu/site/img/right_grey_arrow.gif";
         if (!isIE5) mye.style.cursor = "auto";
   } else {
      if ((mye=document.getElementById("ucal"+id+"_rarrow")) != null) 
         mye.src = "http://itthon.hu/site/img/right_arrow.gif";
         if (!isIE5) mye.style.cursor = "pointer";
   }
   if ((mye=document.getElementById("ucal"+id+"_larrow")) != null) {
      if (!isIE5) mye.style.cursor = "pointer";
   }

   /*-------------------------------------------------------------------
   --- Set the Day
   --------------------------------------------------------------------*/
   var md = new Date(pdate.year,pdate.month-1,1);
   var dow = md.getDay();
   var ef1=0;
   while (dow != ucal_start_dow) {
      md.setDate(md.getDate()-1);
      dow--;
      if (dow < 0) dow = 6;
   }
   for (var ii=1;ii<=42;ii++) {
      if ((mye=document.getElementById("ucal"+id+"_day_"+ii)) == null) continue;
      if (md.getMonth()+1 != pdate.month) mye.innerHTML = "";
      else mye.innerHTML = md.getDate();

      var mdate = (100*md.getFullYear())+(md.getMonth()+1);
      mdate = (100*mdate)+md.getDate();

      if (mdate == sdate.date && md.getMonth()+1 == pdate.month) { 
         mye.style.backgroundColor = ucal_oncolor;
         mye.style.color          = "";
         mye.style.textDecoration = "";
         if (!isIE5) mye.style.cursor = "pointer";
      } else if (mdate >  ucal_cdate.date) { 
         mye.style.backgroundColor = "";
         mye.style.color          = "#999999";
         mye.style.textDecoration = "line-through";
         if (!isIE5) mye.style.cursor = "auto";
      } else {
         mye.style.backgroundColor = "";
         mye.style.color          = "";
         mye.style.textDecoration = "";
         if (!isIE5) {
            if (md.getMonth()+1 != pdate.month) mye.style.cursor = "auto";
            else mye.style.cursor = "pointer";
        }
      }

      md.setDate(md.getDate()+1);
      if (md.getMonth()+1 > pdate.month && (pdate.year == md.getFullYear())) ef1=1;
      if (pdate.year < md.getFullYear()) ef1=1;
      if (ef1 && md.getDay() == ucal_start_dow) break;
   }

   for (var yy=1;yy<=6;yy++) {
      if ((mye=document.getElementById("ucal"+id+"_week_"+yy)) != null)
      if (yy < ii/6)  mye.style.display = "";
      else mye.style.display = "none";
   }

}

/*------------------------------------------------------------------
---  toggle the year month for popup calendar
------------------------------------------------------------------*/
function ucalYearMonthToggle(cmd,which) {
   var sdate;
   if (which == 1) { sdate = ucal_pbdate; }
   if (which == 2) { sdate = ucal_pedate; }

   var mym = (100*sdate.year) + sdate.month;
   var cym = (100*ucal_cdate.year) + ucal_cdate.month;

   if (cmd == 1 && mym >= cym) return; 

   var md = new Date(sdate.year,sdate.month-1,1);
   var month = md.getMonth();
   var year = md.getFullYear();

   if (cmd == 1)  month+=1;
   if (cmd == -1) month-=1;

   if (month < 0)  { year--; month=11; }
   if (month > 11) { year++; month=0; }

   md.setMonth(month);
   md.setFullYear(year);

   sdate.year = md.getFullYear();
   sdate.month = md.getMonth()+1;
   sdate.day = 0;
   sdate.setFullDate();

   ucalBuildPopup(which);
   
   return;
}

/*------------------------------------------------------------------
---  toggle day for the popup
------------------------------------------------------------------*/
function ucalSetDay(day,which) {
   var sdate;
   var pdate;
   var mye;
   var id;

   if (!which) return;
   if (which == 1) { sdate = ucal_pbsdate; pdate = ucal_pbdate; id = "pb"; }
   if (which == 2) { sdate = ucal_pesdate; pdate = ucal_pedate; id = "pe"; }

   if ((mye=document.getElementById("ucal"+id+"_day_"+day)) == null) return;
   var cday = 1*ucalStripTag(mye.innerHTML);

   var md = 100*((100*pdate.year)+pdate.month)+cday;
   if (md > ucal_cdate.date) return;

   sdate.year = pdate.year;
   sdate.month = pdate.month;
   sdate.day = cday;
   sdate.setFullDate();

   if (which == 2 && ucal_pbsdate.date > ucal_pesdate.date) { 
      ucal_pbsdate = new ucalDate(ucal_pesdate.date); ucal_pbdate = new ucalDate(ucal_pesdate.date); ucalBuildPopup(1); 
   }
   if (which == 1 && ucal_pesdate.date < ucal_pbsdate.date) { 
      ucal_pesdate = new ucalDate(ucal_pbsdate.date); ucal_pedate = new ucalDate(ucal_pbsdate.date); ucalBuildPopup(2); 
   }

   ucalBuildPopup(which);
}

/*------------------------------------------------------------------
---  apply the popup date to the report
------------------------------------------------------------------*/
function ucalApplyRange() {
   if (ucal_which) {
      ucal_bdate2 = new ucalDate(ucal_pbsdate.date);
      ucal_edate2 = new ucalDate(ucal_pesdate.date);
      ucal2_bdate = ucal_bdate2;
      ucal2_edate = ucal_edate2;
      ucal2_dtype = 10;
   } else {
      ucal_bdate = new ucalDate(ucal_pbsdate.date);
      ucal_edate = new ucalDate(ucal_pesdate.date);
	  setCalDate(ucal_pbsdate.date,ucal_pesdate.date);
      ucal_dtype = 10;
   }

   ucalClose();
   ucalBuild(ucal_which);
   ucal_trigger=1;
}

function setCalDate(a,b) {
  if (document.getElementById('calendar_bdate')) {
		document.getElementById('calendar_bdate').value=a;
  }
  if (document.getElementById('calendar_edate')) {
		document.getElementById('calendar_edate').value=b;
  }
}

/*------------------------------------------------------------------
---  display the compare calendar
------------------------------------------------------------------*/
function ucalToggleComp(off,on,which,reset) {
   ucalClose();
   var mye1=null,mye2=null,mye3=null,mye4=null;
   var syear=0,smonth=0,sday=0;
   var eyear=0,emonth=0,eday=0;
   if ((mye1 = document.getElementById("ucal2")) == null) return;
   if ((mye2 = document.getElementById("ucal2_optcal")) == null) return;
   if ((mye3 = document.getElementById("ucal2_dtselect")) == null) return;
   if ((mye4 = document.getElementById("ucal_dtselect")) == null) return;
   mye3.innerHTML = mye4.options[mye4.selectedIndex].text;

   if ((mye1.style.display == "none" && !off) || on) {
      var sd=null,ed=null; 

      if (which) {
         sd = new ucalDate(ucal2_bdate.date);
         ed = new ucalDate(ucal2_edate.date);
         ucal_dtype = ucal2_dtype;
         ucal_dow = ucal2_dow;
      } else {
         sd = new ucalDate(ucal_bdate.date);
         ed = new ucalDate(ucal_edate.date);
         ucal2_dtype = ucal_dtype;
         ucal2_dow = ucal_dow;
      }

      var sdate = new Date(sd.year,sd.month-1,sd.day);
      var edate = new Date(ed.year,ed.month-1,ed.day);

      if (ucal_dtype == 3) {
         if (which) {
            sdate.setDate(sdate.getDate()+7);
            edate.setDate(edate.getDate()+7);
         } else {
            sdate.setDate(sdate.getDate()-7);
            edate.setDate(edate.getDate()-7);
         }
      } else if (ucal_dtype == 4) {
         if (which) {
            sdate.setDate(sdate.getDate()+1);
            edate.setDate(edate.getDate()+1);
         } else {
            sdate.setDate(sdate.getDate()-1);
            edate.setDate(edate.getDate()-1);
         }
      } else if (ucal_dtype == 2 || ucal_dtype == 5) {
         if (which) {
            edate.setDate(1);
            sdate.setMonth(sdate.getMonth()+1);
            edate.setMonth(edate.getMonth()+1);
            edate.setDate(ucalGetDay(edate.getMonth()+1));
         } else {
            edate.setDate(1);
            if (isSaf && sdate.getMonth() == 0) {
               sdate.setMonth(11); sdate.setFullYear(sdate.getFullYear()-1);
               edate.setMonth(11); edate.setFullYear(edate.getFullYear()-1);
            } else {
               sdate.setMonth(sdate.getMonth()-1);
               edate.setMonth(edate.getMonth()-1);
            }
            edate.setDate(ucalGetDay(edate.getMonth()+1));
         }
      } else if (ucal_dtype == 1) {
         if (which) {
            sdate.setFullYear(sdate.getFullYear()+1);
            edate.setFullYear(edate.getFullYear()+1);
         } else {
            sdate.setFullYear(sdate.getFullYear()-1);
            edate.setFullYear(edate.getFullYear()-1);
         }
      }

      syear  = sdate.getFullYear();
      smonth = sdate.getMonth()+1;
      sday   = sdate.getDate();
      if (smonth < 10) smonth = "0"+smonth;
      if (sday < 10) sday = "0"+sday;

      eyear  = edate.getFullYear();
      emonth = edate.getMonth()+1;
      eday   = edate.getDate();
      if (emonth < 10) emonth = "0"+emonth;
      if (eday < 10) eday = "0"+eday;

      if (which) {
         ucal_bdate = new ucalDate("" + syear + smonth + sday);
         ucal_edate = new ucalDate("" + eyear + emonth + eday);
      } else {
         ucal2_bdate = new ucalDate("" + syear + smonth + sday);
         ucal2_edate = new ucalDate("" + eyear + emonth + eday);
      }

      ucal_comp=1;
      mye1.style.display = ""; 
      mye2.style.display = ""; 

   } else {
      mye1.style.display = "none"; 
      mye2.style.display = "none"; 
      ucal_comp=0;
   }
   if (reset) {
      ucal_trigger=1;
   }
}

/*------------------------------------------------------------------
---
---  General calendar functions
---
------------------------------------------------------------------*/
function ucalGetDay(mon) {
   var myday=0;
   if (!mon) return;

   if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) myday = 31;
   if (mon == 4 || mon == 6 || mon == 9 || mon == 11) myday = 30;
   if (mon == 2) {
      if (ucal_edate.year%4 == 0) myday = 29;
      else                   myday = 28;
   }
   return myday;
}

function ucalStripTag(html) {
   var idx1=0,idx2=0;
   var myhtml=html;
   if (!myhtml) return;

   if ((idx1=myhtml.indexOf(">")) == -1) return myhtml;
   if ((idx2=myhtml.indexOf("<",idx1+1)) == -1) return myhtml;
   myhtml = myhtml.substring(idx1+1,idx2);
      
   return myhtml;
}

// html code

var isIE5=false;
var isns6=false;
var isOp=false;

var ucal_bdate = new ucalDate(""); 
var ucal_edate = new ucalDate("");
// mutatott hónap:
var ucal_edate2 = new ucalDate("");
var ucal_cdate = new ucalDate("20151231");
var cx = new Date();
var cy = cx.getFullYear();
var cm = cx.getMonth()+1;
var cd = cx.getDate();
if (cm < 10) cm = "0"+cm;
if (cd < 10) cd = "0"+cd;
var ucal_todaydate = new ucalDate(""+cy+cm+cd);


/*
Aitia.Event.addEvent = function(el, evname, func) {
	  if (el.attachEvent) {
			    el.attachEvent("on" + evname, func);
					  } else if (el.addEventListener){
							    el.addEventListener(evname, func, false);   //bubbling
									  }
} // addEvent
*/

term308 = "Nap";
term309 = "Hét";
term310 = "Hónap";
term311 = "Év";
term583 = "Napok";
term584 = "Egyéni";

ucal_smonths[0] = "Jan";
ucal_smonths[1] = "Feb";
ucal_smonths[2] = "Már";
ucal_smonths[3] = "Ápr";
ucal_smonths[4] = "Máj";
ucal_smonths[5] = "Jún";
ucal_smonths[6] = "Júl";
ucal_smonths[7] = "Aug";
ucal_smonths[8] = "Szept";
ucal_smonths[9] = "Okt";
ucal_smonths[10] = "Nov";
ucal_smonths[11] = "Dec";

ucal_sdows[0] = "V";
ucal_sdows[1] = "H";
ucal_sdows[2] = "K";
ucal_sdows[3] = "Sz";
ucal_sdows[4] = "Cs";
ucal_sdows[5] = "P";
ucal_sdows[6] = "Sz";
