var fixedX = -1, fixedY = -1;
var showToday = 1
var gotoString = "Przejdź do bieżącego miesiąca"
var todayString = "Dziś jest"
var closeString = "Zamknij kalendarz"
var scrollLeftMessage = "Kliknij by przewinąć na poprzedni miesiąc. Przytrzymaj wciśnięty przycisk myszki by przewijać w sposób ciągły."
var scrollRightMessage = "Kliknij by przewinąć na następny miesiąc. Przytrzymaj wciśnięty przycisk myszki by przewijać w sposób ciągły."
var scrollLeftYearMessage = "Kliknij by przewinąć na poprzedni rok. Przytrzymaj wciśnięty przycisk myszki by przewijać w sposób ciągły."
var scrollRightYearMessage = "Kliknij by przewinąć na następny rok. Przytrzymaj wciśnięty przycisk myszki by przewijać w sposób ciągły."
var scrollLeftShortMessage = 'Poprzedni miesiąc';
var scrollRightShortMessage = 'Następny miesiąc';
var scrollLeftShortYearMessage = 'Poprzedni rok';
var scrollRightShortYearMessage = 'Następny rok';
var selectMonthMessage = "Kliknij lub przytrzymaj by wybrać miesiąc"
var selectYearMessage = "Kliknij by wybrać rok"
var selectDateMessage = "Wybierz dzień [date]"

var dayName = new Array ("Pn","Wt","Śr","Cz","Pt","So","Nd")
var dayName2 = new Array ("Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota","Niedziela")
var monthName = new Array("Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień",
									"Wrzesień", "Październik", "Listopad", "Grudzień")
var monthName2 = new Array("Stycznia", "Lutego", "Marca", "Kwietnia", "Maja", "Czerwca", "Lipca", "Sierpnia",
 							 "Września", "Października", "Listopada", "Grudnia")

var crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, daySelected, omonthSelected, oyearSelected, odaySelected;
var monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlNow, nStartingYear;
var ctlDay, ctlMonth, ctlYear;
var formName;
var beginYear, endYear;
var bPageLoaded=false, bShow = false;

var ie=document.all;
var dom=document.getElementById;
var ns4=document.layers

var today = new Date();
var dateNow = today.getDate()
var monthNow = today.getMonth()
var yearNow = today.getYear() 
 
if(yearNow < 1000)
{
    yearNow += 1900;
}

function hideElement( elmID, overDiv)
{
    if( ie )
    {
        for( i = 0; i < document.all.tags( elmID ).length; i++ )
        {
            obj = document.all.tags( elmID )[i];
            if ( !obj || !obj.offsetParent ) 
                continue;
            
            objLeft   = obj.offsetLeft;
            objTop    = obj.offsetTop;
            objParent = obj.offsetParent;
            
            while( objParent.tagName.toUpperCase() != "BODY")
            {
                objLeft  += objParent.offsetLeft;
                objTop   += objParent.offsetTop;
                objParent = objParent.offsetParent;
            }
            
            objHeight = obj.offsetHeight;
            objWidth = obj.offsetWidth;
            
            if(( overDiv.offsetLeft + overDiv.offsetWidth ) <= objLeft );
            else if(( overDiv.offsetTop + overDiv.offsetHeight + 50 ) <= objTop );
            else if( overDiv.offsetTop >= ( objTop + objHeight ));
            else if( overDiv.offsetLeft >= ( objLeft + objWidth ));
            else
                obj.style.visibility = "hidden";
        }
    }
}
 
function showElement( elmID )
{
    if( ie )
    {
        for( i = 0; i < document.all.tags( elmID ).length; i++ )
        {
            obj = document.all.tags( elmID )[i];
        
            if( !obj || !obj.offsetParent )
                continue;
            obj.style.visibility = "";
        }
    }
}

