/***********************************************
CALENDAR FUNCTIONS
************************************************/

var aCalendars = new Array();
/* 
aCalendar Map
	aCalendars[x] = calendar index
	aCalendars[x][0] = id name
	aCalendars[x][1] = current year
	aCalendars[x][2] = current month
	aCalendars[x][3] = selected date
	aCalendars[x][4] = calendar type
*/

function buildCalendar(y, m, type, sId, bDateTool, sGraphicsPath){
	var mn = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var dim = [31,0,31,30,31,30,31,31,30,31,30,31];
	var oD = new Date(y, m-1, 1);
	var nIdx = 0;
	var sDate = '';
	oD.od = oD.getDay() + 1;
	var aDaySelected = new Array();
	
	if(typeof sGraphicsPath == "undefined" || sGraphicsPath == ''){
		sGraphicsPath = '../../graphics/buttons/default';
	}
	
	if(getCalendarIndex(sId) == -1){
		//if calendar is being built for the first time add it to array
		aCalendars.push(new Array(sId, y, m, '', type));
	}
	
	nIdx = getCalendarIndex(sId);
	
	if(document.getElementById(sId).value != ""){
	
		if(aCalendars[nIdx][4] == 1){
			//single day
			//set selected day if we are in the current month and year view of the selected day
			
			if(document.getElementById(sId).value.substring(0, document.getElementById(sId).value.indexOf('-', 6)) == 
			   (aCalendars[nIdx][1].toString() + '-' + appendZero(aCalendars[nIdx][2].toString()))){
				aDaySelected.push(document.getElementById(sId).value.substr(document.getElementById(sId).value.indexOf('-', 6) + 1, 2));
			}
		}else{
			//full week	
			var dStart = new Date(convertStringToDate(document.getElementById(sId).value));
			for(var i = 0; i < 7; i++){
				var dTemp = new Date(dStart);
				dTemp.setDate(dTemp.getDate() + i)
				if(dTemp.getFullYear() == oD.getFullYear() && dTemp.getMonth() == oD.getMonth()){
					aDaySelected.push(dTemp.getDate());
				}
			}
		}
	}

	/***********************************************
	* Calendar equation script from:
	* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
	***********************************************/
	dim[1] = (((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	var sHTML = '<div class="calendarMain">' +
				'<table cellpadding="2" cellspacing="0" border="0" width="100%" style="position: relative; top: -4px;">' +
				'<tr align="center">' +
					'<td align="center">' +
						'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
	if(typeof sCalendarHeaderFontColor == "undefined"){
		sHTML = sHTML + '<tr class="headerFont">';
	}else{
		//sCalendarHeaderFontColor defined outside of this module (currently date_tool.cfm custom tag)
		sHTML = sHTML + '<tr style="color: ' + sCalendarHeaderFontColor + '">';
	}
	sHTML = sHTML +			'<td><a href="javascript: changeMonth(0, \'' + sId + '\', ' + bDateTool.toString() + ', \'' + sGraphicsPath + '\');"><img src="' + sGraphicsPath + '/buttons/default/previousrecord.gif" border="0"></a></td>' +
							'<td width="100%" align="center"><b>'+mn[m-1]+' - '+y+'</b></td>' +
							'<td><a href="javascript: changeMonth(1, \'' + sId + '\',' + bDateTool.toString() + ', \'' + sGraphicsPath + '\');"><img src="' + sGraphicsPath + '/buttons/default/nextrecord.gif" border="0"></a></td>' +
						'</tr>' +
						'</table>' +
					'</td>' +
				'</tr>' +
				'</table>' +
				'<table class="calendarMain headerBorder" cols="7" cellpadding="0" cellspacing="1">' + 
				'<tr align="center">';
	
	for(s = 0; s < 7; s++){
		sHTML += '<td class="subHeaderSolid">'+"SMTWTFS".substr(s,1)+'</td>';
	}
	
	sHTML += '</tr>' +
		 '<tr align="center">';
 	for(i = 1; i <= 42; i++){
		var x = ((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		if(aCalendars[nIdx][4] == 1){
			sDate = standardizeDate(oD.getFullYear().toString(), (oD.getMonth() + 1).toString(), x.toString());
		}else{
			sDate = getStartWeekDate(oD.getFullYear().toString(), (oD.getMonth() + 1).toString(), x.toString());
		}
		if (aDaySelected.length > 0 && findSelectedDay(x, aDaySelected)){
			//selected box
			sHTML += '<td width="14%" class="calendarDays calendarDaySelected" onMouseOver="javascript: this.style.cursor = \'pointer\';" onMouseOut="javascript: this.style.cursor = \'default\';" onClick="javacript: daySelect(\'' + sDate + '\', \'' + sId + '\', 1, ' + bDateTool.toString() + ', \'' + sGraphicsPath + '\');">'+x+'</td>';
		}else if (x != '&nbsp;'){
			//numbered box
			sHTML += '<td width="14%" class="calendarDays" onMouseOver="javascript: dayMouseOver(this);" onMouseOut="javascript: dayMouseOut(this);" onClick="javacript: daySelect(\'' + sDate + '\', \'' + sId + '\', 0, ' + bDateTool.toString() + ', \'' + sGraphicsPath + '\');">'+x+'</td>';
		}else{
			//blank box
			sHTML += '<td width="14%" class="calendarDays">'+x+'</td>';
		}

		if(((i)%7==0)&&(i<36)){
			sHTML += '</tr><tr align="center">';
		}
	}
	
	sHTML += '</tr></table></div>';
	
	return sHTML;
}

function getCalendarIndex(sId){
	for(var i = 0; i < aCalendars.length; i++){
		if(sId == aCalendars[i][0]){
			return i;
			break;
		}
	}
	return -1;
}

function dayMouseOver(o){
	if(o.className != "calendarDaySelected"){
		o.className = "calendarDays calendarMouseOver";
	}
}

function dayMouseOut(o){
	if(o.className != "calendarDaySelected"){
		o.className = "calendarDays";
	}
}

function appendZero(s){
	if(s.length == 1){
		s = '0' + s;
	}
	return s;
}

function daySelect(sDate, sId, bClear, bDateTool, sGraphicsPath){
	var nIdx = getCalendarIndex(sId);
	var sContainerId = "";
	var nDateFieldIdx = -1;
	
	if(bClear){
		//'unselect' the selected day
		document.getElementById(sId).value = "";
	}else{
		document.getElementById(sId).value = sDate;
		//date tool ONLY code
		if(typeof aDateFields != "undefined"){
			nDateFieldIdx = getDateFieldIndex(sId);
			if(nDateFieldIdx != -1){
				aDateFields[nDateFieldIdx][3] = true;//flag manual change
				if(aDateFields[nDateFieldIdx][4] != ''){
					//if date field is linked to another, change date
					dateLink(aDateFields[nDateFieldIdx][4], sId, aDateFields[nDateFieldIdx][5]);
				}
			}
		}
		
		//fire a javascript event if date is changed. Only used in some scenarios
		if(document.getElementById(sId).onchange != undefined){
			document.getElementById(sId).onchange();
		}
	}
	
	if(bDateTool){
		sContainerId = "CalendarMain_container";
		setDTFields(sId);
	}else{
		sContainerId = sId + "_container";
	}
	
	document.getElementById(sContainerId).innerHTML = buildCalendar(aCalendars[nIdx][1], aCalendars[nIdx][2], aCalendars[nIdx][4], sId, bDateTool, sGraphicsPath);
}

function findSelectedDay(n, a){
	for(var i = 0; i < a.length; i++){
		if(n == a[i]){
			return true;
		}
	}
	return false;
}

function changeMonth(n, sId, bDateTool, sGraphicsPath){
	var nIdx = getCalendarIndex(sId);
	var sContainerId = "";
	if(n == 0){
		//previous month
		if(aCalendars[nIdx][2] == 1){
			aCalendars[nIdx][2] = 12;
			aCalendars[nIdx][1]--;
		}else{
			aCalendars[nIdx][2]--;
		}
		var nMonth = aCalendars[nIdx]
	}else{
		//next month
		if(aCalendars[nIdx][2] == 12){
			aCalendars[nIdx][2] = 1;
			aCalendars[nIdx][1]++;
		}else{
			aCalendars[nIdx][2]++;
		}
	}
	
	if(bDateTool){
		sContainerId = "CalendarMain_container";
	}else{
		sContainerId = sId + "_container";
	}
	
	document.getElementById(sContainerId).innerHTML = buildCalendar(aCalendars[nIdx][1], aCalendars[nIdx][2], aCalendars[nIdx][4], sId, bDateTool, sGraphicsPath);
}

function standardizeDate(y, m, d){
	return y + '-' + appendZero(m) + '-' + appendZero(d);
}

function getStartWeekDate(y, m, d){
	var dDate = new Date(y, m-1, d);
	dDate.setDate(dDate.getDate() - dDate.getDay());
	return dDate.getFullYear().toString() + '-' + appendZero((dDate.getMonth() + 1).toString()) + '-' + appendZero(dDate.getDate().toString());
}

function convertStringToDate(s){
	return new Date(s.substring(0,4), (s.substring(s.indexOf('-') + 1, s.indexOf('-', 6)) - 1), s.substr(s.indexOf('-', 6) + 1));
}



/***********************************************
DATE TOOL FIELDS AND VALIDATION FUNCTIONS
************************************************/
/* 
aDateFields Map
	aDateFields[x] = date fields index
	aDateFields[x][0] = id name
	aDateFields[x][1] = is required (boolean)
	aDateFields[x][2] = validation type (0 = normal, 1 = date in future, 2 = now or date in past)
*/
var aDateFields = new Array();
var aDaysInMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
var aDateText = new Array();
var nMinYear = 1900;
var nMaxYear = 2078;

for(var i = 0; i < 31; i++){
	aDateText[i] = i + 1;
}
	
function validateDTDates(){
	for(var i = 0; i < aDateFields.length; i++){
		if(!processDate(aDateFields[i][0], true)){
			return false;
		}
	}
	return true;
}	

function getDateFieldIndex(sId){
	for(var i = 0; i < aDateFields.length; i++){
		if(aDateFields[i][0] == sId){
			return i;
			break;
		}
	}
	return -1;
}

function processDate(sId, bWarning){
	var nEmpty = 0;
	for(var i = 0; i < aDateFields.length; i++){
		if(aDateFields[i][0] == sId){
			if(aDateFields[i][1]){
				//required
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_m\')').value != ''){
					if(eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value != ''){
						if(!validateDTYear(aDateFields[i][0], bWarning)){
							eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
							return false;
						}else if(eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')') != undefined && 
								 !validateTime(aDateFields[i][0], bWarning)){
							eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
							return false;
						}
					}else{
						if(bWarning){
							alert("You must enter a day");
							eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').focus();
						}
						eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
						return false;
					}
				}else{
					if(bWarning){
						alert("You must enter a month");
						eval('document.getElementById(\'' + aDateFields[i][0] + '_m\')').focus();
					}
					eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
					return false;
				}
			}else{
				//not required allow all empty or all full
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_m\')').value == ''){
					nEmpty++;
				}
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value == ''){
					nEmpty++;
				}
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').value == ''){
					nEmpty++;
				}
				if(nEmpty != 0 && nEmpty != 3){
					//partial date
					if(bWarning){
						alert("You must fill out the full date");
						eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').focus();
					}
					eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
					return false;
				}else if(nEmpty == 0){
					if(!validateDTYear(aDateFields[i][0], bWarning)){
						eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
						return false;
					}else if(eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')') != undefined && 
							 !validateTime(aDateFields[i][0], bWarning)){
						eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
						return false;
					}
				}
			}
			//update hidden field with full date and time
			var sDateTime = "";
			if(nEmpty == 0){
				sDateTime = sDateTime + eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').value;
				sDateTime = sDateTime + '-' + eval('document.getElementById(\'' + aDateFields[i][0] + '_m\')').value;
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value.length == 1){
					//add a zero before single digit
					sDateTime = sDateTime + '-0' + eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value;
				}else{
					sDateTime = sDateTime + '-' + eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value;
				}
				
				if(eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')') != undefined){
					var sTime = eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')').value;
					if(sTime.length == 4){
						//if 24 hour time field requires a ':';
						if(sTime == "2400"){//2400 is not valid on back end
							sTime = "0000";
						}
						sDateTime = sDateTime + " " + sTime.substr(0,2) + ':' + sTime.substr(2,2);
					}else if(sTime.length == 6 || sTime.length == 7 || sTime.length == 8){
						sDateTime = sDateTime + " " + sTime;
					}
				}
				
				if(aDateFields[i][2] == 1){
					//validate future date
					if(!validateDTFuture(sDateTime, true)){
						if(bWarning){
							alert("The date entered cannot be in the past");
							eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').focus();
							return false;
						}
					}
				}else if(aDateFields[i][2] == 2){
					//validate now or past date
					if(validateDTFuture(sDateTime, false)){
						if(bWarning){
							alert("The date entered cannot be in the future");
							eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').focus();
							return false;
						}
					}
				}
				
				eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = sDateTime;
				
			}else if(nEmpty == 3){
				//user may have pressed 'back' on their browser and had filled out a date before which may have remained. We need to 
				//clear it since nothing is selected now
				eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
			}
			return true;
		}
	}
	return false;
}

