var nn4 = (document.layers) ? true : false;
var ie  = (document.all) ? true : false;
var dom = (document.getElementById && !document.all) ? true : false;
var popups = new Array(); // keeps track of popup windows we create
var calHtml = '';

// language and preferences
wDay = new Array('ÀÏ¿äÀÏ','¿ù¿äÀÏ','È­¿äÀÏ','¼ö¿äÀÏ','¸ñ¿äÀÏ','±Ý¿äÀÏ','Åä¿äÀÏ');
wDayAbbrev = new Array('<font color=#CC0000>ÀÏ</font>','¿ù','È­','¼ö','¸ñ','±Ý','<font color=#0000CC>Åä</font>');
wMonth = new Array('1¿ù','2¿ù','3¿ù','4¿ù','5¿ù','6¿ù','7¿ù','8¿ù','9¿ù','10¿ù','11¿ù','12¿ù');
wOrder = new Array('Ã¹¹øÂ°','µÎ¹øÂ°','¼¼¹øÂ°','³×¹øÂ°','¸¶Áö¸·');
wStart = 0;

function calPopup(obj, id, xOffset, yOffset, inputName, validationScript, currentDay, yTop) {
	
	//alert(obj + " / " + id + " / " + xOffset + " / " + yOffset + " / " + inputName + " / " + validationScript);
	try
	{	
		attachListener(id);
		registerPopup(id);
		calHtml = makeCalHtml(id,null,null,null,inputName,validationScript, currentDay);
		writeLayer(id,calHtml);
		setLayerPos(obj,id,xOffset,yOffset, yTop);
		showLayer(id);
	}
	catch (e)
	{
		alert("calPopup : " + e);
	}

	return true;
}

function attachListener(id) {
	try
	{	
		var layer = new pathToLayer(id)
		if (layer.obj.listening == null) {
			document.oldMouseupEvent = document.onmouseup;
			if (document.oldMouseupEvent != null) {
			document.onmouseup = new Function("document.oldMouseupEvent(); hideLayersNotClicked();");
			} 
			else {
			document.onmouseup = hideLayersNotClicked;
			}
			
			layer.obj.listening = true;
		}
	}
	catch (e)
	{
		alert("attachListener : " + e);
	}
}

function registerPopup(id) {
	// register this popup window with the popups array
	try
	{
		var layer = new pathToLayer(id);
		if (layer.obj.registered == null) {
		  var index = popups.length ? popups.length : 0;
		  popups[index] = layer;
		  layer.obj.registered = 1;
		}	
	}
	catch (e)
	{
		alert("registerPopup : " + e);
	}
	
}

