// Javascript Calendar
// MiniCal([ÀÏÀÚÀÔ·Â TextÆû]);

//var ImgPrefix = "/images/common/calendar/";

var now_date = new Date();
var now_year = now_date.getYear();
var now_mon = set_zero(now_date.getMonth()+1);
var now_day = set_zero(now_date.getDate());
if( now_year<1900) now_year=now_year+1900;

var month_name = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
var day_num=new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var day_name   = new Array('ÀÏ','¿ù','È­','¼ö','¸ñ','±Ý','Åä');

var calendarPopup;

var calendarDelay = 50;		//delay before menu appears
var calendarSpeed = 5;		//speed which menu appears (lower=faster)

var calendarWidth = 154;
var calendarHeight = 161;

var scriptId;
var dateType = "YYYY-MM-DD";

var calendarXPos = 0;
var calendarYPos = 0;



function hover(on, el)
{
	if (el && el.nodeName == "TD"){
		if (el.id == '') return;
		if (on){
			el.style.background = '#E7E7E7';
		}
		else{
			el.style.background = '#FFFFFF';
		}
	}
}

function selectDate(year, mon, el)
{
	if (el && el.nodeName == "TD")
	{
		if (el.id == "") return;
		return_date(year, mon, el.id);
	}
}

function set_zero(str)
{
	str = "" + str;
	if (str.length == 1) str = "0" + str;
	return str;
}

function replace_date(str, old_char, new_char)
{
	if( str == null || str == "" ) return;
	else
	{
		var fromindex = 0;
		var temp = "";
		for(var i=0 ; i<str.length ; i++)
		{
			fromindex = i;
			pos = str.indexOf(old_char, fromindex);
			if( pos != -1 )
			{
				temp = str.substring(0,pos) + new_char + str.substring(pos+old_char.length);
				str = temp;
				i = pos+new_char.length-1;
			} else break;
		}
		return str;
	}
}

function return_date(iyear, imonth, iday)
{
	if(iyear < 1900) iyear = 1900 + iyear;
	
	imonth = set_zero(imonth);
	iday = set_zero(iday);

	input_date(iyear, imonth, iday);
}

function input_date(iyear, imonth, iday)
{
	if(iyear < 1900) iyear = 1900 + iyear;

	var realDate = dateType.toUpperCase();

	if(realDate.indexOf("YYYY") != -1) realDate = replace_date(realDate, "YYYY", iyear);
	else if (realDate.indexOf("YY") != -1) realDate = replace_date(realDate, "YY", iyear.toString().substr(2,2));

	if (realDate.indexOf("MM") != -1) realDate=replace_date(realDate, "MM", set_zero(imonth));
	else if (realDate.indexOf("M") != -1) realDate=replace_date(realDate, "M", imonth);

	if(realDate.indexOf("DD") != -1) realDate = replace_date(realDate, "DD", set_zero(iday));
	else if (realDate.indexOf("D") != -1) realDate = replace_date(realDate, "D", iday);

	setCalDate(scriptId, realDate);
    closePopup();
}

function setCalDate(target, str)
{
	eval(target).value = str;
}

function get_date()
{
	var year = now_year;
	var mon = now_mon;
	var day = now_day;
	
	getDate  = getCalDate(scriptId);
	typeDate = dateType.toUpperCase();
	
	if (getDate.length == typeDate.length)
	{
		if(typeDate.indexOf("YYYY") != -1){
			year = getDate.substr(typeDate.indexOf("YYYY"), 4);
		}
		else if(typeDate.indexOf("YY") != -1){
			year = getDate.substr(typeDate.indexOf("YY"), 2);
		}

		if(typeDate.indexOf("MM") != -1){
			mon = getDate.substr(typeDate.indexOf("MM"), 2);
		}
		else if(typeDate.indexOf("M") != -1){
			mon = getDate.substr(typeDate.indexOf("M"), 1);
		}
		
		if(typeDate.indexOf("DD") != -1){
			day = getDate.substr(typeDate.indexOf("DD"), 2);
		}
		else if(typeDate.indexOf("D") != -1){
			day = getDate.substr(typeDate.indexOf("D"), 1);
		}
	}

	showCalendarPopup(year, mon, day);
}

function getCalDate(target)
{
	return eval(target).value;
}


