function selectday(field, date)
{
  window.opener.document.getElementById(field).value = date;
  window.close();
}

function openmonth(activityid, field, year, month, islocal, agencyid)
{
  loadCalendarContent(document, activityid, agencyid, field, islocal, month, year);
}

function checkpositivenumber(x)
{
	var anum=/^\d+$/;
	return anum.test(x);
}

var calendarwnd = null;

function calendar(activityid, field, islocal)
{
  calendar2(activityid, field, islocal, 0);
}

function calendar2(activityid, field, islocal, agencyid)
{
  if (!checkpositivenumber(activityid))
  {
    alert('Please select activity');
    return;
  }
  if (calendarwnd != null && !calendarwnd.closed)
  {
    calendarwnd.focus();
    return;
  }
  calendarwnd=window.open("", "calendar", "width=260,height=250,status=no,toolbar=no,menubar=no,location=no,resizable=yes");
  loadCalendarContent(calendarwnd.document, activityid, agencyid, field, islocal);
}

function getBaseUrl() {
	var myName = /^(.*[\/\\])common\/calendar\.js(\?|$)/;
	var scripts = document.getElementsByTagName("script");
	for (var i = 0; i < scripts.length; i++) {
		var src;
		if (src = scripts[i].getAttribute("src")) {
			if (src.match(myName)) {
				return src.replace(myName, "$1");
			}
		}
	}
}

function getBaseUrlWithReservation() 
{
  var url = getBaseUrl();
  if (!/reservation\/$/.test(url)) url += 'reservation/';
  return url;
}

function loadCalendarContent(d, activityid, agencyid, field, islocal, month, year)
{
  var baseurl = (islocal ? "" : getBaseUrlWithReservation());

  var contentUrl = "companyservlet?action=CALENDARPOPUPPAGE&activityid=" + activityid + "&agencyid=" + agencyid + "&field=" + field + "&islocal=" + (islocal ? "1" : "0");
  if(month != undefined) contentUrl = contentUrl + "&month=" + month;
  if(year != undefined) contentUrl = contentUrl + "&year=" + year;

  d.open("text/html", "replace");
  d.write("<html><head>"
          +"<script type='text/javascript' src='" + baseurl + contentUrl + "'></script>"
          +"<script type='text/javascript' src='" + baseurl + "common/calendar_js.jsp'></script>"
          +"<link rel='stylesheet' type='text/css' href='" + baseurl + "common/calendar.css.jsp'/>"
          +"</head><body onload='javascript:showContent();'><table width='100%' height=200><tr><td width=100% valign=center align=center><b>L o a d i n g . . .</b></td></tr></table></body></html>");
  d.close();
}
