var thisdate = new Date ();
var thisDay = thisdate.getDate();
var thisMonth = thisdate.getMonth();
var thisYear = thisdate.getYear();
if (thisYear < 1900)
  thisYear += 1900;
var minYear = thisYear - 80;
var maxYear = thisYear + 20;

var showYear = thisYear;
var showMonth = thisMonth;
var showDay = thisDay;
var selMonth = null;
var dateobj = null;
var holnoclick = false;
var cdatagrid = false;
var savedMonthOptions = null;

function calKeyHandler()
{
	if(window.event.keyCode == 27) 
	   hideCal();
}

function hover (_obj, _hover)
{
   if (!_hover)
   {
      _obj.className = "cal";
   }
   else
   {
      _obj.className = "calhover";
   }
}

function get_days(objdate)
{
   var _objdate = new Date (objdate);
	var monthnr=_objdate.getMonth();
	for(var i=28;i<=31;i++)
	{
		_objdate.setDate(i);
		if(_objdate.getMonth() != monthnr) break;
	}
	return --i;
}

var defaultContent = "";

function SelectMonthYear(range)
{
	var yearIndex = document.getElementById ("year_select").selectedIndex;

	showMonth = document.getElementById ("month_select").selectedIndex;
	showYear = document.getElementById ("year_select")[yearIndex].value;
	
	drawCalContent (range);
	document.getElementById ("month_select").selectedIndex = showMonth;
	document.getElementById ("year_select").selectedIndex = yearIndex;
}

function drawCalContent (range)
{
   if (defaultContent == "")
   {
      defaultContent = document.getElementById ("calcontent").innerHTML;
   }

   var dateStart = null;
   var dateEnd = null;

   if(range)
   {
      var splitted = range.split(";");
      dateStart = new Date(splitted[0]);
      dateEnd = new Date(splitted[1]);
   }

   var month_select = document.getElementById("month_select");
   var year_select = document.getElementById("year_select");

   year_select.value = showYear;
   month_select.value = showMonth;

   if(dateStart != null && dateEnd != null)
   {
      if(dateStart.getFullYear() == dateEnd.getFullYear())
      {
         for (var i = 0; i < month_select.options.length; i++) 
         {
            if(month_select.options[i].value < dateStart.getMonth() || 
               month_select.options[i].value > dateEnd.getMonth())
               month_select.options[i].disabled = true;
            else
               month_select.options[i].disabled = false;
         }
      }
      else if(year_select.value == dateStart.getFullYear())
      {
         for (var i = 0; i < month_select.options.length; i++) 
         {
            if(month_select.options[i].value < dateStart.getMonth())
               month_select.options[i].disabled = true;
            else
               month_select.options[i].disabled = false;
         }
      }
      else if(year_select.value == dateEnd.getFullYear())
      {

         for (var i = 0; i < month_select.options.length; i++) 
         {
            if(month_select.options[i].value > dateEnd.getMonth())
               month_select.options[i].disabled = true;
            else
               month_select.options[i].disabled = false;
         }
      }

      month_select.onchange = function() { SelectMonthYear(range); };
   }
   
   if(dateStart != null && dateEnd != null)
   {
      for (var i = 0; i < year_select.options.length; i++) 
      {
         if(year_select.options[i].value < dateStart.getFullYear() || year_select.options[i].value > dateEnd.getFullYear())
         {
            year_select.options[i] = null;
            i--;
         }
      }

      year_select.onchange = function() { SelectMonthYear(range); };
   }
   
   if(month_select.options[month_select.selectedIndex].disabled)
   {
      for (var i = 0; i < month_select.options.length; i++) 
      {
         if(!month_select.options[i].disabled)
         {
            month_select.value = month_select.options[i].value;
            SelectMonthYear(range);
            break;
         }
      }
   }

   selMonth = new Date(showYear, showMonth, 1);
	preMonth = new Date(showYear, showMonth-1, 1);
	postMonth= new Date(showYear, showMonth+1, 1);

	var firstofMonth = selMonth.getDay();
	if(firstofMonth == 0) 
	   firstofMonth = 7;
	var monthnr = selMonth.getMonth();
   var dayStr = "";

	nrofdays = get_days(selMonth);
	prenrofdays = get_days(preMonth);
	preMonth.setMonth(showMonth-1);
	postnrofdays=get_days(postMonth);
	postMonth.setMonth(showMonth+1);

   for (i = 0; i < 42; i++)
   {
      var monthInIteration = -1;
      var yearInIteration = -1;
      var isDisabledDay = false;
      var currDay = (i - firstofMonth) + 2;
      var daystyle = "";
      var color = "00";
      var today = false;
      var monthinc = 0; 

      if (i < firstofMonth - 1)
      {
         currDay = prenrofdays + (i - firstofMonth) + 2;
         today = thisDay == currDay && preMonth.getMonth() == thisMonth;
         color = (today ? "c0" : "60");
         monthinc = -1;

         monthInIteration = preMonth.getMonth() + 1;
         yearInIteration = preMonth.getFullYear();
      }
      else if (currDay > nrofdays)
      {
         currDay -= nrofdays
         today = thisDay == currDay && postMonth.getMonth() == thisMonth;
         color = (today ? "c0" : "60");
         monthinc = 1;

         monthInIteration = postMonth.getMonth() + 1;
         yearInIteration = postMonth.getFullYear();
      }
      else if (thisDay == currDay)
      { today = monthnr == thisMonth; }

      if(monthInIteration == -1)
      {
         monthInIteration = selMonth.getMonth() + 1;
         yearInIteration = selMonth.getFullYear();
      }

      if (i%7 == 5 || i%7 == 6 || today)
         daystyle = "color:#" + (color == "00" ? "90" : color) + "0000;"
      else
         daystyle = "color:#" + color + color + color + ";";

      if (i%7 == 6)
         daystyle += "border-right:0px; solid #ffffff;";
      if (today)         
         daystyle += "font-weight:bold;";

      if (i % 7 == 0)
         dayStr += (i != 0 ? "</tr>" : "") + "<tr>\r\n";
      
      if(dateStart != null && dateEnd != null)
      {
         var dateNow = new Date(monthInIteration + "-" + currDay + "-" + yearInIteration)

         if(dateNow < dateStart || dateNow > dateEnd)
            isDisabledDay = true;
      }

      if(isDisabledDay)
         dayStr += '<td class="cal" style="' + daystyle + ';cursor:default;color:#C2C2C2"><span class="cal">' + currDay + '</span></td>\r\n';
      else
         dayStr += '<td class="cal" style="' + daystyle + '"><span class="cal" onMouseOver="hover(this, true);" onMouseOut="hover(this);" onclick="selectDay (' + currDay + ', ' + monthinc + ')">' + currDay + '</span></td>\r\n';
   }

	document.getElementById ("calcontent").innerHTML =	defaultContent.replace ("<!--content-->", dayStr);
}

