var calForm = "";
var calSDay = "";
var calSMonth = "";
childwin = null;

var cal_isNetscape = navigator.appName.indexOf("Netscape") != -1;

var cal_today = new Date();
var cal_currentDate = cal_today.getDate();
var cal_currentMonth = cal_today.getMonth();
var cal_currentYear = cal_today.getYear();

if (cal_isNetscape) cal_currentYear += 1900;

function openCalendar(tbForm, sDay, sMonth, e) {
   self.calForm = tbForm;
   self.calSDay = sDay;
   self.calSMonth = sMonth;

   w = screen.width-350
   h = screen.height-220
   x = e.screenX
   y = e.screenY
      if(x > w){
         x = x-350
         }
      if(y > h){
         y = y-220
         }

   var calendarUrl = "/components/popups/calendar.jsp";

   var monthSel = document.getElementsByName(calSMonth);

   if (monthSel != null) {
      var selectedMonthIndex = monthSel[0].selectedIndex;
      var month = selectedMonthIndex;
      var year =  cal_currentYear;

      if (month < cal_currentMonth ) {
         year = year  + 1;
      }
      calendarUrl = calendarUrl + "?y=" + year + "&m=" + month;

   }
   childwin=window.open(calendarUrl,'Calendar','width=460,height=280,top='+y+',left='+x);
}

function setDate(day, month) {
   var daySel = document.getElementsByName(calSDay);
   daySel[0].options[day - 1].selected = true;
   var monthSel = document.getElementsByName(calSMonth);
   monthSel[0].options[month].selected = true;
}

function setRetMonth(tbForm, deptMonth, retMonth){
   var box1 = document.getElementsByName(deptMonth);
   var box2 = document.getElementsByName(retMonth);
   box2[0].selectedIndex = box1[0].selectedIndex;
}

//when dept date chnages , change destination date to +7 days
function setRetDate(deptMonth, retMonth, deptDay, retDay){
   var dayBox1 = document.getElementsByName(deptDay);
   var dayBox2 = document.getElementsByName(retDay);
   var monthBox1 = document.getElementsByName(deptMonth);
   var monthBox2 = document.getElementsByName(retMonth);
   var curDay = dayBox1[0].selectedIndex + 1; //since its 0 based add 1
   var curMonth = monthBox1[0].selectedIndex; //its 0 based, so is the drop box
 
   var cal = new Date();
   cal.setMonth(curMonth);
   cal.setDate(curDay + 7);

   dayBox2[0].selectedIndex = cal.getDate() - 1;
   monthBox2[0].selectedIndex = cal.getMonth();
}

// this will auto close the calendar window if no date is selected

function closeMe(){
   if (childwin != null){
   childwin.close()
   childwin = null
   }
}