/*
(C) www.dhtmlgoodies.com, September 2005

Version 1.2, November 8th - 2005 - Added <iframe> background in IE
Version 1.3, November 12th - 2005 - Fixed top bar position in Opera 7
Version 1.4, December 28th - 2005 - Support for Spanish and Portuguese
Version 1.5, January  18th - 2006 - Fixed problem with next-previous buttons after a month has been selected from dropdown
Version 1.6, February 22nd - 2006 - Added variable which holds the path to images.
 Format todays date at the bottom by use of the todayStringFormat variable
 Pick todays date by clicking on todays date at the bottom of the calendar

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

Format of current day at the bottom of the calendar
[todayString] = the value of todayString
[dayString] = day of week (examle: Mon, Tue, Wed...)
[day] = Day of month, 1..31
[monthString] = Name of current month
[year] = Current year
*/
var todayStringFormat = '[todayString] [dayString], [monthString] [day], [year]';      
var pathToImages = 'i/c/'; // relative

var calendar_offsetTop = 6;
var calendar_offsetLeft = 0;
var calD = false;

var MSIE = false;
var Opera = false;
if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0)MSIE=true;
if(navigator.userAgent.indexOf('Opera')>=0)Opera=true;

var monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var monthArrayShort = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var dayArray = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var dayArrayLong = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var todayString = 'Today is';

var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
var currentMonth;
var currentYear;
var calendarContentDiv;
var returnDateTo;
var returnFormat;
var activeSelectBoxMonth = false;
var activeSelectBoxYear;
var activeCell;
var iframeObj = false;

var returnDateToYear;
var returnDateToMonth;
var returnDateToDay;

var inputYear;
var inputMonth;
var inputDay;

var selectBoxHighlightColor = '#999999';
var selectBoxRolloverBgColor = '#D0EDF5';
function cancelCalendarEvent() { return false; }
function isLeapYear(inputYear) {
 if(inputYear%400==0||(inputYear%4==0&&inputYear%100!=0)) return true;
 return false;
}

function highlightDay() {
 if(this.innerHTML==activeCell) {
  if(this.className=='activeDayHilite') this.className='activeDay';
  else this.className='activeDayHilite';
 }
 else {
  if(this.className=='dayHilite') this.className='';
  else this.className = 'dayHilite';
 }
}

function highlightMonthYear() {
 if(activeSelectBoxMonth) activeSelectBoxMonth.className='';
 if(this.className=='monthYearActive') this.className='';
 else{
  this.className = 'monthYearActive';
  activeSelectBoxMonth = this;
 }
}

function showMonthDropDown() {
 if(d.getElementById('monthDropDown').style.display=='block'){
  d.getElementById('monthDropDown').style.display='none';
 }else{
  d.getElementById('monthDropDown').style.display='block';
  d.getElementById('yearDropDown').style.display='none';
 }
}

function showYearDropDown() {
 if(d.getElementById('yearDropDown').style.display=='block'){
  d.getElementById('yearDropDown').style.display='none';
 }else{
  d.getElementById('yearDropDown').style.display='block';
  d.getElementById('monthDropDown').style.display='none';
 }
}

function selectMonth() {
 d.getElementById('calendar_month_txt').innerHTML = this.innerHTML;
 currentMonth = this.id.replace(/[^\d]/g,'');

 d.getElementById('monthDropDown').style.display='none';
 for(var no=0;no<monthArray.length;no++){
  d.getElementById('monthDiv_'+no).style.color='';
 }
 this.style.color = selectBoxHighlightColor;
 activeSelectBoxMonth = this;
 writeCalendarContent();
}

function selectYear() {
 d.getElementById('calendar_year_txt').innerHTML = this.innerHTML;
 currentYear = this.innerHTML.replace(/[^\d]/g,'');
 d.getElementById('yearDropDown').style.display='none';
 if(activeSelectBoxYear) activeSelectBoxYear.style.color='';
 activeSelectBoxYear=this;
 this.style.color = selectBoxHighlightColor;
 writeCalendarContent();
}

function switchMonth() {
 if(this.src.indexOf('left')>=0){
  currentMonth=currentMonth-1;
  if(currentMonth<0){
   currentMonth=11;
   currentYear=currentYear-1;
  }
 }else{
  currentMonth=currentMonth+1;
  if(currentMonth>11){
   currentMonth=0;
   currentYear=currentYear/1+1;
  }
 }
 writeCalendarContent();
}

