function callMe(url, callback){
  ajaxObject = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
  try { return new XMLHttpRequest() } catch(e){
  throw new Error( "This browser does not support XMLHttpRequest." );
  }}}}}}
  ajax = new ajaxObject();
  ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
      if(ajax.status == 200){
        aDate[callback].DrawMonth(ajax.responseText);
      }
    }
  };
  ajax.open('GET', url, true);
  ajax.send(null);
}

var aDate = [];

datebox = function(oEl, index){
  this.el = oEl;
  this.name = oEl.name;
  this.el.name = this.name+'[full]';
  this.index = index;
  this.data = {};
  this.date = new Date();
  this.width = oEl.offsetWidth;
  this.height = oEl.offsetHeight;
  this.shown = false;
  this.month = this.date.getMonth();
  this.year = this.date.getFullYear();
  this.yearmin = this.date.getFullYear()-10;
  this.yearmax = this.date.getFullYear()+10;
  this.start = false;
  this.init();
}

datebox.prototype.init = function(){
  this.el.index = this.index;
  var oInput = document.createElement('input');
  oInput.name = this.name+'[day]';
  oInput.type = 'hidden';
  this.el.appendChild(oInput);
  oInput = document.createElement('input');
  oInput.name = this.name+'[month]';
  oInput.type = 'hidden';
  this.el.appendChild(oInput);
  oInput = document.createElement('input');
  oInput.name = this.name+'[year]';
  oInput.type = 'hidden';
  this.el.appendChild(oInput);
  
  var oTable = document.createElement('table');
  oTable.className = 'datetable';
  this.el.appendChild(oTable);
  var oR = document.createElement('div');
  oR.className = 'dateright';
  this.el.appendChild(oR);
  var oBr = document.createElement('br');
  oBr.style.lineHeight = '0%';
  oBr.style.clear = 'both';
  this.el.appendChild(oBr);
  this.SetMonth(this.month, this.year);
}

datebox.prototype.SetDate = function(day, month, year){
  if(document.getElementById('day['+this.date.getDate()+']')) document.getElementById('day['+this.date.getDate()+']').className = '';
  if(day) document.getElementById('day['+day+']').className = 'dateactive';
  this.date = new Date();
  if(day) this.date.setDate(day);
  if(year) this.date.setFullYear(year);
  if(month) this.date.setMonth(month);
  this.el.childNodes[0].value = this.date.getDate();
  this.el.childNodes[1].value = this.date.getMonth+1;
  this.el.childNodes[2].value = this.date.getFullYear();
  document.getElementById('day').innerHTML = ll(this.date.getDate())+'.'+ll(this.date.getMonth()+1)+'.'+ll(this.date.getFullYear());
  if(this.data[this.date.getDate()]){
    var set = this.data[this.date.getDate()];
    document.getElementById('dateimg').src = set.photo;
    document.getElementById('datedesc').innerHTML = '<a href="details,id_'+set.id+'.html"><b>'+set.name+'</b></a><br><a href="details,id_'+set.id+'.html" style="float: right">'+L_read_more+'</a>';
  }else{
    document.getElementById('dateimg').src = '/templates/nophoto/126x96.jpg';
    document.getElementById('datedesc').innerHTML = '<b>'+L_no_event+'</b>';
  }
}

datebox.prototype.SetMonth = function(month, year){
  this.el.childNodes[3].style.display = 'none';
  this.el.childNodes[4].style.display = 'none';
  this.month = month;
  this.year = year;
  var index = this.index;
  callMe('/ajax/getmonth.php?month='+month+'&year='+year, index);
}

datebox.prototype.DrawMonth = function(str){
  this.data = {};
  try{
    eval('this.data = '+str+';');
  }catch(e){}
  if(!this.start){
    this.SetDate();
    this.start = true;
  }
  var oTable = this.el.childNodes[3];
  oTable.style.display = '';
  while(oTable.rows.length)
    oTable.deleteRow(0);
  var oCap = oTable.nextSibling;
  oCap.style.display = '';
  var inner = '';
  inner += '<b>Kalendarz</b><br><br>'+L_choose+'<br>';
  inner += '<select onchange="aDate['+this.index+'].Set(this.value, '+this.year+')">';
  for(var i=0;i<12;i++)
    inner += '<option value="'+i+'" '+(i==this.month?'selected="true"':'')+'>'+aMonths[i]+'</option>';
  inner += '</select>';
  inner += '<select onchange="aDate['+this.index+'].SetMonth('+this.month+', this.value)">';
  for(var i=this.yearmin;i<=this.yearmax;i++)
    inner += '<option value="'+i+'" '+(i==this.year?'selected="true"':'')+'>'+i+'</option>';
  inner += '</select>';
  oCap.innerHTML = inner;
  var oRow = oTable.insertRow(0);
  for(var i=0;i<7;i++){
    var oCell = oRow.insertCell(i);
    oCell.className = 'datedays';
    oCell.innerHTML = aDays[i];
  }
  var i=1;
  var dd = new Date();
  dd.setFullYear(this.year);
  dd.setDate(1);
  dd.setMonth(this.month);
  var day = dd.getDay();
  day--;
  if(day<0) day = 6;
  var rindex = 1;
  var oRow = oTable.insertRow(rindex++);
  for(var j=0;j<day;j++){
    var oCell = oRow.insertCell(j);
    oCell.innerHTML = '&nbsp;';
    oCell.className = 'empty';
  }
  while(dd.setDate(i++) && dd.getMonth() == this.month){
    if(j>6){
      j=0;
      oRow = oTable.insertRow(rindex++);
    }
    var oCell = oRow.insertCell(j++);
    if(typeof(this.data[i-1])=='object') oCell.className="full";
    oCell.innerHTML = '<a href="javascript:aDate['+this.index+'].SetDate('+(i-1)+', '+this.month+', '+this.year+');" onclick="blur();" '+(i-1==this.date.getDate()&&this.month==this.date.getMonth()&&this.year==this.date.getFullYear()?'class="dateactive"':'')+' id="day['+(i-1)+']">'+(i-1)+'</a>';
  }
  for(j=j;j<7;j++){
    var oCell = oRow.insertCell(j);
    oCell.innerHTML = '&nbsp;';
    oCell.className = 'empty';
  }
}

function ll(val){
  val = val.toString();
  if(val.length == 2) return val;
  return '0'+val;
}

var dssd = window.onload;
if(typeof(dssd) !== 'function')
  window.onload = InitDatebox;
else
  window.onload = function(){dssd();InitDatebox();};

function InitDatebox(){
  var aEl = document.getElementsByTagName('div');
  var aToDo = [];
  for(var i=0;i<aEl.length;i++){
    var oEl = aEl[i];
    if(oEl.className == 'datebox')
      aToDo[aToDo.length] = oEl;
  }
  for(i=0;i<aToDo.length;i++)
    aDate[aDate.length] = new datebox(aToDo[i], aDate.length);
}