//  JavaScript to Pull Full Date Format Dropdown
function displayFullDateCalendarDropDown(targetObject, selectedMonth, selectedDay, selectedYear) {

  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var existingDate = selectedMonth + selectedDay + selectedYear;
  var existingDate2 = selectedMonth + "/" + selectedDay + "/" + selectedYear;
  
  var currentDateValue = (existingDate == null || existingDate =="" ?  new Date() : formatExistingDateValue(existingDate2));
  
  //Before generating previous and next month links, first check to see if its a leap year  
  var grabYear = currentDateValue.getFullYear();
  var isLeapYear = new Boolean();
  
  if(grabYear%4 == 0) {
    if(grabYear%100 != 0) {
      isLeapYear = true;
    } else {
      if(grabYear%400 == 0) {
        isLeapYear = true;
      } else {
        isLeapYear = false;
      }
    }
  }
  
  var previousMonth = new Date(currentDateValue);
  //Check the following conditions first:   
  if ((previousMonth.getDate() == 29 || previousMonth.getDate() == 30 || previousMonth.getDate() == 31) && previousMonth.getMonth() == 2) {
  // If March 29, 30 or 31 is currently selected
    if (isLeapYear == true) {
      //set previous month link as February 29 if its a leap year
      previousMonth.setDate(29);  
      previousMonth.setMonth(currentDateValue.getMonth()-1);
    } else {
      //set previous month link as February 28 if its a leap year
      previousMonth.setDate(28);
      previousMonth.setMonth(currentDateValue.getMonth()-1);
    }  
  }  else if (previousMonth.getDate() == 31 && (previousMonth.getMonth() == 2 || previousMonth.getMonth() == 4 || previousMonth.getMonth() == 6 || previousMonth.getMonth() == 9 || previousMonth.getMonth() == 11)) {
    // All other months, if current selected date is 31 and the previous month does not have a day 31, set previous month day as 30
    previousMonth.setDate(currentDateValue.getDate()-1);
    previousMonth.setMonth(currentDateValue.getMonth()-1);
  } else {
    // Any other condition, use current day for previous month
    previousMonth.setMonth(currentDateValue.getMonth()-1);
  }
  
  var nextMonth = new Date(currentDateValue);
  //Check the following conditions first:   
  if ((nextMonth.getDate() == 29 || nextMonth.getDate() == 30 || nextMonth.getDate() == 31) && nextMonth.getMonth() == 0) {
  // If January 29, 30 or 31 is currently selected
    if (isLeapYear == true) {
      //set next month link as February 29 if its a leap year
      nextMonth.setDate(29);  
      nextMonth.setMonth(currentDateValue.getMonth()+1);
    } else {
      //set next month link as February 28 if its a leap year
      nextMonth.setDate(28);
      nextMonth.setMonth(currentDateValue.getMonth()+1);
    }  
  }  else if (nextMonth.getDate() == 31 && (nextMonth.getMonth() == 2 || nextMonth.getMonth() == 4 || nextMonth.getMonth() == 7 || nextMonth.getMonth() == 9)) {
    // All other months, if current selected date is 31 and the next month does not have a day 31, set next month day as 30
    nextMonth.setDate(currentDateValue.getDate()-1);
    nextMonth.setMonth(currentDateValue.getMonth()+1);
  } else {
    // Any other condition, use current day for next month
    nextMonth.setMonth(currentDateValue.getMonth()+1);
  }  
    
  
  var firstDay = new Date(currentDateValue);
  firstDay.setDate(1);
  firstDay.setDate(1-(7+firstDay.getDay())%7);
  
  var lastDay = new Date(nextMonth);
  lastDay.setDate(0);
  
// Begin HTML Generation and Title Row with Current Month and Links to Previous and Next Months
  var generateHTML = new String (
    "<html>\n"+
    "<head>\n"+
    "  <title>Calendar</title>\n"+
    "  <link href=\"css/calendar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"+
    "</head>\n"+
    "<body>\n"+
    "<table class=\"calendar-layout\" summary=\"\">\n"+
    "<tr class=\"header\">\n"+
    "  <td class=\"previous\">"+
    "<a href=\"javascript:window.opener.displayFullDateCalendarDropDown('"+targetObject+"', " +formatMonthValue(previousMonth)+", "+formatDayValue(previousMonth)+", "+formatYearValue(previousMonth)+");\">"+"&laquo;&nbsp;&nbsp;&nbsp;&nbsp;</a>"+
    "</td>\n"+
    "  <td class=\"month\" colspan=\"5\">"+
    months[currentDateValue.getMonth()]+" "+currentDateValue.getFullYear()+
    "</td>\n"+
    "  <td class=\"next\">"+
    "<a href=\"javascript:window.opener.displayFullDateCalendarDropDown('"+targetObject+"', " +formatMonthValue(nextMonth)+", "+formatDayValue(nextMonth)+", "+formatYearValue(nextMonth)+");\">"+"&nbsp;&nbsp;&nbsp;&nbsp;&raquo;</a>"+
    "</td>\n"+
    "</tr>\n"
  );
// End of HTML Generation and Title Row with Current Month and Links to Previous and Next Months  
 
  var currentDay = new Date(firstDay);

// Begin Weekday Names Section
  generateHTML += "<tr class=\"day-name\">\n";
  
  for (var n=0; n<7; n++) {
    generateHTML += "  <td>"+ weekDays[(n)%7]+"</td>\n";
  }

  generateHTML += "</tr>\n";
// End Weekday Names Section  

// Begin Generate Calendar Rows  
  while (currentDay.getMonth() == currentDateValue.getMonth() || currentDay.getMonth() == firstDay.getMonth()) {
    generateHTML += "<tr>\n";
    
    for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
  
      if (currentDay.getDate() == currentDateValue.getDate() && currentDay.getMonth() == currentDateValue.getMonth()) {
        // print current date
        generateHTML += "  <td class=\"current-day\">";
      } else if (currentDay.getDay() == 0 || currentDay.getDay() == 6) {      
        // weekend days
        generateHTML += "  <td class=\"weekend\">";
      } else {
        // print working days of current month
        generateHTML += "  <td>";
      }
        
      if (currentDay.getMonth() == currentDateValue.getMonth()) {
        // print days of current month
        generateHTML += "<a href=\"javascript:window.opener."+targetObject+".month.value='"+formatMonthValue(currentDay)+"'; window.opener."+targetObject+".day.value='"+formatDayValue(currentDay)+"'; window.opener."+targetObject+".year.value='"+formatYearValue(currentDay)+"'; window.close();\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      } else {
        // print days of other months
        generateHTML += "<a href=\"javascript:window.opener."+targetObject+".month.value='"+formatMonthValue(currentDay)+"'; window.opener."+targetObject+".day.value='"+formatDayValue(currentDay)+"'; window.opener."+targetObject+".year.value='"+formatYearValue(currentDay)+"'; window.close();\" class=\"other-month\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";          
      }
      
      generateHTML +=  currentDay.getDate()+"</a></td>\n";
      currentDay.setDate(currentDay.getDate()+1);      
    }
    
    generateHTML += "</tr>\n";
  }
