
// Paul Suda - Nov. 2006
// Calendar popup widget, for date selection.

__calendar_list = new Object();

// This is a constructor, that should be called with a popup_id, and
// javascript code to call when a selection is made.
function calendar_popup_object(popup_id, select_handler){
  // Define our month names. Need to figure out how to do this for other
  // locales.
  this.month_names = new Array(
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  // Remember parameters.
  this.popup_id = popup_id;
  // Set up object methods.
  this.cal_week_html = cal_week_html;
  this.cal_table_html = cal_table_html;
  this.select_handler = select_handler;
  this.cal_nav = cal_nav;
  this.cal_do_popup = cal_do_popup;
  this.cal_close_popup = cal_close_popup;
  // Set by caller, if a date has been selected.
  this.selected_date = null;
  // Remember the object for later.
  __calendar_list[popup_id] = this;
}

// // // Below are start-up functions used by form cal icon controls. // // //

// This is used by a page element to launch a new calendar, the form element
// IDs of the year, month, and day are set.
function event_calendar_popup(popup_id, year_id, month_id, day_id, refresh_end){
  var calendar = new calendar_popup_object(popup_id, cal_update_event);
  // Get the start date.
  var start = null;
  if($(year_id).value > 0 && $(day_id).value > 0 && $(month_id).value > 0){
    start = new Date($(year_id).value, $(month_id).value - 1, $(day_id).value);
  }
  else{
    start = new Date();
  }
  // Add event fields.
  calendar.year_id = year_id;
  calendar.month_id = month_id;
  calendar.day_id = day_id;
  calendar.refresh_end = refresh_end;
  calendar.selected_date = start;
  // Create the calendar, starting with today.
  calendar.cal_do_popup(start);
  return calendar;
}

// This is used by a text form element, to create a calendar for filling
// in a text format date.
function text_calendar_popup(popup_id, text_id){
  var calendar = new calendar_popup_object(popup_id, cal_update_text);
  // Add event fields.
  calendar.text_id = text_id;
  // Create the calendar, starting with today.
  calendar.cal_do_popup(new Date());
  return calendar;
}


// // // All functions below are methods of the calendar object. // // //

// Sets the calendar element to nothing, clearing the control from the
// screen.
function cal_close_popup(){
  var element = $(this.popup_id);
  element.innerHTML = '';
}

// This sets the end date control equal to the start date control.
function cal_do_popup(start){
  var element = $(this.popup_id);
  // If the popup is empty, fill it with the calendar.
  if(element.innerHTML == ''){
    element.innerHTML = this.cal_table_html(start);
  }
  else{
    // If this element is already open, close it.
    this.cal_close_popup();
  }
}


// // // Event Handlers... // // //

// The default event handler. For debugging, never really gets used.
function cal_update_default(year, month, day){
  alert("Selected: " + year + "-" + (month + 1) + "-" + day);
}

// Handles clicks on the month days, for the event module controls.
function cal_update_event(year, month, day){
  $(this.year_id).value = year;
  $(this.month_id).value = month + 1;
  $(this.day_id).value = day;
  // This auto-sets the end date, if that option is set.
  if(this.refresh_end){
    event_check_end();
  }
  // Close the control, after setting.
  this.cal_close_popup();
}

// Handle requests related to text controls.
function cal_update_text(year, month, day){
  var date_text = year + "-" +
    cal_pad_date_num(month + 1) + "-" + cal_pad_date_num(day);
  var value = $(this.text_id).value
  $(this.text_id).value = date_text + value.substring(10);
  this.cal_close_popup();
}

// Pad nubers to two zeroes, for date formatting.
function cal_pad_date_num(x){
  if(x < 10){
    return "0" + x;
  }
  return x;
}

// This handles clicks on the forward and backward nav buttons.
function cal_nav(year, month){
  var start = new Date(year, month, 1);
  this.cal_close_popup();
  this.cal_do_popup(start); 
}

// // // HTML Generation... // // //

// Generates the content for the calendar popup.
function cal_table_html(start){
  var html = '';
  var index = new Date(start);
  var this_month = start.getMonth();
  var this_year = start.getFullYear();
  var object_name = '__calendar_list["' + this.popup_id + '"]';
  // Get the start of the month.
  index.setHours(0);
  index.setMinutes(0);
  index.setDate(1);
  // add a control row at the top.
  html = html + "<tr class='cal-top-row'>" +
    "<td class='onclick-text' align='left' onclick='" +
      object_name + ".cal_nav(" +
      this_year + ', ' + (this_month - 1) + 
      ");'>&lt;</td>" +
    "<td colspan='5' align='center'><strong>" +
      this.month_names[this_month] + " " + index.getFullYear() +
      "</strong></td>" +
    "<td class='onclick-text' align='right' onclick='" +
      object_name + ".cal_nav(" +
      this_year + ", " + (this_month + 1) +
      ");'>&gt;</td>" +
   "</tr>";
  // Get the week html.
  do{
    html = html + this.cal_week_html(index);
    // Advance to next week start.
    index.setDate(index.getDate() + (7 - index.getDay()));
  } while(index.getMonth() == this_month);
  // add a control row at the bottom.
  html = html +
    "<tr class='cal-bottom-row'><td colspan='7' align='right' class='onclick-text' onclick='" +
    object_name + ".cal_close_popup(" +
    '"' + this.popup_id + '"' + 
    ");'>Close <strong class='warn'>X</strong></td></tr>";
  // Form table.
  html = "<table class='cal-popup'>" + html + "</table>";
  return html;
}

// Generates the HTML for a calendar week.
function cal_week_html(start){
  var html = '';
  var blank_days = start.getDay();
  var d = new Date(start);
  var end = new Date(start);
  var this_month = start.getMonth();
  var object_name = "__calendar_list['" + this.popup_id + "']";
  var td_class = "onclick-text";
  // Get week end date.
  end.setDate(end.getDate() + (6 - blank_days));
  // Fill in blank days.
  if(blank_days > 0){
    html = html + "<td colspan='" + blank_days + "'> &nbsp; </td>";
  }
  // Do the calendar week days.
  do{
    // Format calendar day.
    var year = d.getFullYear();
    var month = d.getMonth();
    var mday = d.getDate();
    if((this.selected_date != null) &&
       (this.selected_date.getFullYear() == year) &&
       (this.selected_date.getMonth() == month) &&
       (this.selected_date.getDate() == mday)
       ){
      td_class = "selected-onclick-text";
    }
    else{
      td_class = "onclick-text";
    }
    html = html +
      '<td class="' + td_class + '" align="left" onclick="' +
      object_name +
      '.select_handler(' +
      year + "," + 
      month + "," + 
      mday + 
      ')">' + 
      mday + "</td>";
    // Advance to next day.
    d.setDate(mday + 1);
  } while(d <= end && d.getMonth() == this_month);
  // put in row
  html = "<tr class='cal-week-row'>" + html + "</tr>";
  return html;
}