function make_calendar(year, mon, day)
{
	var isIE = false;
	var Tag="";
	
	year = parseInt(year, 10);
	mon = parseInt(mon, 10);
	day = parseInt(day, 10);

    if(window.navigator.appName == "Microsoft Internet Explorer" &&
		window.navigator.appVersion.substring(window.navigator.appVersion.indexOf("MSIE") + 5, window.navigator.appVersion.indexOf("MSIE") + 8) >= 5.5){
		isIE = true;
	}
	
	if(isIE)
	{
		if (mon == 0) { year = year - 1; mon = 12; }
		else if (mon == 13) { year = year + 1; mon = 1; }

		// À±³â È®ÀÎ
    	if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) day_num[1]=29;

    	var first = new Date(year, mon-1, 1);
    	firstday = first.getDay()+1;
    	DaysInMonth = day_num[mon-1];

    	if ((mon-2) == -1) DaysInBefMonth = day_num[11];
    	else DaysInBefMonth = day_num[mon-2];

		if (day > DaysInMonth) day = DaysInMonth;

    	Tag += "<TABLE cellpadding=2 cellspacing=0 border=0 style='background-color:#EFE7E7;border:solid 1px #CCCCCC;TABLE-layout:fixed'>\n";
		Tag += "<TR height='"+(calendarHeight-2)+"'><TD align=center valign='top'>\n";

    	Tag += "<TABLE cellpadding=0 cellspacing=0 border=0>\n";
		Tag += "<TR height='"+(calendarHeight-6)+"'><TD align=center valign='top' style='background-color:#FFFFFF'>\n";

		Tag += "<TABLE cellspacing='0' cellpadding='0' border='0'";
		Tag += "OnMouseOver='parent.hover(true, window.event.srcElement)' OnMouseOut='parent.hover(false, window.event.srcElement)' OnClick='parent.selectDate("+year+","+mon+", window.event.srcElement)'>";
		Tag += "<tr><td align=center colspan=7 height='25'>\n";
		
		Tag += "<TABLE cellspacing='0' cellpadding='0' border='0' width=95% align='center'><TR><TD width='65%'>";
		Tag += "<font style='font-size:9pt;font-family:tahoma; font-weight:bold; vertical-align:middle;'>";
/*
		Tag += "<img src='"+ImgPrefix+"arrow_left.gif' width=9 height=11 align=absmiddle border=0 OnClick='parent.showCalendarPopup("+(year-1)+","+mon+","+day+");' style='cursor:hand'>"+year;
		Tag += "<img src='"+ImgPrefix+"arrow_right.gif' width=9 height=11 align=absmiddle border=0 OnClick='parent.showCalendarPopup("+(year+1)+","+mon+","+day+");' style='cursor:hand'>";
		Tag += "&nbsp;";
		Tag += "<img src='"+ImgPrefix+"arrow_left.gif' width=9 height=11 align=absmiddle border=0 OnClick='parent.showCalendarPopup("+year+","+(mon-1)+","+day+");' style='cursor:hand'>"+set_zero(mon);
		Tag += "<img src='"+ImgPrefix+"arrow_right.gif' width=9 height=11 align=absmiddle border=0 OnClick='parent.showCalendarPopup("+year+","+(mon+1)+","+day+");' style='cursor:hand'>";
*/
		Tag += "<span OnClick='parent.showCalendarPopup("+(year-1)+","+mon+","+day+");' style='font-size:9;color:585858;cursor:hand'>&lt;</span>"+year;
		Tag += "<span OnClick='parent.showCalendarPopup("+(year+1)+","+mon+","+day+");' style='font-size:9;color:585858;cursor:hand'>&gt;</span>";
		Tag += "&nbsp;";
		Tag += "<span OnClick='parent.showCalendarPopup("+year+","+(mon-1)+","+day+");' style='font-size:9;color:585858;cursor:hand'>&lt;</span>"+set_zero(mon);
		Tag += "<span OnClick='parent.showCalendarPopup("+year+","+(mon+1)+","+day+");' style='font-size:9;color:585858;cursor:hand'>&gt;</span>";
	
		Tag += "</font><span style='font-size:8px'>&nbsp;</span>";

		Tag += "</TD><TD width='35%' align=right>";

		Tag += "<span style='font-size:8pt; font-weight:bold; font-family:tahoma; color:CC0000; text-decoration:none; cursor:hand' onClick='parent.showCalendarPopup("+now_year+","+now_mon+","+now_day+");' >Today</span>";

		Tag += "</TD></TR></TABLE>";

    	Tag += "</td></tr>\n";
		
		Tag += "<TR height='20' bgcolor='#F4F4F4' align='center'>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#FF0000;'>"+day_name[0]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#000000;'>"+day_name[1]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#000000'>"+day_name[2]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#000000'>"+day_name[3]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#000000'>"+day_name[4]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#000000'>"+day_name[5]+"</TD>\n";
		Tag += "<TD width='22' style='font-size:9pt;color:#0000FF'>"+day_name[6]+"</TD>\n";
		Tag += "</TR><TR height='20'>\n";
		
		var column = 0;
		for (i=1 ; i <= (firstday-1);i++){
			Tag += "<TD align=center style='font-size:9pt;' width='18'></TD>\n";
			++column;
		}
		
		for( i=1 ; i<= DaysInMonth;i++){
			if(year == now_year && mon == now_mon && i == now_day){
				Tag += "<TD align=center style='font-size:9pt; font-family:tahoma; color:#CE0000; font-weight:bold; cursor:hand'";
			}
			else if(column ==0){
				Tag += "<TD align=center style='font-size:9pt; font-family:tahoma; color:#FF0000; cursor:hand'";
			}
    		else if(column ==6){
				Tag += "<TD align=center style='font-size:9pt; font-family:tahoma; color:#0000FF; cursor:hand'";
			}
    		else{
				Tag += "<TD align=center style='font-size:9pt; font-family:tahoma; cursor:hand'";
			}

			Tag += " id='"+i+"'>"+i+"</TD>\n";

			++column;
			
			if(column == 7 && i < DaysInMonth){
				Tag += "</TR><TR height='18'>\n";
				column = 0
			}
		}
			
		if((column > 0) && (column < 7)){
			for (i=1 ;i <= (7-column) ; i++)
				Tag += "<TD align=center style='font-size:9pt;'></TD>\n";
		}
		
		Tag += "</TD></TR></TABLE>\n";
		Tag += "</TD></TR></TABLE>\n";
		
		calendarPopup = window.createPopup();
		calendarPopup.document.body.innerHTML = Tag;
	}
}

