/*--  CONFIGURE THE CALENDAR HERE ------------------------------------------------------------------------------*/

var yearBefore = 0;               // Number of years before the current year the YEAR drop down list should contain
var yearAfter = 10 ;               // Number of years after the current year the YEAR drop down list should contain
var rcDateFormat = "mm.dd.yyyy"   // Format of returned date. (yyyy returns 4 character year, yy returns 2 characher year)
var rcHighlightToday = 1;         // Highlight today's date.  (0 = no, 1 = yes)
var showNavigation = 1;           // Show navigation title.
                                  // 0 = no title
                                  // 1 = show drop down lists allowing user to change month and year
                                  // 2 = show selected month and year but cannot change to another

// Define custom day and month names
rcDayArray = new Array('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa');
rcMonthArray = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')

/*----------------------------------------------------------------------------------------------------------------
------ DO NOT EDIT BELOW THIS LINE -------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------*/

function Leap(Year){
  // Determines if the passed year is a leap year or not
  Result =  ( (Year % 4) == 0) ? ( (Year % 100) == 0) ? ( (Year % 400) == 0) : Result = 1 : 0;
 	return (Result);
}

// Adding options to select boxes is done differently in different browsers
function addOption(selectElement,newOption) {
  try {selectElement.add(newOption,null);} // First try the DOM2 method ...
  catch (e) {selectElement.add(newOption,selectElement.length);} // ... And if that doesn't work use the IE-only method
}
 
