var target;
var pop_top;
var pop_left;
var cal_Day;
//var oPopup = window.createPopup();
var oPopup = null;
function Calendar_Click(e) {
	cal_Day = e.title;
	if (cal_Day.length > 6) {
		target.value = cal_Day
	}
	//oPopup.hide();
        hideCalendar();
}
function hideCalendar(){
  document.getElementById("calendar_div").style.display = "none";
  //document.getElementById("calendar_frm").style.display = "none";
}
function Calendar_D(obj) {
	var now = obj.value.split("-");
	target = obj;
	pop_top = document.body.clientTop + GetObjectTop(obj) - document.body.scrollTop;
	pop_left = document.body.clientLeft + GetObjectLeft(obj) -  document.body.scrollLeft;

	if (now.length == 3) {
		Show_cal(now[0],now[1],now[2]);
	} else {
		now = new Date();
		Show_cal(now.getFullYear(), now.getMonth()+1, now.getDate());
	}
}

function Calendar_M(obj) {
	var now = obj.value.split("-");
	target = obj;
	pop_top = document.body.clientTop + GetObjectTop(obj) - document.body.scrollTop;
	pop_left = document.body.clientLeft + GetObjectLeft(obj) -  document.body.scrollLeft;

	if (now.length == 2) {
		Show_cal_M(now[0],now[1]);
	} else {
		now = new Date();
		Show_cal_M(now.getFullYear(), now.getMonth()+1);
	}
}

function doOver(event) {
  el = null;
  if (window.event){
    el = window.event.srcElement;
  }
  else{
    //el = (event.target.className ? event.target : event.target.parentNode);
    el = event.target;
  }


  cal_Day = el.title;

  if (cal_Day.length > 7) {
          el.style.borderColor = "#FF0000";
  }
}

function doOut(event) {
  el = null;
  if (window.event){
    el = window.event.srcElement;
  }
  else{
    //el = (event.target.className ? event.target : event.target.parentNode);
    el = event.target;
  }
  cal_Day = el.title;

  if (cal_Day.length > 7) {
          el.style.borderColor = "#FFFFFF";
  }
}

function day2(d) {	// convert 2 digits
	var str = new String();

	if (parseInt(d) < 10) {
		str = "0" + parseInt(d);
	} else {
		str = "" + parseInt(d);
	}
	return str;
}

