    var year_offset = 3; //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(8);
	s_day[0]="M"; //monday
	s_day[1]="T";
	s_day[2]="W";
	s_day[3]="T";
	s_day[4]="F";
	s_day[5]="S";
	s_day[6]="S"; //sunday
	s_day[7]="w."; //week
	
	var s_month = new Array(13);
	s_month[0]="January";
	s_month[1]="February";
	s_month[2]="March";
	s_month[3]="April";
	s_month[4]="May";
	s_month[5]="June";
	s_month[6]="July";
	s_month[7]="August";
	s_month[8]="September";
	s_month[9]="October";
	s_month[10]="November";
	s_month[11]="December";
	s_month[12]="Unknown";
	
	calendarTitle = "Calendar";
	

    function inc_year(date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo){
       date_year++;
     if(date_format.length > 10)
     {
	   date_hour = doc.hour.value;
	   date_hour = parseInt(date_hour);
	   date_minute = doc.minute.value;
	   date_minute = parseInt(date_minute);
	   date_second = doc.second.value;
	   date_second = parseInt(date_second);
     }
       show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);
    }

    function dec_year(date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo){
       date_year--;
     if(date_format.length > 10)
  	 {
	   date_hour = doc.hour.value;
	   date_hour = parseInt(date_hour);
	   date_minute = doc.minute.value;
	   date_minute = parseInt(date_minute);
	   date_second = doc.second.value;
	   date_second = parseInt(date_second);
      }
       show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);
    }

    function inc_month(date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo){
     if (date_month == 11)
          {date_year++; date_month=0;}
       else
          {date_month++;}
     if(date_format.length > 10)
     {
	   date_hour = doc.hour.value;
	   date_hour = parseInt(date_hour);
	   date_minute = doc.minute.value;
	   date_minute = parseInt(date_minute);
	   date_second = doc.second.value;
	   date_second = parseInt(date_second);
     }
       show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);

    }
    function dec_month(date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo){
     if (date_month == 0)
          {date_year--; date_month=11;}
       else
          {date_month--;}
     if(date_format.length > 10)
     {
	  date_hour = doc.hour.value;
	  date_hour = parseInt(date_hour);
	  date_minute = doc.minute.value;
	  date_minute = parseInt(date_minute);
	  date_second = doc.second.value;
	  date_second = parseInt(date_second);
     }
       show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);
    }

/*
              "<style>" +
                 "body{font-family:Tahoma,Verdana;font-size:8pt}"+
                 "select{font-family: tahoma, verdana; font-size: 8pt; color: #000080;background-color: #f0f0FF; border: 1 solid #000080}" +
                 "td{border-color:navy;font-family:Tahoma,Verdana;font-size:8pt}table{border:none}"+
                  "a{text-decoration:none}</style>" +
*/


//function which shows the calendar
    function show_cal(date_day, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo){	  
		if(date_year<999)
		{
			date_year=date_year+2000;
		}
       first_date = new Date(date_year, date_month, 1);
       first_day = first_date.getDay();



       doc=window.open("resources/html/blank.html",
                        "calendar",
                        "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=200,height=189, dependent");


        doc.document.open();
        doc.document.writeln("<html>");
        doc.document.writeln("<head><title>" + calendarTitle + "</title>");
        doc.document.writeln("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" +
                  "<LINK rel='stylesheet' href='resources/css/calendar.css' type='text/css'>" +
              "</head>"+
                  "<body>"+
                  "<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+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")>"+
                             "&lt;</A>");
        doc.document.writeln("</td><td width=40%>");
        doc.document.writeln("<center>" + year_combo(date_year, date_month, date_hour, date_minute, date_second, yearFrom, yearTo) + "</center>");
        doc.document.writeln("</td><td align='left' width=30%>");
        doc.document.writeln("<A href=javascript:opener.inc_year("+date_month+","+date_year+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")>"+
                             "&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+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")>"+
                             "&lt;</A>");
        doc.document.writeln("</td><td>");
        doc.document.writeln("<center>" + month_combo(date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo) + "</center>");
        doc.document.writeln("</td><td align='left'>");
        doc.document.writeln("<A href=javascript:opener.inc_month("+date_month+","+date_year+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")>"+
                             "&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>");
              }
            }
            // ordinary days
          else {
          	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>");
        }

		if(date_format.length>10)
		{
			doc.document.writeln("</tr><tr><td colspan=\"9\">");
        	doc.document.writeln(hour_combo(date_hour)+minute_combo(date_minute)+second_combo(date_second));
        	doc.document.writeln("</td></tr></table></center>");
		}
		else
		{
        	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 dateTime;
   	if(date_format.length>10)
   	{
   		hour = doc.hour.value;
   		minute = doc.minute.value;
   		second = doc.second.value;
   		dateTime = new Date(yyyy, m, d, hour, minute, second);
   	}
   	else
   	{
   		dateTime = new Date(yyyy, m, d);
   	}
   	var date_val;
    doc.close();
	date_val = date2string(dateTime, date_format);
    self.document[form_name][input_name].value=date_val;
   }
  
