/*==========================================
*           »ï¼ºÈ­Àç À¶ÀÚ½Ã½ºÅÛ
*==========================================
*  Program ID :Popup_calendar.js
*  Description: calendar¸¦ º¸¿©ÁÖ°í ¼±ÅÃÇÑ Date¿¡ ´ëÇÑ Formatting °á°ú¸¦
*                        window.opennerÀÇ returnObjct¿¡°Ô ¹ÝÈ¯ÇÔ
*  Author     : °­¼±Èñ
*  Date       : 2001/10/19
*  Ver	Date		Editor	Modification
*  1.0  2001/10/19	°­¼±Èñ	EIP version
*  1.1  2002/05/17	±è¿µ¿ì	À¶ÀÚ½Ã½ºÅÛ version
*  2008/08/20 À±Ã¶Èñ ³¯Â¥ Çü½Ä ¿¡·¯ ¼öÁ¤
*==========================================*/

var DATE_ONCLICK_FUNCTION = "window.opener.returnDate";

// --------------------------------------------------------------------------
//  Style Setting
//  ÇØ´ç ColorÅæ¿¡ ¸Â°Ô º¯°æ
// --------------------------------------------------------------------------
var FONT_SIZE_TITLE_HEADER	= "22pt";
var FONTCOLOR_TITLE_HEADER	= "#003399";

var FONT_SIZE_TITLE			= "9pt";
var FONTCOLOR_TITLE			= "#000000";

var BGCOLOR_CALENDAR	= "#F5F8F5";
var NORMAL_FONT	= "#F5F8F5";

var BGCOLOR_WEEK_END	= "#FFFFFF";
var BGCOLOR_NOMAL_DAY	= "#FFFFFF";

var BGCOLOR_TODAY		= "#F5F8F5";
var FONTCOLOR_TODAY 	= "#000099";

var FONT_FACE			= "±¼¸²";
var FONT_SIZE			= "9pt";

var HEADER_FONT_SIZE	= "9pt";
var BGCOLOR_HEADER		= "#FFFFFF";
var FONTCOLOR_HEADER	= "#F2931E";
var FONTCOLOR_SUNDAY	= "#F2931E";
var FONTCOLOR_SATDAY	= "#0092B7";

var FONTCOLOR_NEXTMONTH	= "#d5d5d5";
var FONT_SIZE_NEXTMONTH	= "7pt";
// ==================================================

// ---------------------------------------------------
//  Àü¿ªº¯¼ö
// isYearlyMonth = trueÀÏ °æ¿ì, ÇØ´ç¿ùÀÇ ºó Day¿¡
//					´ÙÀ½¿ùÀÇ ³¯Â¥¸¦ Ã¤¿ö³ÖÀ½
var WEEK_END = [0,6];
var TODAY = new Date();
var isYearlyMonth = false;
var openWinControl = self;
var curMonthTitle=null;
// ---------------------------------------------------

