var daysinmonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31); 

// Setup calendar code
var cal = new CalendarPopup("cal1");
function resetCalendar() {
  cal = new CalendarPopup("cal1");
  cal.offsetX = -10;
/*  cal.offsetY = -375;*/
  cal.offsetY = -100;

  cal.setCssPrefix("TEST");
  
  cal.setReturnFunction("setMultipleValues");
  curDate = new Date();
  curDate.setDate(curDate.getDate() - 1);
  cal.addDisabledDates(null, curDate.getFullYear() + "-" + (curDate.getMonth()+1) + "-" + curDate.getDate());
}

resetCalendar();

function formInitialize() {
  var curDate = new Date();
  daysSelect = document.getElementById('select1');
  i = 0;
  daysSelect.length = daysinmonth[curDate.getMonth()] - curDate.getDate() + 1;
  for(d = curDate.getDate(); d <= daysinmonth[curDate.getMonth()]; d++) {
    daysSelect[i] = new Option(d, d);
    i++;
  }
  var endDt = new Date();
  endDt.setDate(curDate.getDate() + 26 * 7);
  monthSelect = document.getElementById('select2');

  var monthDiff = 0;
  if (endDt.getFullYear() > curDate.getFullYear()) {
    if((endDt.getFullYear() - curDate.getFullYear()) > 1) {
      monthDiff = monthDiff + ((endDt.getYear() - curDate.getYear()) - 1) * 12;
    }
    monthDiff = monthDiff + (11 - curDate.getMonth() + endDt.getMonth() + 1);
  } else if(endDt.getMonth() > curDate.getMonth()) {
    monthDiff = endDt.getMonth() - curDate.getMonth();
  }
   
  curDate.setDate(1);
  for(var i = 0; i < monthDiff + 1; i++) {
    var optionValue = formatDate(curDate,"NNN / yyyy");
    var optionName = formatDate(curDate,"MM/yyyy");
    var month = curDate.getMonth();

    monthSelect[i] = new Option(optionValue,optionName);

    curDate.setMonth(curDate.getMonth() + 1);
  }
  curDate = new Date();
  document.MBE.arrDate.value = monthSelect.value.substring(5) + monthSelect.value.substring(0,2) + curDate.getDate();
  cal.addDisabledDates(endDt.getFullYear() + "-" + (endDt.getMonth()+1) + "-" + endDt.getDate(), null); 
}

function changemonth() {
  monthSelect = document.getElementById('select2');
  strDate = monthSelect.value.substring(0,2) + '/01/' + monthSelect.value.substring(3);
  selDate = new Date(strDate);
  curDate = new Date();
  if(curDate.getMonth() == selDate.getMonth() && curDate.getFullYear() == selDate.getFullYear()) {
    selDate.setDate(curDate.getDate());
  }
  endDt = new Date();
  endDt.setDate(curDate.getDate() + 26 * 7);
  daysSelect = document.getElementById('select1');
  i = 0;
  if(endDt.getMonth() == selDate.getMonth()) {
    daysSelect.length = endDt.getDate() - selDate.getDate();
  } else {
    daysSelect.length = daysinmonth[selDate.getMonth()] - selDate.getDate() + 1;
  }
  for(d = selDate.getDate(); d <= daysinmonth[selDate.getMonth()]; d++) {
    if(endDt.getFullYear() == selDate.getFullYear() && endDt.getMonth() == selDate.getMonth() && d == endDt.getDate()) 
       break;
    daysSelect[i] = new Option(d, d);
    i++;
  }
  document.MBE.arrDate.value = monthSelect.value.substring(5) + monthSelect.value.substring(0,2) + "01";
}

function setMultipleValues(y,m,d) 
{
  var curform = document.getElementById('MBE');
  var tempdate;
  if (m<10)
  {
    tempdate = "0" + m+ "/" + y;
  }
  else
  {
    tempdate = m+ "/" + y;
  }

  for (var i=0; i<curform.arrMonthYear.options.length; i++) 
  {
    if (curform.arrMonthYear.options[i].value==tempdate) 
    {
      curform.arrMonthYear.selectedIndex=i;
    }
  }

  changemonth();
  
  for (var i=0; i<curform.arrDay.options.length; i++) 
  {
    if (curform.arrDay.options[i].value==d) 
    {
      curform.arrDay.selectedIndex=i;
    }
  }
  document.MBE.arrDate.value = y.toString().substring(2) + ((m < 10) ? "0" + m : m) + ((d < 10) ? "0" + d : d);
}

function changeday() {
  var curform = document.getElementById('MBE');
  my = curform.arrMonthYear.value;
  d = parseInt(curform.arrDay.value);
  document.MBE.arrDate.value = my.substring(5) + my.substring(0,2) + ((d < 10) ? "0" + d : d);
}
