/////////////////////////////////////////////////////////////////////
var clear_calendar  = 0;
var obj_calendar  = '';
var data_calendar;
var picker_is_opened=false;

var today_cal = new Date();
var start_day   = today_cal.getDate();
var start_month = today_cal.getMonth();
var start_year  = today_cal.getYear();
var sel_day   = today_cal.getDate();
var sel_month = today_cal.getMonth();
var sel_year  = today_cal.getYear();

var cal_url = "";

if (start_year<1000) {
	start_year+=1900;
}
if (sel_year<1000) {
	sel_year+=1900;
}

/////////////////////////////////////////////////////////////////////

function Cal_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/////////////////////////////////////////////////////////////////////

function styl() {
  var output='';
  /*output += '<STYLE>';
  output += 'INPUT.Cal {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana';
  output += '}';
  output += 'SELECT.Cal {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: MS Shell Dlg';
  output += '}';
  output += 'TD.Cal {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana';
  output += '}';
  output += 'a.Cal:link {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana;';
  output += '   text-decoration: none;';
  output += '   color: #000000;';
  output += '}';
  output += 'a.Cal:active {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana;';
  output += '   text-decoration: none;';
  output += '   color: #000000;';
  output += '}';
  output += 'a.Cal:visited {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana;';
  output += '   text-decoration: none;';
  output += '   color: #000000;';
  output += '}';
  output += 'a.Cal:hover {';
  output += '	FONT-SIZE: 8pt; FONT-FAMILY: Verdana;';
  output += '   text-decoration: none;';
  output += '   color: #666666;';
  output += '}';
  output += '</STYLE>';*/
  return output;
}

/////////////////////////////////////////////////////////////////////

function padout(number) { return (number < 10) ? '0' + number : number; }

/////////////////////////////////////////////////////////////////////

