//var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
//var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var minDate, maxDate, currentType, currentTask;
var dayNameLength = 2;
var monthNameLength = 3;
var calendarObject = "calendarPopup";
//var form = "document.advanced";
var dateSeparator = "/";
var selectedDates = Array();
var notSelected = true;
var isPC = navigator.userAgent.toLowerCase().indexOf('mac') == -1; 
var isIE = navigator.userAgent.toLowerCase().indexOf('msie') != -1; //var isIE = document.all; 
var isPCIE = false; 
if(isPC && isIE) isPCIE = true; 

// Tim Butterfield 07/06/2007 - do not allow choosing dates already in the selected array
var blockSelectedDates = false;
// end of change

// 10/16/08 CLD - add var for the Month day text box
var monthDayTextBox;
// end of change

// 1/6/09 CLD - add var to track calendar display
var calendarOnDisplay = false;
// end of change

function grab(object)
{	// object = id of object to be manipulated
	if(document.getElementById) { obj = document.getElementById(object); } // method for newer, DOM-compatable browsers
	else if(document.all) {  obj = document.all[obj]; } // method for older versions of IE
	else if(document.layers) { obj = document.layers[obj]; } // method for older versions of Netscape
	return(obj);
}

function getStyle(object)
{	if(document.getElementById || document.all) style = object.style;
	else if(document.layers) style = object;
	return(style);
}


// 10/16/08 CLD Create overload for chooseCalendar that includes paramter for MonthDayTextBox 
function chooseCalendarMonthDay(windowEvent, type, monthDayTextBoxtype, currentValue)
{
  monthDayTextBox = monthDayTextBoxtype;
  chooseCalendar(windowEvent, type, currentValue);
}


// This is the function called when the calendar icon is clicked.
function chooseCalendar(windowEvent, type, currentValue)
{

	// 1/6/09 CLD only process calendar icon click if the calendar is not currently on display
	if (!calendarOnDisplay)
	{

		currentValue=type.value;
		//	eval("form = 'document." + type.parentNode.parentNode.parentNode.parentNode.name + "';");
		// Position the layer, using the top and left passed paramters, obtained from the mouse click.


		var calendar = getStyle(grab(calendarObject));


		var xoff=0;

		if (window.pageXOffset)
			xoff = window.pageXOffset;
		else if (document.documentElement && document.documentElement.scrollLeft) 
			xoff = document.documentElement.scrollLeft;
		else if (document.body)
			xoff = document.body.scrollLeft;

		//calendar.left = windowEvent.clientX + xoff - -25 + 'px';
		// if we're on the TransferFunds page or MakeAPayment
		//*****************************************************************

		if (location.href.indexOf("TransferFunds") > 0) {
			calendar.left = windowEvent.clientX + xoff +110 + 'px';
		} else if (location.href.indexOf("MakeAPayment") > 0) {
			calendar.left = windowEvent.clientX + xoff +110 + 'px';
		} else if (location.href.indexOf("TransferExternalDeposit") > 0) {
			calendar.left = windowEvent.clientX + xoff +40 + 'px';
		} else if (location.href.indexOf("TransferExternalWithdrawal") > 0) {
			calendar.left = windowEvent.clientX + xoff +95 + 'px';
		} else {
			calendar.left = windowEvent.clientX + xoff - -25 + 'px';
		}

		//******************************************************************

		var yoff=0;

		if (window.pageYOffset)
			yoff = window.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop)
			yoff = document.documentElement.scrollTop;
		else if (document.body)
			yoff = document.body.scrollTop;


		var offset=0;


		// if we're on the AcctDetails page
		//******************************************************************
		if (location.href.indexOf("AcctDetails") > 0) {
			offset = 242;
		}else if (location.href.indexOf("TransferFunds") > 0)  {
			offset = 180;
		}else if (location.href.indexOf("MakeAPayment") > 0)  {
			offset = 175;
		}else if (location.href.indexOf("TransferExternalDeposit") > 0)  {
			offset = 100;
		}else if (location.href.indexOf("TransferExternalWithdrawal") > 0)  {
			offset = 60;
		}else {
			offset = 0;
		}
		//******************************************************************
		//var offset = (location.href.indexOf("AcctDetails") > 0)?242 : 0;


		calendar.top = windowEvent.clientY + yoff - offset + 'px';

	// Set the minDate and maxDate, if needed.
	//	minDate = document.projectPlan.close_date.value;
	//	maxDate = document.projectPlan.end_date.value;
		currentType = type; // "begin" or "end"

		// Tim Butterfield 07/06/2007 - do not clear selectedDates if blocking them
		// selectedDates.length = 0;
		if (!blockSelectedDates) {
			selectedDates.length = 0;
		}
		// end of change
		
	//	var theMonth = document.projectPlan.defaultDate.value;
	//	var theMonth = '12/31/2004';
		var dateNow = new Date();
		var curDay = dateNow.getDate() + '';
		var curMonth = ( dateNow.getMonth() + 1 ) + '';
	    
		if ( dateNow.getDate() < 10 ) curDay = '0' + curDay;
		if ( dateNow.getMonth() < 10 ) curMonth = '0' + curMonth;
	    
		var theMonth = curMonth + '/' + curDay + '/' + dateNow.getFullYear()
		if(currentValue)
		{	
			// Tim Butterfield 07/06/2007 - not not add date if blocking selected dates
			// selectedDates.push(currentValue);
			if (!blockSelectedDates) {
				selectedDates.push(currentValue);
			}
			// end of change
		    
			theMonth = currentValue;
		}
		theMonth = theMonth.substr(0, 2) + dateSeparator + theMonth.substr(6, 4);
		renderCalendar(theMonth);
		
		// 1/6/09 CLD set flag to indicate calendar is on display
		calendarOnDisplay = true;
		
	} // 1/6/09 CLD close if calendar on display 
	
}

