﻿var DefaultDateFormat = 'MM/DD/YYYY';
var HideWait = 3; // Number of seconds before the calendar will disappear
var Y2kPivotPoint = 76; // 2-digit years before this point will be created in the 21st century
var FontSize = 11;
var FontFamily = 'Verdana';
var CellWidth = 25;
var CellHeight = 21;
var ImageURL = 'Search/images/calendar.gif';
var NextURL = '/images/Search/main/next.gif';
var PrevURL = '/images/Search/main/prev.gif';
var CalBGColor = '#ffffff';
var DayBGColor = '#ff9933';
var ZCounter = 100;
var Today = new Date();
var WeekDays = new Array('S','M','T','W','T','F','S');
var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var MonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

with (document) 
{
    writeln('<style>');
    writeln('td.calendarDateInput {letter-spacing:normal;line-height:normal;font-family:' + FontFamily + ',Sans-Serif;font-size:' + FontSize + 'px;}');
    writeln('select.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Sans-Serif;font-size:11px;}');
    writeln('input.calendarDateInput {letter-spacing:.06em;font-family:Verdana,Sans-Serif;font-size:11px;}');
    writeln('</style>');
}

function getLeftPos(obj)
{   
    var xPos = obj.offsetLeft; 
 
    var tempEl = obj.offsetParent;  
    while (tempEl != null) 
    {
          xPos += tempEl.offsetLeft; 
          tempEl = tempEl.offsetParent; 
    } 
    return xPos;
} 

function getTopPos(obj)
{
    var yPos = obj.offsetTop; 
    var tempEl = obj.offsetParent; 
    while (tempEl != null) 
    { 
          yPos += tempEl.offsetTop; 
          tempEl = tempEl.offsetParent; 
    } 
    return yPos;
}

// Mouse-over for the previous/next month buttons
function NeighborHover(Cell, Over) 
{
    if (Over) 
        VirtualButton(Cell, false);
    else 
        Cell.style.border = 'buttonface 1px solid';
    return true;
}

// Displays a message in the status bar when hovering over the calendar days
function DayCellHover(Cell, Over, Color) {
   Cell.style.backgroundColor = (Over) ? DayBGColor : Color;
}

// Returns the number of days in a month (handles leap-years)
function GetDayCount(SomeYear, SomeMonth) {
   return ((SomeMonth == 1) && ((SomeYear % 400 == 0) || ((SomeYear % 4 == 0) && (SomeYear % 100 != 0)))) ? 29 : MonthDays[SomeMonth];
}

// Sets the form elements after a day has been picked from the calendar
function PickDisplayDay(themonth, theday, theyear, basename, nextname) {
	eval('document.getElementById(\'' + fieldPrefix + basename + '_ConCat\').value =\''+(++themonth)+'/'+theday+'/'+theyear+'\'');
	HideCalendar(basename);
	var newMonth = 0;
	var newDay = 0;
	var newYear = 0;
	var dayCount1 = GetDayCount(theyear, themonth-1);
	if((theday+2) > dayCount1){
	    if((theday+1) > dayCount1){
	        if(themonth+1 > 12){
	          newMonth = 1;
	          newDay = 2;
	          newYear = theyear+1;
	        }else{
	          newMonth = themonth+1;
	          newDay = 2;
	          newYear = theyear;
	        }
	    }else{
	        if(themonth+1 > 12){
	          newMonth = 1;
	          newDay = 1;
	          newYear = theyear+1;
	        }else{
	          newMonth = themonth+1;
	          newDay = 1;
	          newYear = theyear;
	        }
	    }
	}else{
      newMonth = themonth;
      newDay = theday + 2;
      newYear = theyear;
	}
	eval('if(nextname == \'EndDate_ConCat\'){ document.getElementById(\'' + fieldPrefix + nextname + '\').value =\''+(newMonth)+'/'+newDay+'/'+newYear+'\'}');
	eval('if(document.getElementById(\'' + fieldPrefix + nextname + '\').type == \'text\'){ document.getElementById(\'' + fieldPrefix + nextname + '\').select(); document.getElementById(\'' + fieldPrefix + nextname + '\').focus()}');
		
}

// Is the specified select-list behind the calendar?
function BehindCal(SelectList, CalLeftX, CalRightX, CalTopY, CalBottomY, ListTopY) {
   var ListLeftX = GetTagPixels(SelectList, 'LEFT');
   var ListRightX = ListLeftX + SelectList.offsetWidth;
   var ListBottomY = ListTopY + SelectList.offsetHeight;
   return (((ListTopY < CalBottomY) && (ListBottomY > CalTopY)) && ((ListLeftX < CalRightX) && (ListRightX > CalLeftX)));
}

