var aDate = [];
var aMonths = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var aDays = ['mo','tu','we','th','fr','sa','su'];

datebox = function(oEl, index){
  this.el = oEl;
  this.el.readons = true;
  this.el.onclick = function(){aDate[index].Clicked();};
  this.pos = [this.el.offsetLeft,this.el.offsetTop];
  this.clicker = null;
  var tmp = this.el;
  while(tmp = tmp.offsetParent){
    this.pos[0] += tmp.offsetLeft;
    this.pos[1] += tmp.offsetTop;
  }
//   this.el.onfocus = function(){aDate[index].Clicked()};
  this.name = oEl.name;
  this.el.name = this.name;
  this.index = index;
  this.date = new Date();
  this.width = oEl.offsetWidth;
  this.height = oEl.offsetHeight;
  this.shown = false;
  this.month = null;
  this.year = null;
  this.yearmin = 1900;
  this.yearmax = this.date.getFullYear()+10;
  this.init();
};

datebox.prototype.init = function(){
  this.el.readOnly = true;
  this.el.index = this.index;
  var cl = this.el.className;
  cl = cl.split(' ');
  for(i in cl){
    var val = cl[i];
    if(typeof(val) != 'function'){
      if(val.substr(0,7) == 'datemin'){
        this.yearmin = val.substr(7,val.length-7);
      }else if(val.substr(0,7) == 'datemax'){
        this.yearmax = val.substr(7,val.length-7);
      }
    }
  }
  var oDiv = document.createElement('div');
  oDiv.style.position = 'relative';
  oDiv.style.display = 'inline';
  oDiv.style.zIndex = 100-this.index;
  oDiv.className = 'overdate';
  this.el.parentNode.insertBefore(oDiv, this.el);
  oDiv.appendChild(this.el);
  if(!this.el.value)
    this.ParseData(null);
  else
    this.ParseData(this.el.value);
};

datebox.prototype.SetDate = function(day, month, year){
  this.date = new Date();
  this.date.setDate(day);
  this.date.setMonth(month);
  this.date.setFullYear(year);
  this.el.value = year+'-'+ll(month+1)+'-'+ll(day);
  this.Close();
};

datebox.prototype.ParseData = function(date){
  this.date = new Date();
  var reg = /[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}/;
  if(date !== null && reg.test(date)){
    var d = date.split('-');
    if(d[0] > 1900){
      this.date.setFullYear(d[0]);
      this.date.setMonth(d[1]-1);
      this.date.setDate(d[2]);
    }else date = null;
  }
  if(date !== null){
    this.el.value = this.date.getFullYear()+'-'+ll(this.date.getMonth()+1)+'-'+ll(this.date.getDate());
  }
  this.month = this.date.getMonth();
  this.year = this.date.getFullYear();
};

datebox.prototype.Clicked = function(){
  this.el.blur();
  if(this.shown) return this.Close();
  var oDiv = document.createElement('div');
  oDiv.className = 'datediv';
  oDiv.style.position = 'absolute';
  oDiv.style.top = (this.pos[1]+this.height)+'px';
  oDiv.style.left = this.pos[0]+'px'; //97
  var oTable = document.createElement('table');
  oTable.className = 'datetable';
  oDiv.appendChild(oTable);
  var oR = document.createElement('div');
  oR.className = 'dateright';
  oDiv.appendChild(oR);
  var oBr = document.createElement('br');
  oBr.style.lineHeight = '0%';
  oBr.style.clear = 'both';
  oDiv.appendChild(oBr);
  document.body.appendChild(oDiv);
  this.clicker = oDiv;
  this.DrawMonth(this.month, this.year);
  this.shown = true;
};

datebox.prototype.DrawMonth = function(month, year){
  this.month = month;
  this.year = year;
  var oTable = this.clicker.firstChild;
  while(oTable.rows.length)
    oTable.deleteRow(0);
  var oCap = oTable.nextSibling;
  var inner = '';
  inner += '<a class="dateclose" href="javascript:aDate['+this.index+'].Close()">X</a>';
  inner += '<br><b>Calendar</b><br>';
  inner += 'Choose month and year and click the date.<br />';
  inner += '<select onchange="aDate['+this.index+'].DrawMonth(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+'].DrawMonth('+this.month+', this.value)">';
  for(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), oCell;
  for(i=0;i<7;i++){
    oCell = oRow.insertCell(i);
    oCell.className = 'datedays';
    oCell.innerHTML = aDays[i];
  }
  i=1;
  var dd = new Date();
  dd.setMonth(this.month);
  dd.setFullYear(this.year);
  dd.setDate(1);
  var day = dd.getDay();
  day--;
  day %= 7;
  var rindex = 1;
  oRow = oTable.insertRow(rindex++);
  for(var j=0;j<day;j++){
    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++);
    }
    oCell = oRow.insertCell(j++);
    oCell.innerHTML = '<a href="javascript:aDate['+this.index+'].SetDate('+(i-1)+', '+this.month+', '+this.year+')" '+(i-1==this.date.getDate()&&this.month==this.date.getMonth()&&this.year==this.date.getFullYear()?'class="dateactive"':'')+'>'+(i-1)+'</a>';
  }
  for(j=j;j<7;j++){
    oCell = oRow.insertCell(j);
    oCell.innerHTML = '&nbsp;';
    oCell.className = 'empty';
  }
};

datebox.prototype.Close = function(){
  if(!this.clicker) return false;
  document.body.removeChild(this.clicker);
  this.clicker = null;
//   if(this.el.parentNode.childNodes.length == 1) return false;
//   this.el.parentNode.removeChild(this.el.parentNode.lastChild);
  this.shown = false;
  this.month = this.date.getMonth();
  this.year = this.date.getFullYear();
};

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

AddLoad(InitDatebox);

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