    var year_offset = 9; //number of years displayed arround current year
    var sun_color = "#eeeeFF";   //colour of Sunday
    var right_arrow_img = "arrowr.gif";  //name of right arrow
    var left_arrow_img = "arrowl.gif";    //name of left arrow

    //some public variables

    var doc; //window with callendar
    var input_name; //name of the input which we want to set
    var form_name; //name of the form in wich input is located
    var date_format; // dd-mm-yyyy, mm-dd-yyyy, dd/mm/yyyy, mm/dd/yyyy, yyyy-mm-dd, yyyy-dd-mm, yyyy/mm/dd, yyyy/dd/mm
	var s_day = new Array(7);
	s_day[0]="Pn"; //monday
	s_day[1]="Wt";
	s_day[2]="Sr";
	s_day[3]="Cz";
	s_day[4]="Pt";
	s_day[5]="Sb";
	s_day[6]="Nd"; //sunday
	s_day[7]="t."; //week

    function inc_year(date_month, date_year){
       date_year = date_year+10;
       show_cal(1, date_month, date_year);
    }

    function dec_year(date_month, date_year){
       date_year = date_year-10;
       show_cal(1, date_month, date_year);
    }

    function inc_month(date_month, date_year){
     if (date_month == 11)
          {date_year++; date_month=0;}
       else
          {date_month++;}
       show_cal(1, date_month, date_year);

    }
    function dec_month(date_month, date_year){
     if (date_month == 0)
          {date_year--; date_month=11;}
       else
          {date_month--;}
       show_cal(1, date_month, date_year);
    }

//function which shows the callendar
	function show_cal(date_day, date_month, date_year) {
       first_date = new Date(date_year, date_month, 1);
       first_day = first_date.getDay();

        // <--- 2006.08.24 MWW: Calc the top of the element '_input_name' to proper positioning calendar (on the right of the specified element) 
        var element = document[form_name][input_name];    	
        var realTop = 0;
        var realLeft = 0;
        while (element.tagName != "BODY")
        {
	 realTop = realTop + element.offsetTop; 
         realLeft = realLeft + element.offsetLeft;
         element = element.offsetParent;
        }
        realTop = realTop + window.screenTop;	
        realLeft = realLeft + window.screenLeft + document[form_name][input_name].offsetWidth;	
        //alert("realTop =" + realTop + " realLeft=" + realLeft);
        // --->

	doc=window.open("",
                        "calendar",
                        "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=200,height=145,top=" + realTop + ",left=" + realLeft +", dependent");

        doc.document.open();
        doc.document.writeln("<html>");
        doc.document.writeln("<head><title>Calendar</title>");
        doc.document.writeln("<meta http-equiv='content-type' content='text/html;charset=utf-8'/>" +
				"<LINK rel='stylesheet' href='/static/css/calendar.css' type='text/css'>" +
				"</head>"+
				"<body class='calendarBody'>"+
				"<center><table width='100%'><tr><td>"+
				"<table width='100%' class='top_table'>"+
				"<tr><td align='right' widht=30%>");

        doc.document.writeln("<A href=javascript:opener.dec_year("+date_month+","+date_year+")>"+
                             "&lt;&lt;</A>");
        doc.document.writeln("</td><td width=40%>");
        doc.document.writeln("<center>" + year_combo(date_year, date_month) + "</center>");
        doc.document.writeln("</td><td align='left' width=30%>");
        doc.document.writeln("<A href=javascript:opener.inc_year("+date_month+","+date_year+")>"+
                             "&gt;&gt;</A>");
//		doc.document.writeln("</td></tr><tr><td align='right'>");
        doc.document.writeln("</td><td align='right'>");
        doc.document.writeln("<A href=javascript:opener.dec_month("+date_month+","+date_year+")>"+
                             "&lt;</A>");
        doc.document.writeln("</td><td>");
        doc.document.writeln("<center>" + month_combo(date_month, date_year) + "</center>");
        doc.document.writeln("</td><td align='left'>");
        doc.document.writeln("<A href=javascript:opener.inc_month("+date_month+","+date_year+")>"+
                             "&gt;</A>");
        doc.document.writeln("</td></tr></table></tr></td></table></center>");


        doc.document.writeln("<center><table width='97%' class='cal_table'><tr>");
        doc.document.writeln("<td width=10% class='week_head'>" + s_day[7] + "</td><td width=6%></td><td width=12% class='ord_head'>" + s_day[0] + "</td><td width=12% class='ord_head'>" + s_day[1] + "</td><td width=12% class='ord_head'>" + s_day[2] + "</td>" +
				"<td width=12% class='ord_head'>" + s_day[3] + "</td><td width=12% class='ord_head'>" + s_day[4] + "</td><td width=12% class='ord_head'>" + s_day[5] + "</td><td width=12% class='sunday_head'>" + s_day[6] + "</td>");
        doc.document.writeln("</tr><tr>");

        doc.document.writeln("</tr><tr><td class='week_cell'><center>"+get_week_nr(1,date_month,date_year)+"</center></td><td></td>");


        var j;
		for (j=0;j<(6+first_day)%7;j++) {
			doc.document.writeln("\t<td><center></center></td>");
        }

		days_count = days_num(date_month, date_year);

		if (date_day > days_count) {
			selected_day = 1;
		} else {
			selected_day = date_day;
		}

		var i;
		var p = ",";
		for (i=1;i<=days_count;i++) {

			if ((i+first_day)%7 == 1) {
				if (selected_day == i) {
					css_class = "sunday_selected_cell";
				} else {
					css_class = "sunday_cell";
				}
	            //sundays
				 doc.document.writeln("\t<td class='" + css_class +
	                                  "'><center><A HREF=javascript:opener.set_date("+i+p+date_month+p+date_year+")>"
	                                + i + "</center></td>\n");
	
				if (i<days_count) {
					//week numbers
					week_nr = get_week_nr(i+1,date_month,date_year);
					doc.document.writeln("</tr>\n<tr>\t<td class='week_cell'><center>"+week_nr+"</center></td>\n\t<td></td>");
				}
            } else {
				// ordinary days
				if (selected_day == i) {
					css_class = "ord_selected_cell";
				} else {
					css_class = "ord_cell";
				}
				doc.document.writeln("\t<td class='" + css_class + "'><center><A HREF=javascript:opener.set_date("+i+p+date_month+p+date_year+")>"
                                + i + "</center></td>");
			}
        }

        for (k=1;(i+j+k-2)%7!=0;k++) {
			doc.document.writeln("<td><center></center></td>");
        }

        doc.document.writeln("</tr></table></center>");
        doc.document.writeln("</body>");
        doc.document.writeln("</html>");
        doc.document.close();
        doc.focus();
   }