//yyyy-MM-dd HH:mm:ss
 
//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)
{
	if(month < 0 || month > 11)
		return s_month[12];
	else		
		return s_month[month];
	/*
     switch(month)
     {
      case 0 : return "January";
      case 1 : return "February";
      case 2 : return "March";
      case 3 : return "April";
      case 4 : return "May";
      case 5 : return "June";
      case 6 : return "July";
      case 7 : return "August";
      case 8 : return "September";
      case 9 : return "October";
      case 10 : return "November";
      case 11 : return "December";
     default: return "unknown";  //it should not happen
     
   }
   */
}


//function called to change currently displayed month
function set_month(date_year, date_hour, date_minute, date_second, yearFrom, yearTo)
{
  date_month = doc.month.value;  //by byla cyferka
  date_month = parseInt(date_month);
  if(date_format.length > 10)
  {
	  date_hour = doc.hour.value;
	  date_hour = parseInt(date_hour);
	  date_minute = doc.minute.value;
	  date_minute = parseInt(date_minute);
	  date_second = doc.second.value;
	  date_second = parseInt(date_second);
  }
  show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);
}

//function called to change the currently displayed year
function set_year(date_month, date_hour, date_minute, date_second, yearFrom, yearTo)
{
  date_year = doc.year.value;  //to make sure it is a number not a String
  date_year = parseInt(date_year);
  date_month = parseInt(date_month);
  if(date_format.length > 10)
  {
	  date_hour = doc.hour.value;
	  date_hour = parseInt(date_hour);
	  date_minute = doc.minute.value;
	  date_minute = parseInt(date_minute);
	  date_second = doc.second.value;
	  date_second = parseInt(date_second);
  }
  show_cal(1, date_month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo);
}


