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 sel_obj1 = null;
var sel_objth = null;
var sel_objtm = null;
var sel_idx = null;
var mf = null;

function init_cal (th, tm, time_f, idx) {
	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.documentElement.scrollLeft + 10 ;
	y = window.event.clientY + document.documentElement.scrollTop - 50 ;

	div_calendar.style.pixelTop	= y-0;
	div_calendar.style.pixelLeft = x;

	mf.style.left = x;
	mf.style.top =  y;

	sel_objth = th;
	sel_objtm = tm;	
	sel_idx = idx;	
	if (sel_idx) {
		sel_obj = document.all(th)[sel_idx];	
		if (time_f == 1) {
			sel_obj1 = document.all(tm)[sel_idx];				
		}
	} else {
		sel_obj = document.all(th);		
		if (time_f == 1) {
			sel_obj1 = document.all(tm);				
		}		
	}
	if (time_f ==1 ) {
		_time_f = 1;
	} else {
		_time_f = 0;
	}

	show_cal(_year, _month, _day, _hour, _min)
}

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='#FFA500';
	}

	window.clearTimeout(stime);
}

function mouse_click() {
	sel_day = window.event.srcElement.title;
	if (sel_day.length > 9) {
		if (sel_obj != null ) {
			
			if (_time_f == 1) {
				as_val = document.all("cal_sel_hour").value + ":" + document.all("cal_sel_min").value  + ":00";	
				sel_obj1.value = as_val;
			}
			sel_obj.value = sel_day;

			if (sel_idx) {		
				document.all(sel_objth+"YY")[sel_idx].value = sel_day.substring(0,4);	
				document.all(sel_objth+"MM")[sel_idx].value = sel_day.substring(5,7);
				document.all(sel_objth+"DD")[sel_idx].value = sel_day.substring(8,10);						
			} else {
				document.all(sel_objth+"YY").value = sel_day.substring(0,4);	
				document.all(sel_objth+"MM").value = sel_day.substring(5,7);
				document.all(sel_objth+"DD").value = sel_day.substring(8,10);											
			}
			if (_time_f == 1) {
				if (sel_idx) {						
					document.all(sel_objtm+"HH")[sel_idx].value = as_val.substring(0,2);			
					document.all(sel_objtm+"MI")[sel_idx].value = as_val.substring(3,5);			
					document.all(sel_objtm+"SS")[sel_idx].value = as_val.substring(6,8);													
				} else {
					document.all(sel_objtm+"HH").value = as_val.substring(0,2);			
					document.all(sel_objtm+"MI").value = as_val.substring(3,5);			
					document.all(sel_objtm+"SS").value = as_val.substring(6,8);							
				}
			}				
			hidden_cal();
		}
	}

}

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 = '#FFFFFF';
		} else if ( el.className =='css_sat' ) {
			el.style.backgroundColor = '#FFFFFF';
		} else {
			el.style.backgroundColor = '#FFFFFF';
		}
	}
}

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() {
	mf.style.display = "none";
	div_calendar.style.display = "none";	
}


