// Title: Tigra Calendar PRO
// URL: http://www.softcomplex.com/products/tigra_calendar_pro/
// Version: 1.1 (pop-up mode)
// Date: 06-20-2002 (mm-dd-yyyy)
// Technical Support: support@softcomplex.com (specify product title and order ID)
// Notes: This Script is shareware. Please visit url above for registration details.

var calendars = [];

// Constructor
function calendar(str_date, day_control, mon_control, str_min_date, str_max_date) {

  this.synchHotelDates = false;
  this.popup = cal_popup;
  this.change_popup = cal_change_popup;

  this.id = calendars.length;
  calendars[this.id] = this;

  if (!day_control || !mon_control)
    return alert("Form element specified can't be found in the document.");
  this.control_day = day_control;
  this.control_mon = mon_control;

  var dt_params = (str_date ? cal_parse_date(str_date) : cal_date_only());

  var re_num = /^\-?\d+$/;
  var dt_startDate;
  if (str_min_date != null) {
    dt_startDate = (re_num.exec(str_min_date)
      ? new Date (dt_params.valueOf() - new Number(str_min_date * 864e5))
      : cal_parse_date(str_min_date)
    )
  }
  else
  {
    dt_startDate = new Date();
    dt_startDate.setHours(0);
    dt_startDate.setMinutes(0);
    dt_startDate.setSeconds(0);
    dt_startDate.setMilliseconds(0);
  }
  this.min_date = dt_startDate.valueOf();

  var dt_endDate;
  if (str_max_date != null)
  {
    dt_endDate = (re_num.exec(str_max_date)
      ? new Date (dt_params.valueOf() + new Number(str_max_date * 864e5))
      : cal_parse_date(str_max_date)
    );
  }
  else
  {
    var endYear = dt_startDate.getFullYear() + 1;
    dt_endDate = new Date(endYear, dt_startDate.getMonth(), dt_startDate.getDate());
    dt_endDate.setHours(0);
    dt_endDate.setMinutes(0);
    dt_endDate.setSeconds(0);
    dt_endDate.setMilliseconds(0);
  }

  this.max_date = dt_endDate.valueOf();

  this.dt_current = dt_params;
}

function cal_popup (popupPage, num_datetime) {
  this.popupPage = popupPage;
  if (num_datetime)
  {
    this.change_popup(num_datetime);
  }
  else
  {
    this.change_popup(null);
  }
}

function cal_change_popup (num_datetime, b_end) {

  var dayVal = this.control_day.options[this.control_day.selectedIndex].value;
  var monVal = this.control_mon.options[this.control_mon.selectedIndex].value;

  if (num_datetime)
  {
    this.dt_current = new Date(num_datetime);
  }
  else  if ((dayVal != '') && (monVal != ''))
  {
    var dateStr = dayVal + "-" + monVal;

    this.dt_current = cal_parse_date(dateStr);

    var currentDateAsTime = this.dt_current.getTime();

    if(this.min_date != null)
    {
        if(currentDateAsTime < this.min_date)
        {
            this.dt_current = new Date(this.min_date);
        }
    }

    if(this.max_date != null)
    {
        if(currentDateAsTime > this.max_date)
        {
            this.dt_current = new Date(this.max_date);
        }
    }
  }

  //this.control_obj.value = cal_generate_date(this.dt_current);

  //if (b_end) return;

  dayVal = this.dt_current.getDate();
  //calculate the month/year combination string with 100 extra months so that 1 becomes 101
  //and we get the leading zero that we need
  monVal = (this.dt_current.getMonth() + 101) + "-" + this.dt_current.getFullYear();
  //now throw away the hundreds column
  monVal = monVal.substr(1);

  for (i = 0; i < this.control_day.length; i++)
  {
    if (this.control_day.options[i].value == dayVal)
    {
      this.control_day.selectedIndex = i;
      break;
    }
  }

  for (i = 0; i < this.control_mon.length; i++)
  {
    if (this.control_mon.options[i].value == monVal)
    {
      this.control_mon.selectedIndex = i;
      break;
    }
  }

  if (!this.obj_calwindow || this.obj_calwindow.closed)
  {
    this.obj_calwindow = window.open(
    this.popupPage + '?datetime='
    + this.dt_current.valueOf()
    + '&id=' + this.id, 'cal_window',
    'width=225,height=250,left=200,top=200status=no,resizable=no,dependent=yes,alwaysRaised=yes'
    );
  }
  else
  {
    //modify the search criteria for this window
    this.obj_calwindow.location.search='?datetime='
    + this.dt_current.valueOf()
    + '&id=' + this.id;
  }

  this.obj_calwindow.opener = window;
  this.obj_calwindow.focus();
}

function cal_date_only (dt_datetime) {
  if (!dt_datetime)
    dt_datetime = new Date();
  dt_datetime.setHours(0);
  dt_datetime.setMinutes(0);
  dt_datetime.setSeconds(0);
  dt_datetime.setMilliseconds(0);
  return dt_datetime;
}
