/***********************************************************************************
	사용 방법

<input type="text" id="sdate" size="8" maxlength="8" value=""/>
<img src="/images/btn_cal.gif" align="absmiddle" Onclick="init_cal(document.getElementById('sdate'), 0 )"/>
-
<input type="text" id="edate" size="8" maxlength="8" value=""/>
<img src="/images/btn_cal.gif" align="absmiddle" Onclick="init_cal(document.getElementById('enddate'), 0 )"/>
	
************************************************************************************/
var _year = 0 , _month = 0 , _day = 0;
var _hour = "00" , _min = "00";
var _time_f = 1;
var _mth_str = new Array('S','M','T','W','T','F','S');
var sel_obj = null;
var mf = null;
var _locale = null;

function init_cal (th, time_f, locale)
{
	_locale = locale;

	if( mf == null )
		makeIframe(0,0);
	var x,y;

	if ( _year == 0 || _month == 0 || _day == 0 ) {
		cur_date = new Date();

		_year = cur_date.getYear();
		_month = cur_date.getMonth() + 1
		_day = cur_date.getDate();

		_hour = cur_date.getHours()
		_min =  cur_date.getMinutes()
	}


	x = window.event.clientX + document.body.scrollLeft+ 10 ;
	y = window.event.clientY + document.body.scrollTop- 50 ;
	
	var div_calendar = document.getElementById("div_calendar");
	
	div_calendar.style.pixelTop	= y-0;
	div_calendar.style.pixelLeft = x;

	mf.style.left = x;
	mf.style.top =  y;

	sel_obj =  th
	if (time_f ==1 ) {
		_time_f = 1;
	} else {
		_time_f = 0;
	}


	show_cal(_year, _month, _day, _hour, _min, locale)

}


function re_cal ( el )
{

	if (el == -2) {
		_year--;
	} else if ( el == -1) {
		if (_month == 1) {
			_year--;
			_month = 12;
		} else {
			_month--;
		}
	} else if (el == 1 ) {
		if (_month == 12) {
			_year++;
			_month = 1;
		} else {
		_month++;
		}
	} else if (el ==2 ) {
		_year++;
	} else {
		return ;
	}

	show_cal(_year, _month, _day, _hour, _min );
}

var stime

function mouse_over() {
	var el = window.event.srcElement;
	sel_day = el.title;

	if (sel_day.length > 7) {
		el.style.backgroundColor='#FFFF00';
	}

	window.clearTimeout(stime);
}

function mouse_click() {
	sel_day = window.event.srcElement.title;
	if (sel_day.length > 7) {
		if (sel_obj != null ) {
			tmp  = sel_day;
			if (_time_f ==1 ){
				tmp  += "-" + document.getElementById("cal_sel_hour").value + "" + document.getElementById("cal_sel_min").value  + "01"
			}
			sel_obj.value = tmp;
			var div_calendar = document.getElementById("div_calendar");
			div_calendar.style.visibility='hidden';
			mf.style.visibility='hidden';
		}
		
		var mfs = document.getElementsByName("calendarObj");
		for(var i=0; i<mfs.length;i++){
			document.body.removeChild(mfs[i]);
		}
	}
}

function mouse_out() {
	var el = window.event.fromElement;
	sel_day = el.title;

	if (sel_day.length > 7) {
		if ( el.className  =='css_sun') {
			el.style.backgroundColor = '#dfddca'; //'#FFDFDF';
		} else if ( el.className =='css_sat' ) {
			el.style.backgroundColor = '#dfddca'; //'#DDF1FF';
		} else {
			el.style.backgroundColor = '#FFFFFF';
		}
	}

	if ( el.tagName != "SELECT" ) {
		// stime=window.setTimeout("div_calendar.style.visibility='hidden';", 300);
	}
}

function is_leap_year(year){
	if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
		return 1
	} else {
		return -1
	}
}

