function calendarClose(id) {
  calendarResult=id;

  if (id)
  {
    date = id.split('-');
    day = document.theForm.selDay;
    day.selectedIndex = date[2]-1;
    month = document.theForm.selMonth;
    month.selectedIndex = date[1]-1;
    year = document.theForm.selYear;
    for( i = 0; i < year.options.length; i++ ) {
      if(year.options[i].value == date[0]) {
        year.options[i].selected = true;
      }
    }
  }

  calendarRemove();
  calcDay('theForm','selDay','selMonth', 'selYear', 'StartDay');
}

function calendarRemove() {
  var div=document.getElementById('calendarPopup');

  if (div)
  { 
    div.style.display = 'none';
  }
  targetId = '';
}

var calendarResult='';
var Classes=new Array();

/* this is used so the calendar can populate an input box */
var targetId='';

/*
 * Style Info
 * 
 * calendar: class for the table
 * calendarYearTitle: class for the Year
 * calendarWrongYearTitle: class for the prev and next Year
 * calendarMonthTitle: class for the month
 * calendarWrongMonthTitle: class for the prev and next month
 * calendarWeekdayHeading: class for the day initials which are weekdays
 * calendarWeekendHeading: class for the day initials which are weekends
 * calendarWeekday: weekdays in the selected month
 * calendarWeekend: weekends in the selected month
 * calendarWrongMonthWeekday: weekdays not in the selected month
 * calendarWrongMonthWeekend: weekends not in the selected month
 * calendarHighlight: class for mouseover
 */

function calendar() {
  if(document.getElementById('advanced')) {
    if(optShown) {
      showHideAdditonal();
    }
  }
  date = document.theForm.selYear.options[document.theForm.selYear.selectedIndex].value+'-'+(document.theForm.selMonth.selectedIndex+1)+'-'+(document.theForm.selDay.selectedIndex+1);
  inputBoxId = 'CalendarDate';
  // someones clicked a second time on the spawning link
  var quickRunAway = (targetId == inputBoxId);

  // calendarRemove resets the targetId, so can't be called before the check
  calendarRemove();

  if (quickRunAway) {
    return;
  }

  targetId = inputBoxId;

  //var div=document.createElement('div');
  var div=document.getElementById('calendarPopup');
  var textBox = document.getElementById(targetId);

  //div.id='calendarPopup';

  /* need to handle differently for ff and opera & ie*/
  if(navigator.userAgent.indexOf("Firefox") != -1) {
    div.style.position='absolute';
  } else {
    div.style.position='absolute';
  }

  div.style.zIndex=2100;

  //document.getElementById('container_'+targetId).appendChild(div);
  //document.body.appendChild(div);
  calendarPopulate(date);
  shimmyShim(div);
}