function createMonthDiv(){
 var div = d.createElement('DIV');
 div.className='monthYearPicker';
 div.id = 'monthPicker';

 for(var no=0;no<monthArray.length;no++){
  var subDiv = d.createElement('DIV');
  subDiv.innerHTML = monthArray[no];
  subDiv.onmouseover = highlightMonthYear;
  subDiv.onmouseout = highlightMonthYear;
  subDiv.onclick = selectMonth;
  subDiv.id = 'monthDiv_' + no;
  subDiv.style.width = '56px';
  subDiv.onselectstart = cancelCalendarEvent;
  div.appendChild(subDiv);
  if(currentMonth && currentMonth==no){
   subDiv.style.color = selectBoxHighlightColor;
   activeSelectBoxMonth = subDiv;
  }
 }
 return div;
}

function changeSelectBoxYear() {
 var yearItems = this.parentNode.getElementsByTagName('DIV');
 if(this.innerHTML.indexOf('-')>=0){
  var startYear = yearItems[1].innerHTML/1 -1;
  if(activeSelectBoxYear) activeSelectBoxYear.style.color='';
 }else{
  var startYear = yearItems[1].innerHTML/1 +1;
  if(activeSelectBoxYear) activeSelectBoxYear.style.color='';
 }

 for(var no=1;no<yearItems.length-1;no++){
  yearItems[no].innerHTML = startYear+no-1;
  yearItems[no].id = 'yearDiv' + (startYear/1+no/1-1);
 }
 if(activeSelectBoxYear){
  activeSelectBoxYear.style.color='';
  if(d.getElementById('yearDiv'+currentYear)){
   activeSelectBoxYear = d.getElementById('yearDiv'+currentYear);
   activeSelectBoxYear.style.color=selectBoxHighlightColor;
  }
 }
}

function updateYearDiv(){
 var div = d.getElementById('yearDropDown');
 var yearItems = div.getElementsByTagName('DIV');
 for(var no=1;no<yearItems.length-1;no++){
  yearItems[no].innerHTML = currentYear/1 -6 + no;
  if(currentYear==(currentYear/1 -6 + no)){
   yearItems[no].style.color = selectBoxHighlightColor;
   activeSelectBoxYear = yearItems[no];
  }
  else yearItems[no].style.color = '';
 }
}

function updateMonthDiv() {
 for(no=0;no<12;no++){
  d.getElementById('monthDiv_' + no).style.color = '';
 }
 d.getElementById('monthDiv_' + currentMonth).style.color = selectBoxHighlightColor;
 activeSelectBoxMonth =  d.getElementById('monthDiv_' + currentMonth);
}

function createYearDiv() {
 if(!d.getElementById('yearDropDown')){
  var div = d.createElement('DIV');
  div.className='monthYearPicker';
 }else{
  var div = d.getElementById('yearDropDown');
  var subDivs = div.getElementsByTagName('DIV');
  for(var no=0;no<subDivs.length;no++){
   subDivs[no].parentNode.removeChild(subDivs[no]);
  }
 }
 var dt = new Date();
 if(currentYear) dt.setFullYear(currentYear);

 var startYear = dt.getFullYear()/1 - 5;
 var subDiv = d.createElement('DIV');
 subDiv.innerHTML = '&nbsp;&nbsp;- ';
 subDiv.onclick = changeSelectBoxYear;
 subDiv.onmouseover = highlightMonthYear;
 subDiv.onmouseout = highlightMonthYear;
 subDiv.onselectstart = cancelCalendarEvent;
 div.appendChild(subDiv);

 for(var no=startYear;no<(startYear+10);no++){
  var subDiv = d.createElement('DIV');
  subDiv.innerHTML = no;
  subDiv.onmouseover = highlightMonthYear;
  subDiv.onmouseout = highlightMonthYear;
  subDiv.onclick = selectYear;
  subDiv.id = 'yearDiv' + no;
  subDiv.onselectstart = cancelCalendarEvent;
  div.appendChild(subDiv);
  if(currentYear && currentYear==no){
   subDiv.style.color = selectBoxHighlightColor;
   activeSelectBoxYear = subDiv;
  }
 }
 var subDiv = d.createElement('DIV');
 subDiv.innerHTML = '&nbsp;&nbsp;+ ';
 subDiv.onclick = changeSelectBoxYear;
 subDiv.onmouseover = highlightMonthYear;
 subDiv.onmouseout = highlightMonthYear;
 subDiv.onselectstart = cancelCalendarEvent;
 div.appendChild(subDiv);
 return div;
}