function makeCalHtml(id, calYear, calMonth, calDay, inputName, validationScript, currentDay) {
   
	try
	{
		var arrDate = "";
		arrDate = currentDay.split("/");
		if(document.all[inputName] != null)
		{
			document.all[inputName].value = document.all[inputName].value.replace(/^\s+|\s+$/g, '');

			if(document.all[inputName].value == "")
			{
			}
			else
			{
				arrDate = document.all[inputName].value.split("-");
			}
		}
	
		if( calYear == null || calYear == "" ) calYear = arrDate[0];
		if( calMonth == null || calMonth == "" ) calMonth = arrDate[1];
		if( calDay == null || calDay == "" ) calDay = arrDate[2];
		var daysInMonth = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
		if ((calYear % 4 == 0 && calYear % 100 != 0) || calYear % 400 == 0) {
		  daysInMonth[2] = 29;
		}

		var calDate = new Date(calYear,calMonth-1,calDay);

		//-----------------------------------------
		// check if the currently selected day is
		// more than what our target month has
		//-----------------------------------------
		if (calMonth < calDate.getMonth()+1) {
		 calDay = calDay-calDate.getDate();
		 calDate = new Date(calYear,calMonth-1,calDay);
		}

		var calNextYear  = calDate.getMonth() == 11 ? calDate.getFullYear()+1 : calDate.getFullYear();
		var calNextMonth = calDate.getMonth() == 11 ? 1 : calDate.getMonth()+2;
		var calLastYear  = calDate.getMonth() == 0 ? calDate.getFullYear()-1 : calDate.getFullYear();
		var calLastMonth = calDate.getMonth() == 0 ? 12 : calDate.getMonth();

		var todayDate = new Date();

		//---------------------------------------------------------
		// this relies on the javascript bug-feature of offsetting
		// values over 31 days properly. Negative day offsets do NOT
		// work with Netscape 4.x, and negative months do not work
		// in Safari. This works everywhere.
		//---------------------------------------------------------
		var calStartOfThisMonthDate = new Date(calYear,calMonth-1,1);
		var calOffsetToFirstDayOfLastMonth = calStartOfThisMonthDate.getDay() >= wStart ? calStartOfThisMonthDate.getDay()-wStart : 7-wStart-calStartOfThisMonthDate.getDay()
		if (calOffsetToFirstDayOfLastMonth > 0) {
		  var calStartDate = new Date(calLastYear,calLastMonth-1,1); // we start in last month
		} else {
		  var calStartDate = new Date(calYear,calMonth-1,1); // we start in this month
		}
		var calStartYear = calStartDate.getFullYear();
		var calStartMonth = calStartDate.getMonth();
		var calCurrentDay = calOffsetToFirstDayOfLastMonth ? daysInMonth[calStartMonth+1]-(calOffsetToFirstDayOfLastMonth-1) : 1;

		var html = '';
		// writing the <html><head><body> causes some browsers (Konquerer) to fail
		   
		html += '<table border=0 cellpadding=0 cellspacing=0 bgcolor="#EEEEFF" align="">\n';
		html += '<tr height=9>\n';
		html += '<td width=9><img src="/images/tbl_round_01.gif" height=9 wight=9></td>\n';
		html += '<td background="/images/tbl_round_02.gif" height=9 wight=9><img src="/images/dot.gif" height=9></td>\n';
		html += '<td width=9><img src="/images/tbl_round_03.gif" height=9 wight=9></td>\n';
		html += '</tr><tr>\n';
		html += '<td background="/images/tbl_round_04.gif" height=9 wight=9>&nbsp;</td>\n';
		html += '<td>\n';
			// inner
			html += '<table width=100% cellpadding="0" cellspacing="1" border="0" bgcolor="#FFFFFF">\n';
			html += '<tr>\n';
			html += '<td valign="top">\n';
			html += '<table width=100% cellpadding="3" cellspacing="1" border="0">\n';
			html += '<tr>\n';
			html += '<td><a class="stylecal" href="#" onClick="updateCal(\''+id+'\','+calLastYear+','+calLastMonth+','+calDay+',\''+inputName+'\',\''+validationScript+'\',\''+currentDay+'\'); return false;">¢Ð</a></td>\n';
			html += '<td class="stylecal" align="center" colspan="5">&nbsp;<b>' +calDate.getFullYear()+'³â '+wMonth[calDate.getMonth()]+ '</b>&nbsp;</td>\n';
			html += '<td><a class="stylecal" href="#" onClick="updateCal(\''+id+'\','+calNextYear+','+calNextMonth+','+calDay+',\''+inputName+'\',\''+validationScript+'\',\''+currentDay+'\'); return false;">¢Ñ</a></td>\n';
			html += '</tr>\n';

			for (var row=1; row <= 7; row++) {
			  // check if we started a new month at the beginning of this row
			  upcomingDate = new Date(calStartYear,calStartMonth,calCurrentDay);
			  if (upcomingDate.getDate() <= 8 && row > 5) {
				 continue; // skip this row
			  }

			  html += '<tr>\n';
			  for (var col=0; col < 7; col++) {
				 //var tdColor = col % 2 ? '"#DAEDFE"' : '"#F8F8F8"';
				 var tdColor = '"#F3F3F3"';
				 if (row == 1) {
					tdColor = '"#EEEEFF"';
					html += '<td bgcolor='+tdColor+' align="center" class="stylecal">'+wDayAbbrev[(wStart+col)%7]+'</td>\n';
				 } else {
					var hereDate = new Date(calStartYear,calStartMonth,calCurrentDay);
					var hereDay = hereDate.getDate();
					var aClass = '"stylecal"';

					if (hereDate.getYear() == todayDate.getYear() && hereDate.getMonth() == todayDate.getMonth() && hereDate.getDate() == todayDate.getDate()) {
					   tdColor = '"#99AAFF"';
					}
					if (hereDate.getMonth() != calDate.getMonth()) {
					   tdColor = '"#CCCCCC"';
					   var aClass = '"notmonth"';
					}

					html += '<td bgcolor='+tdColor+' align="right" onClick="changeFormDate('+hereDate.getFullYear()+','+(hereDate.getMonth()+1)+','+hereDate.getDate()+',\''+inputName+'\',\''+validationScript+'\'); hideLayer(\''+id+'\'); return false;" onMouseOver="this.className=\'row_on_a\'" onMouseOut="this.className=\'row_off\'"><a class='+aClass+' href="#" onClick="changeFormDate('+hereDate.getFullYear()+','+(hereDate.getMonth()+1)+','+hereDate.getDate()+',\''+inputName+'\',\''+validationScript+'\'); hideLayer(\''+id+'\'); return false;">'+hereDay+'</a></td>\n';
					calCurrentDay++;
				 }
			  }
			  html += '</tr>\n';
			}
		   
			html += '<tr>\n';
			html += '<td align="center" bgcolor=#FFFFFF colspan="7" onClick="updateCal(\''+id+'\','+todayDate.getFullYear()+','+(todayDate.getMonth()+1)+','+todayDate.getDate()+',\''+inputName+'\',\''+validationScript+'\',\''+currentDay+'\'); return false;"><a class="stylecal" href="#" onClick="updateCal(\''+id+'\','+todayDate.getFullYear()+','+(todayDate.getMonth()+1)+','+todayDate.getDate()+',\''+inputName+'\',\''+validationScript+'\',\''+currentDay+'\'); return false;">¿À´Ã·Î ÀÌµ¿</a></td>\n';
			html += '</tr>\n';
			html += '</table>\n';
			html += '</td></tr>\n';
			html += '</table>\n';
			// end of inner

		html += '</td>\n';
		html += '<td background="/images/tbl_round_06.gif" height=9 wight=9>&nbsp;</td>\n';
		html += '</tr><tr height=9>\n';
		html += '<td width=9><img src="/images/tbl_round_07.gif" height=9 wight=9></td>\n';
		html += '<td background="/images/tbl_round_08.gif" height=9 wight=9><img src="/images/dot.gif" height=9></td>\n';
		html += '<td width=9><img src="/images/tbl_round_09.gif" height=9 wight=9></td>\n';
		html += '</tr>\n';
		html += '</table>\n';

	}
   catch (e)
   {
		alert("makeCalHtml : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
   }

   return html;
}

function updateCal(id, calYear, calMonth, calDay, inputName, validationScript, currentDay) {

	//alert(id +"/"+ calYear +"/"+ calMonth +"/"+ calDay +"/"+ inputName +"/"+ validationScript);
	
	try
	{
		calHtml = makeCalHtml(id,calYear,calMonth,calDay,inputName,validationScript, currentDay);
		writeLayer(id,calHtml);	
	}
	catch (e)
	{
		alert("updateCal : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
	}
	
}

function writeLayer(id, html) {

	try
	{
		var layer = new pathToLayer(id);
		if (nn4) {
		  layer.obj.document.open();
		  layer.obj.document.write(html);
		  layer.obj.document.close();
		} else {
		  layer.obj.innerHTML = '';
		  layer.obj.innerHTML = html;
		}

	}
	catch (e)
	{
		alert("writeLayer : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
	}

}

function setLayerPos(obj, id, xOffset, yOffset, yTop) {
	try
	{
			var newX = 0;
			var newY = 0;
			if (obj.offsetParent) {
			  // if called from href="setLayerPos(this,'example')" then obj will
			  // have no offsetParent properties. Use onClick= instead.
			  while (obj.offsetParent) {
				 newX += obj.offsetLeft;
				 newY += obj.offsetTop;
				 obj = obj.offsetParent;
			  }
			} else if (obj.x) {
			  // nn4 - only works with "a" tags
			  newX += obj.x;
			  newY += obj.y;
			}

			// apply the offsets
			switch(yTop)
			{
				case "TOP":
					newY += yOffset - 222;
				break;

				default:
					newY += yOffset ;
				break;
			}
			newX += xOffset;


			// apply the new positions to our layer
			//   var layer = new pathToLayer(id);
			var layer = document.getElementById( id );
			if (nn4) {
			  layer.style.left = newX;
			  layer.style.top  = newY;
			} else {
			  // the px avoids errors with doctype strict modes
			  layer.style.left = newX + 'px';
			  layer.style.top  = newY + 'px';
			}
	}
	catch (e)
	{
		alert("setLayerPos : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
	}
}

function hideLayersNotClicked(e) {
   if (!e) var e = window.event;
   e.cancelBubble = true;
   if (e.stopPropagation) e.stopPropagation();
   if (e.target) {
      var clicked = e.target;
   } else if (e.srcElement) {
      var clicked = e.srcElement;
   }

   // go through each popup window,
   // checking if it has been clicked
   for (var i=0; i < popups.length; i++) {
      if (nn4) {
         if ((popups[i].style.left < e.pageX) &&
             (popups[i].style.left+popups[i].style.clip.width > e.pageX) &&
             (popups[i].style.top < e.pageY) &&
             (popups[i].style.top+popups[i].style.clip.height > e.pageY)) {
            return true;
         } else {
            hideLayer(popups[i].obj.id);
            return true;
         }
      } else if (ie) {
         while (clicked.parentElement != null) {
            if (popups[i].obj.id == clicked.id) {
               return true;
            }
            clicked = clicked.parentElement;
         }
         hideLayer(popups[i].obj.id);
         return true;
      } else if (dom) {
         while (clicked.parentNode != null) {
            if (popups[i].obj.id == clicked.id) {
               return true;
            }
            clicked = clicked.parentNode;
         }
         hideLayer(popups[i].obj.id);
         return true;
      }
      return true;
   }
   return true;
}

function pathToLayer(id) {
	try
	{
		if (nn4) {
		  this.obj = document.layers[id];
		  this.style = document.layers[id];
	   } else if (ie) {
		  this.obj = document.all[id];
		  this.style = document.all[id].style;
	   } else {
		  this.obj = document.getElementById(id);
		  this.style = document.getElementById(id).style;
	   }	
	}
	catch (e)
	{
		alert("pathToLayer : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
	}   
}

function showLayer(id) {
   var layer = new pathToLayer(id)
   layer.style.visibility = "visible";
   //hideSelectList();
}
 
function hideLayer(id) {
   var layer = new pathToLayer(id);
   layer.style.visibility = "hidden";
   //showSelectList();
}

function changeFormDate(changeYear,changeMonth,changeDay,inputName,validationScript) {
	try
	{
		var strChangeMonth = "";
		var strChangeDay = "";

		var strLen = "ddddd";

		strChangeMonth =  "" + changeMonth + "";
		strChangeDay = "" + changeDay + "";
		

		if(strChangeMonth.length==1)
		{
			strChangeMonth = "0" + strChangeMonth;
		}

		if(strChangeDay.length==1)
		{
			strChangeDay = "0" + strChangeDay;
		}

		document.all[inputName].value = changeYear + "-" + strChangeMonth + "-" + strChangeDay;

		if (validationScript) {
			eval(validationScript+"('"+inputName+"')"); // to update the other selection boxes in the form
		}	
	}
	catch (e)
	{
		alert("changeFormDate : [" + e + "] [" + e.name + "] [" + e.message + "] [" + e.description + "]"); 
	}
 
}
// -->
