
var m_fSetDateFunction = null;
var m_oElement = null;
var m_cElementValue= '';
var m_cDateFormat = 'mmddyy';
var m_cDateSeperator = '/';
var m_nDayOfWeekStart = 0;
var m_nYearMin = 1601;
var m_nYearMax = 4500;
var m_nYearY2kMin = 1990;
var m_nMonth = 0;
var m_nDay = 0;
var m_nYear = 0;
var m_nMonthDayCountArray = new Array(12);
m_nMonthDayCountArray[0] = 31;
m_nMonthDayCountArray[1] = 28;
m_nMonthDayCountArray[2] = 31;
m_nMonthDayCountArray[3] = 30;
m_nMonthDayCountArray[4] = 31;
m_nMonthDayCountArray[5] = 30;
m_nMonthDayCountArray[6] = 31;
m_nMonthDayCountArray[7] = 31;
m_nMonthDayCountArray[8] = 30;
m_nMonthDayCountArray[9] = 31;
m_nMonthDayCountArray[10] = 30;
m_nMonthDayCountArray[11] = 31;

//- GetDayOfWeekStart -//
function GetDayOfWeekStart() {
	return m_nDayOfWeekStart;
}

//- GetInputDate -//
function GetInputDate(cText, cDateFormat) {
	var nTextLength = cText.length;
	if (nTextLength == 0) {
		return false;
	}
	var cSp = '\0';
	var sSp1 = "";
	var sSp2 = "";
	for (var hText = 0; hText < cText.length; hText++) {
		var c = cText.charAt(hText);
		if ((c == ' ') || (IsDigit(c) == true)) {
			continue;
		} else if ((cSp == '\0') && ((c == '/') || (c == '-') || (c == '.'))) {
			cSp = c;
			sSp1 = cText.substring(hText + 1, nTextLength);
		} else if (c == cSp) {
			sSp2 = cText.substring(hText + 1, nTextLength);
		} else if (c != cSp) {
			return false;
		}
	}
	if (sSp1.length == 0) {
		return false;
	}
	var nMonth;
	var nDay;
	var nYear; 
	if (cDateFormat == 'mmddyy') {
		nMonth = AToI(cText);
		nDay = AToI(sSp1);
		if (sSp2.length != 0) {
			nYear = AToI(sSp2);
		} else {
			nYear = DefaultYear(nMonth, nDay);
		}
	} else if (cDateFormat == 'ddmmyy') {
		nMonth = AToI(sSp1);
		nDay = AToI(cText);
		if (sSp2.length != 0) {
			nYear = AToI(sSp2);
		} else {
			nYear = DefaultYear(nMonth, nDay);
		}
	} else {
		if (sSp2.length == 0) {
			return false;
		}
		nMonth = AToI(sSp1);
		nDay = AToI(sSp2);
		nYear = AToI(cText);
	}
	if (nYear < 100) {
		nYear = 1900 + nYear;
		while (nYear < m_nYearY2kMin) {
			nYear = nYear + 100;
		}
	}
	if ((nYear < m_nYearMin) || (nYear > m_nYearMax) || (nMonth < 1) || (nMonth > 12)) {
		return false;
	}
	if	((nDay < 1) || (nDay > GetDayCount(nMonth, nYear))) {
		return false;
	}
	m_nMonth = nMonth;
	m_nDay = nDay;
	m_nYear = nYear;
	return true;
}

//- DefaultYear -//
function DefaultYear(nMonth, nDay) {
	var oDate = new Date();
	var nCurrentYear = (oDate.getYear() < 1000 ? (1900 + oDate.getYear()) : oDate.getYear());
	if (((nMonth - 1) < oDate.getMonth()) || (((nMonth - 1) == oDate.getMonth()) && (nDay < oDate.getDate()))) {
		return 1 + nCurrentYear;
	} else {
		return nCurrentYear;
	}
}

//- AToI -//
function AToI(cText) {
	var nValue = 0;
	for (var hText = 0; hText < cText.length; hText++) {
		var c = cText.charAt(hText);
		if (IsDigit(c) == false) {
			return nValue;
		} else {
			nValue = (nValue * 10) + (c - '0');
		}
	}
	return nValue;
}

//- IsDigit -//
function IsDigit(c) {
	if ((c >= '0') && (c <= '9')) {
		return true;
	}
	return false;
}

//- GetDayCount -//
function GetDayCount(nMonth, nYear) {
	var nDayCount = m_nMonthDayCountArray[nMonth - 1];
	if ((nMonth == 2) && (IsLeapYear(nYear) == true)) {
		nDayCount++;
	}
	return nDayCount;
}

//- IsLeapYear -//
function IsLeapYear(nYear) {
	if (((nYear % 4) == 0) && (((nYear % 100) != 0) || ((nYear % 400) == 0))) {
		return true;
	}
	return false;
}

