//  JavaScript to Pull Full Date Format mm/dd/yyyy
function displayFullDateCalendar(targetObject, selectedDate) {

  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  
  var currentDateValue = (selectedDate == null || selectedDate =="" ?  new Date() : formatExistingDateValue(selectedDate));
  
  //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=\"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.displayFullDateCalendar('"+targetObject+"', '"+formatDateValue(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.displayFullDateCalendar('"+targetObject+"', '"+formatDateValue(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>";
      }
      
      // Have to split target object to insert &quot; since passing " in the strings causes issues.
      
      var splitObject = targetObject.split('elements[');  
      var splitObject2 = splitObject[1].split(']');
      
      formattedtargetObject = splitObject[0] + "elements[&quot;" + splitObject2[0] + "&quot;]";
              
      if (currentDay.getMonth() == currentDateValue.getMonth()) {
        // print days of current month
        generateHTML += "<a href=\"javascript:window.opener."+formattedtargetObject+".value='"+formatDateValue(currentDay)+"'; window.close();\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      } else {
        // print days of other months
        generateHTML += "<a href=\"javascript:window.opener."+formattedtargetObject+".value='"+formatDateValue(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 (selectedDate) {
  var formatString = /^(\d+)\/(\d+)\/(\d+)$/;
  if (!formatString.exec(selectedDate)) {
    return alert("Invalid Date Format: "+ selectedDate);
  }
  return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2, RegExp.$4, RegExp.$5, RegExp.$6));
  //RegExp 4, 5, 6 are empty
}

function formatDateValue (currentDateValue) {
  formatMonth = currentDateValue.getMonth()+1;
  formatDay = currentDateValue.getDate();
  formatYear = currentDateValue.getFullYear();

  // Adds 0 to single digit months & days to follow format mm/dd/yyyy
  if (formatMonth < 10) {
    formatMonth = "0" + formatMonth;
  }
  if (formatDay < 10) {
      formatDay = "0" + formatDay;
  }
  
  return (new String (formatMonth +"/"+ formatDay +"/"+ formatYear));
}

//  End of JavaScript to Pull Full Date Format mm/dd/yyyy


//  JavaScript to Pull Only Month Date Format mm/yyyy
function displayMonthOnlyCalendar(targetObject, selectedDate) {

  var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var weekDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  
  var currentDateValue = (selectedDate == null || selectedDate =="" ?  new Date() : formatExistingMonthDateValue(selectedDate));
  
  var previousMonth = new Date(currentDateValue);
  previousMonth.setMonth(currentDateValue.getMonth()-1);
  
  var nextMonth = new Date(currentDateValue);
  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=\"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.displayMonthOnlyCalendar('"+ targetObject+"', '"+ formatMonthDateValue(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.displayMonthOnlyCalendar('"+targetObject+"', '"+formatMonthDateValue(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.getDay() == 0 || currentDay.getDay() == 6) {      
        // weekend days
        generateHTML += "  <td class=\"weekend\">";
      } else {
        // print working days of current month
        generateHTML += "  <td>";
      }
      
       // Have to split target object to insert &quot; since passing " in the strings causes issues.
            
      var splitObject = targetObject.split('elements[');  
      var splitObject2 = splitObject[1].split(']');
            
      formattedtargetObject = splitObject[0] + "elements[&quot;" + splitObject2[0] + "&quot;]";
        
      if (currentDay.getMonth() == currentDateValue.getMonth()) {
        // print days of current month
        generateHTML += "<a href=\"javascript:window.opener."+formattedtargetObject+".value='"+formatMonthDateValue(currentDay)+"'; window.close();\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
      } else {
        // print days of other months
        generateHTML += "<a href=\"javascript:window.opener."+formattedtargetObject+".value='"+formatMonthDateValue(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 formatExistingMonthDateValue (selectedDate) {
  // Adds the day as 01 in order to select a day to use the same calendar as the full date one.  Will not transfer the day when adding to textbox.
  var addDay = selectedDate.split('/'); 
  
  // If format mm/dd/yyyy was already incorrectly used (should use mm/yyyy) in the text box, this will grab the correct year (instead of grabbing the day as the year).  
  if (addDay[2] == null) {  
    selectedDate = addDay[0] + "/01/" + addDay[1];
  } else {
    selectedDate = addDay[0] + "/01/" + addDay[2];
  }
  
  var formatString = /^(\d+)\/(\d+)\/(\d+)$/;
  if (!formatString.exec(selectedDate)) {
    return alert("Invalid Date Format: "+ selectedDate);
  }
  return (new Date (RegExp.$3, RegExp.$1-1, RegExp.$2, RegExp.$4, RegExp.$5, RegExp.$6));
  //RegExp 4, 5, 6 are empty
}

function formatMonthDateValue (currentDateValue) {
  formatMonth = currentDateValue.getMonth()+1;
  formatYear = currentDateValue.getFullYear();

  // Adds 0 to single digit months to follow format mm/yyyy
  if (formatMonth < 10) {
    formatMonth = "0" + formatMonth;
  }
  
  return (new String (formatMonth +"/"+ formatYear));
}

//  End of JavaScript to Pull Only Month Date Format mm/yyyy