// Gets the absolute pixel position of the supplied element
function GetTagPixels(StartTag, Direction) {
   var PixelAmt = (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   while ((StartTag.tagName != 'BODY') && (StartTag.tagName != 'HTML')) {
      StartTag = StartTag.offsetParent;
      PixelAmt += (Direction == 'LEFT') ? StartTag.offsetLeft : StartTag.offsetTop;
   }
   return PixelAmt;
}

// For IE, hides any select-lists that are behind the calendar
function FixSelectLists(Over, basename) {
    if (navigator.appName == 'Microsoft Internet Explorer') 
    {
        if (Over)
        {
            hideAllSelects();
            hideFlash();
        }
        else
        {
            showAllSelects();
            showFlash();
        }
    }
    else
    {
        if (Over)
            hideFlash();
        else
            showFlash();
    }
}

// Builds the HTML for the calendar days
function BuildCalendarDays(themonth, theday, theyear, basename, displaymonth, displayyear, nextname) 
{
	var nextmonth = displaymonth;
	var nextyear = displayyear;
	if (nextmonth == 11)
	{
		nextmonth = 0;
		nextyear++;
	}
	else
		nextmonth++;

	var prevmonth = displaymonth;
	var prevyear = displayyear;
	if (prevmonth == 0)
	{
		prevmonth = 11;
		prevyear--;
	}
	else
		prevmonth--;

    var HTML = '<table cellspacing="0" cellpadding="0">' + String.fromCharCode(13);
    HTML += '<tr>';
        //month 1 
        HTML += '<td valign="top">';
            HTML += renderMonth(1, displayyear, displaymonth, themonth, theday, theyear, basename, prevmonth, prevyear, nextname, nextmonth, nextyear);
        HTML += '</td>';

	    var prevmonth = displaymonth;
	    var prevyear = displayyear;
	    if (prevmonth == 0)
	    {
		    prevmonth = 11;
		    prevyear--;
	    }
	    else
		    prevmonth--;

	    if (nextmonth == 0)
	    {
	        displaymonth = 0;
		    displayyear++;
		}
	    else
	        displaymonth++;

        //month 2
        HTML += '<td valign="top" style="background-color:#ffffff;border-left:1px solid #003399">';
            HTML += renderMonth(2, displayyear, displaymonth, themonth, theday, theyear, basename, prevmonth, prevyear, nextname, nextmonth, nextyear);
        HTML += '</td>';
   HTML += '</tr>';
   HTML += '</table>';
   
   return HTML += '<table width="' + ((CellWidth * 7) * 2 + 1) + 'px" cellspacing="0" cellpadding="1"><tr><td align="right" style="cursor:pointer;background-color:#c6d7e9;border-top:1px solid #003399;height:22px;color:#003399;font-family:Arial;font-size:11px;text-decoration:underline;" onclick="HideCalendar(\''+basename+'\')">close window</td></tr></table>';
}

function renderMonth(index, displayyear, displaymonth, themonth, theday, theyear, basename, prevmonth, prevyear, nextname, nextmonth, nextyear)
{
	var FirstDate = new Date(displayyear, displaymonth, 1);
	var firstDay = FirstDate.getDay();

    var Rows = 5;
    var dayCount = GetDayCount(displayyear, displaymonth);

    if (((dayCount == 31) && (firstDay > 4)) || ((dayCount == 30) && (firstDay == 6))) 
        Rows = 6;
    else if ((dayCount == 28) && (firstDay == 0)) 
        Rows = 4;
    
    monthHTML = '<table width="' + (CellWidth * 7) + 'px" cellspacing="0" cellpadding="0" style="cursor:default;">';

       monthHTML += '<tr>';
        monthHTML += '<td colspan="7">';
   	        monthHTML += '<table width="100%" cellspacing="0" cellpadding="1">';
		        monthHTML += '<tr style="background-color:#003399;color:#ffffff;font-family:verdana;font-size:11px;font-weight:bold;height:22px;">';
			        var yearLabel = displayyear.toString();
			        yearLabel = yearLabel.substring(2, 4);

			        switch (index)
			        {
			            case 1:
			            	if (displayyear > Today.getFullYear() || displayyear == Today.getFullYear() && displaymonth > Today.getMonth())
                            {
			                    monthHTML += '<td id="StartDate_Previous_ID" onclick="document.getElementById(\'' + basename + '_DayTable_ID\').innerHTML = BuildCalendarDays('+themonth+','+theday+','+theyear+',\''+basename+'\','+prevmonth+','+prevyear+',\''+nextname+'\')"';
			                    monthHTML += ' style="cursor:pointer;" align="left" class="calendarDateInput" style="height:' + CellHeight + 'px" >';
                                monthHTML += '<img src="' + PrevURL + '" style="width:16px;height:15px;" />';
			                    monthHTML += '</td>';
			                }
			                else 
                                monthHTML += '<td style="height:20px;"><img src="/images/layout/blank.gif" style="width:16px;height:15px;" /></td>';
                                
                            monthHTML += '<td id="StartDate_Current_ID" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px" colspan="5">'+ MonthNames[displaymonth] +' \'' + yearLabel + '</td>';
			            break;
			            case 2:
                            monthHTML += '<td id="StartDate_Current_ID" style="cursor:default" align="center" class="calendarDateInput" style="height:' + CellHeight + 'px" colspan="5">'+ MonthNames[displaymonth] +' \'' + yearLabel + '</td>';
			            	monthHTML += '<td id="StartDate_Next_ID"  style="cursor:pointer;height:20px;" onclick="document.getElementById(\'' + basename + '_DayTable_ID\').innerHTML = BuildCalendarDays('+themonth+','+theday+','+theyear+',\''+basename+'\','+nextmonth+','+nextyear+',\''+nextname+'\')" style="cursor:default;height:20px;" align="right" class="calendarDateInput" style="height:' + CellHeight + 'px" >';
			                monthHTML += '<img src="' + NextURL + '" style="width:16px;height:15px;" /></td>';
			            break;
        			}

		        monthHTML +='</tr>';
	        monthHTML +='</table>';
        monthHTML += '</td>';
    monthHTML += '</tr>';
    
   
    monthHTML +='<tr>';
    for (var w=0;w<7;w++) 
        monthHTML +='<td align="center" class="calendarDateInput" style="height:' + CellHeight + 'px;width:' + CellWidth + 'px;font-weight:bold;color:#003399">' + WeekDays[w] + '</td>';

	eval('thedate = document.getElementById(\'' + fieldPrefix + 'StartDate_ConCat\').value');
    
    monthHTML +='</tr>';   
    for (var j=0;j<Rows;j++) 
    {
        monthHTML += '<tr>';
        for (var i = 1; i <= 7; i++) 
        {
         Day = (j * 7) + (i - firstDay);
         if ((Day >= 1) && (Day <= dayCount)) 
         {
            if (thedate == ((displaymonth + 1) + '/' + Day + '/' + displayyear))
            {
               TextStyle = 'color:white;font-weight:bold;'
               BackColor = DayBGColor;
            }
            else 
            {
               TextStyle = 'color:black;'
               BackColor = CalBGColor;
            }
            
            if ((displayyear == Today.getFullYear()) && (displaymonth == Today.getMonth()) && (Day == Today.getDate())) TextStyle += 'border:1px solid #c0c0c0;padding:0px;';
            monthHTML += '<td align="center" class="calendarDateInput"';
            if ((displayyear == Today.getFullYear()) && (displaymonth == Today.getMonth()))
            {
	            if (Day >= Today.getDate())
		            monthHTML += ' onmouseover="DayCellHover(this,true,\'' + BackColor + '\')" onmouseout="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
	            else
		            TextStyle = 'color:#c3c2c2;';
            }
            else
            {
	            if (displaymonth < Today.getMonth())
	            {
		            if(displayyear < Today.getFullYear())
			            TextStyle = 'color:#c3c2c2;';
		            else
			            monthHTML += ' onmouseover="DayCellHover(this,true,\'' + BackColor + '\')" onmouseout="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
	            }
	            else
	            {
		            if(displayyear < Today.getFullYear())
			            TextStyle = 'color:#c3c2c2;';
		            else
			            monthHTML += ' onmouseover="DayCellHover(this,true,\'' + BackColor + '\')" onmouseout="DayCellHover(this,false,\'' + BackColor + '\')" onclick="PickDisplayDay('+displaymonth+','+Day+','+displayyear+',\''+basename+'\',\''+nextname+'\')" ';
	            }
            }
            monthHTML += ' style="cursor:default;height:' + CellHeight + 'px;width:' + CellWidth + 'px;' + TextStyle + ';background-color:' + BackColor + '">' + Day + '</td>';
         }
         else 
            monthHTML += '<td class="calendarDateInput" style="height:' + CellHeight + 'px">&nbsp;</td>';
        }
        monthHTML += '</tr>';
    }
    monthHTML += '</table>';
   
   return monthHTML;
}
    
var calDiv = document.createElement("div");

// Show or hide the calendar
function ShowCalendar(basename, nextname)
{
    var pixelsTop = 0;
    if (navigator.appName == "Microsoft Internet Explorer")
        pixelsTop = document.documentElement.scrollTop;
    else
        pixelsTop = window.pageYOffset;

    var yAdj = -200;
    if (pixelsTop > 200) 
        yAdj = 20;

    switch (basename)
    {    
        case "StartDate": 
            var startDate_yPos = getTopPos(document.getElementById('startDateCon')) + yAdj;
            var startDate_xPos = getLeftPos(document.getElementById('startDateCon'));
            
            var Today = new Date();
            calDiv.style.cssText = "position:absolute;z-index:100;top:" + startDate_yPos + "px;left:" + startDate_xPos + "px;";
            calDiv.innerHTML =  '<span id="StartDate_ID" style="position:absolute;display:none;width:' + ((CellWidth * 7)*2 + 1) + 'px;background-color:' + CalBGColor + ';border:2px solid #003399;"><span id="StartDate_DayTable_ID"></span></span>';
            document.body.appendChild(calDiv);
        break;
        case "EndDate": 
            var endDate_yPos = getTopPos(document.getElementById('endDateCon')) + yAdj;
            var endDate_xPos = getLeftPos(document.getElementById('endDateCon'));

            var Today = new Date();
            calDiv.style.cssText = "position:absolute;z-index:100;top:" + endDate_yPos + "px;left:" + endDate_xPos + "px;";
            calDiv.innerHTML =  '<span id="EndDate_ID" style="position:absolute;display:none;width:' + ((CellWidth * 7)*2 + 1) + 'px;background-color:' + CalBGColor + ';border:2px solid #003399;"><span id="EndDate_DayTable_ID"></span>';
            document.body.appendChild(calDiv);
        break;        
    }
    
	eval('thedate = document.getElementById(\'' + fieldPrefix + basename + '_ConCat\').value');
	if(document.getElementById(fieldPrefix+'StartDate_ConCat').value != 'mm/dd/yyyy' && document.getElementById(fieldPrefix+'EndDate_ConCat').value == 'mm/dd/yyyy')
		thedate = document.getElementById(fieldPrefix+'StartDate_ConCat').value;
	var firstslash = thedate.search('/');
	var themonth = thedate.substring(0,firstslash);
	var thedayyear = thedate.substring(firstslash+1,thedate.length);
	var secondslash = thedayyear.search('/');
	var theday = thedayyear.substring(0, secondslash);
	var theyear = thedayyear.substring(secondslash+1,thedayyear.length);
	
	if (isNaN (themonth) || isNaN (theday) || isNaN (theyear))
	{
		theday = Today.getDate();
		themonth = Today.getMonth();
		theyear = Today.getFullYear();
	}
	
	if (theday > 0 && themonth > 0 && theyear > 0)
	{
		if (theyear.length == 4 && themonth > 0 && themonth < 12)
		{
			if (theday <= GetDayCount(theyear, themonth))
			{
				themonth = --themonth;
				theyear = theyear;
				theday = theday;
			}
			else
			{
				theday = Today.getDate();
				themonth = Today.getMonth();
				theyear = Today.getFullYear();
			}
		}
		else
		{
			theday = Today.getDate();
			themonth = Today.getMonth();
			theyear = Today.getFullYear();
		}
	}
	else
	{
		theday = Today.getDate();
		themonth = Today.getMonth();
		theyear = Today.getFullYear();
	}
	
	eval('document.getElementById(\''+basename+'_DayTable_ID\').innerHTML = BuildCalendarDays(themonth,theday,theyear,basename,themonth,theyear,nextname)');
	eval('document.getElementById(\'' + basename + '_ID\').style.display = \'\'');
	FixSelectLists(true, basename);
}

function HideCalendar(basename)
{
    if (eval('document.getElementById(\'' + basename + '_ID\') != null'))
    	eval('document.getElementById(\'' + basename + '_ID\').style.display = \'none\'');
	FixSelectLists(false, basename);
}

function hideDiv() {
    showAllSelects();
    showFlash();

    if (div != null)
        div.style.cssText = "display:none;";
}

function hideFlash() {
    if (document.getElementById('flashcontent') != null)
        document.getElementById('flashcontent').style.display = 'none';
}

function showFlash() {
    if (document.getElementById('flashcontent') != null)
        document.getElementById('flashcontent').style.display = '';
}

function getLeft(obj) {
    var xPos = obj.offsetLeft;

    var tempEl = obj.offsetParent;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getTop(obj) {
    var yPos = obj.offsetTop;
    var tempEl = obj.offsetParent;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}