//- ShowCalendar -//
function ShowCalendar(oElementAnchor, oElement, oElement2, cDateMin, cDateMax, fSetDateFunction) {
   var oFrameDocument = document.all.DateChooserFrame;
	var oFrameWindow = window.frames.DateChooserFrame;
	//+
	if ((oFrameWindow.IsLoaded == null) || (oFrameWindow.IsLoaded == false)) {
		alert("Unable to load popup calendar.\r\nPlease reload the page.");
		return;
	}
	oFrameWindow.SetMinMax(new Date(cDateMin), new Date(cDateMax));
	m_fSetDateFunction = fSetDateFunction;
	//+
	if ((oElement == m_oElement) && (oFrameDocument.style.display == 'block')) {
		//+ already opened
		if ((m_cElementValue != oElement.value) && (GetInputDate(oElement.value, m_cDateFormat) == true)) {
			oFrameWindow.SetInputDate(m_nDay, m_nMonth, m_nYear);
			oFrameWindow.SetDate(m_nDay, m_nMonth, m_nYear);
			m_cElementValue = oElement.value;
		} else {
			oFrameDocument.style.display = 'none';
		}
	} else {
		//+ first time
		if (GetInputDate(oElement.value, m_cDateFormat) == true) {
			oFrameWindow.SetInputDate(m_nDay, m_nMonth, m_nYear);
			oFrameWindow.SetDate(m_nDay, m_nMonth, m_nYear);
		} else if ((oElement2 != null) && (GetInputDate(oElement2.value, m_cDateFormat) == true)) {
			oFrameWindow.SetInputDate(m_nDay, m_nMonth, m_nYear);
			oFrameWindow.SetDate(m_nDay, m_nMonth, m_nYear);
		} else {
			var oDate = new Date(); //: (cDateMin);
			oFrameWindow.SetInputDate(-1, -1, -1);
			oFrameWindow.SetDate(oDate.getDate(), oDate.getMonth() + 1, oDate.getFullYear());
		}
		//+
		var nLeft = 0;
		var nTop = 0;
		for (var o = oElementAnchor; (o != null) && (o.tagName !='BODY'); o = o.offsetParent) {
			nLeft += o.offsetLeft;
			nTop += o.offsetTop;
		}
		var nHeight = oElementAnchor.offsetHeight;
		var nHeight2 = oFrameDocument.style.pixelHeight;
		var nTop2 = document.body.scrollTop;
		oFrameDocument.style.left = nLeft;
		nTop += 4;
		if (((nTop - nHeight2) >= nTop2) && ((nTop + nHeight + nHeight2) > (document.body.clientHeight + nTop2))) {			
			oFrameDocument.style.top = nTop - nHeight2;
		} else {
			oFrameDocument.style.top = nTop + nHeight;
		}
		if (oFrameDocument.style.display == 'none') {
			oFrameDocument.style.display = 'block';
		}
		m_oElement = oElement;
		m_cElementValue = oElement.value;
	}
}

//- SetDate -//
function SetDate(nDay, nMonth, nYear) {
	var cDateSeperator = m_cDateSeperator;
	m_oElement.focus();
	if (m_cDateFormat == 'mmddyy') {
		m_oElement.value = nMonth + cDateSeperator + nDay + cDateSeperator + nYear;
	} else if (m_cDateFormat == 'ddmmyy') {
		m_oElement.value = nDay + cDateSeperator + nMonth + cDateSeperator + nYear;
	} else {
		m_oElement.value = nYear + cDateSeperator + nMonth + cDateSeperator + nDay;
	}
	m_cElementValue = m_oElement.value;
	if ((m_fSetDateFunction != null) && (m_fSetDateFunction != '')) {
		eval(m_fSetDateFunction);
	}
}

//- GetDayOfWeek -//
function GetDayOfWeek(nDay, nMonth, nYear) {
	var oDate = new Date(nYear, nMonth - 1, nDay);
	return (oDate.getDay() + (7 - m_nDayOfWeekStart)) % 7;
}

//- LoadMonths -//
function LoadMonth(nMonthCount) {
	var oDate = new Date();
	var nMonth = oDate.getMonth() + 1;
	var nYear = oDate.getFullYear();
	var cMonthArray = new Array(nMonthCount);
	for (var hMonth = 0; hMonth < nMonthCount; hMonth++) {
		cMonthArray[hMonth] = document.createElement('IMG');
		cMonthArray[hMonth].src = '/_PROJECT/_Atom/DateChooser/Month/W' + GetDayOfWeek(1, nMonth, nYear) + 'D' + GetDayCount(nMonth, nYear) + '.gif';
		nMonth++;
		if (nMonth > 12) {
			nMonth = 1;
			nYear++;
		}
	}
}
//+
LoadMonth(12);

function AddDay(oFieldFrom, oFieldTo, nDay) {
	var d = new Date(oFieldFrom.value);
	d.setTime(d.getTime() + (1000 * 60 * 60 * 24 * nDay));
	oFieldTo.value = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getYear();
}