if (dom)
{	
	
	document.write ("<div onclick='bShow=true' id='calendar' style='z-index:+999;position:absolute;visibility:hidden;'><table width=220	class=calTable><tr><td class=calTopRow><table width=218><tr><td class=calTopControls><span id='caption'></span></td><td align=center><span onMouseOver='this.className=\"calCloseOver\";window.status=\""+closeString+".\";' onMouseOut='this.className=\"calClose\";window.status=\"\";' onClick='javascript:hideCalendar()' class=calClose title='"+closeString+"'>X</span></td></tr></table></td></tr><tr><td class=calContent><span id='content'></span></td></tr>")
	if (showToday==1)
	{
		document.write ("<tr><td class=calBottomRow><span id='lblToday'></span></td></tr>")
	}
	document.write ("</table></div><div id='selectMonth' style='z-index:+999;position:absolute;visibility:hidden;'></div><div id='selectYear' style='z-index:+999;position:absolute;visibility:hidden;'></div>");
}

function initCalendar()	
{
	if (!ns4)
	{
		crossobj=(dom)?document.getElementById("calendar").style : ie? document.all.calendar : document.calendar
		hideCalendar()

		crossMonthObj=(dom)?document.getElementById("selectMonth").style : ie? document.all.selectMonth	: document.selectMonth

		crossYearObj=(dom)?document.getElementById("selectYear").style : ie? document.all.selectYear : document.selectYear

		monthConstructed=false;
		yearConstructed=false;

		if (showToday==1)
		{
			document.getElementById("lblToday").innerHTML =	todayString + " <a onMouseOver='window.status=\""+gotoString+".\"; return true;' onMouseOut='window.status=\"\"' title='"+gotoString+"' class=calA  href='javascript:gotoToday();'>" + dateToString(today.getDay(),dateNow,monthNow,yearNow) + "</a>"
		}

		sHTML1 ="<span id='spanLeft2' class=calControl onmouseover='this.className=\"calControlOver\";window.status=\""+scrollLeftYearMessage+"\"' onclick='javascript:decOneYear()' onmouseout='clearInterval(intervalID1);this.className=\"calControl\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecYear()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)' title=\""+scrollLeftShortYearMessage+"\"'>&nbsp&lt;&lt;&nbsp</span>&nbsp;"
		sHTML1+="<span id='spanYear' class=calYear></span>&nbsp;"
		sHTML1+="<span id='spanRight2' class=calControl  onmouseover='this.className=\"calControlOver\";window.status=\""+scrollRightYearMessage+"\"' onmouseout='clearInterval(intervalID1);this.className=\"calControl\";window.status=\"\"' onclick='incOneYear()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncYear()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)' title=\""+scrollRightShortYearMessage+"\"'>&nbsp&gt;&gt;&nbsp</span>&nbsp;&nbsp;"
		sHTML1+="<span id='spanLeft' class=calControl onmouseover='this.className=\"calControlOver\";window.status=\""+scrollLeftMessage+"\"' onclick='javascript:decMonth()' onmouseout='clearInterval(intervalID1);this.className=\"calControl\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)' title=\""+scrollLeftShortMessage+"\"'>&nbsp&lt;&nbsp</span>&nbsp;"
		sHTML1+="<span id='spanMonth' class=calMonth></span>&nbsp;"
		sHTML1+="<span id='spanRight' class=calControl  onmouseover='this.className=\"calControlOver\";window.status=\""+scrollRightMessage+"\"' onmouseout='clearInterval(intervalID1);this.className=\"calControl\";window.status=\"\"' onclick='incMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)' onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)' title=\""+scrollRightShortMessage+"\"'>&nbsp&gt;&nbsp</span>&nbsp"

		document.getElementById("caption").innerHTML  =	sHTML1

		bPageLoaded=true
	}
}

function gotoToday()
{
    if(yearNow > endYear || yearNow < beginYear)
        return;
        
    monthSelected=monthNow;
    yearSelected=yearNow;
    daySelected=dateNow;
    constructCalendar();
}

function hideCalendar()	
{
    if(crossobj != null) crossobj.visibility="hidden"
    if (crossMonthObj != null)
        crossMonthObj.visibility="hidden";
    if (crossYearObj != null)
        crossYearObj.visibility="hidden";
    
    showElement( 'SELECT' );
    showElement( 'APPLET' );
}