function get_last_day(e_year, e_month)
{
	rtval = 31;
	tp1 = e_year % 4;
	tp2 = e_year % 100;

	if (e_month == 4 || e_month == 6 || e_month == 9 || e_month == 11) {
		rtval = 30;
	} else if (e_month == 2 && tp1 != 0 ) {
		rtval = 28;
	} else if (e_month == 2 && tp1 == 0 ) {
		if (tp2 == 0 ) {
			rtval = (tp2 == 0 ? 29 : 28 );
		} else {
			rtval=29;
		}
	}
	return  rtval;
}

function hidden_cal()
{	
	var div_calendar = document.getElementById("div_calendar");
	div_calendar.style.visibility='hidden';
	mf.style.visibility='hidden';
	
	var mfs = document.getElementsByName("calendarObj");
	for(var i=0; i<mfs.length;i++){
		document.body.removeChild(mfs[i]);
	}
}


function show_cal(e_year, e_month, e_day, e_hour, e_min, locale)
{
	cur_date = new Date();

	today_year = cur_date.getYear();
	today_month = cur_date.getMonth() + 1 ;
	today_day = cur_date.getDate();

	if ( e_year == "" || e_year <= 0 || e_month == "" || e_month <= 0 || e_day == "" || e_day <= 0 ) {
		e_year = cur_date.getYear();
		e_month = cur_date.getMonth() + 1
		e_day = cur_date.getDate();
	} else {
		cur_date.setYear(e_year);
		cur_date.setMonth(e_month-1);
		cur_date.setDate(e_day);
	}


	str = "<table width='170' border='0' cellspacing='2' cellpadding='0' bgcolor='#333333' id='cal_table' class='cal_table' onSelectstart='return false' onSelect='return false' ";
	str += "  	onmouseover='mouse_over()' onfocus='mouse_over()' onmouseout='mouse_out()' onblur='mouse_out()' onclick ='mouse_click()' > \n";
	str += "  <tr> \n";
	str += "    <td bgcolor='#FFFFFF'> \n";
	str += "      <table width='100%' border='0' cellspacing='0' cellpadding='0'> \n";
	str += "        <tr>  \n";
	str += "          <td>  \n";
	str += "            <table width='100%' border='0' cellspacing='0' cellpadding='0'> \n";
	str += "              <tr>  \n";
	str += "                <td>  \n";
	str += "                  <table border='0' cellspacing='1' cellpadding='0'> \n";
	str += "                    <tr>  \n";
	str += "                      <td colspan='2'><img src='/images/d_year.gif'></td> \n";
	str += "                    </tr> \n";
	str += "                    <tr>  \n";
	str += "                      <td><a href='javascript:re_cal(-2)'><img src='/images/d_prev.gif' border=0></a></td> \n";
	str += "                      <td><a href='javascript:re_cal(2)'><img src='/images/d_next.gif' border=0></a></td> \n";
	str += "                    </tr> \n";
	str += "                  </table> \n";
	str += "                </td> \n";
	str += "                <td width='100%' align='center'><font face='Tahoma' size='1'><b>" + _year + " / " + _month + "</b></font></td> \n";
	str += "                <td>  \n";
	str += "                  <table border='0' cellspacing='1' cellpadding='0'> \n";
	str += "                    <tr>  \n";
	str += "                      <td colspan='2'><img src='/images/d_month.gif' width='33' height='9'></td> \n";
	str += "                    </tr> \n";
	str += "                    <tr>  \n";
	str += "                      <td><a href='javascript:re_cal(-1)'><img src='/images/d_prev.gif' border=0></a></td> \n";
	str += "                      <td><a href='javascript:re_cal(1)'><img src='/images/d_next.gif' border=0></a></td> \n";
	str += "                    </tr> \n";
	str += "                  </table> \n";
	str += "                </td> \n";
	str += "              </tr> \n";
	str += "            </table> \n";
	str += "          </td> \n";
	str += "        </tr> \n";
	str += "        <tr>  \n";
	str += "          <td><img src='/images/d_line.gif' width='100%' height='1'></td> \n";
	str += "        </tr> \n";
	str += "        <tr> \n";
	str += "          <td> \n";
	str += "            <table width='100%' border='0' cellspacing='1' cellpadding='1'> \n";
	
	if(_locale=="EN"){
		str += "              <tr bgcolor='#FFFFFF' align='center'>  \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>S</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>M</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>T</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>W</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>T</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>F</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>S</font></td> \n";
		str += "              </tr> \n";	
	}else{
		str += "              <tr bgcolor='#FFFFFF' align='center'>  \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>일</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>월</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>화</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>수</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>목</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>금</font></td> \n";
		str += "                <td class='cal_week'><font size='2' face='돋움'>토</font></td> \n";
		str += "              </tr> \n";	
	}

	str += "              <tr bgcolor='#FFFFFF'> \n";
	str += "                <td colspan='7'><font size='1' face='Verdana, Tahoma'><img src='/images/d_line2.gif' width='100%' height='1'></font></td> \n";
	str += "              </tr> \n";

	count_day = 1;

	last_day = get_last_day(e_year, e_month)
	row_cnt = 0;

	for ( week=0 ; week < 6 ; week++ ) {

		tmp_cur_date = new Date(e_year, e_month-1, count_day);
	//	alert("tmp_cur_date: " + tmp_cur_date);

		if (last_day==31 && today_day > 31){
			s_day_of_week = tmp_cur_date.getDay()-1;
		} else {
			s_day_of_week = tmp_cur_date.getDay();
		}


		row_cnt++;
		str += "<tr height='12'>"
		for (dow=0; dow <= 6 ; dow++) {
			if (dow == 0 ) {
				small_css = "css_sun";
			} else if ( dow == 6) {
				small_css = "css_sat";
			} else {
				small_css = "css_def";
			}
			if ( dow < s_day_of_week ) {
				str += "<td class='" + small_css + "'>&nbsp;</td>"
			} else {
				if (count_day > last_day) {
					str += "<td class='" + small_css + "'>&nbsp;</td>"
					week = 10;
				} else {
					tmp_month  = (_month < 10) ? "0" + _month : _month;
					tmp_count_day = (count_day < 10 ) ? "0" + count_day : count_day;
					if (_year == today_year && tmp_month == today_month && tmp_count_day == today_day ){
						str += "<td class='css_tod' title='" + _year + "" + tmp_month + "" + tmp_count_day + "' ";
						str += "	onmouseover=\"this.style.backgroundColor='#FFFF00'\" onfocus=\"this.style.backgroundColor='#FFFF00'\">" + count_day + "</td>"
					} else {
						str += "<td class='" + small_css + "' title='" + _year + "" + tmp_month + "" + tmp_count_day + "' ";
						str += "	onmouseover=\"this.style.backgroundColor='#FFFF00'\" onfocus=\"this.style.backgroundColor='#FFFF00'\">" + count_day + "</td>"
					}
				}
				count_day++;
			}
		}
		str += "</tr>\n"
	}
	if(row_cnt == 5)
		t_height = 178;
	else if(row_cnt == 6)
		t_height = 198;
	else
		t_height = 178;


	if (_time_f ==1 ){
		str += show_time(e_hour, e_min);
		t_height += 23;
	}


	str += "            </table> \n";
	str += "          </td> \n";
	str += "        </tr> \n";
	str += "        <tr>  \n";
	str += "          <td><img src='/images/d_line.gif' width='100%' height='1'></td> \n";
	str += "        </tr> \n";
	str += "        <tr> \n";
	str += "          <td align='center' height='20'><font size='1' face='Tahoma'><a href='javascript:hidden_cal()'><b class='css_cancel'>Cancel</b></a></font></td> \n";
	str += "        </tr> \n";
	str += "      </table> \n";
	str += "    </td> \n";
	str += "  </tr> \n";
	str += "</table> \n";

	document.getElementById("div_calendar").innerHTML = str
	mf.style.visibility = "visible";
	mf.style.height = t_height -20;
	document.getElementById("div_calendar").style.visibility = "visible";

	_year = e_year;
	_month = e_month;
	_day = e_day;
}