function dateChange(sField){
	var nMonthIdx = -1;
	var nDaysInMonth = -1;
	var bRequired = false;
	var oSelect = document.getElementById(sField + '_d');
	var nLength = oSelect.options.length;
	
	if(eval('document.getElementById(\'' + sField + '_m\')').value != ''){
		var nMonthValue = eval('document.getElementById(\'' + sField + '_m\')').value;
		var nDaySelected = eval('document.getElementById(\'' + sField + '_d\')').value;
		bRequired = checkRequired(sField);
		if(nMonthValue.charAt(0) == 0){
			//trim zero
			nMonthIdx = nMonthValue.charAt(1) - 1;
		}else{
			nMonthIdx = nMonthValue - 1;
		}
		if(nMonthIdx == 1){
			//February
			if(eval('document.getElementById(\'' + sField + '_y\')').value != '' && validateDTYear(sField, true)){
				nDaysInMonth = daysInFebruary(eval('document.getElementById(\'' + sField + '_y\')').value);
			}else{
				//year was not defined but still change days
				nDaysInMonth = aDaysInMonth[nMonthIdx];
			}
		}else{
			nDaysInMonth = aDaysInMonth[nMonthIdx];
		}
		//user selected end of the month day, changed month and days for that month is less than selection
		if(nDaySelected != '' && nDaySelected > nDaysInMonth){
			nDaySelected = nDaysInMonth;
		}else if(nDaySelected == ''){
			nDaySelected = 1;
		}
		
		//clear select box
		for(var i = 0; i < nLength; i++){
			oSelect.remove(0);
		}
		
		if(!bRequired){
			oSelect[oSelect.options.length] = new Option('', '');
		}
		
		for(var i = 0; i < nDaysInMonth; i++){
			oSelect[oSelect.options.length] = new Option(aDateText[i], aDateText[i]);
		}
		
		oSelect.value = nDaySelected;
	}
}