function highlightSelect() {
 if(this.className=='selectBox'){
  this.className = 'selectBoxOver';
  this.getElementsByTagName('IMG')[0].src = pathToImages + 'down_over.gif';
 }else{
  this.className = 'selectBox';
  this.getElementsByTagName('IMG')[0].src = pathToImages + 'down.gif';
 }
}

function highlightArrow() {
 if(this.src.indexOf('over')>=0){
  if(this.src.indexOf('left')>=0)this.src = pathToImages + 'left.gif';
  if(this.src.indexOf('right')>=0)this.src = pathToImages + 'right.gif';
 }else{
  if(this.src.indexOf('left')>=0)this.src = pathToImages + 'left_over.gif';
  if(this.src.indexOf('right')>=0)this.src = pathToImages + 'right_over.gif';
 }
}

function highlightClose() {
 if(this.src.indexOf('over')>=0){
  this.src = pathToImages + 'close.gif';
 }else{
  this.src = pathToImages + 'close_over.gif';
 }
}

function closeCalendar(){
 d.getElementById('yearDropDown').style.display='none';
 d.getElementById('monthDropDown').style.display='none';

 calD.style.display='none';
 if(iframeObj)iframeObj.style.display='none';
 if(activeSelectBoxMonth)activeSelectBoxMonth.className='';
 if(activeSelectBoxYear)activeSelectBoxYear.className='';
}

function writeTopBar() {
 var topBar = d.createElement('DIV');
 topBar.className = 'topBar';
 topBar.id = 'topBar';
 calD.appendChild(topBar);

 // Left arrow
 var leftDiv = d.createElement('DIV');
 leftDiv.style.marginRight = '1px';
 var img = d.createElement('IMG');
 img.src = pathToImages + 'left.gif';
 img.onmouseover = highlightArrow;
 img.onclick = switchMonth;
 img.onmouseout = highlightArrow;
 leftDiv.appendChild(img);
 topBar.appendChild(leftDiv);
 if(Opera)leftDiv.style.width = '16px';

 // Right arrow
 var rightDiv = d.createElement('DIV');
 rightDiv.style.marginRight = '1px';
 var img = d.createElement('IMG');
 img.src = pathToImages + 'right.gif';
 img.onclick = switchMonth;
 img.onmouseover = highlightArrow;
 img.onmouseout = highlightArrow;
 rightDiv.appendChild(img);
 if(Opera)rightDiv.style.width = '16px';
 topBar.appendChild(rightDiv);

 // Month selector
 var monthDiv = d.createElement('DIV');
 monthDiv.id = 'monthSelect';
 monthDiv.onmouseover = highlightSelect;
 monthDiv.onmouseout = highlightSelect;
 monthDiv.onclick = showMonthDropDown;
 var span = d.createElement('SPAN');
 span.innerHTML = monthArray[currentMonth];
 span.id = 'calendar_month_txt';
 monthDiv.appendChild(span);

 var img = d.createElement('IMG');
 img.src = pathToImages + 'down.gif';
 img.style.position = 'absolute';
 img.style.right = '0px';
 monthDiv.appendChild(img);
 monthDiv.className = 'selectBox';
 if(Opera){
  img.style.cssText = 'float:right;position:relative';
  img.style.position = 'relative';
  img.style.styleFloat = 'right';
 }
 topBar.appendChild(monthDiv);

 var monthPicker = createMonthDiv();
 monthPicker.style.left = '37px';
 monthPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
 monthPicker.style.width ='60px';
 monthPicker.id = 'monthDropDown';

 calD.appendChild(monthPicker);

 // Year selector
 var yearDiv = d.createElement('DIV');
 yearDiv.onmouseover = highlightSelect;
 yearDiv.onmouseout = highlightSelect;
 yearDiv.onclick = showYearDropDown;
 var span = d.createElement('SPAN');
 span.innerHTML = currentYear;
 span.id = 'calendar_year_txt';
 yearDiv.appendChild(span);
 topBar.appendChild(yearDiv);

 var img = d.createElement('IMG');
 img.src = pathToImages + 'down.gif';
 yearDiv.appendChild(img);
 yearDiv.className = 'selectBox';

 if(Opera){
  yearDiv.style.width = '50px';
  img.style.cssText = 'float:right';
  img.style.position = 'relative';
  img.style.styleFloat = 'right';
 } 
 var yearPicker = createYearDiv();
 yearPicker.style.left = '113px';
 yearPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
 yearPicker.style.width = '35px';
 yearPicker.id = 'yearDropDown';
 calD.appendChild(yearPicker);

 var img = d.createElement('IMG');
 img.src = pathToImages + 'close.gif';
 img.style.styleFloat = 'right';
 img.onmouseover = highlightClose;
 img.onmouseout = highlightClose;
 img.onclick = closeCalendar;
 topBar.appendChild(img);
 if(!d.all){
  img.style.position = 'absolute';
  img.style.right = '2px';
 }
}

