/* File Name : calendar.js
 * File Type : Java script
 * Function  : Pop-up calendar utility functions
 * Date      : 23.JUL.2001
 * Author    : Katherine Lee
 */

/* 
 * Variables for calendar
 *
 * today         - date object to get date, month and year value
 * day           - used by pop-up calendar to store day value
 * month         - used by pop-up calendar to store month value
 * year          - used by pop-up calendar to store year value
 * jo_caller_fld - the text box object of the date field
 *
 */

  var fld_day   = '';
  var fld_month = '';
  var fld_year  = '';
  var fld_today = '';

  var jo_caller_fld;

/* 
 * Function name        call_calendar()
 *                      Invoke calendar pop-up window 
 *
 * @param po_fld_obj    (Field object of the date field - ie. text box)
 * @param ps_cal_url    Calendar jsp url
 *
 */
function call_calendar(po_fld_obj, ps_cal_url) {

  // Highlight the previous selected date
  // (The date entered in the text field - dd/mm/yyyy)
  if (po_fld_obj.value != '') {

    fld_day   = trimzero((po_fld_obj.value).substr(0,2));
    fld_month = trimzero((po_fld_obj.value).substr(3,2)) - 1;
    fld_year  = (po_fld_obj.value).substr(6,4);

  } else {  // Highlight today's day if no previous selected day

    today     = new Date();
    fld_day   = today.getDate();
    fld_month = today.getMonth();
    fld_year  = y2k(today.getYear());
 
  }

  day       = fld_day;
  month     = fld_month;
  year      = fld_year;

  cal_window = open(ps_cal_url,'Calendar','resizable=no, width=350, height=350');
  cal_window.location.href = ps_cal_url;
  jo_caller_fld = po_fld_obj;
  if (cal_window.opener == null) {
    cal_window.opener = self;
  }
}

/* 
 * Function name        restart()
 *
 * This function is called by the pop-up calendar to paste back the date field
 *
 */
function restart() {
  <!-- Remark: Return the date selection to the textbox -->
  jo_caller_fld.value = padout(day) + "/" + padout(month - 0 + 1) + "/" + y2k(year) ;
  cal_window.close();
}

/*
 * Fix j2k function
 */
function y2k(number) {
  return (number < 1000) ? number + 1900 : number;
}

/*
 * Pad zero in front of the number
 * (e.g. "5" --> "05")
 */
function padout(number) {
  return (number < 10) ? '0' + number : number;
}

/*
 * Trim zero in front of the number
 * (e.g. "07" --> "7")
 */
function trimzero(number) {
  return (number.substr(0,1) == 0) ? number.substr(1,1) : number;
}

/*
 * Check whether the date is in format of dd/mm/yyyy
 */
function j_chk_date_fmt(po_fld) {
  if (po_fld.value.length != 0) {
    if (po_fld.value.match(
      new RegExp('[0-9]{2}/[0-9]{2}/[0-9]{4}')) == null) {
      alert('Wrong date format');
      po_fld.value = '';
    }
    else {
      var ji_d = parseInt(po_fld.value.substr(0, 2), 10);
      var ji_m = parseInt(po_fld.value.substr(3, 2), 10)-1;
      var ji_y = parseInt(po_fld.value.substr(6), 10);
      var jo_date = new Date();
      jo_date.setFullYear(ji_y, ji_m, ji_d);
      if ((jo_date.getDate() != ji_d) ||
        (jo_date.getMonth() != ji_m) ||
        (jo_date.getFullYear() != ji_y)) {
        alert('Date not exist');
        po_fld.value = '';
      }
    }
  }
}