function daysInFebruary (year){
	// February code from SmartWebby.com (http://www.smartwebby.com/dhtml/
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function validateDTYear(sField, bWarning){
	s = eval('document.getElementById(\'' + sField + '_y\')').value;
	if(s.length == 4){
		for(var i = 0; i < s.length; i++){
			var c = s.charAt(i);
			if(c < "0" || c > "9"){
				if(bWarning){
					alert("You must enter a valid year");
					eval('document.getElementById(\'' + sField + '_y\')').focus();
				}
				return false;
			}
		}
		if(s < nMinYear || s > nMaxYear){
			if(bWarning){
				alert("The year you have entered must be between " + nMinYear + " and " + nMaxYear + ".");
				eval('document.getElementById(\'' + sField + '_y\')').focus();
			}
			return false;
		}
	}else{
		if(bWarning){
			alert("You must enter a four digit year");
			eval('document.getElementById(\'' + sField + '_y\')').focus();
		}
		return false;
	}
	return true;
}

function checkRequired(sField){
	//determine if field is required
	var bRequired = false;
	for(var i = 0; i < aDateFields.length; i++){
		if(aDateFields[i][0] == sField && aDateFields[i][1]){
			bRequired = true;
		}
	}
	return bRequired;
}

function yearChange(sField){
	//allow blank year if not required
	var bRequired = checkRequired(sField)
	if(bRequired || (!bRequired && eval('document.getElementById(\'' + sField + '_y\')').value != '')){
		if(validateDTYear(sField, true)){
			dateChange(sField);
		}
	}else{
		dateChange(sField);
	}
}

function validateTime(sField, bWarning){
	var sTime = eval('document.getElementById(\'' + sField + '_t\')').value;
	var bValid = true;

	if(sTime != '' && sTime.length != 4 && sTime.length != 6 && sTime.length != 7 && sTime.length != 8){
		bValid = false;
	}
	
	if(bValid){
		if(sTime.length == 4){
			//24 hour time
			for(var i = 0; i < sTime.length; i++){
				var c = sTime.charAt(i);
				if(c < "0" || c > "9"){
					bValid = false;
					break;
				}
			}
			if(bValid){
				if(sTime != "2400"){// 2400 is the exception in 24h time
					if(sTime.substr(0, 2) > "23"){
						bValid = false;
					}else if(sTime.substr(2, 2) > "59"){
						bValid = false;
					}
				}
			}
		}else if(sTime.length == 6){
			//standard time single digit hour no space in suffix
			if(bValid && (sTime.charAt(0) < "1" || sTime.charAt(0) > "9")){
				bValid = false;
			}else if(sTime.charAt(1) != ":"){
				bValid = false;
			}else if(sTime.charAt(2)  < "0" || sTime.charAt(2) > "5"){
				bValid = false;
			}else if(sTime.charAt(3) < "0" || sTime.charAt(3) > "9"){
				bValid = false;
			}else if(sTime.charAt(4) != "A" && sTime.charAt(4) != "a" && sTime.charAt(4) != "P" && sTime.charAt(4) != "p"){
				bValid = false;
			}else if(sTime.charAt(5) != "M" && sTime.charAt(5) != "m"){
				bValid = false;
			}
		}else if(sTime.length == 7){
			//standard time single digit hour with space in suffix
			if(bValid && (sTime.charAt(0) < "1" || sTime.charAt(0) > "9")){
				bValid = false;
			}else if(sTime.charAt(1) != ":"){
				bValid = false;
			}else if(sTime.charAt(2)  < "0" || sTime.charAt(2) > "5"){
				bValid = false;
			}else if(sTime.charAt(3) < "0" || sTime.charAt(3) > "9"){
				bValid = false;
			}else if(sTime.charAt(4) != " "){
				bValid = false;
			}else if(sTime.charAt(5) != "A" && sTime.charAt(5) != "a" && sTime.charAt(5) != "P" && sTime.charAt(5) != "p"){
				bValid = false;
			}else if(sTime.charAt(6) != "M" && sTime.charAt(6) != "m"){
				bValid = false;
			}
			
			if(!bValid){
				bValid = true;
				//standard time double digit hour no space
				if(sTime.charAt(0) != "1"){
					bValid = false;
				}else if(sTime.charAt(1) < "0" || sTime.charAt(1) > "2"){
					bValid = false;
				}else if(sTime.charAt(2) != ":"){
					bValid = false;
				}else if(sTime.charAt(3)  < "0" || sTime.charAt(3) > "5"){
					bValid = false;
				}else if(sTime.charAt(4) < "0" || sTime.charAt(4) > "9"){
					bValid = false;
				}else if(sTime.charAt(5) != "A" && sTime.charAt(5) != "a" && sTime.charAt(5) != "P" && sTime.charAt(5) != "p"){
					bValid = false;
				}else if(sTime.charAt(6) != "M" && sTime.charAt(6) != "m"){
					bValid = false;
				}
			}
		}else if(sTime.length == 8){
			//standard time two digit hour
			if(sTime.charAt(0) != "1"){
				bValid = false;
			}else if(sTime.charAt(1) < "0" || sTime.charAt(1) > "2"){
				bValid = false;
			}else if(sTime.charAt(2) != ":"){
				bValid = false;
			}else if(sTime.charAt(3)  < "0" || sTime.charAt(3) > "5"){
				bValid = false;
			}else if(sTime.charAt(4) < "0" || sTime.charAt(4) > "9"){
				bValid = false;
			}else if(sTime.charAt(5) != " "){
				bValid = false;
			}else if(sTime.charAt(6) != "A" && sTime.charAt(6) != "a" && sTime.charAt(6) != "P" && sTime.charAt(6) != "p"){
				bValid = false;
			}else if(sTime.charAt(7) != "M" && sTime.charAt(7) != "m"){
				bValid = false;
			}
		}
	}
	
	if(!bValid){
		if(bWarning){
			alert("You must enter a time like '9:45 AM' or like '0945' or leave the time field blank.");
			eval('document.getElementById(\'' + sField + '_t\')').focus();
		}
		return false;
	}else{
		return true;
	}
}

function setDTFields(sId){
	var sValue = document.getElementById(sId).value;
	
	if(sValue.length == 10){
		//set month
		document.getElementById(sId + '_m').value = sValue.substring(5, 7);
		//repopulate month field
		dateChange(sId);
		if(sValue.charAt(8) == '0'){
			//strip leading zero for dropdown
			document.getElementById(sId + '_d').value = sValue.substring(9, 10);
		}else{
			document.getElementById(sId + '_d').value = sValue.substring(8, 10);
		}
		document.getElementById(sId + '_y').value = sValue.substring(0, 4);
		closeCalendar();
	}
}

//this function is called externally and is not part of validation
function clearAllNonRequiredDTFields(){
	for(var i = 0; i < aDateFields.length; i++){
		if(!aDateFields[i][1]){
			eval('document.getElementById(\'' + aDateFields[i][0] + '_m\')').value = '';
			eval('document.getElementById(\'' + aDateFields[i][0] + '_d\')').value = '';
			eval('document.getElementById(\'' + aDateFields[i][0] + '_y\')').value = '';
			if(eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')') != undefined){
				eval('document.getElementById(\'' + aDateFields[i][0] + '_t\')').value = '';
			}
			eval('document.getElementById(\'' + aDateFields[i][0] + '\')').value = '';
		}			
	}
}