function padZero(num) 
{
	return (num	< 10)? '0' + num : num ;
}

function constructDate(d,m,y)
{
	return y+'-'+padZero(m+1)+'-'+padZero(d);
}

function closeCalendar() 
{
	hideCalendar();
    for (var i = 0; i < ctlYear.length; i++)
        if(ctlYear.options[i].value == yearSelected)
            ctlYear.options[i].selected = true;
    for (var i = 0; i < ctlMonth.length; i++)
        if(ctlMonth.options[i].value == padZero(monthSelected+1))
            ctlMonth.options[i].selected = true;
    for (var i = 0; i < ctlDay.length; i++)
        if(ctlDay.options[i].value == padZero(daySelected))
            ctlDay.options[i].selected = true;
}

function StartDecMonth()
{
	intervalID1=setInterval("decMonth()",100)
}

function StartIncMonth()
{
	intervalID1=setInterval("incMonth()",100)
}

function StartDecYear()
{
	intervalID1=setInterval("decOneYear()",100)
}

function StartIncYear()
{
	intervalID1=setInterval("incOneYear()",100)
}

function incOneYear ()
{
if (yearSelected < endYear)
    {
        yearSelected++;
    }
	constructCalendar()
}

function decOneYear ()
{
if (yearSelected > beginYear)
    {
        yearSelected--;
    }
	constructCalendar()
}

function incMonth () 
{
	if (monthSelected >=11 || yearSelected > endYear || yearSelected < beginYear) 
    {
        if (yearSelected < beginYear)
        {
            monthSelected=0;
            yearSelected=beginYear;
        }
        else if (yearSelected < endYear)
        {
            monthSelected=0;
            yearSelected++;
        }
        else
        {
            monthSelected=11;
            yearSelected=endYear;
        }
	}
    else 
    {
        monthSelected++;
    }
	constructCalendar()
}

function decMonth () 
{
	if (monthSelected <= 0 || yearSelected < beginYear || yearSelected > endYear) 
    {
        if (yearSelected > endYear)
        {
            monthSelected = 11;
            yearSelected = endYear;
        }
        else if (yearSelected > beginYear)
        {
            monthSelected=11;
            yearSelected--;
        }
        else
        {
            monthSelected=0;
            yearSelected=beginYear;
        }
	}
    else 
    {
        monthSelected--;
    }
	constructCalendar()
}

function constructMonth() 
{
	popDownYear()
	if (!monthConstructed) 
    {
		sHTML =	""
		for	(i=0; i<12;	i++) 
        {
			sName = monthName[i];
			if (i==monthSelected)
            {
				sName =	"<B>" +	sName +	"</B>"
			}
			sHTML += "<tr><td id='m" + i + "' onmouseover='this.className=\"calComboOver\";' onmouseout='this.className=\"\"' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
		}
		document.getElementById("selectMonth").innerHTML = "<table width=70	class=calCombo cellspacing=0 onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +	sHTML +	"</table>"
		monthConstructed=true
	}
}



function incYear() 
{
    if(nStartingYear + 7 > endYear)
    {
        bShow=true;
        return;
    }
    for(i=0; i<7; i++)
    {
        newYear = (i+nStartingYear)+1;
        if (newYear==yearSelected)
        { 
            txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;";
        }
        else
        { 
            txtYear =	"&nbsp;" + newYear + "&nbsp;"; 
        }
        document.getElementById("y"+i).innerHTML = txtYear;
    }
    nStartingYear ++;
    bShow=true;
}

function decYear()
{
    if (nStartingYear <= beginYear)
    {
        bShow = true;
        return;
    }
	for	(i=0; i<7; i++)
    {
		newYear	= (i+nStartingYear)-1
		if (newYear==yearSelected)
		{ 
            txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" 
        }
		else
		{ 
            txtYear =	"&nbsp;" + newYear + "&nbsp;" 
        }
		document.getElementById("y"+i).innerHTML = txtYear
	}
	nStartingYear --;
	bShow=true
}

function selectYear(nYear) 
{
	yearSelected=parseInt(nYear+nStartingYear);
	yearConstructed=false;
	constructCalendar();
	popDownYear();
}

