/*
 * @param str id unikátní jméno kalendáře
 * @param int day přednastavený den (nevyplněno = dnešní)
 * @param int month přednastavený měsíc (nevyplněno = dnešní)
 * @param int year přednastavený rok (nevyplněno = dnešní)
 * @param req now je -1, request naplněn, tak datum zobraz, i když je emppty = true
 */
function calendar_data(id,now,from,to,req)
{
  // HOTFIX
  if(typeof now == "undefined" || now == '')
    this.initial_empty = true;
  else
    this.initial_empty = false;

  // implicitní hodnoty dat
  var date = new Date();
  date.setHours(0,0,0,0);
  if(typeof now == "undefined") now = "";
  if(typeof from == "undefined") from = "";
  if(typeof to == "undefined") to = "";
  if(now!="-1")
  {
    now_arr = now.split("-");
    if(now_arr.length!=3)
    {
      now_arr = now.split(".");
      this.day = now!="" ? now_arr[0] : date.getDate();
      this.month = now_arr.length>1 ? now_arr[1]-1 : date.getMonth();
      this.year = now_arr.length>2 ? now_arr[2]-0 : date.getFullYear();
    }
    else
    {
      this.year = now_arr[0];
      this.month = now_arr[1]-1;
      this.day = now_arr[2]-0;
    }
  }
  from_arr = from.split("-");
  if(from_arr.length!=3)
  {
    from_arr = from.split(".");
    
    this.from_day = from!="" ? from_arr[0]-1 : date.getDate();
    // puvodni radek
    // this.from_day = from!="" ? from_arr[0] : date.getDate();
    
    this.from_month = from_arr.length>1 ? from_arr[1]-1 : date.getMonth();
    this.from_year = from_arr.length>2 ? from_arr[2]-0 : date.getFullYear()-80;
    
  }
  else
  {
    this.from_year = from_arr[0];
    this.from_month = from_arr[1]-1;
    this.from_day = from_arr[2]-0;
  }
  to_arr = to.split("-");
  if(to_arr.length!=3)
  {
    to_arr = to.split(".");
    this.to_day = to!="" ? to_arr[0] : date.getDate();
    this.to_month = to_arr.length>1 ? to_arr[1]-1 : date.getMonth();
    this.to_year = to_arr.length>2 ? to_arr[2]-0 : date.getFullYear()+30;
  }
  else
  {
    this.to_year = to!="" ? to_arr[0] : date.getFullYear()+30;
    this.to_month = to_arr.length>1 ? to_arr[1]-1 : date.getMonth();
    this.to_day = to_arr.length>2 ? to_arr[2]-0 : date.getDate();
  }
  if(now=="-1")
  {
    this.day = this.from_day;
    this.month = this.from_month;
    this.year = this.from_year;
  }

  // kontrola, je-li now v rozsahu from - to (a kdyžtak uprav)
  ftime = new Date();
  ftime.setFullYear(this.from_year,this.from_month,this.from_day);
  ftime.setHours(0,0,0,0);
  ftimex = ftime.getTime();
  ttime = new Date();
  ttime.setFullYear(this.to_year,this.to_month,this.to_day);
  ttime.setHours(0,0,0,0);
  ttimex = ttime.getTime();
  ntime = new Date();
  ntime.setFullYear(this.year,this.month,this.day);
  ntime.setHours(0,0,0,0);
  ntimex = ntime.getTime();
  if(ntimex<ftimex || ntimex>ttimex)
  {
    if(ntimex<ftimex) ntime.setTime(ftimex);
    if(ntimex>ttimex) ntime.setTime(ttimex);
    this.day = ntime.getDate();
    this.month = ntime.getMonth();
    this.year = ntime.getFullYear();
  }

  // ostatní nastavení
  this.id = id;
  this.col = "blue";
  this.format = "d.m.y";
  this.style = ""; // datového pole, zejména margin, padding, float
  this.tabindex = -1;
  this.title = "";
  this.accesskey = "";
  this.empty = true;
  this.imgpath = "";
  this.now = now;
  this.req = req;

  // nastavení data
  var date = new Date();
  if(this.day!==false) date.setDate(this.day);
  if(this.month!==false) date.setMonth(this.month);
  if(this.year!==false) date.setFullYear(this.year);
  this.date = date;

  // zobrazovací proměnné
  this.hide = true; // skrýt kalendář po ztrátě zaměření datového pole
  this.keep_on = false; // nechat kalendář zobrazen, pokud je ztrátazaměření způsobena klikem na šipku

} // calendar dynamic