function formatDTDate(sDate){
	var nYear = sDate.substring(0, 4);
	var nMonth = sDate.substring(5, sDate.indexOf('-', 6)) - 1;
	var nDay = sDate.substr(sDate.indexOf('-', 6) + 1);
	return new Date(nYear, nMonth, nDay);
}

function formatToDTDate(d){
	var sFormatted = d.getFullYear() + '-';
	
	if(typeof bTodayValid == "undefined"){
		bTodayValid = false;
	}
	
	if((d.getMonth() + 1).toString().length == 1){
		sFormatted = sFormatted + '0';
	}
	sFormatted = sFormatted + (d.getMonth() + 1) + '-';
	if(d.getDate().toString().length == 1){
		sFormatted = sFormatted + '0';
	}
	sFormatted = sFormatted + d.getDate();
	
	return sFormatted;
}

function compareDTDates(d1, d2){
	return Math.round((formatDTDate(d2)-formatDTDate(d1)) / (1000 * 60 * 60 * 24));
}

function validateDTCompare(d1, d2){
	if(compareDTDates(d1, d2) >= 0){
		return true;
	}
	alert("The start date can not be greater than the end date.");
	return false;
}

function validateDTFuture(d, bTodayValid){
	var dNow = new Date();
	var sNow = formatToDTDate(dNow);

	if(bTodayValid){
		if(compareDTDates(d.substring(0, 10), sNow) <= 0){
			return true;
		}
	}else{
		if(compareDTDates(d.substring(0, 10), sNow) < 0){
			return true;
		}
	}
	return false;
}