function show_cal(e_year, e_month, e_day, e_hour, e_min) {
	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' onmouseover='mouse_over()' onmouseout='mouse_out()' onclick ='mouse_click()' > \n";
	str += "  <tr> \n";
	str += "    <td bgcolor='#F4F4F4'> \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' bgcolor='#FFFFFF'> \n";
	str += "              <tr>  \n";
	str += "                <td width='50' valign='top'>  \n";
	str += "                  <table width='100%' border='0' cellspacing='1' cellpadding='0'> \n";
	str += "                    <tr>  \n";
	str += "                      <td colspan='2' class='cal_td1' align='center'>YEAR</td> \n";
	str += "                    </tr> \n";
	str += "                    <tr>  \n";
	str += "                      <td align='right'><a href='javascript:re_cal(-2)' class='cal'>¢¸</a></td> \n";
	str += "                      <td><a href='javascript:re_cal(2)' class='cal'>¢º</a></td> \n";
	str += "                    </tr> \n";
	str += "                  </table> \n";
	str += "                </td> \n";
	str += "                <td width='70' align='center' class='cal_td'><b>" + _year + " / " + _month + "</b></td> \n";
	str += "                <td width='50' valign='top'>  \n";
	str += "                  <table width='100%' border='0' cellspacing='1' cellpadding='0'> \n";
	str += "                    <tr>  \n";
	str += "                      <td colspan='2' class='cal_td1' align='center'>MONTH</td> \n";
	str += "                    </tr> \n";
	str += "                    <tr>  \n";
	str += "                      <td align='right'><a href='javascript:re_cal(-1)' class='cal'>¢¸</a></td> \n";
	str += "                      <td><a href='javascript:re_cal(1)' class='cal'>¢º</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><table width='100%' height='1' border='0' cellspacing='0' cellpadding='0' bgcolor='#D6D7CF'><tr><td></td></tr></table></td> \n";
	str += "        </tr> \n";
	str += "        <tr> \n";
	str += "          <td> \n";
	str += "            <table width='100%' border='0' cellspacing='1' cellpadding='1'> \n";
	str += "              <tr bgcolor='#FFFFFF' align='center'>  \n";
	str += "                <td height='16' width='18' class='cal_week_sun'>Sun</td> \n";
	str += "                <td height='16' width='18' class='cal_week'>Mon</td> \n";
	str += "                <td height='16' width='18' class='cal_week'>Tue</td> \n";
	str += "                <td height='16' width='18' class='cal_week'>Wed</td> \n";
	str += "                <td height='16' width='18' class='cal_week'>Thu</td> \n";
	str += "                <td height='16' width='18' class='cal_week'>Fri</td> \n";
	str += "                <td height='16' width='18' class='cal_week_sat'>Sat</td> \n";
	str += "              </tr> \n";
	str += "              <tr bgcolor='#FFFFFF'>\n";
	str += "                <td colspan='7'></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);

		if (last_day==29 && today_day > 29){
			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 height='18' width='18' class='css_tod' title='" + _year + "/" + tmp_month + "/" + tmp_count_day + "' ";
						str += "	onmouseover=\"this.style.backgroundColor='#FFA500'\">" + count_day + "</td>"
					} else {
						str += "<td  height='18' width='18' class='" + small_css + "' title='" + _year + "/" + tmp_month + "/" + tmp_count_day + "' ";
						str += "	onmouseover=\"this.style.backgroundColor='#FFA500'\">" + 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><table width='100%' height='1' border='0' cellspacing='0' cellpadding='0' bgcolor='#D6D7CF'><tr><td></td></tr></table></td> \n";
	str += "        </tr> \n";
	str += "        <tr> \n";
	str += "          <td bgcolor='#FFFFFF' align='center' height='20'><a href='javascript:delete_date()' class='cal'><b>Delete</b></a> &nbsp;&nbsp;<a href='javascript:hidden_cal()' class='cal'><b>Cancel</b></a></td> \n";
	str += "        </tr> \n";
	str += "      </table> \n";
	str += "    </td> \n";
	str += "  </tr> \n";
	str += "</table> \n";

	document.all("div_calendar").innerHTML = str
	mf.style.display = "block";
	mf.style.height = t_height -20;
	document.all("div_calendar").style.display = "block";

	_year = e_year;
	_month = e_month;
	_day = e_day;
}

// ³¯Â¥ »èÁ¦
function delete_date() {
	if (sel_idx) {	
		document.all(sel_objth+"YY")[sel_idx].value = "";
		document.all(sel_objth+"MM")[sel_idx].value = "";
		document.all(sel_objth+"DD")[sel_idx].value = "";	
		sel_obj.value = "";			
		if (_time_f == 1) {
			document.all(sel_objtm+"HH")[sel_idx].value = "";			
			document.all(sel_objtm+"MI")[sel_idx].value = "";			
			document.all(sel_objtm+"SS")[sel_idx].value = "";							
			sel_obj1.value = "";				
		}		
	} else {
		document.all(sel_objth+"YY").value = "";
		document.all(sel_objth+"MM").value = "";
		document.all(sel_objth+"DD").value = "";	
		sel_obj.value = "";			
		if (_time_f == 1) {
			document.all(sel_objtm+"HH").value = "";			
			document.all(sel_objtm+"MI").value = "";			
			document.all(sel_objtm+"SS").value = "";							
			sel_obj1.value = "";				
		}
	}
	hidden_cal();
} 

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 cal_sel'>\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> <font class='css_def'>H</font> \n";
	str += "  <select id='cal_sel_min' class='cal_td cal_sel'>\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> <font class='css_def'>M</font></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="/inc/blank.html";

	mf.marginWidth=0 ;
	mf.marginHeight=0 ;
	var mfs = mf.style;
	mfs.position = "absolute";
	mfs.display = "none";
	mfs.left = leftPos;
	mfs.top = topPos;
	mfs.width = "170px";
	mfs.height = "170px";

	document.body.appendChild( mf );
}

//////////////////////////////////////////////////////////////////////////////////////
  
str = " <style> \n";
str += " 	.cal_table 		{font-size: 7pt; font-family :Tahoma; background-color: #DDDDDD; color: #333333;cursor:hand };  \n";
str += " 	.cal_td  		{font-size: 7pt; font-family :Tahoma }; \n";
str += " 	.cal_td1  		{font-size: 7pt; font-family :Tahoma }; \n";
str += " 	.cal_sel 		{color: #333333; text-decoration: none ; font-family :Tahoma };  \n";
str += " 	.cal_week  		{font-size: 7pt; font-family :Tahoma; background-color: #EDEEE5;text-align: center;cursor:hand };  \n";
str += " 	.cal_week_sun  	{color:#F2510A; font-size: 7pt; font-family :Tahoma; background-color: #EDEEE5;text-align: center;cursor:hand };  \n";
str += " 	.cal_week_sat  	{color:#2695B9; font-size: 7pt; font-family :Tahoma; background-color: #EDEEE5;text-align: center;cursor:hand };  \n";
str += " 	.css_sun   		{font-size: 7pt; font-family :Tahoma; background-color: #ffffff; color: #F2510A ;text-align: center;cursor:hand };  \n";
str += " 	.css_sat   		{font-size: 7pt; font-family :Tahoma; background-color: #FFFFFF; color: #2695B9 ;text-align: center;cursor:hand };  \n";
str += " 	.css_def   		{font-size: 7pt; font-family :Tahoma; background-color: #FFFFFF; color: #666666 ;text-align: center;cursor:hand };  \n";
str += " 	.css_tod   		{font-size: 7pt; font-family :Tahoma; background-color: #FFFFFF; color: #000000 ;text-align: center;cursor:hand;font-weight: bold };  \n";
str += " 	A.cal:link    	{color:#666666; text-decoration: none; font-size: 12px};  \n";
str += " 	A.cal:visited 	{color:#666666; text-decoration: none; font-size: 12px};  \n";
str += " 	A.cal:hover   	{color:#000000; text-decoration: underline; font-size: 12px};  \n";
str += " </style> \n";

str += " <div id='div_calendar' style=\"background : buttonface;width:100;display:none;position: absolute; z-index: 99\" onSelect='return false'>";
str += " </div> \n";

document.write(str);