function calendarPopulate(date) {
  var dayNames=new Array('Mo','Tu','We','Th','Fr','Sa','Su');
  var monthNames=new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
                           'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  var id;
  var styleClass='';
  var table='';
  var events='onClick="calendarClose(this.id);" onMouseOver="this.className=\'calendarHighlight\';" onMouseOut="this.className=Classes[this.id];"';

  var dateArray = date.split('-');

  today = new Date();

  blah=new Date();
  blah.setYear(dateArray[0]);
  blah.setDate(1);
  blah.setMonth(dateArray[1]-1);

  /* set up the prev and next month links */
  zarg = new Date();
  zarg.setYear(blah.getFullYear());
  zarg.setDate(1);
  zarg.setMonth(blah.getMonth()-1);

  if(zarg.getMonth()+1 == today.getMonth()) {
    zarg.setYear(today.getFullYear()+1);
  }

  prevMonth = "&laquo;&nbsp;<a href='javascript:calendarPopulate(\""+zarg.getFullYear()+"-"+(zarg.getMonth()+1)+"-"+zarg.getDate()+"\")'>"+monthNames[zarg.getMonth()]+"</a>";

  zarg.setYear(blah.getFullYear());
  zarg.setDate(1);
  zarg.setMonth(blah.getMonth()+1);

  if(zarg.getMonth() == today.getMonth()) {
    zarg.setYear(today.getFullYear());
  }

  nextMonth = "<a href='javascript:calendarPopulate(\""+zarg.getFullYear()+"-"+(zarg.getMonth()+1)+"-"+zarg.getDate()+"\")'>"+monthNames[zarg.getMonth()]+"</a>&nbsp;&raquo;";


  /* links for next and prev year */
  nextYear = "<a href='javascript:calendarPopulate(\""+(blah.getFullYear()+1)+"-"+(blah.getMonth()+1)+"-01\")'>&laquo;"+(blah.getFullYear()+1)+"</a>";
  prevYear = "<a href='javascript:calendarPopulate(\""+(blah.getFullYear()-1)+"-"+(blah.getMonth()+1)+"-01\")'>"+(blah.getFullYear()-1)+"&raquo;</a>";


  table+='<table class=calendar>\n';
  //table+='<tr>';
  //table+='<td class=calendarWrongYearTitle style="text-align:left" colspan=2>'+prevYear+'</td>';
  //table+='<td class=calendarYearTitle style="text-align:center" colspan=3>'+blah.getFullYear()+'</td>';
  //table+='<td class=calendarWrongYearTitle style="text-align:right" colspan=2>'+nextYear+'</td>';
  //table+='</tr>\n';
  table+='<tr><td colspan=7 class=calendarMonthTitle align=center>'
  table+=prevMonth+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  table+=monthNames[blah.getMonth()]+'&nbsp;'+blah.getFullYear()+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  table+=nextMonth+'</td>';
  table+='</tr>\n';
  table+='<tr>';
  for(i=0;i<5;i++) table+='<td class=calendarWeekdayHeading align=center>'+dayNames[i]+'</td>';
  for(;i<7;i++) table+='<td class=calendarWeekendHeading align=center>'+dayNames[i]+'</td>';


  table+='</tr>\n';
  table+='<tr>';

  // we want to start on a Monday

  if (blah.getDay()!=1)
  {
    var i=-(blah.getDay()+5)%7;

    for(;i<1;i++) 
    {
      workingDate = new Date(blah);
      workingDate.setDate(i);

      if ((month=(workingDate.getMonth()+1))<10) 
      {
        month='0'+month;
      }

      if ((day=(workingDate.getDate()))<10)
      {
        day='0'+day;
      }

      id=workingDate.getFullYear()+'-'+month+'-'+day;

      if(today.getDate() == workingDate.getDate() && today.getMonth() == workingDate.getMonth()
         && today.getFullYear() == workingDate.getFullYear()) 
      {
        styleClass='calendarWrongToday';
      }
      else if ((workingDate.getDay==0)||(workingDate.getDay()==6)) 
      {
        styleClass='calendarWrongMonthWeekend';
      }
      else 
      {
        styleClass='calendarWrongMonthWeekday';
      }

      table+='<td id="'+id+'" '+events+' class='+styleClass+' align=center>';
      Classes[id]=styleClass;
      table+=workingDate.getDate();
      table+='</td>\n';
    }
  }
  else
  {
    var i=1;
  }

  for(;;i++) 
  {
    workingDate=new Date(blah);
    workingDate.setDate(i);
    if((month=(workingDate.getMonth()+1))<10) month='0'+month;
    if((day=(workingDate.getDate()))<10) day='0'+day;
    id=workingDate.getFullYear()+'-'+month+'-'+day;

    // keep going until it's the next month
    if (workingDate.getMonth()!=blah.getMonth()) 
    {
      break;
    }

    if (workingDate.getDay()==1) {
      table+='</tr>\n<tr>';
    }

    if(today.getDate() == workingDate.getDate() && today.getMonth() == workingDate.getMonth()
       && today.getFullYear() == workingDate.getFullYear()) 
    {
      styleClass='calendarToday';
    }
    else if ((workingDate.getDay()==0)||(workingDate.getDay()==6)) 
    {
      styleClass='calendarWeekend';
    }
    else 
    {
      styleClass='calendarWeekday';
    }

    table+='<td id="'+id+'" '+events+' class='+styleClass+' align=center>';
    Classes[id]=styleClass;
    table+=workingDate.getDate();
    table+='</td>\n';
  }
  if (workingDate.getDay()!=1) 
  {
    for(;;i++) {
      workingDate=new Date(blah);
      workingDate.setDate(i);
      if((month=(workingDate.getMonth()+1))<10) month='0'+month;
      if((day=(workingDate.getDate()))<10) day='0'+day;
      id=workingDate.getFullYear()+'-'+month+'-'+day;
      if(today.getDate() == workingDate.getDate() && today.getMonth() == workingDate.getMonth()
         && today.getFullYear() == workingDate.getFullYear()) 
      {
        styleClass='calendarWrongToday';
      }
      else if ((workingDate.getDay()==0)||(workingDate.getDay()==6))
			{
				 styleClass='calendarWrongMonthWeekend';
			}
      else
			{
				 styleClass='calendarWrongMonthWeekday';
			}
      table+='<td id="'+id+'" '+events+' class='+styleClass+' align=center>';
      Classes[id]=styleClass;
      table+=workingDate.getDate();
      table+='</td>\n';
      if (workingDate.getDay()==0) break;
    }
  }

  table+='</tr>\n'

  table+='<tr><td colspan=7 align=center><a href="javascript:calendarClose(0)">Close</a></td></tr>';
  table+='</table>';

  var div = document.getElementById('calendarPopup');
  div.innerHTML=table;
  div.style.display = 'block';
}
