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');
  document.documentElement.onclick = null;
}

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.getElementById('calendarPopup');
  if (!div) {
    div = document.createElement('div');
    div.setAttribute('id', "calendarPopup");
    var CalImg = document.getElementById('CalendarImage');
    //var posX = CalImg.offsetLeft+CalImg.clientWidth;
    //var posY = CalImg.offsetTop;
    //div.style.left = posX+'px';
    //div.style.top = posY+'px';
    CalImg.parentNode.appendChild(div);
  }
  
  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.documentElement.onclick = function(e) {
    if (clickedOutsideElement('CalendarImage', e) && clickedOutsideElement('calendarPopup', e)) {
      calendarClose(0);
    } 
  }
  
  //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';
}

function calcDay(frm,nm,nm2, nm3, day){
  curr_year = document.forms[frm].elements[nm3].options[document.forms[frm].elements[nm3].selectedIndex].value;
  var cl,cForm,cMonthYearField,cDayField,cDateField;
  var mDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
  var days =  ["Sun","Mon","Tue","Wed","Thu","Fri","Sat",];
  var days2 = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
  var d = new Date();
  dateVal = document.forms[frm].elements[nm].options[document.forms[frm].elements[nm].selectedIndex].value;
  monthVal = curr_year+document.forms[frm].elements[nm2].options[document.forms[frm].elements[nm2].selectedIndex].value;
  var isLeapYear = (((monthVal.substr(0,4) % 4 == 0) && (monthVal.substr(0,4) % 100 != 0)) || (monthVal.substr(0,4) % 400 == 0));
  mDays[1] = (isLeapYear)?29:28;
  if(dateVal > mDays[monthVal.substr(4,2)-1]){
    setDDValue(document.forms[frm].elements[nm],mDays[monthVal.substr(4,2)-1]);
    dateVal = document.forms[frm].elements[nm].options[document.forms[frm].elements[nm].selectedIndex].value;
  }
  newDate = new Date(monthVal.substr(0,4)+"/"+monthVal.substr(4,2)+"/"+dateVal);
}

function setDDValue(elem, val) {
  num = elem.options.length;
  for(i=0; i<num; i++) {
    if(elem.options[i].value == val) {
      elem.options.selectedIndex = i;
    }
  }
}


function clickedOutsideElement(elemId, evt) {
  var theElem = '';
  if (window.event) {
    theElem = getEventTarget(window.event);
  }  else {
    theElem = getEventTarget(evt);
  }
  while(theElem != null) {
    if (theElem.id == elemId) {
      return false;
    }
    theElem = theElem.offsetParent;
  }
  return true;
}

function getEventTarget(evt) {
  var targ = (evt.target) ? evt.target : evt.srcElement;
  if(targ != null) {
    if (targ.nodeType == 3) {
      targ = targ.parentNode;
    }
  }
  return targ;
}

var bo_ns_id = 0;

function startIeFix(){
  if(isIE()){
    document.write('<div id="bo_ns_id_' + bo_ns_id + '">