//creates combo box with months for specified year selecting specified month
function month_combo(month, date_year, date_hour, date_minute, date_second, yearFrom, yearTo)
  {
   var combo = "<select name=\"month\" onChange=\"javascript:opener.set_month("+date_year+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")\">";
    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, date_hour, date_minute, date_second, yearFrom, yearTo)
  {
   if(!isNaN(yearFrom) & yearFrom != '' & !isNaN(yearTo) & yearTo != '')
   {
   	b_date_year = parseInt(yearFrom);
   	e_date_year = parseInt(yearTo);
   }
   else
   {
   	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+","+date_hour+","+date_minute+","+date_second+","+yearFrom+","+yearTo+")\">";
    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 hour_combo(date_hour)
  {
   b_hour = 0;
   e_hour = 23;
   var combo = "<select name=\"hour\">";
    for(i=b_hour;i<=e_hour;i++)
     {
      if (i==date_hour) sel="selected"; else sel="";
      combo += "<option value="+i+" "+ sel+">"+i+"</option>";
     }
   combo += "</select>";
    return combo;
 }

function minute_combo(date_minute)
  {
   b_minute = 0;
   e_minute = 59;
   var combo = "<select name=\"minute\">";
    for(i=b_minute;i<=e_minute;i++)
     {
      if (i==date_minute) sel="selected"; else sel="";
      combo += "<option value="+i+" "+ sel+">"+i+"</option>";
     }
   combo += "</select>";
    return combo;
 }
 
 function second_combo(date_second)
  {
   b_second = 0;
   e_second = 59;
   var combo = "<select name=\"second\">";
    for(i=b_second;i<=e_second;i++)
     {
      if (i==date_second) 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, days, months, title, yearFrom, yearTo, yearOffset)
{
   input_name = _input_name;
   form_name  = _form_name;
   date_format = _date_format;
   
    //s_day 	= days;
   for(var i = 0; i < 8; i++)
   	s_day[i] = days[i];
   //s_month	= months;	
   for(var i = 0; i < 13; i++)
   	s_month[i] = months[i];
      
   calendarTitle = title;

   cdate = new String(self.document[form_name][input_name].value);

	var hour = 0;
   	var minute = 0;
   	var second = 0;
	
	var dateTime;
	
	if(cdate == "")
	{
		dateTime = new Date();
	}
	else
	{
		dateTime = string2dateCal(cdate, date_format);
	}
   	
   	if(isNaN(dateTime))
   	{
   		dateTime = new Date();
   	}
   	
	year = dateTime.getFullYear();
	day = dateTime.getDate();
	month = dateTime.getMonth();
	hour = dateTime.getHours();
	minute = dateTime.getMinutes();
	second = dateTime.getSeconds();
	
	yearFrom = parseInt(yearFrom);
   	yearTo = parseInt(yearTo);
   	
   	yearOffset = parseInt(yearOffset);
   	if(!isNaN(yearOffset))
   	{
   		year_offset = yearOffset;
   	}
   //shows calendar for specified month and year
   show_cal(day,month,year,hour,minute,second,yearFrom,yearTo);
}

function string2dateCal(str, format) {
	var err = false;
	var y = NaN;
	var m = NaN;
	var d = NaN;
	var HH = NaN;
	var mm = NaN;
	var ss = NaN;
	var index = 0;
	
	var tmpFormat = format;
	tmpFormat = tmpFormat.replace("yyyy", "\\d{4}");
	tmpFormat = tmpFormat.replace("yy", "\\d{2}");
	tmpFormat = tmpFormat.replace("MM", "\\d{2}");
	tmpFormat = tmpFormat.replace("dd", "\\d{2}");
	
	var regExp = new RegExp(tmpFormat);
	var validFormat = regExp.test(str);
	
	index = format.indexOf('yyyy');
	if (index != -1) {
		y = str.substring(index, index + 4);
	} else {
		index = format.indexOf('yy');
		if (index != -1) {
			y = str.substring(index, index + 2);
		}
	}
	
	index = format.indexOf('MM');
	if (index != -1) {
		m = str.substring(index, index + 2);
	}
	
	index = format.indexOf('dd');
	if (index != -1) {
		d = str.substring(index, index + 2);
	}
	
	if(format.length>10)
	{
		index = format.indexOf('HH');
		if (index != -1) {
			HH = str.substring(index, index + 2);
		}
		
		index = format.indexOf('mm');
		if (index != -1) {
			mm = str.substring(index, index + 2);
		}
		
		index = format.indexOf('ss');
		if (index != -1) {
			ss = str.substring(index, index + 2);
		}
	}
	
	/*
	if(isNaN(y) || y < 0) {err = true;}
	if(isNaN(m) || m < 0) {err = true;}
	if(isNaN(d) || d < 0) {err = true;}
	
	if (err) { 
		return null; 
	}
	*/
	
	var ytest = parseInt(y);
	if(ytest<99)
	{
		y = "20"+y;
	}
	else
	if(ytest<999)
	{
		y = "2"+y;
	}
	
	var data = new Date();
	
	if(format.length>10)
	{
	 	data = new Date(y, m-1, d, HH, mm, ss);
	}
	else
	{
		data = new Date(y, m-1, d);
	}
	return data;
}
//-->