// End of Generate Calendar Rows

// Begin Closing HTML Page
  generateHTML += "</table>\n</body>\n</html>\n";
// End of Closing HTML Page


// Begin Calendar Window Properties
  var calendarWindow = window.open("", "Calendar", "width=272,height=228,status=no,resizable=no,top=200,left=200");
    calendarWindow.opener = self;
  var calendarDocument = calendarWindow.document;
    calendarDocument.write (generateHTML);
    calendarDocument.close();
// End of Calendar Window Properties    
}

function formatExistingDateValue (existingDate2) {
  var formatString = /^(\d+)\/(\d+)\/(\d+)$/;
  if (!formatString.exec(existingDate2)) {
    return alert("Invalid Date Format: "+ existingDate2);
  }
  return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2, RegExp.$4, RegExp.$5, RegExp.$6));
  //RegExp 4, 5, 6 are empty
}

function formatMonthValue (currentDateValue) {
  formatMonth = currentDateValue.getMonth()+1;

  // Adds 0 to single digit months to follow format mm/dd/yyyy
  if (formatMonth < 10) {
    formatMonth = "0" + formatMonth;
  }

  return (new String (formatMonth));
}

function formatDayValue (currentDateValue) {
  formatDay = currentDateValue.getDate();

  // Adds 0 to single digit days to follow format mm/dd/yyyy

  if (formatDay < 10) {
      formatDay = "0" + formatDay;
  }
  
  return (new String (formatDay));
}

function formatYearValue (currentDateValue) {
  formatYear = currentDateValue.getFullYear(); 
  
  return (new String (formatYear));
}

//  End of JavaScript to Pull Full Date Format in Dropdown