Today  = new Date();
Tomorrow = new Date(new Date - 0 + 86400000);

var TodayDay = Today.getDate();
var TodayMonth = Today.getMonth()+1;
var TodayYear = Today.getYear();
var TodayHours = Today.getHours();
var TodayMinutes = Today.getMinutes() ;
//alert(TodayMonth);

if (TodayYear < 2000) TodayYear += 1900; //for Netscape

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11) DaysInMonth = 30;
  if (WhichMonth == 2 && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == 2 && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function chinp()
{
  Which = 'FirstSelect';
  DaysObject = eval("document.weform.resdate");
  MonthObject = eval("document.weform.resmonth");
  YearObject = eval("document.weform.resyear");

  Month = MonthObject[MonthObject.selectedIndex].value;
  Year = YearObject[YearObject.selectedIndex].value;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;


  if (CurrentDaysInSelection > DaysForThisSelection){
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++){
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection){
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++){
      NewOption = new Option(DaysObject.options.length + 1, DaysObject.options.length + 1);
	    DaysObject.options[DaysObject.options.length] = NewOption;
    }
  }
  if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

//==============================================================================
function FormsOnLoad(){
  if (document.gwreserv_form){
    if (!document.gwreserv_form || !document.gwreserv_form.resmonth) return;

    DaysObject = document.gwreserv_form.resdate;
    MonthObject = document.gwreserv_form.resmonth;
    HoursObject = document.gwreserv_form.reshours;

    if (
      (DaysObject.value < TodayDay && MonthObject.value <= TodayMonth) ||
      (DaysObject.value == TodayDay && MonthObject.value == TodayMonth && (HoursObject.value <= parseInt(TodayHours) + 2) )
      )
    {
      document.getElementById("calendar-inputField").value = Calendar.printDate(Today, "%d %B %Y");
      CalenderPrepareDate(Today);
    }
    SetTimes();
  }else{
    if (!document.weform || !document.weform.resmonth)return;

    DaysObject = document.weform.resdate;
    MonthObject = document.weform.resmonth;
    HoursObject = document.weform.reshours;

    //alert('1) '+DaysObject.value+'   ---   '+MonthObject.value+' = '+TodayMonth+'   ---   '+HoursObject.value);
    if (
      (DaysObject.value == 1 && (MonthObject.value) == 1) ||
      (DaysObject.value < TodayDay && MonthObject.value <= TodayMonth) ||
      (DaysObject.value == TodayDay && MonthObject.value == TodayMonth && (HoursObject.value <= parseInt(TodayHours) + 2) )
      )
    {
      SetToToday();
    }
    chinp();
  }
};

function SetTimes(){
  if (!document.gwreserv_form || !document.gwreserv_form.resmonth) return;

  DaysObject = document.gwreserv_form.resdate;
  MonthObject = document.gwreserv_form.resmonth;
  YearObject = document.gwreserv_form.resyear;
  HoursObject = document.gwreserv_form.reshours;

  //alert('2) '+DaysObject.value+'   ---   '+MonthObject.value+' = '+TodayMonth+'   ---   '+HoursObject.value);
  if(DaysObject.value == TodayDay && MonthObject.value == (TodayMonth+1) && (HoursObject.value <= parseInt(TodayHours) + 2)){
    if(TodayHours == 12 && TodayHours <= 20){ HoursObject.value = parseInt(TodayHours) + 2 }
    else if (TodayHours < 12){ HoursObject.value = 14 }
    else if(TodayHours > 20){
      document.getElementById("calendar-inputField").value = Calendar.printDate(Tomorrow, "%d %B %Y");
      CalenderPrepareDate(Tomorrow);
    }
    //document.getElementById("reshours").options[0].selected = true;
  }
};

function CalenderPrepareDate(o){
  var dates = Calendar.intToDate(o);
  var date1 = Calendar.printDate(dates, "%e");
  var date2 = Calendar.printDate(dates, "%o");
  var date3 = Calendar.printDate(dates, "%Y");

  document.getElementById("resdate").options[0] = new Option(date1, date1);
  document.getElementById("resmonth").options[0] = new Option(date2, date2);
  document.getElementById("resyear").options[0] = new Option(date3, date3);
};

function SetToToday(){
  if (!document.weform || !document.weform.resmonth)return;

  DaysObject = document.weform.resdate;
  MonthObject = document.weform.resmonth;
  YearObject = document.weform.resyear;
  HoursObject = document.weform.reshours;

  DaysObject.value = TodayDay;
  MonthObject.value = TodayMonth;

  if(DaysObject.value == TodayDay && MonthObject.value == TodayMonth){
    if(TodayHours >= 19 && TodayHours <= 21){ HoursObject.value = parseInt(TodayHours) + 2 }
    else if (TodayHours < 19){ HoursObject.value = 21 }
    else if(TodayHours > 21){
      DaysObject.value = TodayDay + 1;
      MonthObject.value = TodayMonth + 1;
      //document.getElementById("calendar-inputField").value = Calendar.printDate(Tomorrow, "%d %B %Y");
      //CalenderPrepareDate(Tomorrow);
    }
    //document.getElementById("reshours").options[0].selected = true;
  }
}


function SetTimeRange(){
  if (!document.weform || !document.weform.resmonth)return;

  DaysObject = document.weform.resdate;
  MonthObject = document.weform.resmonth;
  YearObject = document.weform.resyear;
  HoursObject = document.weform.reshours;

  chinp;

  if (DaysObject.value == TodayDay && MonthObject.value == TodayMonth && TodayHours < 22)
  {
    HoursObject.value = parseInt(TodayHours) + 1;
  }
  else if(DaysObject.value <= TodayDay && MonthObject.value <= TodayMonth && YearObject.value <= TodayYear){
    DaysObject[TodayDay-1].selected = true;
    MonthObject[TodayMonth].selected = true;
    YearObject.value = TodayYear;
    HoursObject.value = parseInt(TodayHours) + 1;
  }
}