function writeCalendarContent(){
 var calendarContentDivExists = true;
 if(!calendarContentDiv){
  calendarContentDiv = d.createElement('DIV');
  calD.appendChild(calendarContentDiv);
  calendarContentDivExists = false;
 }
 currentMonth = currentMonth/1;
 var dt = new Date();

 dt.setFullYear(currentYear);
 dt.setDate(1);
 dt.setMonth(currentMonth);

 var dayStartOfMonth = dt.getDay();

 d.getElementById('calendar_year_txt').innerHTML = currentYear;
 d.getElementById('calendar_month_txt').innerHTML = monthArray[currentMonth];

 var existingTable = calendarContentDiv.getElementsByTagName('TABLE');
 if(existingTable.length>0) calendarContentDiv.removeChild(existingTable[0]);
 
 var calTable = d.createElement('TABLE');
 calTable.cellSpacing = '0';
 calendarContentDiv.appendChild(calTable);
 var calTBody = d.createElement('TBODY');
 calTable.appendChild(calTBody);
 var row = calTBody.insertRow(-1);

 for(var no=0;no<dayArray.length;no++){
  var cell = row.insertCell(-1);
  cell.width = '22px';
  cell.innerHTML = dayArray[no];
 }

 var row = calTBody.insertRow(-1);
 for(var no=0;no<dayStartOfMonth;no++){
  var cell = row.insertCell(-1);
  cell.innerHTML = '&nbsp;';
 }

 var colCounter = dayStartOfMonth;
 var daysInMonth = daysInMonthArray[currentMonth];
 if(daysInMonth==28){
  if(isLeapYear(currentYear))daysInMonth=29;
 }

 for(var no=1;no<=daysInMonth;no++){
  dt.setDate(no-1);
  if(colCounter>0 && colCounter%7==0){
   var row = calTBody.insertRow(-1);
  }
  var cell = row.insertCell(-1);
  if(currentYear==inputYear && currentMonth == inputMonth && no==inputDay){
   cell.className='activeDay';
   activeCell = no;
  }
  cell.innerHTML = no;
  cell.onmouseover = highlightDay;
  cell.onmouseout = highlightDay;
  cell.onclick = pickDate;
  colCounter++;
 }

 if(!d.all){
  if(calendarContentDiv.offsetHeight)
   d.getElementById('topBar').style.top = calendarContentDiv.offsetHeight + d.getElementById('topBar').offsetHeight -1 + 'px';
  else{
   d.getElementById('topBar').style.top = '';
   d.getElementById('topBar').style.bottom = '0px';
  }
 }

 if(iframeObj){
  if(!calendarContentDivExists)setTimeout('resizeIframe()',350);else setTimeout('resizeIframe()',10);
 }
}

function resizeIframe() {
 iframeObj.style.width = calD.offsetWidth + 'px';
 iframeObj.style.height = calD.offsetHeight + 'px';
}

function pickTodaysDate() {
 var dt = new Date();
 currentMonth = dt.getMonth();
 currentYear = dt.getFullYear();
 pickDate(false,dt.getDate());
}

function pickDate(e,inputDay) {
 var month = currentMonth/1 +1;
// if(month<10)month = '0' + month;
 var day;
 if(!inputDay && this)day = this.innerHTML; else day = inputDay;

 if(day/1<10)day = '0' + day;
 if(returnFormat){
  returnFormat = returnFormat.replace('dd',day);
  returnFormat = returnFormat.replace('mm',month);
  returnFormat = returnFormat.replace('yyyy',currentYear);
  returnDateTo.value = returnFormat;
  returnDateTo.focus();
 }else{
  for(var no=0;no<returnDateToYear.options.length;no++){
   if(returnDateToYear.options[no].value==currentYear){
    returnDateToYear.selectedIndex=no;
    break;
   }
  }
  for(var no=0;no<returnDateToMonth.options.length;no++){
   if(returnDateToMonth.options[no].value==month){
    returnDateToMonth.selectedIndex=no;
    break;
   }
  }
  for(var no=0;no<returnDateToDay.options.length;no++){
   if(returnDateToDay.options[no].value==day){
    returnDateToDay.selectedIndex=no;
    break;
   }
  }
 }
 closeCalendar();
}

