/// <summary>
///		´Þ·Â Å¬·¡½º (V 0.0000002)
///  ¤·. »ç¿ë¿¹  ==> <input type=text value="2005-11-18" size="10" readonly style="cursor:hand;" onfocus="FnExeCalendar(this)">
///  ¤·. »ç¿ë¿¹2 ==> <input type=text value="2005-11-18" id="test1" size="10" class="BasicInput" maxlength="10">&nbsp;
///						<img src="<%=Config.IMAGE_URL%>/calendar.gif" border="0" align="absmiddle" width="16" height="15" onclick="FnExeCalendar(document.all.test1)">
///  ¤·. ±â´É
///		- ¿©·¯°³ÀÇ Calendar°¡ µû·Î µ¿ÀÛÇÔ
///		- combo box(<select>)¿¡ °¡¸®Áö ¾ÊÀ½
///		- Calendar°¡ left/right/bottom ÃÊ°ú½Ã ¾Ë¾Æ¼­ ¸ÂÃß¾î º¸¿©ÁÜ
///		- [ADD] ´Þ·Â¿¡¼­ Focus°¡ ¾ø¾îÁö¸é »ç¶óÁö°Ô ÇÔ
///     - IE 7.0¿¡¼­ Æ÷Ä¿½º ÀÒ¾î ¹ö¸®´Â°Í ¼öÁ¤µÊ
/// </summary>
/// <param name="sName">Calendar instance idx</param>
/// <param name="oInput">input text object</param>
function Calendar(sName, oInput)
{
	// Member Variable
	this.name			= sName;
	this.inputObj		= oInput;					// Input Text Object
	this.dt				= new Date();
	this.year			= this.dt.getFullYear();	// ³â
	this.month			= this.dt.getMonth();		// ¿ù (0 ~ 11, 1¿ù~12¿ù)
	this.date			= this.dt.getDate();		// ÀÏ
	this.day			= this.dt.getDay();			// ¿äÀÏ (0 ~ 6, ÀÏ~Åä)
	this.existDefault	= false;	// Input Text Object¿¡ ³¯Â¥°ªÀÌ ÀÖ´ÂÁö ¿©ºÎ(ÀÖÀ¸¸é ¼±ÅÃµÈ ³¯Â¥Ç¥½ÃÇÔ)
	this.defaultYear	= 0;		// Input Text Object¿¡ ³â
	this.defaultMonth	= 0;		// Input Text Object¿¡ ¿ù
	this.defaultDate	= 0;		// Input Text Object¿¡ ÀÏ

	// Method
	this.GetLastDate		= GetLastDate;		// ¸¶Áö¸· ³¯Â¥
	this.GetStartDay		= GetStartDay;		// ½ÃÀÛ¿äÀÏ
	this.GetLocalString		= GetLocalString;	// YYYY-MM-DDÇü½ÄÀÇ String¹ÝÈ¯
	this.SetYear			= SetYear;			// ³â°ª º¯°æ
	this.SetMonth			= SetMonth;			// ¿ù°ª º¯°æ (0~6, ÀÏ~Åä)
	this.SetDate			= SetDate;			// ÀÏ°ª º¯°æ
	this.SetChangeYear		= SetChangeYear;
	this.SetChangeMonth		= SetChangeMonth;
	this.SetSelectCalendar	= SetSelectCalendar;
	this.SetHideCalendar	= SetHideCalendar;
	this.PrtShowCalendar	= PrtShowCalendar;

	function GetLastDate()	{return new Date(this.year, this.month + 1, 0).getDate();}
	function GetStartDay()	{return new Date(this.year, this.month, 1).getDay();}
	function GetLocalString(nDate)
	{
		var sRet = "";
		sRet += (this.year < 10) ? "0" + this.year : "" + this.year;
		sRet += "-";
		sRet += (this.month + 1 < 10) ? "0" + (this.month + 1) : "" + (this.month + 1);
		sRet += "-";
		sRet += (nDate < 10) ? "0" + nDate : "" + nDate;
		return sRet;
	}
	function SetYear(nYear)		{this.year = nYear;}
	function SetMonth(nMonth)	{this.month = nMonth;}
	function SetDate(nDate)		{this.date = nDate;}
	function SetChangeYear(nYearGap)
	{
		this.SetYear(this.year + nYearGap);

		this.PrtShowCalendar();
	}
	function SetChangeMonth(nMonthGap)
	{
		if (this.month == 11 && nMonthGap > 0)	// 12¿ùÀÌ°í +ÀÏ¶§
		{
			this.SetYear(this.year + 1);
			this.SetMonth(0);
		}
		else if (this.month == 0 && nMonthGap < 0)	// 1¿ùÀÌ°í -ÀÏ¶§
		{
			this.SetYear(this.year - 1);
			this.SetMonth(11);
		}
		else
			this.SetMonth(this.month + nMonthGap);

		this.PrtShowCalendar();
	}
	function SetSelectCalendar(sSelectedDate)
	{
		if (typeof(this.inputObj) == "object")
		{
			this.inputObj.value = sSelectedDate;
			this.SetHideCalendar();
			this.inputObj.focus();  // Focus¸¦ ÁÖÁö ¾ÊÀ¸¸é Å¬¸¯ ÈÄ IE 7.0¿¡¼­ ¹öÆ°Å¬¸¯ÀÌ ¾ÈµÈ´Ù.
			//this.inputObj.select();
		}
	}
	function SetHideCalendar()
	{
		// Document¿¡¼­ ÇØ´çCalendarÀÇ IframeÀ» »èÁ¦ÇÔ
		var oCalendarTbl = eval("document.all.CALENDAR_ID_" + this.name);
		if (typeof(oCalendarTbl) == "object")
			document.body.removeChild(oCalendarTbl);
	}
	function PrtShowCalendar()
	{
		var oCalendarWrap = document.all.spnCalendar;
		var oDiv = oIframe = oTbl = oTbl2 = oTr = oTr2 = oTd = oTd2 = null;
		
		this.SetHideCalendar();	// Document¿¡¼­ ÇØ´çCalendarÀÇ IframeÀ» »èÁ¦ÇÔ
		
		oIframe = document.createElement ("<iframe scrolling=no frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0>")
		oIframe.style.position = "absolute";
		oIframe.style.pddding = "0 0 0 0";
		oIframe.id = "CALENDAR_ID_" + this.name;

		// document.body.scroll°ü·Ã Á¤º¸°ª(body¿¡ iframe objectÃâ·ÂÀü¿¡ ÇØ´ç°ªµéÀ» ¾òÀ½)
		var nBodyScrollWidth	= document.body.scrollWidth;
		var nBodyScrollHeight	= Math.max(document.body.scrollHeight, document.body.clientHeight);
		var nBodyScrollTop		= document.body.scrollTop;
		var nBodyScrollLeft		= document.body.scrollLeft;

		document.body.appendChild(oIframe);	// Document¿¡ IframeÃß°¡

		var oIframeFrame = self.frames["CALENDAR_ID_" + this.name];
		var oIfrmDocument = oIframeFrame.document;	// Iframe.Document
		oIfrmDocument.open();
		oIfrmDocument.write("<html><head></head><body style='margin:0'></body></html>");	// Iframe.Document.Body Ãß°¡
		oIfrmDocument.close();

		// table
		oTbl = oIfrmDocument.createElement("TABLE");	// Iframe.Document.Body¿¡ TableÃß°¡
		oTbl.style.filter	= "progid:DXImageTransform.Microsoft.Shadow(color=#666666, Direction=135, Strength=2)";
		oTbl.tableLayout	= "fixed";
		oTbl.cellPadding	= 0;
		oTbl.cellSpacing	= 0;
		oTbl.style.fontFamily = "Tahoma";
		oTbl.border = 0;
		oTbl.style.border = "1px solid #eeeeee";
		oTbl.style.background = "#FFFFFF";
		oTbl.id = "firstTable";

		//  tbody¿Í tr Ãß°¡
		oTr = oTbl.insertRow();
		oTr.style.height = 10;
		oTr.style.backgroundColor = "#eeeeee";

		//   td	(Year -)
		oTd = oTr.insertCell();
		oTd.style.width = "12px";
		oTd.style.cursor = "default";
		oTd.style.fontSize = "8pt";
		oTd.innerText = "¡ì";
		oTd.align = "center";
		oTd.onmouseover = new Function("this.style.background='highlight'; this.style.color='highlighttext'");
		oTd.onmouseout = new Function("this.style.background='#eeeeee'; this.style.color=''");
		oTd.onclick= new Function("oCalendarInstanceObj[" + this.name + "].SetChangeYear(-1);");
		oTd.ondblclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeYear(-1);");
		//   td (Month -)
		oTd = oTr.insertCell();
		oTd.style.width = "12px";
		oTd.style.cursor = "default";
		oTd.style.fontSize = "8pt";
		oTd.align = "center";
		oTd.innerText = "<";
		oTd.onmouseover = new Function("this.style.background='highlight'; this.style.color='highlighttext'");
		oTd.onmouseout = new Function("this.style.background='#eeeeee'; this.style.color=''");
		oTd.onclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeMonth(-1);");
		oTd.ondblclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeMonth(-1);");
		//   td (2005 12)
		oTd = oTr.insertCell();
		oTd.style.width = "63px";
		oTd.style.fontSize = "9pt";
		oTd.style.cursor = "default";
		oTd.align = "center";
		oTd.innerText = this.year + " " + (this.month + 1);
		//   td (Month +)
		oTd = oTr.insertCell();
		oTd.style.width = "12px";
		oTd.style.fontSize = "8pt";
		oTd.style.cursor = "default";
		oTd.align = "center";
		oTd.innerText = ">";
		oTd.onmouseover = new Function("this.style.background='highlight'; this.style.color='highlighttext'");
		oTd.onmouseout = new Function("this.style.background='#eeeeee'; this.style.color=''");
		oTd.onclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeMonth(+1);");
		oTd.ondblclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeMonth(+1); return (false);");
		//   td (Year +)
		oTd = oTr.insertCell();
		oTd.style.width = "12px";
		oTd.style.fontSize = "8pt";
		oTd.style.cursor = "default";
		oTd.align = "center";
		oTd.innerText = "¡í";
		oTd.onmouseover = new Function("this.style.background='highlight'; this.style.color='highlighttext'");
		oTd.onmouseout = new Function("this.style.background='#eeeeee'; this.style.color=''");
		oTd.onclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeYear(+1);");
		oTd.ondblclick = new Function("oCalendarInstanceObj[" + this.name + "].SetChangeYear(+1);");

		// tr
		oTr = oTbl.insertRow();
		//	td
		oTd = oTr.insertCell();
		oTd.colSpan = 6;
		oTd.align = "center";
		
		// table
		oTbl2 = oIfrmDocument.createElement("TABLE");
		oTbl2.border = 0;
		oTbl2.cellPadding = 0;
		oTbl2.cellSpacing = 2;
		oTbl2.tableLayout	= "fixed";
		oTbl2.style.fontFamily = "Tahoma";
		oTd.appendChild(oTbl2);
		
		// tr
		oTr2 = oTbl2.insertRow();
		oTr2.style.height = 10;
		//oTr2.onclick = new Function("oCalendarInstanceObj[" + this.name + "].SetHideCalendar();");
		
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "SU";
		oTd2.style.color = "darkred";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "MO";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "TU";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "WE";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "TH";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "FR";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;
		//   td
		oTd2 = oTr2.insertCell();
		oTd2.innerText = "SA";
		oTd2.style.color = "darkblue";
		oTd2.style.fontSize = "8pt";
		oTd2.style.width = 10;

		var nWeek			= 0;					// 'ÁÖ' ´ÜÀ§
		var nDate			= 1;					// 'ÀÏ' ´ÜÀ§
		var sTdFontColor	= "";					// 'ÀÏ'ÀÇ Font Color
		var sTdBackground	= "";					// 'ÀÏ' Background Color
		var oNowDate		= new NowDate();		// ÇöÀç½Ã°£ Object
		var nLastDate		= this.GetLastDate();	// ¸¶Áö¸· 'ÀÏ'
		var nStartDay		= this.GetStartDay();	// ½ÃÀÛ '¿äÀÏ'

		do
		{
			// tr
			oTr2 = oTbl2.insertRow();
			for (var nDay = 0; nDay <= 6; nDay++)
			{
				sTdFontColor	= "";
				sTdBackground	= "";
				//   td
				oTd2 = oTr2.insertCell();
				oTd2.style.cursor	= "default";
				oTd2.style.width	= 10;
				oTd2.style.height	= 10;
				oTd2.style.fontSize = "8pt";
				
				if ( (nWeek == 0 && this.GetStartDay() <= nDay) || (nWeek != 0 && nDate <= nLastDate) )	// ³¯Â¥°¡ º¸ÀÏ¶§
				{
					// ¿À´Ã³¯Â¥ÀÏ°æ¿ì
					if (this.year == oNowDate.year && this.month == oNowDate.month && this.date == nDate)
						oTd2.style.fontWeight = "bold";

					// ÀÏ¿äÀÏ or Åä¿äÀÏÀÎ °æ¿ì
					if (nDay == 0)
						sTdFontColor = "darkred";
					else if (nDay == 6)
						sTdFontColor = "darkblue";
					// Default³¯Â¥°ªÀÌ ÀÖ´Â°æ¿ì(input text Object¿¡ °ªÀÌ ÀÖ´Â°æ¿ì)
					if (this.existDefault && this.year == this.defaultYear && this.month == this.defaultMonth && nDate == this.defaultDate)
					{
						oTd2.style.background = "highlight";

						sTdBackground	= "highlight";
						sTdFontColor	= "highlighttext";
					}

					oTd2.style.color = sTdFontColor;
					oTd2.align = "center";
					oTd2.innerText = nDate.toString();
					oTd2.onmousedown= new Function("oCalendarInstanceObj[" + this.name + "].SetSelectCalendar('" + this.GetLocalString(nDate) + "')");
					oTd2.onmouseover= new Function("this.style.background='highlight'; this.style.color='highlighttext'");
					oTd2.onmouseout	= new Function("this.style.background='" + sTdBackground + "'; this.style.color='" + sTdFontColor + "'");

					nDate++;
				}
				else																					// ³¯Â¥°¡ Ç¥½Ã µÇÁö ¾ÊÀ»¶§
				{
					oTd2.innerHTML = "&nbsp;";
					//oTd2.onclick = new Function("oCalendarInstanceObj[" + this.name + "].SetHideCalendar();");
				}
			}
			nWeek++;	// 'ÁÖ'´ÜÀ§ Áõ°¡
		}while(nDate <= nLastDate);

		oIfrmDocument.body.appendChild(oTbl);	// Iframe¿¡ TableÃß°¡

		var oCalendarIframe = eval("document.all.CALENDAR_ID_" + this.name);

		// ºÎÇÏ°¡ Á» ÀÖ¾î¼­ Á÷Á¢ ÇØ´ç°ªÀ» ³ÖÀ½
		var nCalendarWidth	= 127;						//127 ==> oIfrmDocument.all.firstTable.offsetWidth;
		var nCalendarHeight	= (nWeek == 5) ? 110 : 125;	//125 ==> oIfrmDocument.all.firstTable.offsetHeight;
		oCalendarIframe.width	= nCalendarWidth;
		oCalendarIframe.height	= nCalendarHeight;

		var oInputPosition = this.inputObj.getBoundingClientRect();
		var nLeft = oInputPosition.left + nBodyScrollLeft + Number(this.inputObj.offsetWidth / 2) - Number(nCalendarWidth / 2);
		// LeftÃÊ°ú
		if (nLeft < 0)
			nLeft = 0;
		// right ÃÊ°ú
		else if ( nBodyScrollWidth < (oInputPosition.left + nBodyScrollLeft + Number(this.inputObj.offsetWidth / 2) + Number(nCalendarWidth / 2)) )
			nLeft = nBodyScrollWidth - nCalendarWidth + nBodyScrollLeft;
//Debug 
//window.status="inputTop[" + oInputPosition.top + "]bodyScrollTop[" + document.body.scrollTop + "]inputOffsetHeight[" + this.inputObj.offsetHeight + "]bodyScrollHeight[" + nBodyScrollHeight + "]";
		var nTop = oInputPosition.top + nBodyScrollTop + this.inputObj.offsetHeight;
		if ( nBodyScrollHeight < (nTop + nCalendarHeight) )	// Bottom ÃÊ°ú½Ã(body.clientº¸´Ù Å¬¶§)
		{
			if (oInputPosition.top + nBodyScrollTop >= nCalendarHeight)	// »ó´Ü¿¡ Calendar Height¸¸Å­ °ø°£ÀÌ ÀÖÀ¸¸é Input Object »ó´ÜÀ¸·Î Calendar ÀÌµ¿)
				nTop = oInputPosition.top + nBodyScrollTop - nCalendarHeight;
		}
		oCalendarIframe.style.left	= nLeft;
		oCalendarIframe.style.top	= nTop;
		oCalendarIframe.focus();
		oCalendarIframe.detachEvent("onblur", FnHideCalendar);
		oCalendarIframe.attachEvent("onblur", FnHideCalendar);
	}
	
	function NowDate()	// ¿À´Ã³¯Â¥ Å¬·¡½º
	{
		// Member Variable
		this.dt = new Date();
		this.year		= this.dt.getFullYear();	// ³â
		this.month		= this.dt.getMonth();		// ¿ù (0 ~ 11, 1¿ù~12¿ù)
		this.date		= this.dt.getDate();		// ÀÏ
		this.day		= this.dt.getDay();			// ¿äÀÏ (0 ~ 6, ÀÏ~Åä)
		this.lastDate	= new Date(this.year, this.month + 1, 0).getDate();	// ¸¶Áö¸· ³¯Â¥
		this.startDay	= new Date(this.year, this.month, 1).getDay();	// ½ÃÀÛ¿äÀÏ
	}
}
var nCalendarInstanceIdx = 0;			// Calendar Instance Idx
var oCalendarInstanceObj = new Array();	// Calendar Instanceµé
/// <summary>
///		Calendar instance¸¦ µ¿ÀûÀ¸·Î ¸¸µé°í º¸¿©ÁÜ
/// </summary>
/// <param name="oInput">input text object</param>
function FnExeCalendar(oInput)	// ¹öÆ°À» Å¬¸¯ÇßÀ»°æ¿ì
{    
	var strPattern = /^(\d{4})-([0-1]\d)-([0-3]\d)$/;	// YYYY-MM-DD
	var nYear = nMonth = nDate = 0;
	var oCalendar = null;
	
	if (typeof(oInput) != "object")
		return ;
	
	// ´Þ·Âµé ¸ðµÎ ¾ø¾Ö±â
	for (var i = 0; i < nCalendarInstanceIdx; i++)
		oCalendarInstanceObj[i].SetHideCalendar();

	if (typeof(oInput.uniqNumber) == "undefined")	// Ã³À½ ½ÇÇà½Ã
	{
		// Instance Ãß°¡
		oCalendarInstanceObj[nCalendarInstanceIdx] = new Calendar(nCalendarInstanceIdx.toString(), oInput);
		oCalendar = oCalendarInstanceObj[nCalendarInstanceIdx];

		// InputÀÇ uniqNumber¼Ó¼º¿¡ Calendar Instance IdxÃß°¡
		oInput.uniqNumber = nCalendarInstanceIdx.toString();

		// Calendar Instance IdxÁõ°¡
		nCalendarInstanceIdx++;
	}
	else											// ÀÌÈÄ ½ÇÇà½Ã
	{
		oCalendar = oCalendarInstanceObj[Number(oInput.uniqNumber)];
	}
	
	if (strPattern.test(oInput.value))	// input text object¿¡ ³¯Â¥Çü½ÄÀÇ °ªÀÌ ÀÖÀ»°æ¿ì
	{
		nYear	= Number(oInput.value.substring(0, 4));
		nMonth	= Number(oInput.value.substring(5, 7));
		nDate	= Number(oInput.value.substring(8, 10));
		
		oCalendar.SetYear(nYear);
		oCalendar.SetMonth(nMonth - 1);	// 0 ~ 11

		oCalendar.existDefault = true;
		oCalendar.defaultYear	= nYear;
		oCalendar.defaultMonth	= nMonth - 1;
		oCalendar.defaultDate	= nDate;
	}
	else
		oCalendar.existDefault = false;

	oCalendar.PrtShowCalendar();
}
function FnHideCalendar()
{
	for (var i = 0; i < nCalendarInstanceIdx; i++)
		oCalendarInstanceObj[i].SetHideCalendar();
	//oCalendarIframe.detachEvent("onblur", FnHideCalendar);
	//document.body.detachEvent ("onfocus", FnHideCalendar);
}