var calendar =
{
  // seznam instancí kalendářových dat
  list: new Array(),

  // jazyková nastavení (0 = pondělí, 6 = neděle)
  days: new Array("Po","t","St","t","P","So","Ne"),
  months_abb: new Array("Led","Úno","Bře","Dub","Kvě","Čvn","Čvc","Srp","Zář","Říj","Lis","Pro"),
  months: new Array("Leden","nor","Bezen","Duben","Kvten","erven","ervenec","Srpen","Z","jen","Listopad","Prosinec"),
  
  styled: new Array(),
  add_style: function(col)
  {
    style = "<style>";
      style+= "\nspan.calendar"+col+" nobr {margin: 0px; padding: 0px 3px 0px 0px;}";
      style+= "\nspan.calendar"+col+" table {border-collapse: collapse; border-width: 3px; padding: 10px 10px 15px 10px; margin-top: 10px; _margin-top: 5px;}";
      style+= "\nspan.calendar"+col+" table img {cursor: pointer; padding: 0px 3px 0px 3px; margin: -8px 0px -10px 0px; _margin: 2px 4px -2px 4px;}";
      style+= "\nspan.calendar"+col+" table.wrapper {border-right: 1px solid #dadada; border-bottom: 6px solid #dadada; width: 100px; position: relative; top: 18px;}";
      style+= "\nspan.calendar"+col+" table.wrapper td {vertical-align: middle; border-width: 0px; }";
      style+= "\nspan.calendar"+col+" table.calendar {padding: 0px; width: 100%;}";
      style+= "\nspan.calendar"+col+" table.calendar td {vertical-align: middle; border: 1px solid #dadada; width: 14%; text-align: center; cursor: pointer; font-size: 11px; font-family: Verdana; padding: 3px; background-color: white;}";
      style+= "\nspan.calendar"+col+" table.calendar td:first-child {border-left-width: 2px;}";
      style+= "\nspan.calendar"+col+" table.calendar td.title {background-color: rgb(130,130,130); color: white; font-weight: bold; padding: 3px; cursor: default;}";
      style+= "\nspan.calendar"+col+" table.calendar td.over {background-color: dodgerblue; color: white;}";
      style+= "\nspan.calendar"+col+" table.calendar td.weekend {background-color: #F6F6F6;}";
      style+= "\nspan.calendar"+col+" table.calendar td.active {background-color: lavender;}";
      style+= "\nspan.calendar"+col+" td.menu {background-color: #dadada; text-align: center}";
      style+= "\nspan.calendar"+col+" select {border: none; margin: 0px; font-size: 10px; position: relative; top: -3px;}";
      style+= "\nspan.calendar"+col+" select.over {border: none;}";
      style+= "\nspan.calendar"+col+" option {font-size: 10px;}";
    style+= "\n</style>";
    calendar.styled[col] = 1;
    return style;
  },

  // vrátí řádek tabulky dní kalendáře (4-5 týdnů v měsíci)
  makerow: function(id,daysrow,active,monday)
  {
    if(typeof daysrow != "object" || daysrow.length != 7) return;
    obj = calendar.list[id];
    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    date_from.setHours(0,0,0,0);
    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    date_to.setHours(0,0,0,0);
    row = "<tr>";
    for(key in daysrow)
    {
      vclass = key==5 || key==6 ? "weekend" : "";
      daytime = monday+(key-1)*86400000;
      allowed = daytime-date_from.getTime()>0 && date_to.getTime()-daytime>0;
      colstring = allowed ? "color:black;" : "font-style: italic; color:gray;";
      if(key==active) vclass = "active";
      if(daysrow[key]=="&nbsp;") row+= "<td class=\""+vclass+"\" style=\"cursor: default;\" onmousedown=\"calendar.actions('"+id+"');\">&nbsp;</td>";
      else row+= "<td onmousedown=\"calendar.set_day('"+id+"',this.innerHTML);\" onmouseover=\"this.className='over'\" onmouseout=\"this.className='"+vclass+"'\" class=\""+vclass+"\" style=\""+colstring+"\">"+daysrow[key]+"</td>";
    }
    row+= "</tr>";
    return row;
  },

  // vrátí tabulku kalendáře pro aktuální měsíc a rok
  tablecontent: function(id)
  {
    obj = calendar.list[id];

    // nestvořil-li Bůh svět za sedm dní, vůbec se s tím nemaž
    if(calendar.days.length!=7) return;

    // date2 nastav na prvního v aktuálním měsíci
    date2 = new Date();
    date2.setTime(obj.date.getTime());
    date2.setDate(1);

    // dny v týdnu
    rowarray = new Array();
    rowtitle = "<tr>"
    for(i in calendar.days)
      rowtitle+= "<td class=\"title\" onmousedown=\"calendar.actions('"+id+"');\">"+calendar.days[i]+"</td>";
    rowtitle+= "</tr>";

    // active = vybrané datum (jiný styl)
    active = new Array();

    // časy pondělků (pro kontrolu rozsahu)
    monday = new Array();

    // doplnění začátku prvního týdne prázdnými poli, je-li nekompletní
    day2 = date2.getDay(); // ne-so (0..6) upravit na po-ne(0..6)
    if(!day2) day2 = 7;
    day2--;
    if(day2) // není pondělí
    {
      rowarray[0] = new Array();
      active[0] = -1;
      for(i=0;i<day2;i++)
        rowarray[0].push("&nbsp;");
    }

    i = 0;
    while(true)
    {
      day2 = date2.getDay(); // ne-so (0..6) upravit na po-ne(0..6)
      if(!day2) day2 = 7;
      day2--;

      if(!day2) // pondělí
      {
        i++;
        rowarray[i] = new Array(); // pondělí, nový řádek
        active[i] = -1; // -1 - neaktivní den (aktivní se změní níže)
        monday[i] = date2.getTime();
      }
      rowarray[i].push(date2.getDate()); // přidej další den
      if(obj.year==date2.getFullYear() && obj.month==date2.getMonth() && obj.day==date2.getDate())
        active[i] = day2; // nastav aktivní den

      // konec měsíce
      month2 = date2.getMonth();
      date2.setTime(date2.getTime()+86400000); // posuň se o den (milisekundy dne)
      if(date2.getMonth()!=month2) break; // další měsíc už se nebude vypisovat
    }

    // doplnění pondělku prvního neúplného týdne
    if(typeof rowarray[0] != "undefined")
    if(rowarray[0][0]=="&nbsp;") monday[0] = monday[1]-7*86400000;

    // doplnění posledního týdne o prázdná pole, je-li nekompletní
    while(rowarray[i].length<7)
      rowarray[i].push("&nbsp;");

    // vykreslení tabulky
    result = "<table class=\"calendar\">";
    result+= rowtitle;
    for(i in rowarray)
      result+= calendar.makerow(id,rowarray[i],active[i],monday[i]);
    result+= "</table>";
    return result;
  },


  show: function(id)
  {
    obj = calendar.list[id];
    obj.hide=true;
    obj.field.focus();
    obj.table.style.display = "inline";
  },

  hide: function(id)
  {
    obj = calendar.list[id];
    obj.hide=true;
    if(obj.keep_on) return;
    setTimeout("calendar.hide_now('"+id+"')",50);
  },

  hide_now: function(id)
  {
    obj = calendar.list[id];
    if(!obj.hide) return;
    obj.table.style.display = "none";
  },

  set_keep: function(id,on)
  {
    obj = calendar.list[id];
    obj.keep_on = on ? true : false;
  },

  actions: function(id)
  {
    obj = calendar.list[id];
    obj.hide=false;
    obj.chart.innerHTML = calendar.tablecontent(id);
  },

  set_value: function(id)
  {
    obj = calendar.list[id];
    var val = obj.format.toLowerCase();

    // nastavení datového pole
    val = val.replace("d",obj.day);
    val = val.replace("month",calendar.months[obj.month]);
    val = val.replace("mon",calendar.months_abb[obj.month]);
    val = val.replace("m",obj.month+1);
    val = val.replace("y",obj.year);
    obj.field.value = val;

    // nastavení hidden pole
    hidden_month = ""+(obj.month+1);
    if(hidden_month.length==1) hidden_month = "0"+hidden_month;
    hidden_day = ""+(obj.day);
    if(hidden_day.length==1) hidden_day = "0"+hidden_day;
    obj.hidden_field.value = ""+obj.year+"-"+hidden_month+"-"+hidden_day;
  },

  set_year: function(id,yearnow)
  {
    obj = calendar.list[id];
    obj.field.focus();
    obj.date.setFullYear(yearnow);
    // obj.year = yearnow; tohle neodkomentovávat !
  },

  set_month: function(id,monthnow)
  {
    obj = calendar.list[id];

    // test, je-li datum v rozsahu
    date2 = new Date();
    date2.setTime(obj.date.getTime());
    date2.setMonth(++monthnow);--monthnow; //musí to být takhle
    date2.setDate(1);
    date2.setTime(date2.getTime()-86400000); // poslední den v měsíci

    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    from_ok = date2.getTime()-date_from.getTime()>0;

    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    date2.setDate(1);
    to_ok = date_to.getTime()-date2.getTime()>0;
    if(!(to_ok && from_ok))
    {
      obj.month_field.selectedIndex = obj.date.getMonth();
      obj.year_field.selectedIndex = obj.date.getFullYear()-obj.from_year;
      return;
    }

    // provedení změn
    obj.field.focus();
    obj.date.setMonth(monthnow);
    // obj.month = monthnow; tohle neodkomentovávat !
    obj.chart.innerHTML = calendar.tablecontent(id);
  },

  set_day: function(id,daynow)
  {
    obj = calendar.list[id];
    
    // nastavení nového dne
    date2 = new Date();
    date2.setTime(obj.date.getTime());
    date2.setDate(daynow);

    // test, je-li datum v rozsahu
    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    if(date2.getTime()-date_from.getTime()<0 || date_to.getTime()-date2.getTime()<0)
    {
      calendar.actions(id);
      return;
    }

    // provedení změn
    obj.date.setDate(daynow);
    obj.year = obj.date.getFullYear();
    obj.month = obj.date.getMonth();
    obj.day = daynow;
    calendar.set_value(id);
    obj.chart.innerHTML = calendar.tablecontent(id);
    obj.field.blur();
    calendar.hide(id);
    
    
  },

  shift_month: function(id,shift)
  {
    obj = calendar.list[id];
    obj.field.focus();

    // nastavení nového měsíce
    date2 = new Date();
    date2.setTime(obj.date.getTime());
    var monthnow = date2.getMonth()+shift;
    date2.setFullYear(date2.getFullYear(),monthnow);

    // test, je-li datum v rozsahu
    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    if(date2.getTime()-date_from.getTime()<0 || date_to.getTime()-date2.getTime()<0)
    {
      obj.month_field.selectedIndex = obj.date.getMonth();
      obj.year_field.selectedIndex = obj.date.getFullYear()-obj.from_year;
      return;
    }

    // potvrzení změn
    obj.date.setTime(date2.getTime());
    obj.month_field.selectedIndex = obj.date.getMonth();
    obj.year_field.selectedIndex = obj.date.getFullYear()-obj.from_year;
    obj.chart.innerHTML = calendar.tablecontent(id);
  },

  shift_day: function(id,shift)
  {
    obj = calendar.list[id];
    obj.field.focus();
 
    // nastavení dne
    date2 = new Date();
    date2.setTime(obj.date.getTime());
    var daynow = date2.getDate()+shift;
    date2.setFullYear(date2.getFullYear(),date2.getMonth(),daynow);
      
    // datum mimo rozsah, odmítni
    if(date2.getFullYear()<obj.from_year || date2.getFullYear()>obj.to_year) return;
    if(date2.getMonth()<obj.from_month && date2.getFullYear()==obj.from_year) return;
    if(date2.getMonth()>obj.to_month && date2.getFullYear()==obj.to_year) return;
    if(date2.getDate()<=obj.from_day && date2.getMonth()==obj.from_month && date2.getFullYear()==obj.from_year)return;
    if(date2.getDate()>obj.to_day && date2.getMonth()==obj.to_month && date2.getFullYear()==obj.to_year) return;

    // potvrzení změn
    obj.date.setTime(date2.getTime());
    obj.year = obj.date.getFullYear();
    obj.month = obj.date.getMonth();
    obj.day = obj.date.getDate();
    obj.month_field.selectedIndex = obj.date.getMonth();
    obj.year_field.selectedIndex = obj.year-obj.from_year;
    calendar.set_value(id);
    obj.chart.innerHTML = calendar.tablecontent(id);
  },

  shift_today: function(id)
  {
    // je-li dnes mimo povolený rozsah, ignoruj
    obj = calendar.list[id];
    date_now = new Date();
    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    
    // puvodni podminka
    // if(date_from.getTime()-date_now.getTime()>0 || date_now.getTime()>date_to.getTime())
    if(date_from.getTime()-date_now.getTime()>=0 || date_now.getTime()>date_to.getTime())
    {
      return;
    }

    // nastav na dnes
    obj.field.focus();
    obj.date = new Date();
    obj.year = obj.date.getFullYear();
    obj.month = obj.date.getMonth();
    obj.day = obj.date.getDate();
    obj.month_field.selectedIndex = obj.month;
    obj.year_field.selectedIndex = obj.year-obj.from_year;
    calendar.set_value(id);
    obj.chart.innerHTML = calendar.tablecontent(id);
  },

  
  validate_today: function(id)
  {
    // je-li dnes mimo povolený rozsah, ignoruj
    obj = calendar.list[id];
    date_now = new Date();
    date_from = new Date();
    date_from.setFullYear(obj.from_year,obj.from_month,obj.from_day);
    date_to = new Date();
    date_to.setFullYear(obj.to_year,obj.to_month,obj.to_day);
    
    if(date_from.getTime()-date_now.getTime()>=0 || date_now.getTime()>date_to.getTime())
    {
      return false;
    }
     
     return true;
  },
  
  keyhandle: function(e,id)
  {
    obj = calendar.list[id];
    var k = window.event ? e.keyCode : e.which;
    if((k==46 || k==8) && obj.empty) // delete nebo backspace
      obj.field.value = obj.hidden_field.value = "";
    if(k==37 || k==100) //vlevo
      calendar.shift_day(id,-1);
    if(k==39 || k==102) //vpravo
      calendar.shift_day(id,+1);
    if(k==38 || k==104) //nahoru
      calendar.shift_day(id,-7);
    if(k==40 || k==98) //dolů
      calendar.shift_day(id,+7);
    if(k==27 || k==13) // escape nebo enter
      obj.field.blur();
  },

  tag: function(name,atts,value)
  {
    // ošetření párového tagu
    pair = true;
    if(name.length==0) return value;
    if(name.substr(name.length-1,1)=="/") pair = false;
    if(!pair) name = name.substr(0,name.length-1);

    // atributy
    result = "<"+name;
    if(atts.length>0)
    {
      if(typeof atts == "string" && atts.match("=")==null) atts = "style=\""+atts+"\"";
      if(typeof atts == "string") result+= " "+atts;
    }
    if((typeof atts == "object"))
      for(i in atts) result+= " "+i+"=\""+atts[i]+"\"";
    result+= pair ? ">" : "/>";

    if(!pair) return result;

    // hodnota
    if(typeof value == "string") result+= value;
    result+= "</"+name+">";
    return result;
  },


  /*
   * Vypíše kalendář na výstup
   *
   * @param obj objekt kalendáře, který se má vypsat
   */
  write: function(obj)
  {
    // seznam instancí
    calendar.list[obj.id] = obj;

    // přidej styl, unikátní pro každý kalendář
    if(typeof calendar.styled[obj.col] == "undefined")
      document.write(calendar.add_style(obj.col));

    // datové pole
    arr_in = new Array();
    arr_in["type"] = "text";
    arr_in["readonly"] = 1;
    arr_in["id"] = "calendar_"+obj.id;
    arr_in["onclick"] = "calendar.show('"+obj.id+"');";
    arr_in["onfocus"] = "calendar.show('"+obj.id+"');";
    arr_in["onblur"] = "calendar.hide('"+obj.id+"');";
    arr_in["onkeydown"] = "calendar.keyhandle(event,'"+obj.id+"');";

    // HOFIX
    if( ! obj.initial_empty ) arr_in["value"] = obj.day+'.'+(parseInt(obj.month)+1)+'.'+obj.year;

    if(obj.style!="") arr_in["style"] = obj.style;
    if(obj.title!="") arr_in["title"] = obj.title;
    if(obj.accesskey!="") arr_in["accesskey"] = obj.accesskey;
    input = calendar.tag("input/",arr_in);

    // obrázek k datovému poli
    arr_im = new Array();
    arr_im["src"] = obj.imgpath+"calendar.gif";
    arr_im["onclick"] = "calendar.show('"+obj.id+"');";
    arr_im["style"] = "vertical-align: bottom; margin-bottom: -5px; cursor: pointer; _margin: 0px 0px 4px 10px";
    img = calendar.tag("img/",arr_im);

    // hidden pole (které se posílá)
    arr_hid = new Array();
    arr_hid["type"] = "hidden";
    arr_hid["name"] = obj.id;
    arr_hid["id"] = obj.id;
    // HOFIX
    if( ! obj.initial_empty ) arr_hid["value"] = obj.year+'-'+((parseInt(obj.month) + 1).toString().length!=2?'0':'')+(parseInt(obj.month) + 1)+'-'+(parseInt(obj.day).toString().length!=2?'0':'')+parseInt(obj.day);
    hidden = calendar.tag("input/",arr_hid);

// buňka datového pole
arr_data = new Array();
arr_data["valign"] = "bottom";
if(obj.tabindex) arr_data["tabindex"] = obj.tabindex;
cell_data = input + img + hidden;

    // šipka vlevo
    arr_iml = new Array();
    arr_iml["src"] = obj.imgpath+"arr-left-"+obj.col+".png";
    arr_iml["onmouseover"] = "calendar.set_keep('"+obj.id+"',1);this.src='"+obj.imgpath+"arr-left-"+obj.col+"-hover.png'";
    arr_iml["onmouseout"] = "calendar.set_keep('"+obj.id+"',0);this.src='"+obj.imgpath+"arr-left-"+obj.col+".png'";
    arr_iml["onmousedown"] = "return false;";
    
    // šipka vpravo
    arr_imr = new Array();
    arr_imr["src"] = obj.imgpath+"arr-right-"+obj.col+".png";
    arr_imr["onmouseover"] = "calendar.set_keep('"+obj.id+"',1);this.src='"+obj.imgpath+"arr-right-"+obj.col+"-hover.png'";
    arr_imr["onmouseout"] = "calendar.set_keep('"+obj.id+"',0);this.src='"+obj.imgpath+"arr-right-"+obj.col+".png'";
    arr_imr["onmousedown"] = "return false;";
    
      // šipka měsíc vlevo
      arr_iml["onclick"] = "calendar.shift_month('"+obj.id+"',-1)";
      month_left = calendar.tag("img/",arr_iml);

      // šipka s měsíci vpravo
      arr_imr["onclick"] = "calendar.shift_month('"+obj.id+"',+1)";
      month_right = calendar.tag("img/",arr_imr);

      // šipka s léty vlevo
      arr_iml["onclick"] = "calendar.shift_month('"+obj.id+"',-12)";
      year_left = calendar.tag("img/",arr_iml);

      // šipka s léty vpravo
      arr_imr["onclick"] = "calendar.shift_month('"+obj.id+"',+12)";
      year_right = calendar.tag("img/",arr_imr);

      // šipka se dny vlevo
      arr_iml["onclick"] = ""; //"calendar.shift_day('"+obj.id+"',-1)";
      arr_iml["id"]      = ""; //"button_left";
      day_left = ""; //calendar.tag("img/",arr_iml);

      // šipka se dny vpravo
      arr_imr["onclick"] = ""; //"calendar.shift_day('"+obj.id+"',+1)";
      arr_imr["id"]      = ""; //"button_right";
      day_right = ""; //calendar.tag("img/",arr_imr);



    // selectboxy
    arr_sel = new Array();
    arr_sel["onmouseover"] = "this.className='over';";
    arr_sel["onmouseout"] = "this.className='';";
    arr_sel["onclick"] = "calendar.actions('"+obj.id+"');";
    arr_sel["onfocus"] = "calendar.actions('"+obj.id+"');";
    //arr_sel["onblur"] = "calendar.hide('"+obj.id+"');";

      // selectbox měsíců
      arr_sel["id"] = "calendar_month_"+obj.id;
      arr_sel["onchange"] = "calendar.set_month('"+obj.id+"',this.options[this.selectedIndex].value);calendar.actions('"+obj.id+"');";
      arr_sel["tabindex"] = 9990;
        opt_month = "";
        for(i in calendar.months) opt_month+= calendar.tag("option","value=\""+i+"\"",calendar.months[i]);
      sel_month = calendar.tag("select",arr_sel,opt_month);

      //selectbox let
      arr_sel["id"] = "calendar_year_"+obj.id;
      arr_sel["onchange"] = "calendar.set_year('"+obj.id+"',this.options[this.selectedIndex].value);calendar.actions('"+obj.id+"');";
      arr_sel["tabindex"] = 9991;
        opt_year = "";
        for(i=obj.from_year;i<=obj.to_year;i++) opt_year+= calendar.tag("option","value=\""+i+"\"",""+i);
      sel_year = calendar.tag("select",arr_sel,opt_year);

    // křížek na zavření
    arr_close = new Array();
    arr_close["style"] = "float: right; margin-right: 3px; position: relative; top: 2px;";
    arr_close["src"] = obj.imgpath+"close-grey.png";
    arr_close["onmouseover"] = "this.src='"+obj.imgpath+"close.png';";
    arr_close["onmouseout"] = "this.src='"+obj.imgpath+"close-grey.png';";
    // arr_close["onclick"] = zavře se kliknutím kamkoliv
    img_close = calendar.tag("img/",arr_close);

    // obrázek dnes
    arr_imgtoday = new Array();
    arr_imgtoday["onmouseover"] = ""; //"calendar.set_keep('"+obj.id+"',1);this.src ='"+obj.imgpath+"today-"+obj.col+"-hover.png';";
    arr_imgtoday["onmouseout"]  = ""; //"calendar.set_keep('"+obj.id+"',0);this.src ='"+obj.imgpath+"today-"+obj.col+".png';";
    arr_imgtoday["onmousedown"] = ""; //"return false;";
    arr_imgtoday["src"] = ""; //obj.imgpath+"today-"+obj.col+".png";
    imgtoday = ""; //calendar.tag("img/",arr_imgtoday);
    
    // odkaz dnes
    arr_today = new Array();
    arr_today["onmouseover"] = "calendar.set_keep('"+obj.id+"',1);";
    arr_today["onmouseout"]  = "calendar.set_keep('"+obj.id+"',0);";
    arr_today["onmousedown"] = "return false;";
    arr_today["onclick"] = "calendar.shift_today('"+obj.id+"');return false;";
    arr_today["id"] = "button_today";
    
    // zneviditenit kdyz nema byt funkcni
    if(!this.validate_today(obj.id))
    {
      arr_today["style"]    = "display:none;";
      //arr_imgtoday["style"] = "display:none;";
    }
    
    img_today = calendar.tag("a",arr_today,imgtoday);


// ---wrapper---

  // podbuňka selectboxů
  month_box = month_left + sel_month + month_right;
  year_box = year_left + sel_year + year_right;
  nobr_box = calendar.tag("nobr","",month_box + year_box);
  arr_sub = new Array();
  arr_sub["class"] = "menu";
  subcell_selects = calendar.tag("td",arr_sub, nobr_box);
  subcell_selects = calendar.tag("tr","",subcell_selects);

  // podbuňka obsahu kalendáře
  subcell_calendar = calendar.tag("td","id=\"calendar_chart_"+obj.id+"\" style=\"padding: 0px\"","");
  subcell_calendar = calendar.tag("tr","",subcell_calendar);

  // podbuňka selectboxu dní
  day_box = img_close + day_left + img_today + day_right;
  arr_sub = new Array();
  arr_sub["class"] = "menu";
  subcell_day = calendar.tag("td",arr_sub,day_box);
  subcell_day = calendar.tag("tr","",subcell_day);

// -------------

    // obalovací skrývaná tabulka kalendáře
    table_wrapper = subcell_selects + subcell_calendar + subcell_day;
    table_wrapper = calendar.tag("table",'class="wrapper"',table_wrapper);
    table_wrapper = calendar.tag("span","style=\"display: none; position: absolute; z-index: 1\" id=\"calendar_data_"+obj.id+"\"",table_wrapper);

// buňka kalendáře
cell_calendar = table_wrapper;

    // obalovací tabulka
    arr_tab = new Array();
    arr_tab["class"] = "calendar"+obj.col;
    table = calendar.tag("span", arr_tab, cell_calendar + cell_data);
    document.write(table);

    // identifikátory funkčních prvků
    obj.field = document.getElementById("calendar_"+obj.id);
    obj.table = document.getElementById("calendar_data_"+obj.id);
    obj.chart = document.getElementById("calendar_chart_"+obj.id);
    obj.year_field = document.getElementById("calendar_year_"+obj.id);
    obj.month_field = document.getElementById("calendar_month_"+obj.id);
    obj.hidden_field = document.getElementById(obj.id);

    // nastavení objektu
    if(!obj.empty || obj.req)
      calendar.set_value(obj.id);
    obj.month_field.selectedIndex = obj.month;
    obj.year_field.selectedIndex = obj.year-obj.from_year;
    document.getElementById("calendar_chart_"+obj.id).innerHTML = calendar.tablecontent(obj.id);
  }
} // calendar static