function Navigation(Day,Month,Year) {

  var output = '';
  var rows = 1;
  var today = new Date();

  if (clear_calendar!=1) {
  	wysokosc=180;
	wysokosc2=160;
  }
  else {
  	wysokosc=200;
	wysokosc2=180;
  }

  //output += '<TABLE BORDER=0 cellpadding=0 cellspacing=0 width=100% height='+wysokosc+' bgcolor=#ffffff><TR><TD class=Cal ALIGN=CENTER WIDTH=100%>';

  output += '<TABLE BORDER=0 cellpadding=0  cellSpacing=0 width=100% height=30 style1="background: #EFEBEF"><TR class=CalHead>';
  //output += '<TD class=Cal ALIGN=CENTER WIDTH=20>';
  //output += ' <a class=Cal href=\'javascript: moveBackward()\'><img src="i/drop_left.gif" border="0"/></a> </TD>';
  output += '<TD class=Cal ALIGN=CENTER>';

  var tylko_odczyt='';
  if (document.all) {
	  tylko_odczyt='';
  }
  else {
	  //tylko_odczyt='disabled'
  }
  output += '<SELECT '+tylko_odczyt+' class=Cal1 NAME="Month" id="Month" onChange="changeMonth();" style2="width: 80">';

  for (month=0; month<12; month++)
   {
    if ((month) == Month) output += '<OPTION VALUE="' + (month) + '" SELECTED>' + names[month] + '<\/OPTION>';
    else                output += '<OPTION VALUE="' + (month) + '">'          + names[month] + '<\/OPTION>';
   }

  output += '<\/SELECT><SELECT  '+tylko_odczyt+'  class=Cal2 NAME="Year" id="Year" onChange="changeYear();" style2="width: 50">';

  var ystart = today.getYear();

  if (parseInt(ystart)<1000) {
    ystart = parseInt(ystart)+1900;
  }
  if (parseInt(Year)<1000) {
	Year = parseInt(Year)+1900;
  }
  var yend = ystart + 5;

  for (year=ystart-5; year<yend; year++)
   {
    if (year == Year) output += '<OPTION VALUE="' + year + '" SELECTED>' + year + '<\/OPTION>';
    else              output += '<OPTION VALUE="' + year + '">'          + year + '<\/OPTION>';
   }

  output += '<\/SELECT>';
  //output += '</TD><TD class=Cal ALIGN=CENTER width=17>';
  //output += '<a class=Cal href=\'javascript: moveForward()\'><img src="i/drop_right.gif" border="0"/></a> ';
  output += '<\/TD><\/TR><\/TABLE>';

  return output;

}
function Calendar(Day,Month,Year) {
  var output = '';
  var rows = 1;
  var today = new Date();

  if (clear_calendar!=1) {
  	wysokosc=180;
	wysokosc2=140;
  }
  else {
  	wysokosc=200;
	wysokosc2=180;
  }

  firstDay = new Date(Year,Month,1);
  startDay = firstDay.getDay() -1 ;
  if (startDay < 0) startDay = 6;

  if (((Year % 4 == 0) && (Year % 100 != 0)) || (Year % 400 == 0))
       days[1] = 29;
  else
       days[1] = 28;


  output += '<TABLE CALLSPACING=0 CELLPADDING=0 BORDER=0 class=CalPanel height2='+wysokosc2+'><TR>';

  for (i=0; i<6; i++)
      output += '<TD width=18 class=Cal ALIGN=CENTER VALIGN=MIDDLE><B id=v9>' + dow[i] +'<\/B><\/TD>';

  output += '<TD width=18 class=Cal ALIGN=CENTER VALIGN=MIDDLE><B id=v9><FONT COLOR="#FF0000">' + dow[i] +'<\/FONT><\/B><\/TD>';

  output += '<\/TR><TR height1=20 ALIGN=CENTER VALIGN=MIDDLE>';

  var column = 0;
  var lastMonth = Month - 1;
  if (lastMonth == -1) lastMonth = 11;

  for (i=0; i<startDay; i++, column++)
      output += '<TD class=Cal><\/TD>';

  var yellow_span = '';
  for (i=1; i<=days[Month]; i++, column++)
   {

		var orange_border='';
		if (typeof notatki_kalendarza != 'undefined') {
			var dateToCheck = Year+"-";
			if ((parseInt(Month)+1)<10) {
				dateToCheck += "0"+(parseInt(Month)+1)+"-";
			}
			else {
				dateToCheck += (parseInt(Month)+1)+"-";
			}
			if (i<10) {
				dateToCheck += "0"+i;
			}
			else {
				dateToCheck += i;
			}
			orange_border=' style="border: dotted 1px #ECE9DB"';
			if (notatki_kalendarza.exists(dateToCheck)) {
				orange_border = ' style="border: dotted 1px #ff6820"';
			}
		}
   	    if (i == sel_day && (Month == sel_month)  && (Year == sel_year)) {
			yellow_span = ' style="background: #ffff00"';
		}
		else {
			yellow_span = '';
		}
    if (column == 6)
      if (i == today.getDate())
   	    output += '<TD width=18 class=Cal onmouseover="this.style.color=\'#999999\'" onmouseout="this.style.color=\'#000000\'" onclick=";changeDay(' + i + ')" '+yellow_span+orange_border+' style="cursor: hand">' + '<B id=v9><FONT COLOR="#FF0000">' + i + '<\/FONT><\/b><\/A>' +'<\/TD>';
      else
        output += '<TD width=18 class=Cal onmouseover="this.style.color=\'#999999\'" onmouseout="this.style.color=\'#000000\'" onclick=";changeDay(' + i + ')" '+yellow_span+orange_border+' style="cursor: hand">' + '<B id=v9><FONT COLOR="#FF0000">' + i + '<\/FONT><\/b><\/A>' +'<\/TD>';
  	else
     {
      if (Month == 123)
        output += '<TD width=18 class=Cal '+orange_border+'>' + '<a class=Cal HREF="javascript:changeDay(' + i + ')"><B id=v9>' + i + '<\/b><\/A>' +'<\/TD>';

      else
       {
	    if (i == sel_day && (Month == sel_month)  && (Year == sel_year)) {
			yellow_span = ' style="background: #ffff00"';
		}
		else {
			yellow_span = '';
		}
        if (i == today.getDate())
       		output += '<TD width=18 class=Cal onmouseover="this.style.color=\'#999999\'" onmouseout="this.style.color=\'#000000\'" onclick=";changeDay(' + i + ')" '+yellow_span+orange_border+' style="cursor: hand">' + '<b id=v10>' + i + '<\/b><\/A>' +'<\/TD>';
        else
          output += '<TD width=18 class=Cal  onmouseover="this.style.color=\'#999999\'" onmouseout="this.style.color=\'#000000\'" onclick="changeDay(' + i + ')" '+yellow_span+orange_border+' style="cursor: hand">' + '' + i + '<\/A>' +'<\/TD>';
       }
     }
    if (column == 6)
     {
	  if (i!=days[Month]) {
		  rows++;
	      output += '<\/TR><TR  height=20 ALIGN=CENTER VALIGN=MIDDLE>';
	  }
      column = -1;
     }
   }

  if (column > 0)
   {
    for (i=1; column<6; i++, column++)
        output +=  '<TD class=Cal>\&nbsp;<\/TD>';
    output +=  '<TD class=Cal>\&nbsp;<\/TD>';

   }

  if (clear_calendar==1) {
	  if (rows<6 && clear_calendar==1) {
		  output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE HEIGHT=10><TD colspan=7 class=Cal></TD>';
		  output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE HEIGHT=30><TD colspan=7 class=Cal style="border-top: solid 1px #000000">';
	  }
	  else {
		  output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE HEIGHT=20><TD colspan=7 class=Cal>';
	  }
	  }
  else {
	  if (rows<6) {
		  output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE HEIGHT=20><TD colspan=7 class=Cal></TD>';
	 }
	  //output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE><TD colspan=7 class=Cal>';
  }


  if (clear_calendar==1) {
	 output += '<a class=Cal href="javascript:data_calendar=\'\';przepisz_calendar(obj_calendar);ukryj_warstwe();">wyczyść pole<\/b> <\/TD>';
  }

  output += '<\/TR><\/TABLE>';

  //output += ' <\/TD><\/TR><\/TABLE>';

  return output;
}