function Show_cal(sYear, sMonth, sDay) {
	var Months_day = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
	var Month_Val = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
	var intThisYear = new Number(), intThisMonth = new Number(), intThisDay = new Number();

	datToday = new Date();													// set current date

	intThisYear = parseInt(sYear,10);
	intThisMonth = parseInt(sMonth,10);
	intThisDay = parseInt(sDay,10);

	if (intThisYear == 0) intThisYear = datToday.getFullYear();				// if value is not exist
	if (intThisMonth == 0) intThisMonth = parseInt(datToday.getMonth(),10)+1;	// the month is smaller 1 than real value
	if (intThisDay == 0) intThisDay = datToday.getDate();

	switch(intThisMonth) {
		case 1:
				intPrevYear = intThisYear -1;
				intPrevMonth = 12;
				intNextYear = intThisYear;
				intNextMonth = 2;
				break;
		case 12:
				intPrevYear = intThisYear;
				intPrevMonth = 11;
				intNextYear = intThisYear + 1;
				intNextMonth = 1;
				break;
		default:
				intPrevYear = intThisYear;
				intPrevMonth = parseInt(intThisMonth,10) - 1;
				intNextYear = intThisYear;
				intNextMonth = parseInt(intThisMonth,10) + 1;
				break;
	}
	intPPyear = intThisYear-1
	intNNyear = intThisYear+1

	NowThisYear = datToday.getFullYear();									// Current year
	NowThisMonth = datToday.getMonth()+1;									// Current month
	NowThisDay = datToday.getDate();											// Current date

	datFirstDay = new Date(intThisYear, intThisMonth-1, 1);			// create date object using first day of current month
	intFirstWeekday = datFirstDay.getDay();									// get a day of current month's first day (0:sunday, 1: monday)
	//intSecondWeekday = intFirstWeekday;
	intThirdWeekday = intFirstWeekday;

	datThisDay = new Date(intThisYear, intThisMonth, intThisDay);	// create date
	//intThisWeekday = datThisDay.getDay();										//

	intPrintDay = 1;																// the start date of month
	secondPrintDay = 1;
	thirdPrintDay = 1;

	Stop_Flag = 0

	if ((intThisYear % 4)==0) {												// if it is a leap year
		if ((intThisYear % 100) == 0) {
			if ((intThisYear % 400) == 0) {
				Months_day[2] = 29;
			}
		} else {
			Months_day[2] = 29;
		}
	}
	intLastDay = Months_day[intThisMonth];						// get last date of month

	Cal_HTML = "";
	//Cal_HTML += "<form name='calendar'>";
	Cal_HTML += "<table id=Cal_Table border=0 bgcolor='#f4f4f4' cellpadding=1 cellspacing=1 width=100% onmouseover='doOver(event)' onmouseout='doOut(event)' style='font-size : 12;font-family:Arial;'>";
	Cal_HTML += "<tr height='35' align=center bgcolor='#f4f4f4'>";
	Cal_HTML += "<td colspan=7 align=center>";
	Cal_HTML += "	<select name='selYear' STYLE='font-size:11;' OnChange='fnChangeYearD(selYear.value, selMonth.value, "+intThisDay+")';>";
	for (var optYear=(intThisYear-2); optYear<(intThisYear+2); optYear++) {
		Cal_HTML += "		<option value='"+optYear+"' ";
		if (optYear == intThisYear) Cal_HTML += " selected>\n";
		else Cal_HTML += ">\n";
		Cal_HTML += optYear+"</option>\n";
	}
	Cal_HTML += "	</select>";
	Cal_HTML += "&nbsp;&nbsp;&nbsp;";
	Cal_HTML += "&nbsp;&nbsp;&nbsp;<a style='cursor:pointer;font-weight:bold' OnClick='Show_cal("+intPrevYear+","+intPrevMonth+","+intThisDay+");'>&lt;</a> ";
	Cal_HTML += "<select name='selMonth' STYLE='font-size:11;' OnChange='fnChangeYearD(selYear.value, selMonth.value, "+intThisDay+")';>";
	for (var i=1; i<13; i++) {
		Cal_HTML += "		<option value='"+Month_Val[i-1]+"' ";
		if (intThisMonth == parseInt(Month_Val[i-1],10)) Cal_HTML += " selected>\n";
		else Cal_HTML += ">\n";
		Cal_HTML += Month_Val[i-1]+"</option>\n";
	}
	Cal_HTML += "	</select>&nbsp;";
	Cal_HTML += "<a style='cursor:pointer;font-weight:bold' OnClick='Show_cal("+intNextYear+","+intNextMonth+","+intThisDay+");'>&gt;</a>";
	Cal_HTML += "</td></tr>";
	Cal_HTML += "<tr align=center bgcolor='#eaeaea' style='color:#2065DA;' height='25'>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=red>S</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>M</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>T</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>W</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>T</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>F</font></td>";
	Cal_HTML += "	<td style='padding-top:3px;' width='24'><font color=black>S</font></td>";
	Cal_HTML += "</tr>";

	for (intLoopWeek=1; intLoopWeek < 7; intLoopWeek++) {	// loop start in weeks.maxium is 6.
		Cal_HTML += "<tr height='24' align=right bgcolor='white'>"
		for (intLoopDay=1; intLoopDay <= 7; intLoopDay++) {	// loop by weekday,from sunday
			if (intThirdWeekday > 0) {											// if the start day of first week is bigger than 1
				Cal_HTML += "<td>";
				intThirdWeekday--;
			} else {
				if (thirdPrintDay > intLastDay) {								// if input date is bigger than last date of month
					Cal_HTML += "<td>";
				}
                                else {																// if input date is assigned to current month
                                        if(intThisYear < NowThisYear || (intThisYear == NowThisYear && intThisMonth < NowThisMonth) || (intThisYear == NowThisYear && intThisMonth == NowThisMonth && thirdPrintDay <= NowThisDay)){
                                          Cal_HTML += "<td onClick=Calendar_Click(this); title="+intThisYear+"-"+day2(intThisMonth).toString()+"-"+day2(thirdPrintDay).toString()+" style=\"cursor:Hand;border:1px solid white;";
					}
                                        else{
                                          Cal_HTML += "<td title="+intThisYear+"-"+day2(intThisMonth).toString()+"-"+day2(thirdPrintDay).toString()+" style=\"border:1px solid white;";
					}
					if (intThisYear == NowThisYear && intThisMonth==NowThisMonth && thirdPrintDay==intThisDay) {
						Cal_HTML += "background-color:#f8d446;";
					}

					switch(intLoopDay) {
						case 1:															// if it is sunday, fill red color.
							Cal_HTML += "color:red;"
							break;
						//case 7:
						//	Cal_HTML += "color:blue;"
						//	break;
						default:
							Cal_HTML += "color:black;"
							break;
					}
					Cal_HTML += "\">"+thirdPrintDay;
				}
				thirdPrintDay++;

				if (thirdPrintDay > intLastDay) {								// if date is bigger than last of month, escape loop
					Stop_Flag = 1;
				}
			}
			Cal_HTML += "</td>";
		}
		Cal_HTML += "</tr>";
		if (Stop_Flag==1) break;
	}
	Cal_HTML += "</table>";
        Cal_HTML += "</form>";

	//var oPopBody = oPopup.document.body;
	//oPopBody.style.backgroundColor = "lightyellow";
	//oPopBody.style.border = "solid black 1px";
	//oPopBody.innerHTML = Cal_HTML;
        document.getElementById("calendar_div").style.display = "";
        //document.getElementById("calendar_frm").style.display = "";
        document.getElementById("calendar_div").innerHTML = Cal_HTML;
        document.getElementById("calendar_div").style.height = document.getElementById("Cal_Table").style.height;
	//var calHeight = oPopBody.document.all.Cal_Table.offsetHeight;
	//if (intLoopWeek == 6)	calHeight = 214;
	//else	calHeight = 189;

	//oPopup.show(pop_left, (pop_top + target.offsetHeight), 170, calHeight, document.body);
}


