/*  Required variables

  _id_from_date
  _id_to_date
  _default_todate_offset
  _default_fromdate_offset
  _default_max_rental_time
  _default_date_format

*/


/*   Calendar setup and functions. Configuratin options and variable definition  */


/*
   Is called for every day of the month when calendar is displayed onscreen.
   Receives the Javascript Date object of the date the check is supposed to be made on.
   If returns "true", the date is rendered as 'disabled'
*/


function fromdate_disableDate_callback(givenDate)
{
    givenDate.setHours(0);
    givenDate.setMinutes(0);
    givenDate.setSeconds(0);

    var todayDate = new Date();
    var dateDisabled;

    ////  disable all dates selection prior the current day
    //
    if (givenDate.getTime()+Date.DAY > todayDate.getTime())
        dateDisabled = false;
    else
        dateDisabled = true;

    return dateDisabled;
}



/*
   Calendar functions are called whenever the date in the 'fromdate' calendar is changed.
   Receives a instance of the calendar object
*/


function fromdate_onUpdate_callback(cal)
{
    var cal_fromday = Math.round(cal.date.getTime()/Date.DAY);
    var tempdate        = new Date();
    var now_day         = Math.round(tempdate.getTime()/Date.DAY);

    if (cal_fromday < now_day)
    {
        $(_id_from_date).value = tempdate.print(cal.params.ifFormat);
        cal.date.setTime(tempdate.getTime());
        cal.refresh();
    }


    ////  update the "toDate" field according to the "fromDate" field date value
    //
    var tempdate = new Date();
    tempdate.setTime(cal.date.getTime()+(_default_todate_offset-_default_fromdate_offset));

    $(_id_to_date).value = tempdate.print(cal.params.ifFormat);
}


/*
   Is called whenever the date in the 'fromdate' calendar is changed.
   Receives a instance of the calendar object
*/



function todate_onUpdate_callback(cal)
{
    return true;
}


/*
   Is called for every day of the month when calendar is displayed onscreen.
   Receives the Javascript Date object of the date the check is supposed to be made on.
   If returns "true", the date is rendered as 'disabled'
*/


function todate_disableDate_callback(date)
{
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);

    var cal_date          = date.getTime();
    var current_from_date = date_get_from_value($F(_id_from_date), '00', '00')
    var max_time          = current_from_date + _default_max_rental_time;


    if (cal_date <= max_time && cal_date+Date.DAY > current_from_date)
    {
        return false;
    }
    else
        return true;
}














 //
 ////  Calendar
 //



 ////  Make sure we have only the current and the next years enabled in the calendar.
 //    yearRange is an array of 2 elements, [startYear, endYear]
 //
 var yearRange;
 var tmpDate     = new Date();
 var currentYear = tmpDate.getFullYear();
 yearsRange      = [currentYear, currentYear+1];


 ////  Calendar setup constructs
 //
 Calendar.setup
 ({
     inputField     :    _id_from_date,
     button         :    _id_from_date+'_calendar',
     ifFormat       :    _default_date_format,
     singleClick    :    true,
     firstDay       :    1,
     align          :    "Br",
     range          :    yearsRange,
     weekNumbers    :    false,
     disableFunc    :    fromdate_disableDate_callback,
     onUpdate       :    fromdate_onUpdate_callback,
     showsTime      :    false,
     timeFormat     :    "24",
     step           :    1,
     position       :    null
 });

 Calendar.setup
 ({
     inputField     :    _id_to_date,
     button         :    _id_to_date+'_calendar',
     ifFormat       :    _default_date_format,
     singleClick    :    true,
     firstDay       :    1,
     align          :    "Br",
     range          :    yearsRange,
     weekNumbers    :    false,
     disableFunc    :    todate_disableDate_callback,
     onUpdate       :    todate_onUpdate_callback,
     showsTime      :    false,
     timeFormat     :    "24",
     step           :    1,
     position       :    null
 });