function chooseDate(date)
{	closeCalendar();
//	eval(form + "." + currentType.name + ".value='" + date + "';");
currentType.value = date;
	//dateChange();
}

function chooseDate(date)
{	closeCalendar();
//	eval(form + "." + currentType.name + ".value='" + date + "';");
currentType.value = date;
	//dateChange();

// 10/16/08 CLD Set monthday text box value
try{
var theDay = date;
var datePicked = new Date(theDay);
theday = datePicked.getDate();
monthDayTextBox.value = theday;
} catch(ex) {}
}


function clearDate(element)
{	eval(form + "." + element + ".value = '';");
	closeCalendar();
}

function closeCalendar()
{	
	hide(calendarObject);
	/*if(document.getElementById) document.getElementById(calendarObject).innerHTML = "";*/
	// 1/6/09 CLD set flag to indicate calendar is not on display
	calendarOnDisplay = false;	
}

function renderCalendar(calendarMonth)
{	if(!document.getElementById) return false;
		
	// Parse out the month and year from calendarMonth.
	var month = calendarMonth.substr(0, 2);
	if(month.substr(0, 1) == "0") month = month.substr(1, 1);
	month = parseInt(month);
	var year = parseInt(calendarMonth.substr(3, 4));

	// Set some basic information and determine days of calendarMonth.
	var daysNeeded = daysInMonth[month - 1];
	if(month == 2 && year != "" && year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0)) daysNeeded++;
	// Parse minDate if value passed.
	if(minDate)
	{	minMonth = minDate.substr(0, 2);
		if(minMonth.substr(0, 1) == "0") minMonth = minMonth.substr(1, 1);
		minMonth = parseInt(minMonth);
		minDay = minDate.substr(3, 2);
		if(minDay.substr(0, 1) == "0") minDay = minDay.substr(1, 1);
		minDay = parseInt(minDay);
		minYear = parseInt(minDate.substr(6, 4));
	}
	// Parse maxDate if value passed.
	if(maxDate)
	{	maxMonth = maxDate.substr(0, 2);
		if(maxMonth.substr(0, 1) == "0") maxMonth = maxMonth.substr(1, 1);
		maxMonth = parseInt(maxMonth);
		maxDay = maxDate.substr(3, 2);
		if(maxDay.substr(0, 1) == "0") maxDay = maxDay.substr(1, 1);
		maxDay = parseInt(maxDay);
		maxYear = parseInt(maxDate.substr(6, 4));
	}
	// For the back by one year arrow, making sure that this is not before the minDate.
	var backOneYear = year - 1;
	
	if(minDate && (backOneYear < minYear || (backOneYear == minYear && month < minMonth))) backOneYear = null;
	else
	{	backOneYear = month + dateSeparator + backOneYear;
		if(month < 10) backOneYear = "0" + backOneYear;
	}
	// For the up by one year arrow, making sure that this is not after the maxDate.
	var upOneYear = year + 1;
	if(maxDate && (upOneYear > maxYear || (upOneYear == maxYear && month > maxMonth))) upOneYear = null;
	else
	{	upOneYear = month + dateSeparator + upOneYear;
		if(month < 10) upOneYear = "0" + upOneYear;
	}
	// For the back by one month arrow, making sure that this is not before the minDate.
	var backOneMonth = year;
	var previousMonth = month - 1;
	if(previousMonth < 1)
	{	backOneMonth--;
		previousMonth = 12;
	}
	if(minDate && (backOneMonth < minYear || (backOneMonth == minYear && previousMonth < minMonth))) backOneMonth = null;
	else
	{	backOneMonth = previousMonth + dateSeparator + backOneMonth;
		if(previousMonth < 10) backOneMonth = "0" + backOneMonth;
	}
	// For the up by one month arrow, making sure that this is not after the maxDate.
	var upOneMonth = year;
	var nextMonth = month + 1;
	if(nextMonth > 12)
	{	upOneMonth++;
		nextMonth = 1;
	}
	if(maxDate && (upOneMonth > maxYear || (upOneMonth == maxYear && nextMonth > maxMonth))) upOneMonth = null;
	else
	{	upOneMonth = nextMonth + dateSeparator + upOneMonth;
		if(nextMonth < 10) upOneMonth = "0" + upOneMonth;
	}
	// Create calendar markup code.
	var html = '';
	// Top table row, containing "Select a Date" messages and Close link.
	html += '<div id="handle" class="handle"><a id="closeCalendar" onclick="closeCalendar();" title="close calendar">' + calClose + '</a></div>';	
	// New row, containing the months, if needed.
	// New row, containing the year and backOneMonth and upOneMonth arrow links.
	html += '<div class="next" id="le">';
	if(backOneMonth) html += '<a onclick="renderCalendar(\'' + backOneMonth + '\');" title="previous month">' + calPrevious + '</a>';
	html += '</div><div class="next" id="month">' + monthNames[month - 1] + ' ' + year + '</div><div class="next" id="ri">';
	if(upOneMonth) html += '<a onclick="renderCalendar(\'' + upOneMonth + '\');" title="next month">' + calNext + '</a>';
	html += '</div></th></tr>';
	// New row, contains day column headings.
	html += '<div style="clear: both"></div><table cellpadding="3"><tr id="days">';
	for(x = 0; x < dayNames.length; x++) html += '<th align="right">' + dayNames[x].substr(0, 1) + '</th>';
	html += '</tr>';
	// Finally, the date rows.
	var day = 1;
	var row = 0;
	
	while(day <= daysNeeded)
	{	row++;
		html += "<tr>";
		for(var d = 0; d < 7; d++)
		{	//html += "<td>";
			var dateStamp = new Date(year, month - 1, day, 0, 0, 0);
			if(dateStamp.getDay() != d || day > daysNeeded) html += "<td>&nbsp;</td>";
			else
			{	// Create an mm.dd.yyyy representation, working backwards, of that date to see if it equals the current value.
				var tempDate = day + dateSeparator + year.toString();
				//alert(tempDate);
				if(day < 10)
				{	//html += '<td>&nbsp;</td>';
					tempDate = "0" + tempDate;
				}
				tempDate = month + dateSeparator + tempDate;
				if(month < 10) tempDate = "0" + tempDate;
				var display = day;
				// Check through the array of selected "dates" to see if there is a match.
				for(var s = 0; s < selectedDates.length; s++) {
					//alert('checking');
					if(tempDate == selectedDates[s])
					{
					    // Tim Butterfield 07/06/2007 - block choosing any selected dates
					    // display = '<td class="selected" onclick="chooseDate(\'' + tempDate + '\');"><a onclick="chooseDate(\'' + tempDate + '\');">' + display + '</a>'; // A match: hightlight it.
					    if (blockSelectedDates) {
					        display = '<td class="selected">' + display; // A match: hightlight it, but don't handle click event.
					    } else {
					        display = '<td class="selected" onclick="chooseDate(\'' + tempDate + '\');"><a onclick="chooseDate(\'' + tempDate + '\');">' + display + '</a>'; // A match: hightlight it.
					    }
					    // end of change
						notSelected=false;
						break;
					}
				}
				// Display the date, as a link so that it can be selected (if within the minDate/maxDate range.
				if(notSelected)
					display = '<td class="available" onclick="chooseDate(\'' + tempDate + '\');"><a onclick="chooseDate(\'' + tempDate + '\');">' + display + '</a>';
				else notSelected=true;
				html += display;
				day++;
			}
			html += "</td>";
		}
		html += "</tr>";
	}
	// Make sure there are always six date rows, to ensure a uniform size, when toggling to different months.
	if(row < 6) html += '<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
	html += '</table>';
	
	// Markup code is complete; place it inside the empty, invisible calendarObject layer.
	document.getElementById(calendarObject).innerHTML = html;

	unHide(calendarObject); // Show the layer.
	makeHandle();
}

// For validating a typed in date entry to a text input.
function validateDate(elementName)
{	var element = eval(form + "." + elementName);
	return true;
}

function dateChange()
{	validateDate(currentType);
	var beginDate = eval(form + ".from" + ".value;");
	var endDate = eval(form + ".to" + ".value;");
}

/* get MousePos for calendar */
function getMousePos(evt)
{	if(window.event)
	{	s_srcElement = window.event.srcElement.nodeName;
		//Safari will set the clienty of an event from a form input to the form offset
		yPos = (s_srcElement == "INPUT" && KON) ? Math.abs(window.event.offsetY) + 10 : window.event.clientY - 5;
	}
	else if(document.getElementById && !document.all)
	{	xPos = evt.clientX + 165;
		yPos = evt.clientY - 5;
	}
	else
	{	xPos = 50;
		yPos = 50;
	}
}

function unHide(obj)
{	object = getStyle(grab(obj));
	//alert('test');
	object.visibility = "visible";
}

function hide(obj)
{	object = getStyle(grab(obj));
	object.visibility = "hidden";
}