function show_time(hour, min)
{

	if ( hour == 0 || hour == "0" || hour == "00" )	{
		tmp_date = new Date();
		hour = tmp_date.getHours();
		min =  tmp_date.getMinutes();
	}

	min =  min - (min % 10 )

	_hour = hour
	_min = min

	str = " <tr bgcolor='#FFFFFF'> \n";
	str += " <td colspan='7' align='center' NOWRAP> <b > </b > \n";
	str += " <select id='cal_sel_hour' class='cal_td'>\n";

	for ( h=0; h <24 ; h++ ){
		tmp = (h<10 ? "0"+h : h );
		if ( hour == tmp){
			str += " <option value='" + tmp + "' selected>" + tmp + "</option>\n";
		} else {
			str += " <option value='" + tmp + "' >" + tmp + "</option>\n";
		}
	}

	str += "  </select> H \n";
	str += "  <select id='cal_sel_min' class='cal_td'>\n";

	for ( h=0; h <60 ; h += 10 ){
		tmp = (h<10 ? "0"+h : h );
		if (min == tmp){
			str += " <option value='" + tmp + "' selected>" + tmp + "</option>\n";
		} else {
			str += " <option value='" + tmp + "' >" + tmp + "</option>\n";
		}
	}

	str += "    </select> M</td>\n";
	str += "  </tr>\n";
	
	return str;
}