function Show_cal_M(sYear, sMonth) {
	var intThisYear = new Number(), intThisMonth = new Number()
	datToday = new Date();													// set current date

	intThisYear = parseInt(sYear,10);
	intThisMonth = parseInt(sMonth,10);

	if (intThisYear == 0) intThisYear = datToday.getFullYear();				//
	if (intThisMonth == 0) intThisMonth = parseInt(datToday.getMonth(),10)+1;	//

	switch(intThisMonth) {
		case 1:
				intPrevYear = intThisYear -1;
				intNextYear = intThisYear;
				break;
		case 12:
				intPrevYear = intThisYear;
				intNextYear = intThisYear + 1;
				break;
		default:
				intPrevYear = intThisYear;
				intNextYear = intThisYear;
				break;
	}
	intPPyear = intThisYear-1
	intNNyear = intThisYear+1

	Cal_HTML = "<html><head>\n";
	Cal_HTML += "</head><body>\n";
	Cal_HTML += "<table id=Cal_Table border=0 bgcolor='#f4f4f4' cellpadding=1 cellspacing=1 width=100% onmouseover='parent.doOver(window.event.srcElement)' onmouseout='parent.doOut(window.event.srcElement)' style='font-size : 12;font-family:Arial;'>\n";
	Cal_HTML += "<tr height='30' align=center bgcolor='#f4f4f4'>\n";
	Cal_HTML += "<td colspan='4' align='center'>\n";
	Cal_HTML += "<a style='cursor:pointer;' OnClick='parent.Show_cal_M("+intPPyear+","+intThisMonth+");'>Ēļ</a>&nbsp;";
	Cal_HTML += "<select name='selYear' STYLE='font-size:11;' OnChange='parent.fnChangeYearM(this.value, "+intThisMonth+")';>";
	for (var optYear=(intThisYear-2); optYear<(intThisYear+2); optYear++) {
			Cal_HTML += "		<option value='"+optYear+"' ";
			if (optYear == intThisYear) Cal_HTML += " selected>\n";
			else Cal_HTML += ">\n";
			Cal_HTML += optYear+"</option>\n";
	}
	Cal_HTML += "	</select>\n";
	Cal_HTML += "<a style='cursor:pointer;' OnClick='parent.Show_cal_M("+intNNyear+","+intThisMonth+");'>Ēš</a>";
	Cal_HTML += "</td></tr>\n";
	Cal_HTML += "<tr><td colspan=4 height='1' bgcolor='#000000'></td></tr>";
	Cal_HTML += "<tr height='20' align=center bgcolor=white>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-01"+" style=\"cursor:Hand;\">Jan</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-02"+" style=\"cursor:Hand;\">Feb</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-03"+" style=\"cursor:Hand;\">Mar</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-04"+" style=\"cursor:Hand;\">Apr</td>";
	Cal_HTML += "</tr>\n";
	Cal_HTML += "<tr height='20' align=center bgcolor=white>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-05"+" style=\"cursor:Hand;\">May</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-06"+" style=\"cursor:Hand;\">Jun</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-07"+" style=\"cursor:Hand;\">Jul</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-08"+" style=\"cursor:Hand;\">Aug</td>";
	Cal_HTML += "</tr>\n";
	Cal_HTML += "<tr height='20' align=center bgcolor=white>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-09"+" style=\"cursor:Hand;\">Sep</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-10"+" style=\"cursor:Hand;\">Oct</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-11"+" style=\"cursor:Hand;\">Nov</td>";
	Cal_HTML += "<td onClick=parent.Calendar_Click(this); title="+intThisYear+"-12"+" style=\"cursor:Hand;\">Dec</td>";
	Cal_HTML += "</tr>\n";
	Cal_HTML += "</table>\n</body></html>";

	var oPopBody = oPopup.document.body;
	oPopBody.style.backgroundColor = "lightyellow";
	oPopBody.style.border = "solid black 1px";
	oPopBody.innerHTML = Cal_HTML;
        oPopup.show(pop_left, (pop_top + target.offsetHeight), 160, 99, document.body);
}


//----------------------------------
//	select year in year list at daily calendar
//----------------------------------
function fnChangeYearD(sYear,sMonth,sDay){
	Show_cal(sYear, sMonth, sDay);
}


//----------------------------------
//	select year in year list at m calendar
//----------------------------------
function fnChangeYearM(sYear,sMonth){
	Show_cal_M(sYear, sMonth);
}


/**
	util function
**/
function GetObjectTop(obj)
{
	if (obj.offsetParent == document.body)
		return obj.offsetTop;
	else
		return obj.offsetTop + GetObjectTop(obj.offsetParent);
}

function GetObjectLeft(obj)
{
	if (obj.offsetParent == document.body)
		return obj.offsetLeft;
	else
		return obj.offsetLeft + GetObjectLeft(obj.offsetParent);
}