function openMenu(height, width)
{
    iHeight = height;
    iWidth = width;


    if(iHeight < calendarHeight)
    {
        menuTimer = setTimeout("openMenu(iHeight + menuYIncrement, iWidth + menuXIncrement)", 1);
    }
    else
    {
        calendarPopup.show(calendarXPos, calendarYPos, iWidth, iHeight, document.body);
        clearTimeout(menuTimer);
    }
}

function closePopup()
{
    if(calendarPopup != null){
        calendarPopup.hide();
    }
}

function showCalendarPopup(sYear, sMonth, sDay)
{
	make_calendar(sYear, sMonth, sDay);
	
	menuXIncrement = calendarWidth / calendarSpeed;
	menuYIncrement = calendarHeight / calendarSpeed;
	
	menuTimer = setTimeout("openMenu(0,0)", calendarDelay);
	
	return false;
}

// Calendar Main Function
function MiniCal(scriptId)
{
	if(window.navigator.appName == "Microsoft Internet Explorer" && 
		window.navigator.appVersion.substring(window.navigator.appVersion.indexOf("MSIE") + 5, window.navigator.appVersion.indexOf("MSIE") + 8) >= 5.5){
	}
	else{
    	return alert("Version of Internet Explorer must higher than 5.5");
	}

	this.scriptId = scriptId;
	
	calendarXPos = parseInt(event.clientX);
	calendarYPos = parseInt(event.clientY);
	
	get_date();
}

// ³¯ÀÚ ±â°£ °è»ê
function InputDate(sdate, edate, type, term) {
	today = new Date();
	now_year = today.getYear();
	now_month = today.getMonth()+1;
	now_day = today.getDate();

	month_temp = now_month-1;
	day_temp = GetLastDay(now_year, month_temp);

	the_day = now_day;
	the_month = now_month;
	the_year = now_year;

	if(type=='D') {
		opt_day = now_day-term;
		if(opt_day>0) {
			the_day = opt_day;
		}
		else {
			opt_month = now_month-1;
			the_day = day_temp+opt_day;
			if(opt_month>0) {
				the_month = opt_month;
			}
			else {
				the_year = now_year-1;
				the_month = 12;
			}
		}
	}
	else if(type == 'M') {
		opt_month = now_month-term;
		if(opt_month>0) {
			the_month = opt_month;
		}
		else {
			the_year = now_year-1;
			the_month = 12+opt_month;
		}
	}
	else if(type=='Y'){
		the_year = now_year-term;
	}

	the_ymLastDay = GetLastDay(the_year, the_month);
	if (the_ymLastDay < the_day) the_day = the_ymLastDay;

	if(the_month<10) the_month='0'+the_month;
	if(the_day<10) the_day = '0'+the_day;

	the_date = the_year+'-'+the_month+'-'+the_day;
	sdate.value = the_date;

	if(now_month<10) now_month = '0'+now_month;
	if(now_day<10) now_day = '0'+now_day;

	now_date = now_year+'-'+now_month+'-'+now_day;
	edate.value = now_date;
	
	if(type == 'W'){
		sdate.value='';
		edate.value='';
	}
}

// ¿ùº° ÀÏÀÚ¼ö ÃßÃâ
function GetLastDay(year, mon) {
	var last_day = 31;

	switch(mon) {
		case(1): last_day=31; break;
		case(2):
			// À±³â È®ÀÎ
			if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
				last_day=29;
			}
			else{
				last_day=28;
			}
			break;
		case(3): last_day=31; break;
		case(4): last_day=30; break;
		case(5): last_day=31; break;
		case(6): last_day=30; break;
		case(7): last_day=31; break;
		case(8): last_day=31; break;
		case(9): last_day=30; break;
		case(10): last_day=31; break;
		case(11): last_day=30; break;
		case(12): last_day=31; break;
		default: last_day=31; break;
	}

	return last_day;
}