/////////////////////////////////////////////////////////////////////

function changeDay(day) {
    start_day = day + '';
    if (parseInt(start_year)<1000) {
  	  start_year = parseInt(start_year)+1900;
    }
	data_calendar='' + parseInt(start_year) + '-' + padout(parseInt(start_month)+1) + '-' + padout(parseInt(start_day));
//	data_calendar='' + padout(parseInt(start_day)) + '-' + padout(parseInt(start_month)+1) + '-' + parseInt(start_year);
	//cal_url=cal_url.replace(/docId=([0-9])*/g, "docId=276");
	location.href=cal_url+"&data="+data_calendar;
}

/////////////////////////////////////////////////////////////////////

function changeMonth() {
    start_month = document.getElementById("Month").options[document.getElementById("Month").selectedIndex].value;
	var kal=Cal_findObj('kalendarz');
	kal.innerHTML=Calendar(start_day,start_month,start_year);
}

/////////////////////////////////////////////////////////////////////

function changeYear() {
    start_year = document.getElementById("Year").options[document.getElementById("Year").selectedIndex].value;
	var kal=Cal_findObj('kalendarz');
	kal.innerHTML=Calendar(start_day,start_month,start_year);
}

/////////////////////////////////////////////////////////////////////

function moveForward() {
    if (document.getElementById("Month").selectedIndex==11) {
		if (document.getElementById("Year").selectedIndex<9) {
			document.getElementById("Month").selectedIndex=0;
			document.getElementById("Year").selectedIndex=document.getElementById("Year").selectedIndex+1;
		    start_year = document.getElementById("Year").options[document.getElementById("Year").selectedIndex].value;
		}
	}
	else {
		document.getElementById("Month").selectedIndex=document.getElementById("Month").selectedIndex+1;
	}
    start_month = document.getElementById("Month").options[document.getElementById("Month").selectedIndex].value;
	var kal=Cal_findObj('kalendarz');
	kal.innerHTML=Calendar(start_day,start_month,start_year);
}

/////////////////////////////////////////////////////////////////////

function moveBackward() {
    if (document.getElementById("Month").selectedIndex==0) {
		if (document.getElementById("Year").selectedIndex>0) {
			document.getElementById("Month").selectedIndex=11;
			document.getElementById("Year").selectedIndex=document.getElementById("Year").selectedIndex-1;
		    start_year = document.getElementById("Year").options[document.getElementById("Year").selectedIndex].value + '';
		}
	}
	else {
		document.getElementById("Month").selectedIndex=document.getElementById("Month").selectedIndex-1;
	}
    start_month = document.getElementById("Month").options[document.getElementById("Month").selectedIndex].value + '';
	var kal=Cal_findObj('kalendarz');
	kal.innerHTML=Calendar(start_day,start_month,start_year);
}

/////////////////////////////////////////////////////////////////////

function makeArray0() {
    for (i = 0; i<makeArray0.arguments.length; i++)
        this[i] = makeArray0.arguments[i];
}

/////////////////////////////////////////////////////////////////////

function SetCalendar(s_year,s_month,s_day) {
	start_year = s_year;
	start_month = s_month-1;
	start_day = s_day;
	sel_year = s_year;
	sel_month = s_month-1;
	sel_day = s_day;
}

/////////////////////////////////////////////////////////////////////

function StartCalendar() {
	var kal=Cal_findObj('kalendarz_nav');
	kal.innerHTML=Navigation(sel_day,sel_month,sel_year);
	var kal=Cal_findObj('kalendarz');
	kal.innerHTML=Calendar(start_day,start_month,start_year);
	document.write(styl());
}

function SetCalUrl(url) {
	cal_url = url;
}

/////////////////////////////////////////////////////////////////////

var names     = new makeArray0('Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec','Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień');
var days      = new makeArray0(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var dow       = new makeArray0('Po','Wt','Śr','Cz','Pt','So','Ni');

/////////////////////////////////////////////////////////////////////