function writeBottomBar() {
 var dt = new Date();
 var topBar = d.createElement('DIV');
 topBar.id = 'bottomBar';
 topBar.onclick = pickTodaysDate;
 topBar.style.cursor = 'pointer';
 topBar.className = 'todaysDate';
 var day = dt.getDay();

 var bottomString = todayStringFormat;
 bottomString = bottomString.replace('[monthString]',monthArray[dt.getMonth()]);
 bottomString = bottomString.replace('[day]',dt.getDate());
 bottomString = bottomString.replace('[year]',dt.getFullYear());
 bottomString = bottomString.replace('[dayString]',dayArrayLong[day]);
 bottomString = bottomString.replace('[todayString]',todayString);

 topBar.innerHTML = todayString+': '+dt.getDate()+'. '+monthArrayShort[dt.getMonth()]+', '+dt.getFullYear();
 topBar.innerHTML = bottomString;
 calD.appendChild(topBar);
}

function getTopPos(inputObj) {
 var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
 while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
 return returnValue + calendar_offsetTop;
}

function getleftPos(inputObj) {
 var returnValue = inputObj.offsetLeft;
 while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
 return returnValue + calendar_offsetLeft;
}

function positionCalendar(inputObj) {
 calD.style.left = getleftPos(inputObj) + 'px';
 calD.style.top = getTopPos(inputObj) + 'px';
 if(iframeObj){
  iframeObj.style.left = calD.style.left;
  iframeObj.style.top =  calD.style.top;
 }
}

function initCalendar() {
 if(MSIE){
  iframeObj = d.createElement('IFRAME');
  iframeObj.style.position = 'absolute';
  iframeObj.border='0px';
  iframeObj.style.border = '0px';
  iframeObj.style.backgroundColor = '#FF0000';
  d.body.appendChild(iframeObj);
 }

 calD = d.createElement('DIV');
 calD.id = 'calD';
 calD.style.zIndex = 1000;

 d.body.appendChild(calD);
 writeBottomBar();
 writeTopBar();
 
 if(!currentYear){
  var dt = new Date();
  currentMonth = dt.getMonth();
  currentYear = dt.getFullYear();
 }
 writeCalendarContent();
}

function displayCalendar(inputField,format,buttonObj) {
 var tput=inputField.value.split(' ');
 var iput=tput[0];
 if(parseInt(iput) < 10) iput='0'+iput;
 if(iput.length==format.length){
  var monthPos = format.indexOf('mm');
  currentMonth = iput.substr(monthPos,2)/1 -1;
  var yearPos = format.indexOf('yyyy');
  currentYear = iput.substr(yearPos,4);
  var dayPos = format.indexOf('dd');
  tmpDay = iput.substr(dayPos,2);
 }else{
  var dt = new Date();
  currentMonth = dt.getMonth();
  currentYear = dt.getFullYear();
  tmpDay = dt.getDate();
 }

 inputYear = currentYear;
 inputMonth = currentMonth;
 inputDay = tmpDay/1;

 if(!calD) initCalendar();
 else{
  if(calD.style.display=='block'){
   closeCalendar();
   return false;
  }
  writeCalendarContent();
 }
 returnFormat = format;
 returnDateTo = inputField;
 positionCalendar(buttonObj);
 calD.style.visibility = 'visible';
 calD.style.display = 'block';
 if(iframeObj){
  iframeObj.style.display = '';
  iframeObj.style.height = '140px';
  iframeObj.style.width = '195px';
 }
 updateYearDiv();
 updateMonthDiv();
}

function displayCalendarSelectBox(yearInput,monthInput,dayInput,buttonObj) {
 currentMonth = monthInput.options[monthInput.selectedIndex].value/1-1;
 currentYear = yearInput.options[yearInput.selectedIndex].value;

 inputYear = yearInput.options[yearInput.selectedIndex].value;
 inputMonth = monthInput.options[monthInput.selectedIndex].value/1-1;
 inputDay = dayInput.options[dayInput.selectedIndex].value/1;

 if(!calD) initCalendar();
 else writeCalendarContent();

 returnDateToYear = yearInput;
 returnDateToMonth = monthInput;
 returnDateToDay = dayInput;
 returnFormat = false;
 returnDateTo = false;
 positionCalendar(buttonObj);
 calD.style.visibility = 'visible';
 calD.style.display = 'block';
 if(iframeObj){
  iframeObj.style.display = '';
  iframeObj.style.height = calD.offsetHeight + 'px';
  iframeObj.style.width = calD.offsetWidth + 'px'; 
 }
 updateYearDiv();
 updateMonthDiv();
}