/***********************************************
POPUP FUNCTIONS
************************************************/
var sContentDiv = "";
var sContainerDiv = "";
		
function showCalendarPopup(oClickEvent, sId, sGraphicsPath){			
	var nRight = document.body.clientWidth - oClickEvent.clientX;
	var nBottom = document.body.clientHeight - oClickEvent.clientY;
	var nClickOffset = -2;
	var nTempMonth = '';
	var nIdx = -1;
	var dNow = new Date();
	
	sContentDiv = document.getElementById('CalendarContent');
	sContainerDiv = document.getElementById('CalendarContainer');

	if (nRight < sContentDiv.offsetWidth){
		sContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX - sContentDiv.offsetWidth + nClickOffset;
	}else{
		sContentDiv.style.left = document.body.scrollLeft + oClickEvent.clientX + nClickOffset;
	}
	
	if (nBottom < sContentDiv.offsetHeight){
		sContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY - sContentDiv.offsetHeight + nClickOffset;
	}else{
		sContentDiv.style.top = document.body.scrollTop + oClickEvent.clientY + nClickOffset;
	}
	
	// move box to the left if out of screen bounds on right (currently hardcoded due to calendars width being hardcoded)
	if((document.body.clientWidth - 220) <  sContentDiv.style.left.substring(0, sContentDiv.style.left.length - 2)){
		sContentDiv.style.left =  (sContentDiv.style.left.substring(0, sContentDiv.style.left.length - 2) - 214);
	}
	
	// move box to the top if out of screen bounds on bottom (currently hardcoded due to calendars height being hardcoded)
	if((document.body.clientHeight - 180) <  sContentDiv.style.top.substring(0, sContentDiv.style.top.length - 2)){
		sContentDiv.style.top =  (sContentDiv.style.top.substring(0, sContentDiv.style.top.length - 2) - 165);
	}
	
	if(processDate(sId, false)){
		if(document.getElementById(sId).value != ''){
			if(document.getElementById(sId).value.substring(5, 7).charAt(0) == '0'){
				nTempMonth = document.getElementById(sId).value.substring(6, 7);
			}else{
				nTempMonth = document.getElementById(sId).value.substring(5, 7);
			}
			 nIdx = getCalendarIndex(sId);
			 
			if(nIdx == -1){
				//since there is only one calendar object that is shared for all dates, we must initialize virtual calendars
				aCalendars.push(new Array(sId, document.getElementById(sId).value.substring(0, 4), nTempMonth, '', 1));
			}else{
				aCalendars[nIdx][1] = document.getElementById(sId).value.substring(0, 4);
				aCalendars[nIdx][2] = nTempMonth;
			}
			nIdx = getCalendarIndex(sId);
			
			document.getElementById("CalendarMain_container").innerHTML = buildCalendar(document.getElementById(sId).value.substring(0, 4), nTempMonth, 1, sId, true, sGraphicsPath);
		}else{
			document.getElementById("CalendarMain_container").innerHTML = buildCalendar(dNow.getFullYear(), dNow.getMonth() + 1, 1, sId, true, sGraphicsPath);
		}
	}else{
		document.getElementById(sId).value = '';
		document.getElementById("CalendarMain_container").innerHTML = buildCalendar(dNow.getFullYear(), dNow.getMonth() + 1, 1, sId, true, sGraphicsPath);
	}
	
	sContentDiv.style.visibility = "visible";
	//used by div shim
	sContentDiv.style.display = "block";
	sContainerDiv.style.width = sContentDiv.offsetWidth;
	sContainerDiv.style.height = sContentDiv.offsetHeight;
	sContainerDiv.style.top = sContentDiv.style.top;
	sContainerDiv.style.left = sContentDiv.style.left;
	sContainerDiv.style.zIndex = sContentDiv.style.zIndex - 1;
	sContainerDiv.style.display = "block";
	//end div shim
	
	return false;
}

function closeCalendar(){
	if(typeof(sContentDiv.style) != "undefined"){
		sContentDiv.style.display = 'none';
		sContainerDiv.style.display = 'none';
	}
}