function selectDay (daynr, minc)
{
   selMonth.setMonth (selMonth.getMonth() + minc);
	dateStr = daynr + "." + (selMonth.getMonth() + 1) + "." + selMonth.getFullYear();
	dateobj.value = dateStr;
	//korrektur für wiederholten start
	showYear = selMonth.getFullYear();
	showMonth = selMonth.getMonth();
	extFunc(); 
   CalendarCallback(dateobj, dateStr);
	hideCal ();
}

function redrawYears (_selType)
{
   var yearSelector = document.getElementById ("year_select");
   minYear = thisYear - (_selType < 1 ? 80 : 0);
   maxYear = thisYear + (_selType > -1 ? 10 : 0);

   while (yearSelector.firstChild)
      yearSelector.removeChild (yearSelector.firstChild);
   
   for (i = minYear; i <= maxYear; i++)
   {
      newOpt = new Option(i+"", i+"", i == showYear, true);
      yearSelector.options[yearSelector.length] = newOpt;
   }
}

var selType = -99;
var oldOnclick = null;
var calOpen = false;

var mozillaMouseX = 0;
var mozillaMouseY = 0;
function mozillaWindowEvent(evt)
{
   if (evt)
   {
      mozillaMouseX = evt.screenX - window.screenX;
      mozillaMouseY = evt.screenY - window.screenY;
   }
}
document.onmousedown = mozillaWindowEvent;

function showCal (target, xpos, ypos, _selType, _debug, range)
{
   if (window.event || mozillaMouseY)
   {
      xpos = 0;
      ypos = 0;
   }
   return showCalPos (target, xpos, ypos, _selType, _debug, range);
}

function showCalPos (target, xpos, ypos, _selType, _debug, range)
{
   var scrollTop = document.body.scrollTop;
   var scrollLeft = document.body.scrollLeft;
   
   if (window.pageYOffset != 'undefined' && scrollTop == 0 && scrollLeft == 0)
   {
      scrollTop = document.documentElement.scrollTop;
      scrollLeft = document.documentElement.scrollLeft;
   }
   else if (document.documentElement && scrollTop == 0 && scrollLeft == 0)
   {
      scrollTop = document.documentElement.scrollTop;
      scrollLeft = document.documentElement.scrollLeft;
   }

   var mouseX = 0;
   var mouseY = 0;
   if (!window.event && mozillaMouseX)
   {
      mouseX = mozillaMouseX;
      mouseY = mozillaMouseY;
      mozillaMouseX = 0;
      mozillaMouseY = 0;
   }
   
   if (window.event)
   {
      mouseX = window.event.clientX;
      mouseY = window.event.clientY;
   }
   else if (calOpen)
   {
      hideCal ();
      return;
   }

   if (!xpos)
   {
      xpos = mouseX + scrollLeft - 180;
      if (xpos < 0)
         xpos = 5;
   }
   if (!ypos)
   {
      ypos = mouseY + scrollTop - 110;
      if (ypos < 0)
         ypos = 5;
   }

   if (!_selType)
      _selType = 0;
   if (selType != _selType)
      redrawYears (_selType)

	dateobj = eval(target);
   //holnoclick = hideholiday ? true : false;
   drawCalContent (range);

	document.getElementById ("calendar").onKeyDown = document.body.onkeydown;
	document.body.onkeydown = calKeyHandler;
	oldOnclick = document.body.onclick;
	document.body.onclick = clickEvent;

   var calDiv = document.getElementById ("calendar");
   calDiv.style.display = "inline";
   calDiv.style.left = xpos + "px";
   calDiv.style.top = ypos + "px";
   calOpen = true;
}

function clickEvent ()
{
   if (window.event && window.event.srcElement.tagName == "TD" && window.event.srcElement.className != "cal")
      hideCal ();
}


function hideCal ()
{
   document.body.onkeydown = document.getElementById ("calendar").onkeydown;
	document.body.onclick = oldOnclick;
	oldOnclick = null;
	calOpen = false;
   
   var calDiv = document.getElementById ("calendar");
   calDiv.style.display = "none";
}

function CalendarCallback()
{
	return;
}

function extFunc()
{
	return;
}