function makeIframe(leftPos, topPos) {
	mf = document.createElement("IFRAME");
	mf.id = "calendarObj";
	mf.name = "calendarObj";
	mf.frameBorder = 0;
	mf.src="blank.html";

	mf.marginWidth=0 ;
	mf.marginHeight=0 ;
	var mfs = mf.style;
	mfs.position = "absolute";
	mfs.visibility = "hidden";
	mfs.left = leftPos;
	mfs.top = topPos;
	mfs.width = "170px";
	mfs.height = "170px";

	document.body.appendChild( mf );
}


//////////////////////////////////////////////////////////////////////////////////////
/*
str = " <style> \n";
str += " 	.cal_table {font-size: 8pt; background-color: #666666; color: #333333;cursor:hand };  \n";
str += " 	.cal_table td {line-height:20px; };  \n";
str += " 	.cal_td    {font-size: 8pt }; \n";
str += " 	.cal_week  {font-size: 8pt; background-color: #CCCCCC;text-align: center;cursor:hand };  \n";
str += " 	.css_sun   {font-size: 8pt; background-color: #FFDFDF; color: #FF0000 ;text-align: center;cursor:hand };  \n";
str += " 	.css_sat   {font-size: 8pt; background-color: #DDF1FF; color: #003399 ;text-align: center;cursor:hand };  \n";
str += " 	.css_def   {font-size: 8pt; background-color: #FFFFFF; color: #000000 ;text-align: center;cursor:hand };  \n";
str += " 	.css_tod   {font-size: 8pt; background-color: #FFFFFF; color: #0000FF ;text-align: center;cursor:hand;font-weight: bold };  \n";
str += " 	.css_cancel   {FONT-SIZE: 12px; COLOR: #666666; TEXT-DECORATION: none};  \n";
str += " </style> \n";
*/
str = " <style> \n";
str += "  .cal_table {font-size: 8pt; background-color: #666666; color: #333333;cursor:hand };  \n";
str += "  .cal_table td {line-height:20px; };  \n";
str += "  .cal_td    {font-size: 8pt }; \n";
str += "  .cal_week  {font-size: 8pt; background-color: #CCCCCC;text-align: center;cursor:hand };  \n";
str += "  .css_sun   {font-size: 8pt; background-color: #dfddca; color: #621414 ;text-align: center;cursor:hand };  \n";
str += "  .css_sat   {font-size: 8pt; background-color: #dfddca; color: #621414 ;text-align: center;cursor:hand };  \n";
str += "  .css_def   {font-size: 8pt; background-color: #FFFFFF; color: #000000 ;text-align: center;cursor:hand };  \n";
str += "  .css_tod   {font-size: 8pt; background-color: #FFFFFF; color: #0000FF ;text-align: center;cursor:hand;font-weight: bold };  \n";
str += "  .css_cancel   {FONT-SIZE: 12px; COLOR: #666666; TEXT-DECORATION: none};  \n";
str += " </style> \n";

str += " <div id='div_calendar' style=\"background : buttonface;width:100;visibility:hidden;position: absolute; z-index: 99\" ";
str += "	onSelect='return false'>";
str += " </div> \n";



document.write(str)