//finally called function
//It set value in appriopriate input and close window with calendar
	function set_date(d, m, yyyy) {
		var dd;
		var mm;
		doc.close();
		var date_val;
		m++;
		if (d < 10) {
			dd = "0" + d;
		} else {
			dd = d;
		}
		if (m < 10) {
			mm = "0" + m;
		} else {
			mm = m;
		}
	
		if (date_format == "dd-mm-yyyy") {
			date_val = dd + "-" + mm + "-" + yyyy;
		} else if (date_format == "mm-dd-yyyy") {
			date_val = mm + "-" + dd + "-" + yyyy;
		} else if (date_format == "dd/mm/yyyy") {
			date_val = dd + "/" + mm + "/" + yyyy;
		} else if (date_format == "mm/dd/yyyy") {
			date_val = mm + "/" + dd + "/" + yyyy;
		} else if (date_format == "yyyy-mm-dd") {
			date_val = yyyy + "-" + mm + "-" + dd;
		} else if (date_format == "yyyy-dd-mm") {
			date_val = yyyy + "-" + dd + "-" + mm;
		} else if (date_format == "yyyy/mm/dd") {
			date_val = yyyy + "/" + mm + "/" + dd;
		} else if (date_format == "yyyy/dd/mm") {
			date_val = yyyy + "/" + dd + "/" + mm;
		} else {
			date_val = dd + "-" + mm + "-" + yyyy;
		}

		self.document[form_name][input_name].value=date_val;
   }


//returns number of days in the specified month in specified year
	function days_num(month, year) {
		switch(month) {
			case 0 : return 31;
			case 1 :
				if (year%4 == 0)
					if (year%100 == 0) {
						if (year%400 ==0)
							return 29;
						else
							return 28;
					} else
						return 29;
				else
					return 28;
			case 2 : return 31;
			case 3 : return 30;
			case 4 : return 31;
			case 5 : return 30;
			case 6 : return 31;
			case 7 : return 31;
			case 8 : return 30;
			case 9 : return 31;
			case 10 : return 30;
			case 11 : return 31;
			default: return 50;  //it should not happen
		}
	}


//returns the name of the month
	function month_name(month) {
		switch(month) {
			case 0 : return "Stycze&\#324";
			case 1 : return "Luty";
			case 2 : return "Marzec";
			case 3 : return "Kwiecie&\#324";
			case 4 : return "Maj";
			case 5 : return "Czerwiec";
			case 6 : return "Lipiec";
			case 7 : return "Sierpie&\#324";
			case 8 : return "Wrzesie&\#324";
			case 9 : return "Pa&\#378dziernik";
			case 10 : return "Listopad";
			case 11 : return "Grudzie&\#324";
			default: return "unknown";  //it should not happen
		}
	}


//function called to change currently displayed month
	function set_month(date_year) {
		date_month = doc.month.value;  //by byla cyferka
		date_month = parseInt(date_month);
		show_cal(1, date_month, date_year);
	}

//function called to change the currently displayed year
	function set_year(date_month) {
		date_year = doc.year.value;  //to make sure it is a number not a String
		date_month = parseInt(date_month);
		date_year = parseInt(date_year);
		show_cal(1, date_month, date_year);
	}