function constructYear() 
{
	popDownMonth()
	sHTML =	""
	//if (!yearConstructed) 
  //  {
        if(yearSelected < beginYear)
            yearSelected = beginYear;
        if(yearSelected > endYear)
            yearSelected = endYear;
            
        if (endYear - beginYear > 7)
        {
            sHTML =	"<tr><td align='center' onmouseover='this.className=\"calComboOver\";' onmouseout='this.className=\"\"' 	onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>";
        }

        if(endYear - yearSelected < 3)
            nStartingYear = endYear - 6;
        else
            nStartingYear =	yearSelected-3;
        if (nStartingYear < beginYear)
            nStartingYear = beginYear;
            
		j = 0;
		for (i=nStartingYear; i<(nStartingYear + 7) && i <= endYear; i++) 
        {
			sName =	i;
			if (i==yearSelected){
				sName =	"<B>" +	sName +	"</B>"
			}

			sHTML += "<tr><td id='y" + j + "' onmouseover='this.className=\"calComboOver\";' onmouseout='this.className=\"\"' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
			j ++;
		}

        if (endYear - beginYear > 7)
        {
            sHTML += "<tr><td align='center' onmouseover='this.className=\"calComboOver\";' onmouseout='this.className=\"\"' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>";
        }

		document.getElementById("selectYear").innerHTML	= "<table width=44 class=calCombo onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>"	+ sHTML	+ "</table>"

		yearConstructed = true;
	//}
}



function constructCalendar () 
{
	var aNumDays = Array (31,0,31,30,31,30,31,31,30,31,30,31)

	var dateMessage
	var startDate = new Date (yearSelected,monthSelected,1)
	var endDate

	if (monthSelected==1)
	{
		endDate	= new Date (yearSelected,monthSelected+1,1);
		endDate	= new Date (endDate - (24*60*60*1000));
		numDaysInMonth = endDate.getDate()
	}
	else
	{
		numDaysInMonth = aNumDays[monthSelected];
	}

	datePointer	= 0
	dayPointer = startDate.getDay() - 1
	
	if (dayPointer<0)
	{
		dayPointer = 6
	}

	sHTML =	"<table border=0 width='100%'><tr>"

	for (i=0; i<7; i++)	
    {
		sHTML += "<td class=calTdHead>"+ dayName[i]+"&nbsp;</td>"
	}
	sHTML +="</tr><tr>"
	
	for ( var i=1; i<=dayPointer;i++ )
	{
		sHTML += "<td>&nbsp;</td>"
	}

	for ( datePointer=1; datePointer<=numDaysInMonth; datePointer++ )
	{
		dayPointer++;
		sHTML += "<td ";
        
		if (datePointer==dateNow && monthSelected==monthNow && yearSelected==yearNow)
            sHTML += "class=calToday ";
		else if	(dayPointer % 7 == 6 || dayPointer % 7 == 0)
            sHTML += "class=calHoliday ";
        else
            sHTML += "class=calOrdDay ";
            
        sHint = selectDateMessage.replace("[date]",constructDate(datePointer,monthSelected,yearSelected));
		dateMessage = "onmouseover='window.status=\""+sHint+"\";return true;' onmouseout='window.status=\"\"' "
        
        sHTML += dateMessage+" title=\"" + sHint + "\" onClick='javascript:daySelected="+datePointer + ";closeCalendar();'";

		if (datePointer==odaySelected && monthSelected==omonthSelected && yearSelected==oyearSelected)
            sHTML+="><span class=calDaySelected>&nbsp;" + datePointer + "&nbsp;</span>";  
        else
            sHTML +=">&nbsp;" + datePointer + "&nbsp;";
            
        sHTML += "</td>";

        if (dayPointer % 7 == 0) 
			sHTML += "</tr><tr>" 
	}

	document.getElementById("content").innerHTML   = sHTML;
	document.getElementById("spanMonth").innerHTML = "&nbsp;" +	monthName[monthSelected] + "&nbsp;"
	document.getElementById("spanYear").innerHTML = "&nbsp;" + yearSelected	+ "&nbsp;"
}

