// Unpublished Work. ©Financial Industry Regulatory Authority, Inc.

var currentControl;
document.onkeydown = keyDownHappened;
document.onmousedown = mouseDownHappened;
/*
  called by focus of textbox; sets x,y position of calendar and establishes the
  'currentcontrol' (i.e. the control that will get the selected date value.
*/
function getCalendar(dateControl,y,x)
{
 clearDateControl(dateControl);
 var month = getTheMonth(dateControl.value);
 var day = getTheDay(dateControl.value);
 var year = getTheYear(dateControl.value);
 
 if (isNaN(new Date(year,month,day)) || (month < 0) || (month > 11) || (day < 1) || (day > 31) || (year < 1))
 {
    //dateControl.value = "";
    month = getTheMonth("");
    day = getTheDay("");
    year = getTheYear("");
 }
 
 drawCalendar(year,month,day);
 currentControl = dateControl;
 
 var calendar = document.getElementById('calendar'); 
 calendar.style.position='absolute';
 calendar.style.width='172px';
 calendar.style.height='166px';
 calendar.style.visibility='visible';
 var p = position(dateControl);
 calendar.style.top=parseInt(p.y + dateControl.clientHeight + 4) + "px";
 calendar.style.left=parseInt(p.x) + "px";
}
function position(element) {
        var p = {x: element.offsetLeft || 0, y:element.offsetTop || 0};
        while (element = element.offsetParent) {
            p.x += element.offsetLeft;
            p.y += element.offsetTop;
        }
        return p;
    }
function clearDateControl(dateControl)
{
  if (dateControl.value == 'mm/dd/yyyy') { dateControl.value=''; }
}

function getScrollTop()
{
	if(document.documentElement.scrollTop) return document.documentElement.scrollTop;
	if(document.body.scrollTop) return document.body.scrollTop;
	if(window.pageYOffset) return window.pageYOffset;
	return 0;
}

/*
  the logic for drawing the calendar; relies on a DIV with an id of 'calendar'
*/
function drawCalendar(year,month,day)
{
 // adjust month/year appropriately if out of bounds
 if (month<0) { month=11;year--;}
 if (month>11) { month=0;year++;}

 //get the current date (either today or date in control)
 var today = new Date(year,month,day);

 //get the first day of the month of 'today'
 var firstDay = new Date(year,month,1);

 //get the last day of the month of 'today'; simpler than determining leap years, etc., get the first
 //day of the next month, and then subtract a day.  Take into account that the next month might be in
 //the next year (i.e. 'today' is in December)
 var nextMonth = month+1;
 var nextMonthYear = year;
 if (nextMonth==12){nextMonth=0;nextMonthYear++;}
 var lastDay = new Date(nextMonthYear,nextMonth,1);
 var temp = lastDay - (1000*60*60*24);
 lastDay.setTime(temp);
 
 //prepare to draw calendar
 var calendarMarkup;
 var lastNumber = firstDay.getDay()+lastDay.getDate()+1; 
 var index = lastNumber + Math.round(lastNumber - (Math.floor(lastNumber/7)*7));

 calendarMarkup = "<p class='display'>" + getMonthName(month) + " " + year + "</p>";
 calendarMarkup += "<p><span class='previous'><a href='javascript:drawCalendar(" + year + "," + (month-1) + "," + day + ");'>&lt;</a></span>";
 calendarMarkup += "<span class='next'><a href='javascript:drawCalendar("  + year + "," + (month+1) + "," + day + ");'>&gt;</a></span></p>";
 //calendarMarkup += "<ul><li class='day'>&nbsp;S</li><li class='day'>&nbsp;M</li><li class='day'>&nbsp;T</li><li class='day'>&nbsp;W</li><li class='day'>&nbsp;T</li><li class='day'>&nbsp;F</li><li class='day'>&nbsp;S</li><br/>";
 calendarMarkup += "<Table align='center' width='90%' cellspacing='0' cellspacing='0' border='0'><tr><td class='day'>&nbsp;S</td><td class='day'>&nbsp;M</td><td class='day'>&nbsp;T</td><td class='day'>W</td><td class='day'>&nbsp;T</td><td class='day'>&nbsp;F</td><td class='day'>&nbsp;S</td></tr><tr>";

 //arrange the dates of the month appropriately by skipping non-month days
 for (var i=1;i<index;i++)
 { 
   if (i<=firstDay.getDay() || i>=lastNumber) {calendarMarkup += "<td class='none'>&nbsp;&nbsp;</td>";}
   else 
   { 
     var num =  i-firstDay.getDay();
     if (num<10) { num = "&nbsp;" + num; }

     calendarMarkup += "<td class='simple'><a href='javascript:setControlValue(" + year + "," + month + "," + num + ");'>" + num + "</a></td>";
   }
   //add a break after 7 days; the below does a mod
   if (Math.round(i - (Math.floor(i/7)*7)) == 0)
   {
     calendarMarkup += "<tr/><tr>";
   }
 }
 
 if(calendarMarkup.substr(calendarMarkup.length - 5) != '</tr>')
    calendarMarkup += "</tr>";
 calendarMarkup += "</table>";

 //set the markup to the innerHTML of the calendar DIV
 document.getElementById("calendar").innerHTML = calendarMarkup;
}

function getMonthName(month)
{
  //var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var monthNames = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
  return monthNames[month];
}

/*
  set the value of the control to the date selected on the calendar. 
  note that month is zero based.
*/
function setControlValue(year,month,day)
{
   currentControl.value = (month+1) + "/" + day + "/" + year;
//   currentControl.focus();
   closeCalendar();
}
function closeCalendar()
{
   document.getElementById('calendar').style.visibility='hidden';
}

/* gets the month from the date string, or the current month */
function getTheMonth(date)
{
  if (date=="") { return (new Date().getMonth()); }
  else { return (date.substr(0,date.indexOf("/"))-1); }
}

/* gets the day from the date string, or the current day */
function getTheDay(date)
{
  if (date=="") { return (new Date().getDate()); }
  else 
  { 
    var index1 = date.indexOf("/");
    var index2 = date.lastIndexOf("/");
    return (date.substr(index1+1,index2-index1-1)); 
  }
}

/* gets the year from the date string, or the current year */
function getTheYear(date)
{
  if (date=="") { return (new Date().getFullYear()); }
  else { return (date.substr(date.lastIndexOf("/")+1,4)); }
}

/*
   if a mousedown occurs, check to see if the event occurred on the calendar control.
   if it did, suppress the closing of the calendar.
*/
function mouseDownHappened(e)
{
   var doClose = true;
   var cntrl;
   if (e==null) //IE
   {
     cntrl = window.event.srcElement;
   }
   else //Firefox
   { 
     cntrl = e.target;
   }
   while (cntrl!=null)
   {
     if (cntrl.id=="calendar") {doClose=false;} //clicked on the calendar control
     cntrl = cntrl.parentNode;
   }
   if (doClose) {closeCalendar();}
}

function keyDownHappened(e)
{
   closeCalendar();
}