function buildCal(outerDiv, rcDateField, rcYear, rcMonth) {
  if(!document.getElementById(outerDiv)){alert('ERROR:\nYou have specified a DIV tag with an id of "' + outerDiv + '" but this does not exist on the page');}                  
  
  // Declare various dates and days in each month.
  var monthDays = new Array(31, (Leap(rcYear)) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  document.getElementById(outerDiv).innerHTML = "";
  rcMonth = (parseInt(rcMonth) > -1 && rcMonth != null) ? rcMonth : new Date().getMonth(); 
  rcYear = (rcYear) ? rcYear : new Date().getYear();
  rcToday = new Date();
  rcTodayYear = rcToday.getYear();
  if (rcYear < 1000) {rcYear += 1900}
  if (rcTodayYear < 1000) {rcTodayYear += 1900}  
  rcRawDate = new Date(rcYear,rcMonth,rcToday.getDate())    
  rcDate = rcRawDate.getDate();
  rcFirstDay = new Date(rcYear,rcMonth,1).getDay();


  // Create Outer Table
  outer = document.createElement("table");
  tbBody = document.createElement("tbody");  
  outer.className = 'rcCalendar'
  
  // Create Title Row
  ttRow = document.createElement("tr");
  ttCell = document.createElement("td");  
  ttCell.setAttribute('colSpan', 7)
  
  // Create Month Select Box
  if(showNavigation == 1){
    ttMonthSelect = document.createElement('select');
    for(m=0; m<12; m++){
      ttMonthOption = document.createElement('option');
      ttMonthOption.text = rcMonthArray[m];
      ttMonthOption.value = m;
      addOption(ttMonthSelect, ttMonthOption)
    }  
    ttMonthSelect.options[rcMonth].selected = 1
    
    ttMonthSelect.onchange = function(){
      buildCal(outerDiv, rcDateField, rcYear, this.options[this.selectedIndex].value);
    }  
     
    // Create Year Select Box
    ttYearSelect = document.createElement('select');
    for (j=0; j<yearBefore + 1; j++){
      ttYearOption = document.createElement('option');
      ttYearOption.value = (rcTodayYear-yearBefore) + (j); 
      ttYearOption.text = (rcTodayYear-yearBefore) + (j);
      addOption(ttYearSelect, ttYearOption)
    }
    for (j=0; j<yearAfter; j++){
      ttYearOption = document.createElement('option');
      ttYearOption.value = (rcTodayYear + (j+1)); 
      ttYearOption.text = (rcTodayYear + (j+1));   
      addOption(ttYearSelect, ttYearOption)
    } 
    for(j=0; j<ttYearSelect.options.length; j++){
      ttYearSelect.options[j].selected = (ttYearSelect.options[j].value == rcYear) ? 1 : 0;
    } 
  
    ttYearSelect.onchange = function(){
      buildCal(outerDiv, rcDateField, this.options[this.selectedIndex].value, rcMonth);
    }     
  }
  
  // Compile Title Row  
  ttCell.className = 'rcCalTitle';
  if(showNavigation == 1){
    ttMonthSelect.className = 'rcCalMonth';
    ttYearSelect.className = 'rcCalYear';
    ttCell.appendChild(ttMonthSelect);
    ttCell.appendChild(ttYearSelect);
  } else if(showNavigation == 2){
    ttCell.appendChild(document.createTextNode(rcMonthArray[rcMonth] + " " + rcYear));
  }
  
  if (showNavigation > 0){
    ttRow.appendChild(ttCell);
    tbBody.appendChild(ttRow);
  }
  
  // Create day Names
  tdRow = document.createElement('tr');
  for (n=0; n<7; n++){
    tdCell = document.createElement('td');
    tdDay = document.createTextNode(rcDayArray[n]);
    tdCell.appendChild(tdDay);
    tdCell.className = (n == 0 || n == 6)?'rcCalWeekendDay':'rcCalWeekDay';
    tdRow.appendChild(tdCell)
  }
  tbBody.appendChild(tdRow)

  // Create and format Date cells
  for (m=0; m<Math.ceil((monthDays[rcMonth] + rcFirstDay) / 7); m++){    
    dRow = document.createElement('tr');
    for(i=0; i<7; i++){
      dCell = document.createElement('td');
      if((i < rcFirstDay && m < 1) || i + 1 + (m * 7) - rcFirstDay > monthDays[rcMonth]){dDate = document.createTextNode(' ');      
      } else {
        dDate = document.createTextNode(i + 1 + (m * 7) - rcFirstDay);
      }
      if(rcDateField){        
        dLink = document.createElement('a')
        dLink.setAttribute('href', 'javascript:void(0)');
        if(rcHighlightToday && rcYear == rcTodayYear && rcMonth == rcToday.getMonth() && (i + 1 + (m * 7)) == rcDate) {dLink.className = 'rcCalTodayLink';}
        else { dLink.className = (i == 0 || i == 6)?'rcCalWeekendLink':'rcCalWeekLink'; }         
        dLink.appendChild(dDate)        
        dCell.appendChild(dLink)                
        dLink.onclick = function(){
          if(rcDateField && !document.getElementById(rcDateField)){alert('ERROR:\nYou have specified a text field with an id of "' + rcDateField + '" but this does not exist on the page');}                   
          rcDisplayDate = rcDateFormat.replace('mm', ((parseInt(rcMonth) + 1) < 10) ? '0' + (parseInt(rcMonth) + 1) : parseInt(rcMonth) + 1)
          rcDisplayDate = rcDisplayDate.replace('dd', (this.childNodes[0].nodeValue < 10) ? '0' + this.childNodes[0].nodeValue : this.childNodes[0].nodeValue)
          rcDisplayDate = rcDisplayDate.replace('yyyy', rcYear)
          rcDisplayDate = rcDisplayDate.replace('yy', rcYear.toString().substring(2))
          document.getElementById(rcDateField).value = rcDisplayDate;
        }  
      } else {
        dCell.appendChild(dDate)      
      }        
      if(rcHighlightToday && rcYear == rcTodayYear && rcMonth == rcToday.getMonth() && (i + 1 + (m * 7)) == rcDate) {dCell.className = 'rcCalToday';}
      else { dCell.className = (i == 0 || i == 6)?'rcCalWeekendDate':'rcCalWeekDate'; }          
      dRow.appendChild(dCell)
    }
    tbBody.appendChild(dRow)
  }

  // Add all to page. 
  outer.appendChild(tbBody);
  document.getElementById(outerDiv).appendChild(outer); 
}