/* Gemaakt door Gerrit van Roekel
   GvRStudio -- 13-12-2006 -- 1.0*/

var arMonths = new Array("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December");
var arDays = new Array('Z','M','D','W','D','V','Z');
var ie = (document.all) ? 1:0;

function getOffset(objID) {
  var x = document.getElementById(objID).offsetLeft;
  var y = document.getElementById(objID).offsetTop + document.getElementById(objID).offsetHeight;
 
  var parent = document.getElementById(objID);
  while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop ;
   }
   return new Array(x,y);
 }


function showCalendar(objID, objIDWhere, date) {
  var ie = (document.all) ? 1:0;
  if (document.getElementById('showCalendar') == null) {
    objCalendar = document.createElement("div");
    objCalendar.setAttribute('id', 'showCalendar');
    document.body.appendChild(objCalendar);
  }
  posObjID = getOffset('showCalendar');
  pos = getOffset(objIDWhere);
  if (posObjID[0] != pos[0] && posObjID[1] != pos[1]) {
    var re = new RegExp("([0-9]{2}).?([0-9]{2}).?([0-9]{2}$)","g");
    if (date == '' || date == null) {
      date = document.getElementById(objID).value;
    }
    var match = re.exec(date);
    if (match != '' && match != null) {
      day = match[1];
      month = match[2];
      year = match[3];
    } else {
      date = new Date();
      day = date.getDate();
      month = date.getMonth() + 1;
      year = date.getFullYear();
    }
    objCalendar.style.left = (pos[0]-40)+'px';
    objCalendar.style.top = (pos[1]+5)+'px';
    changeMonth(objID, day, month, year);
  } else {
    document.body.removeChild(document.getElementById('showCalendar'), true);
  }
}

function getYear(d) { 
  return (d < 1000) ? d + 1900 : d;
}

function isDate (year, month, day) {
  month--;
  var tempDate = new Date(year,month,day);
  if (getYear(tempDate.getYear()) == year && month == tempDate.getMonth() && day == tempDate.getDate()) {
    return true;
  } else {
    return false
  }
}

function setDate(objID, day, month, year) {
  if (day != '' && month != '' && year != '') {
    var date = null;
    if (day < 10) {
      date = '0'+day;
    } else {
      date = day;
    }
    if (month < 10) {
      date+= '-0'+month;
    } else {
      date+= '-'+month;
    }
    date+= '-'+year;
    document.getElementById(objID).value = date;
  }
  document.body.removeChild(document.getElementById('showCalendar'), true);
}

function changeMonth(objID, day, month, year) {
  document.getElementById('showCalendar').innerHTML = "";
  form = document.createElement("form");
  select = document.createElement("select");
  select.setAttribute("id", "calendarMonth");
  select.onchange = function() {
    changeMonth(objID, day, this.value, document.getElementById('calendarYear').value);
  }
  for (i=0;i<12;i++) {
    var option = document.createElement("option");
    option.setAttribute("value", i + 1);
    if (i == month - 1) {
      option.setAttribute('selected',true);
    }
    option.appendChild(document.createTextNode(arMonths[i]));
    select.appendChild(option);
  }
  form.appendChild(select);
  select = document.createElement("select");
  select.setAttribute("id", "calendarYear");
  select.onchange = function() {
    changeMonth(objID, day, document.getElementById('calendarMonth').value, this.value);
  }
  for (i=1900;i<2100;i++) {
    var option = document.createElement("option");
    option.setAttribute("value", i);
    if (i == year) {
      option.setAttribute('selected',true);
    }
    option.appendChild(document.createTextNode(i));
    select.appendChild(option);
  }
  form.appendChild(select);
  document.getElementById('showCalendar').appendChild(form);
  var date = new Date(year,month - 1,1);
  var lastDay = 0;
  divDays = document.createElement("div");
  divDays.setAttribute('id', 'days');

  for (i=0;i<7;i++) {
    div = document.createElement("div");
    if (ie) {
      div.setAttribute('className', 'dayHeader');
    } else {
      div.setAttribute("class", 'dayHeader');
    }
    div.appendChild(document.createTextNode(arDays[i]));
    divDays.appendChild(div);
    div = null;
  }
  for (i=0;i<date.getDay();i++) {
    div = document.createElement("div");
    if (ie) {
      div.setAttribute('className', 'empty');
    } else {
      div.setAttribute("class", 'empty');
    }
    divDays.appendChild(div);
    div = null;
  }
  for (i=0;i<32;i++) {
    if (isDate(year, month, i)) {
      div = document.createElement("a");
      date = new Date();
      if (date.getDate() == i && date.getMonth() + 1 == month && date.getFullYear() == year) {
        className = 'today';
      } else {
        className = 'day'
      }
      if (ie) {
        div.setAttribute('className', className);
      } else {
        div.setAttribute("class", className);
      }
      div.setAttribute("href", "javascript:setDate('"+objID+"',"+i+","+month+","+year+");");
      div.appendChild(document.createTextNode(i));
      divDays.appendChild(div);
      div = null;
      date = new Date(year,month-1,i);
      lastDay = date.getDay();
    }
  }
  for (i=lastDay;i<6;i++) {
    div = document.createElement("div");
    if (ie) {
      div.setAttribute('className', 'empty');
    } else {
      div.setAttribute("class", 'empty');
    }
    divDays.appendChild(div);
    div = null;
  }
  div = document.createElement("a");
  if (ie) {
    div.setAttribute('className', 'footer');
  } else {
    div.setAttribute("class", 'footer');
  }
  date = new Date();
  day = date.getDate();
  month = date.getMonth() + 1;
  year = date.getFullYear();
  div.setAttribute("href", "javascript:setDate('"+objID+"',"+day+","+month+","+year+");");
  div.appendChild(document.createTextNode('Today'));
  divDays.appendChild(div);
  div = null;

  div = document.createElement("a");
  if (ie) {
    div.setAttribute('className', 'footer');
  } else {
    div.setAttribute("class", 'footer');
  }
  div.setAttribute("href", "javascript:setDate('"+objID+"','','','');");
  div.appendChild(document.createTextNode('Close'));
  divDays.appendChild(div);
  div = null;

  document.getElementById('showCalendar').appendChild(divDays);
}