function popUpCalendar(ctlName, sYear, eYear)
{

    var leftpos=0;
    var lefttmp=0;
    var toppos=0;
    var toptmp=0;
    var ctl = document.getElementById(ctlName); // obiekt obrazka

	if (bPageLoaded)
	{
		if ( crossobj.visibility ==	"hidden" ) 
        {
            // obiekt selecta
            ctlYear = document.getElementById(ctlName+'_y');
            ctlMonth = document.getElementById(ctlName+'_m');
            ctlDay = document.getElementById(ctlName+'_d');
            
            daySelected = ctlDay.options[ctlDay.selectedIndex].value;
            monthSelected = ctlMonth.options[ctlMonth.selectedIndex].value;
            yearSelected = ctlYear.options[ctlYear.selectedIndex].value;
            
            daySelected = parseInt(daySelected, 10);
            monthSelected = parseInt(monthSelected, 10) - 1;
            yearSelected = parseInt(yearSelected, 10);
            
            if(sYear <= eYear) 
            {
                beginYear = sYear;
                endYear = eYear;
            }
            else 
            {
                beginYear = eYear;
                endYear = sYear;
            }
            
			if (isNaN(daySelected)||isNaN(monthSelected)||isNaN(yearSelected))
			{
				daySelected = dateNow
				monthSelected = monthNow
				yearSelected = yearNow
			}
            
            if(yearSelected > endYear)
            {
                daySelected = 31;
                monthSelected = 11;
                yearSelected = endYear;
            }
            if(yearSelected < beginYear)
            {
                daySelected = 1;
                monthSelected = 0;
                yearSelected = beginYear;
            }
            
			odaySelected=daySelected
			omonthSelected=monthSelected
			oyearSelected=yearSelected

			aTag = ctl
			do {
				aTag = aTag.offsetParent;
				leftpos	+= aTag.offsetLeft;
				toppos += aTag.offsetTop;
			} while(aTag.tagName!="BODY");

      lefttmp = ctl.offsetLeft + leftpos + ctl.offsetWidth + 25;
      crossobj.left = fixedX==-1 ? (lefttmp + 180 > document.body.offsetWidth ? document.body.offsetWidth - 250 : lefttmp) : fixedX;
      toptmp = ctl.offsetTop + toppos - 160;
      
      
      crossobj.top = fixedY==-1 ? (toptmp < document.body.scrollTop ? document.body.scrollTop + 5 : toptmp) : fixedY
			constructCalendar ();
			crossobj.visibility=(dom||ie)? "visible" : "show"

			hideElement( 'SELECT', document.getElementById("calendar") );
			hideElement( 'APPLET', document.getElementById("calendar") );			
		}
		else
		{
			hideCalendar()
			if (ctlNow !=ctl) 
            {
                popUpCalendar(ctlName)
            }
		}
		ctlNow = ctl
		
		//malczu
		var oCalendar = document.getElementById('calendar');      
	      if(oCalendar){
	      	var myTop = toptmp + oCalendar.offsetHeight - 20;
	      	oCalendar.style.top =  myTop+'px';
	      	oCalendar.style.left =  lefttmp+'px';
	      }
	}
}

document.onkeypress = function hidecal1() 
{ 
	if (window.event && window.event.keyCode==27) 
	{
		hideCalendar()
	}
}

document.onclick = function hidecal2() 
{ 		
	if (!bShow)
	{
		hideCalendar()
	}
	bShow = false
}

function dateToString (week_day, c_day, c_month, c_year) 
{
    return "" + dayName2[(week_day-1==-1)?6:(week_day-1)] + ", " + c_day + " " + monthName2[c_month]	+ "	" +	c_year;
}

function clearCalendar( ctlName )
{
    document.getElementById(ctlName+'_y').options[0].selected = true;
    document.getElementById(ctlName+'_m').options[0].selected = true;
    document.getElementById(ctlName+'_d').options[0].selected = true;
    if( document.getElementById(ctlName+'_g') ) document.getElementById(ctlName+'_g').options[0].selected = true;
}