//creates combo box with months for specified year selecting specified month
	function month_combo(month, date_year) {
		var combo = "<select name=\"month\" onChange=\"javascript:opener.set_month("+date_year+")\">";
	    for(i=0;i<12;i++) {
			if (i==month)
				sel="selected";
			else
				sel="";
	      	combo += "<option value="+i+" "+ sel+">"+month_name(i)+"</option>";
		}
		combo += "</select>";
		return combo;
	}

// creates combobox with years selecting specified year (mont param is to make proper onChange
// reference
	function year_combo(date_year, date_month) {
		b_date_year = date_year - year_offset;
		e_date_year = date_year + year_offset;
		var combo = "<select name=\"year\" onChange=\"javascript:opener.set_year("+date_month+")\">";
		for (i=b_date_year;i<=e_date_year;i++) {
			if (i==date_year)
				sel="selected";
			else
				sel="";
			combo += "<option value="+i+" "+ sel+">"+i+"</option>";
		}
		combo += "</select>";
		return combo;
	}


 function get_week_nr(day, month, year)
 {
  days = get_day_nr(day, month, year);
  first_date = new Date(year, 0, 1);
  last_date = new Date(year, 11, 31);
  last_day_of_year = last_date.getDay();
  first_day_of_year = first_date.getDay();

  if (first_day_of_year==0) first_day_of_year=7;
 days = days -2 + first_day_of_year;
  with (Math) {
   week = floor(days/7);
  }

   //mond, thus, wensd, thurs -> week nr. 1 starts in previous year so week++
  if((first_day_of_year == 1) ||
     (first_day_of_year == 2) ||
     (first_day_of_year == 3) ||
     (first_day_of_year == 4))
        week++;

  if (week == 0) week = get_week_nr(31,11,year-1); //it is 53 or 52

  //year ends with mond, thus, wensd -> so week nr. 1
  if ((week == 53) && ((last_day_of_year == 1) ||
                      (last_day_of_year == 2) ||
                      (last_day_of_year == 3)))
                       week=1;

  return week;
}

function get_day_nr(day, month, year)
{
 day_nr=0;
 for (i=0; i<month; i++)
   day_nr+=days_num(i, year);

 day_nr+=day;
 return day_nr;
}

//function set_cal(_input_name, _form_name)

function set_cal(_input_name, _form_name, _date_format)
{
   input_name = _input_name;
   form_name  = _form_name;
   date_format = _date_format;

 //trying to get current value of input

    cdate = new String(self.document[form_name][input_name].value);

	var znak;
	if (cdate.indexOf("-",0) > -1) {
		znak = "-";
	} else {
		znak = "/";
	}

     first_id= cdate.indexOf(znak,0);
     sec_id = cdate.indexOf(znak,first_id+1);
     thr_id = cdate.length;

	if ((date_format == "dd-mm-yyyy") || (date_format == "dd/mm/yyyy")) {
		first_str = cdate.substring(0,first_id);
		if (cdate.charAt(first_id+1) == '0') {
			sec_str=cdate.substring(first_id+2, sec_id);
		} else {
			sec_str=cdate.substring(first_id+1, sec_id);
		}
		thr_str= cdate.substring(sec_id+1, thr_id);
	} else if ((date_format == "mm-dd-yyyy") || (date_format == "mm/dd/yyyy")) {
		first_str = cdate.substring(first_id+1, sec_id);
		if (cdate.charAt(0) == '0') {
			sec_str=cdate.substring(1, first_id);
		} else {
			sec_str=cdate.substring(0, first_id);
		}
		thr_str= cdate.substring(sec_id+1, thr_id);
	} else if ((date_format == "yyyy-mm-dd") || (date_format == "yyyy/mm/dd")) {
		first_str = cdate.substring(sec_id+1, thr_id);
		if (cdate.charAt(first_id+1) == '0') {
			sec_str=cdate.substring(first_id+2, sec_id);
		} else {
			sec_str=cdate.substring(first_id+1, sec_id);
		}
		thr_str= cdate.substring(0, first_id);
	} else if ((date_format == "yyyy-dd-mm") || (date_format == "yyyy/dd/mm")) {
		first_str = cdate.substring(first_id+1, sec_id);
		if (cdate.charAt(sec_id+1) == '0') {
			sec_str=cdate.substring(sec_id+2, thr_id);
		} else {
			sec_str=cdate.substring(sec_id+1, thr_id);
		}
		thr_str= cdate.substring(0, first_id);
	}

      year = parseInt(thr_str);
      month=parseInt(sec_str);
	  if (first_str.charAt(0) == '0') {
	  	day=parseInt(first_str.charAt(1));
	  } else {
	  	day=parseInt(first_str);
	  }


   //if it fails get today's date
	if (isNaN(year) || year < 1900 || year > 2100) {
		year = (new Date()).getYear();
		if (year < 200) {
			year += 1900;
		}
	}
	if (isNaN(day) || day < 1 || day > 31) {
		day = (new Date()).getDate();
	}
   if(isNaN(month) || month <1 || month >12)
      month = (new Date()).getMonth();
    else
       month--; //becouse month are indexed from 0

    //alert(first_str + "--"+month + "--"+ year + thr_str);  //test

   //shows calendar for specified month and year
   show_cal(day, month,year);
}

//-->
