calendar = function(name,target,date)
{
 this.name=name;
 this.target=document.getElementById(target);
 this.date=date;
 this.width=210;
 this.height=1;
 this.left=0;
 this.top=0;

 this.createcontainer = function()
 {
  var doc=document;  
  doc.writeln("<div id=\"cal"+this.name+"\" style=\"position:absolute; left:"+this.left+"px; top:"+this.top+"px; width:"+this.width+"px; height:"+this.height+"px; z-index:10; visibility:hidden;\"></div>\n");
  doc.close();
  this.container=document.getElementById("cal"+this.name);
 },

 this.hide = function()
 {
  this.container.style.visibility='hidden';
 },

 this.settarget = function(value)
 {
  this.target.value=value;
  this.hide();
  this.target.focus();
 },

 this.str2dt = function(str_datetime) 
 {
  var re_date = /^(\d+)\.(\d+)\.(\d+)\.$/;
  if (!re_date.exec(str_datetime)) return alert("Helytelen dátumformátum: "+ str_datetime);
  return (new Date (RegExp.$1, RegExp.$2-1, RegExp.$3));
 },

 this.dt2dtstr = function(dt_datetime) 
 {
  var curr=dt_datetime.getFullYear()+".";
  if(dt_datetime.getMonth()+1<10) curr=curr+"0"+(dt_datetime.getMonth()+1)+".";
  else curr=curr+(dt_datetime.getMonth()+1)+".";
  if(dt_datetime.getDate()<10) curr=curr+"0"+dt_datetime.getDate()+".";
  else curr=curr+dt_datetime.getDate()+".";
  return curr;
 },

 this.tostamp = function(datearray)
 {
  if(parseInt(datearray[0])%4==0&&parseInt(datearray[0])%100!=0&&parseInt(datearray[0])%400==0) var months = new Array(0,31,29,31,31,31,31,31,31,31,31,31,31);
  else var months = new Array(0,31,28,31,31,31,31,31,31,31,31,31,31);
  stmp=0;
  for(y=0;y<=parseInt(datearray[0]);y++) if(y%4==0&&y%100!=0&&y%400==0) stmp+=366; else stmp+=365;
  for(m=1;m<parseInt(datearray[1]);m++) stmp+=months[m];
  stmp+=parseInt(datearray[2]);
  return stmp;
 },

 this.show = function(thedate,x,y)
 {
  var arr_months = ["Január", "Február", "Március", "Április", "Május", "Június", "Július", "Augusztus", "Szeptember", "Október", "November", "December"];
  var week_days = ["Va", "Hé", "Ke", "Sze", "Csü", "Pé", "Szo"];
  var n_weekstart = 1;

  var dt_datetime = (thedate == null || thedate =="" ?  new Date() : this.str2dt(thedate));
  var dt_prev_month = new Date(dt_datetime);
  dt_prev_month.setMonth(dt_datetime.getMonth()-1);
  var dt_next_month = new Date(dt_datetime);
  dt_next_month.setMonth(dt_datetime.getMonth()+1);
  var dt_firstday = new Date(dt_datetime);
  dt_firstday.setDate(1);
  dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
  var dt_lastday = new Date(dt_next_month);
  dt_lastday.setDate(0);

  var str_buffer = new String ();

  str_buffer+="<table cellspacing=\"0\" border=\"0\" width=\"100%\" class=\"calbody\">\n";
  str_buffer+="<tr><td>\n";
  str_buffer+="<table cellspacing=\"0\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n";
  str_buffer+="<tr>\n<td class=\"calheadcell\"><input class=\"calbutton\" type=\"button\" name=\"Prev\" value=\"&larr;\" OnMouseUp=\""+this.name+".show('"+this.dt2dtstr(dt_prev_month)+"');\"></td>\n";
  str_buffer+="<td class=\"calheadcell\" colspan=\"5\">"+dt_datetime.getFullYear()+" "+arr_months[dt_datetime.getMonth()]+"</td>\n";
  str_buffer+="<td class=\"calheadcell\"><input class=\"calbutton\" type=\"button\" name=\"Next\" value=\"&rarr;\" OnMouseUp=\""+this.name+".show('"+this.dt2dtstr(dt_next_month)+"');\"></td>\n</tr>\n";

  var dt_current_day = new Date(dt_firstday);
  str_buffer+="<tr>\n";
  for (var n=0; n<7; n++) str_buffer+="<td class=\"calweekcell\" style=\"width:15%;\">"+week_days[(n_weekstart+n)%7]+"</td>\n";
  str_buffer+="</tr>\n";

  while (dt_current_day.getMonth() == dt_datetime.getMonth() || dt_current_day.getMonth() == dt_firstday.getMonth()) 
  {
   str_buffer+="<tr>\n";
   for (var n_current_wday=0; n_current_wday<7; n_current_wday++)
   {
    if(dt_current_day.getDate()==dt_datetime.getDate()&&dt_current_day.getMonth()==dt_datetime.getMonth())
    {
     str_buffer+="<td class=\"calcurrentcell\" onMouseOver=\"this.className='calhovercell';\" onMouseOut=\"this.className='calcurrentcell';\">";
    }
    else
    {
     if(dt_current_day.getDay()==0||dt_current_day.getDay()==6)
     {
      if(dt_current_day.getMonth()==dt_datetime.getMonth())
      {
       str_buffer+="<td class=\"calhilitedcell\" onMouseOver=\"this.className='calhovercell';\" onMouseOut=\"this.className='calhilitedcell';\">";
      }
      else
      {
       str_buffer+="<td class=\"calhilitedomcell\" onMouseOver=\"this.className='calhovercell';\" onMouseOut=\"this.className='calhilitedomcell';\">";
      }
     }
     else
     {
      if(dt_current_day.getMonth()==dt_datetime.getMonth())
      {
       str_buffer+="<td class=\"calnormalcell\" onMouseOver=\"this.className='calhovercell';\" onMouseOut=\"this.className='calnormalcell';\">";
      }
      else
      {
       str_buffer+="<td class=\"calnormalomcell\" onMouseOver=\"this.className='calhovercell';\" onMouseOut=\"this.className='calnormalomcell';\">";
      }
     }
    }

    if(dt_current_day.getMonth()==dt_datetime.getMonth())
    {
     str_buffer+="<a href=\"javascript:"+this.name+".settarget('"+this.dt2dtstr(dt_current_day)+"');\">";
     str_buffer+=dt_current_day.getDate()+"</a></td>\n";
    }
    else
    {
     str_buffer+="<a href=\"javascript:"+this.name+".settarget('"+this.dt2dtstr(dt_current_day)+"');\">";
     str_buffer+=dt_current_day.getDate()+"</a></td>\n";
    }

    dt_current_day.setDate(dt_current_day.getDate()+1);
   }
   str_buffer+="</tr>\n";
  }
  str_buffer+="</table>\n</tr>\n</td>\n</table>\n";
  
  this.container.innerHTML=str_buffer;  
  this.container.style.visibility='visible';
  this.container.style.left=x;
  this.container.style.top=y;
 }

 this.createcontainer();
};