// ================================================================================================================
// Definication Calendar Obj
// (Âü°í 1) Calendar Object¿¡ ´ëÇÑ property ¼³Á¤ ºÎºÐÀÌ¹Ç·Î,
//			ÀÌ¿¡ ´ëÇÑ º¯°æÀ» ±ÝÇÕ´Ï´Ù..
Calendar.Months		= [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
Calendar.ShortMonths= [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
// ÀÏ¹ÝÀûÀÎ ³âÀÇ ¿ùº° ÀÏÀÚ ¼ö
Calendar.DOMonth	= [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
// ¿î³âÀÎ ³âÀÇ ¿ùº° ÀÏÀÚ ¼ö
Calendar.lDOMonth	= [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];


function Calendar( returnObject, curWinCtl, curMonth, curYear, dateFormat )
{
	if ( ( curMonth == null ) && ( curYear == null ) )	return;

	if ( curWinCtl == null )
	{
		this.cal_WinControl = openWinControl;
	}
	else
	{
		this.cal_WinControl = curWinCtl;
	}

	if ( curMonth == null )
	{
		this.cal_MonthName	= null;
		this.cal_Month		= null;
	}
	else
	{
		this.cal_MonthName	= Calendar.getMonthName( curMonth );
		this.cal_Month		= new Number( curMonth );
	}

	this.cal_Year			= curYear;
	this.cal_DateFormat		= dateFormat;
	this.cal_ReturnObject	= returnObject;
}

// ------------------------- Definication a Properties -------------------------------------
Calendar.getMonthName = Calendar_getMonthName;
Calendar.getShordMonthName = Calendar_getShordMonthName;
Calendar.getDaysofMonth = Calendar_getDaysofMonth;
Calendar.getMonthPos= Calendar_getMonthPos;

function Calendar_getMonthName( indexofMonth )
{
	return Calendar.Months[ indexofMonth ];
}

function Calendar_getShordMonthName( indexofMonth )
{
	return Calendar.ShortMonths[ indexofMonth ];
}

function Calendar_getDaysofMonth( indexofMonth, curYear )
{
	if ( ( curYear % 4 ) == 0 )
	{
		if ( ( curYear % 100 ) == 0
			&& (  curYear % 400 ) != 0 )
		{
			return Calendar.DOMonth[ indexofMonth ];
		}

		return Calendar.lDOMonth[ indexofMonth ];
	}
	else
	{
		return Calendar.DOMonth[ indexofMonth ];
	}
}

function Calendar_getMonthPos( curMonth, curYear, incr )
{
	var ret_arr = new Array();

	if ( incr == -1 )
	{
		if ( curMonth == 0 )
		{
			ret_arr[0] = 11;
			ret_arr[1] = parseInt( curYear ) - 1;
		}
		else
		{
			ret_arr[0] = parseInt( curMonth ) - 1;
			ret_arr[1] = parseInt( curYear );
		}
	}
	else if  ( incr == 1 )
	{
		if ( curMonth == 11 )
		{
			ret_arr[0] = 0;
			ret_arr[1] = parseInt( curYear ) + 1;
		}
		else
		{
			ret_arr[0] = parseInt( curMonth ) + 1;
			ret_arr[1] = parseInt( curYear );
		}
	}

	return ret_arr;
}
// ------------------------- End Definication a Properties -------------------------------------

//´Þ·ÂÀ» ±×¸®´Â µ¥ÀÌÅÍ ¼³Á¤
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function()
{
	var vCode			= "";
	var vHeader_Code	= "";
	var vData_Code		= "";

	// Begin Table Drawing code here..
	vCode = vCode + "<table width=100% align=center border=0 cellpadding=2 cellspacing=0>";
	vCode = vCode + "<tr><td colspan=7 height=5></td></tr>";

	vHeader_Code = this.cal_header();
	vData_Code = this.cal_date();

	vCode = vCode + vHeader_Code + vData_Code;
	vCode = vCode + "<tr><td colspan=7></td></tr>";

	vCode = vCode + "</table>";

	return vCode;
}
// ´Þ·ÂÀÇ header¸¦ ¼³Á¤ÇÑ´Ù....
Calendar.prototype.cal_header = function()
{
	var vCode = "";
	//Calendar Title ÁöÁ¤
	var style_string_sun = "font-family:" + FONT_FACE + ";font-size:" + HEADER_FONT_SIZE
						+ ";font-weight:bold;text-align:center"
						+ ";background-color:" + BGCOLOR_HEADER
						+ ";color:" + FONTCOLOR_HEADER;

	var style_string_gel = "font-family:" + FONT_FACE + ";font-size:" + HEADER_FONT_SIZE
						+ ";font-weight:bold;text-align:center"
						+ ";background-color:" + BGCOLOR_HEADER
						+ ";color:black";

	var style_string_sat = "font-family:" + FONT_FACE + ";font-size:" + HEADER_FONT_SIZE
						+ ";font-weight:bold;text-align:center"
						+ ";background-color:" + BGCOLOR_HEADER
						+ ";color:#0092B7";

	vCode = vCode + "<tr>";
	vCode = vCode + "<td height=20 width=22 style=\"" + style_string_sun + "\">ÀÏ</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_gel + "\">¿ù</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_gel + "\">È­</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_gel + "\">¼ö</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_gel + "\">¸ñ</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_gel + "\">±Ý</TD>";
	vCode = vCode + "<td width=22 style=\"" + style_string_sat + "\">Åä</TD>";
	vCode = vCode + "</tr>";

	return vCode;
}

// ´Þ·ÂÀÇ ÀÏÀÚÀ» ¼³Á¤ÇÑ´Ù.
Calendar.prototype.cal_date = function()
{
	var vDate = new Date();
	vDate.setDate( 1 );
	vDate.setMonth( this.cal_Month );
	vDate.setFullYear( this.cal_Year );
	//alert(this.cal_Year);

	var vFirstDay		= vDate.getDay();
	var vDay			= 1;
	var vLastDay		= Calendar.getDaysofMonth( this.cal_Month, this.cal_Year );
	var vOnLastDay		= 0;
	var vCode			= "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary.
	*/
	vCode = vCode + "<tr>";
	for ( i = 0; i < vFirstDay; i++ ) {
		vCode = vCode + "<td height=20 " + this.day_style_string( i ) + "></td>";
	}

	// Write rest of the 1st week
	for ( j = vFirstDay; j<7; j++ ) {

		vCode = vCode + "<td height=20 " + this.day_style_string( j ) + ">" +
						"<a href=\"javascript:"
							+ DATE_ONCLICK_FUNCTION + "('"
							+ this.cal_ReturnObject + "', '"
							+ this.format_date( vDay ) + "');window.close()\" " + this.day_style_string( j ) + ">" + 	this.format_day( vDay ) +
						"</a>" +
						"</td>";
		vDay = vDay + 1;
	}
	vCode = vCode + "</tr>";

	// Write the rest of the weeks
	for ( k = 2; k < 7; k++ ) {
		vCode = vCode + "<tr>";

		for ( j = 0; j < 7; j++ ) {

			vCode = vCode + "<td height=20 " + this.day_style_string( j ) + ">" +
							"<a href=\"javascript:"
							+ DATE_ONCLICK_FUNCTION + "('"
												+ this.cal_ReturnObject + "', '"
												+ this.format_date( vDay ) + "');window.close()\" " + this.day_style_string( j ) + ">" +
							this.format_day(vDay) +
							"</a>" +
							"</td>";

			vDay = vDay + 1;

			if ( vDay > vLastDay ) {
				vOnLastDay = 1;
				break;
			}
		}

		if ( j == 6 )
			vCode = vCode + "</tr>";
		if ( vOnLastDay == 1 )
			break;
	}

	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for ( m = 1; m < ( 7 - j ); m++) {
		if ( this.isYearlyMonth )
			vCode = vCode + "<td height=20 " + this.day_style_string(j+m) +"> </td>";
		else
			vCode = vCode + "<td height=20 " + this.day_style_string(j+m) +
			"><span style=\"font-size:" + FONT_SIZE_NEXTMONTH + ";color:"+ FONTCOLOR_NEXTMONTH + "\">" + m + "</FONT></td>";
	}

	return vCode;
}

Calendar.prototype.day_style_string = function(vday)
{
	var i;
	var SUNDAY_FONT	= "#F2931E";
	var SAT_FONT	= "#0092B7";

	var style_text_sun = "style=\"font-family:" + FONT_FACE + ";font-size:" + FONT_SIZE + ";text-align:center;background-color:" + BGCOLOR_NOMAL_DAY + ";color:#F2931E;\"" ;
	var style_text_sat = "style=\"font-family:" + FONT_FACE + ";font-size:" + FONT_SIZE + ";text-align:center;background-color:" + BGCOLOR_NOMAL_DAY + ";color:#0092B7;\"" ;
	var style_text_normal = "style=\"font-family:" + FONT_FACE + ";font-size:" + FONT_SIZE + ";text-align:center;background-color:" + BGCOLOR_NOMAL_DAY + ";color:black;\"" ;

	// Return special formatting for the weekend day.

	for ( i=0; i < WEEK_END.length; i++ ) {

		if (vday == WEEK_END[i])
		{
			if ( vday == 0 )
			{
				//return ( style_text + "color:" + SUNDAY_FONT + ";\"" );
				return ( style_text_sun);
			}
			else if ( vday == 6 )
			{
				//return ( style_text + "color:" + SAT_FONT + ";\"" );
				return ( style_text_sat );
			}
		}
	}
	//return ( style_text + "color:" + NORMAL_FONT + ";\"" );
	return ( style_text_normal );
}

Calendar.prototype.format_day = function( vday )
{
	var vNowDay = TODAY.getDate();
	var vNowMonth	= TODAY.getMonth();
	var vNowYear = TODAY.getFullYear();

	if ( vday == vNowDay && this.cal_Month == vNowMonth && this.cal_Year == vNowYear )
	{
		return ("<span style=\"background-color:" + BGCOLOR_TODAY + ";color:" + FONTCOLOR_TODAY + ";\"><b>" + vday + "</b></span>");
	}
	else
	{
		return ( vday );
	}
}

Calendar.prototype.cal_wirteln = function( writeText )
{
	this.cal_WinControl.document.writeln( writeText );
}

Calendar.prototype.cal_write = function( writeText )
{
	this.cal_WinControl.document.write( writeText );
}

Calendar.prototype.show = function()
{
	var vCode = "";

	this.cal_WinControl.document.open();

	// Setup the page...
	this.cal_wirteln("<html>");
	this.cal_wirteln("<head><title>Calendar</title>");
	this.cal_wirteln( "<meta content=\"text/html; charset=euc-kr\" http-equiv=\"Content-Type\">");
	this.cal_wirteln( "<link href=http://img.incruit.com/Css/incruit.css rel=stylesheet type=text/css>");
	this.cal_wirteln("</head>");
	this.cal_wirteln( "<body class=etcBody marginwidth=0 marginheight=0 topmargin=0>");

	this.cal_wirteln("<center><img src=/common/image/0.gif width=5 border=0 height=5><br>");
  	this.cal_wirteln("<table width=100% border=0 cellspacing=0 cellpadding=2>");
	this.cal_wirteln("<tr>");
	//this.cal_wirteln("<td align=left valign=bottom height=22 width=31><img src=/common/image/cal_title.gif></td>");
	this.cal_wirteln("<td align=center valign=bottom height=22 style=\"font-size: 10pt; color: #969641; font-weight: bold\" " + ">"+ TODAY.getFullYear().toString() + "³â" + curMonthTitle  + "¿ù" + "</td>");
	this.cal_wirteln("</tr>");
	this.cal_wirteln("</table>");
	this.cal_wirteln("  <table width=100% border=0 cellspacing=0 cellpadding=2>");
	this.cal_wirteln("<tr>");
	this.cal_wirteln("<td align=left valign=bottom height=3 bgcolor=#2AAB35>");
	this.cal_wirteln("</td>");
	this.cal_wirteln("</tr>");
	this.cal_wirteln("</table>");

	// Show navigation buttons
	var aryPrevYearMonth	= Calendar.getMonthPos( this.cal_Month, this.cal_Year, -1 );
	var prevShortMonthNameth			= aryPrevYearMonth[0];
	var prevYear			= aryPrevYearMonth[1];

	var aryNextYearMonth	= Calendar.getMonthPos( this.cal_Month, this.cal_Year, 1 );
	var nextMonth			= aryNextYearMonth[0];
	var nextYear			= aryNextYearMonth[1];

	this.cal_wirteln( "<table width=100% border=0 bgcolor=#CECFCE cellpadding=0 cellspacing=0>" );
	this.cal_wirteln( "<tr>" );
	this.cal_wirteln( "	<td bgcolor=#EFEFEF align=center height=18 width=50% style=\"font-family:" + FONT_FACE + ";font-size:" + FONT_SIZE_TITLE + ";color:" + FONTCOLOR_TITLE + ";font-weight:bold;text-align:center\"><a href=\"" +
						"javascript:window.opener.Build(" + " '" + this.cal_ReturnObject + "', '" + this.cal_Month + "', '" + ( parseInt( this.cal_Year ) - 1 ) + "', '" + this.cal_DateFormat + "'" +
						")\"><img src=http://img.incruit.com/Common/Icon/mset1_next.gif width=10 height=9 border=0><\/a>" );
	this.cal_wirteln(  this.cal_Year  );
	this.cal_wirteln( "<a href=\"" + "javascript:window.opener.Build(" + " '" + this.cal_ReturnObject + "', '" + this.cal_Month + "', '" + ( parseInt( this.cal_Year ) + 1 ) + "', '" + this.cal_DateFormat + "'" +
						")\"><img src=http://img.incruit.com/Common/Icon/mset1_next.gif width=10 height=9 border=0><\/a></td>" );

	this.cal_wirteln( "	<td bgcolor=#EFEFEF align=center width=50% height=18 style=\"font-family:" + FONT_FACE + ";font-size:" + FONT_SIZE_TITLE + ";color:" + FONTCOLOR_TITLE + ";font-weight:bold;text-align:center\"><a href=\"" +
						"javascript:window.opener.Build(" +
						" '" + this.cal_ReturnObject + "', '" + prevShortMonthNameth + "', '" + prevYear + "', '" + this.cal_DateFormat + "'" +
						")\"><img src=http://img.incruit.com/Common/Icon/mset1_prev.gif  width=10 height=9 border=0><\/a>" );
	this.cal_wirteln( ( parseInt( this.cal_Month ) + 1 ) + "¿ù"  );
	this.cal_wirteln( "<a href=\"" + "javascript:window.opener.Build(" +
						" '" + this.cal_ReturnObject + "', '" + nextMonth + "', '" + nextYear + "', '" + this.cal_DateFormat + "'" +
						")\"><img src=http://img.incruit.com/Common/Icon/mset1_next.gif width=10 height=9 border=0><\/a>" );
	this.cal_wirteln( "</td>" );
	this.cal_wirteln( "</tr>" );
	this.cal_wirteln( "</table>" );

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.cal_wirteln( vCode );

	this.cal_wirteln( "</font></center></body></html> ");
	document.close();
}

Calendar.prototype.format_date = function( curDay )
{
	var vData;

	var vMonth = 1 + this.cal_Month;
	vMonth = ( vMonth.toString().length < 2 ) ? "0" + vMonth : vMonth;
	var vShortMonthName = Calendar.getShordMonthName( this.cal_Month ).toUpperCase();
	var vFullMonthName = Calendar.getMonthName( this.cal_Month ).toUpperCase();

	var vFullYear = new String( this.cal_Year );
	var vYear = new String( this.cal_Year.substr( 2,2 ) );

	var vDay = ( curDay.toString().length < 2 ) ? "0" + curDay : curDay;

	switch ( this.cal_DateFormat )
	{
		case "YYYYMMDD"	:
			vData = vFullYear + vMonth+ vDay
			break;
		case "YYYY\/MM"	:
			vData = vFullYear + "\/" + vMonth
			break;
		case "YYYY\/MM\/DD"	:
			vData = vFullYear + "\/" + vMonth + "\/" + vDay
			break;
		case "YY\/MM\/DD"		:
			vData = vYear + "\/" + vMonth + "\/" + vDay
			break;
		case "YYYY-MM-DD"	:
			vData = vFullYear + "-" + vMonth + "-" + vDay
			break;
		case "YY-MM-DD"		:
			vData = vYear + "-" + vMonth + "-" + vDay
			break;
		case "DD\/MON\/YYYY" :
			vData = vDay + "\/" + vShortMonthName + "\/" + vFullYear;
			break;
		case "DD\/MON\/YY" :
			vData = vDay + "\/" + vShortMonthName + "\/" + vYear;
			break;
		case "DD-MON-YYYY" :
			vData = vDay + "-" + vShortMonthName + "-" + vFullYear;
			break;
		case "DD-MON-YY" :
			vData = vDay + "-" + vShortMonthName + "-" + vYear;
			break;
		case "DD\/MONTH\/YYYY" :
			vData = vDay + "\/" + vFullMonthName + "\/" + vFullYear;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDay + "\/" + vFullMonthName + "\/" + vYear;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDay + "-" + vFullMonthName + "-" + vFullYear;
			break;
		case "DD-MONTH-YY" :
			vData = vDay + "-" + vFullMonthName + "-" + vYear;
			break;

		case "DD\/MM\/YYYY" :
			vData = vDay + "\/" + vMonth + "\/" + vFullYear;
			break;
		case "DD\/MM\/YY" :
			vData = vDay + "\/" + vMonth + "\/" + vYear;
			break;
		case "DD-MM-YYYY" :
			vData = vDay + "-" + vMonth + "-" + vFullYear;
			break;
		case "DD-MM-YY" :
			vData = vDay + "-" + vMonth + "-" + vYear;
			break;
		default :
			vData = vFullYear + "\/" + vMonth + "\/" + vDay;
	}

	return vData;
}

function Build( returnObject, curMonth, curYear, dateFormat )
{ 
	var curWinCtl = openWinControl;
	oCalendar = new Calendar( returnObject, curWinCtl, curMonth, curYear, dateFormat );

	// Customize your Calendar here..
	oCalendar.gLinkColor="black";
	oCalendar.gTextColor="black";
	//alert('a');
	oCalendar.show();
}

function returnDate ( ReturnObjctName, returnFormatDate )
{
	var returnObject = eval( "document." + ReturnObjctName );
	
	returnObject.value = returnFormatDate;
	returnObject.focus();
}
// ============================================================


/*------------------------------------------------------------------------------------
Popup Calendar¸¦ ¿­¾îÁÝ´Ï´Ù.
Arqument :
	returnObject	: ¹ÝÈ¯¹ÞÀ» InputBox Name
	curDate 		: SystemDate
	dateFormat	: ¹ÝÈ¯¹ÞÀ» DateÀÇ format String
				  [ Default format - yyyy/mm/dd ]
-------------------------------------------------------------------------------------*/
function show_calendar()
{
	returnObject = arguments[0];

	currDate = arguments[1];
	
	curMonthTitle = currDate.substr(4,2);
	TODAY.setYear ( currDate.substr(0,4) );
	TODAY.setMonth ( Number( currDate.substr(4,2) ) - 1 );
	TODAY.setDate ( currDate.substr(6,2) );
	
	curMonth = new String( TODAY.getMonth() );
	curYear = new String( TODAY.getFullYear().toString() );
	
	if ( arguments[2] == null )
		dateFormat = "yyyy/MM/DD";
	else
		dateFormat = arguments[2];

	// Calendar Popup
	var top = arguments[3];
	var width = arguments[4];
	if(top==null) top = "100";
	if(width==null) width = "100";
	
	curWinCtl = window.open( "", "Calendar", "width=180,height=200,status=no,resizable=no,top="+top+",left="+width );
	curWinCtl.opener = self;
	openWinControl = curWinCtl;

	Build( returnObject, curMonth, curYear, dateFormat );
}
