///////////////////////////////////////
//  $RCSfile: date_functions.js,v $
// $Revision: 1.15 $
// Copyright: UNiREZ, Inc. 2003
///////////////////////////////////////

// For use with the date_select and start_end_select subroutines from
// wrsobj.pm.

// my $date = $w->date_select(-name => 'MY_DATE');
// or for a combo date_select:
// my $combo = $w->date_select(-name => 'COMBO', -combo => 1);
// or for a start and end date:
// my ($start_date, $end_date) = $w->start_end_select(-name => 'DATE');
// or for a combo start and end date:
// my ($in, $out) = $w->start_end_select(-name => 'DATE', -combo => 1);

var oneday = 86400000;

function validDate (day, mon, yr) {
// If the year, month and day passed in
// make a valid date, then return true
// otherwise return false.
  mon--; // month is zero based
  var test = new Date (day, mon, yr);
  return (mon == test.getMonth() &&
		  day   == test.getDate() &&
		  yr  == test.getFullYear()
         ) ? true : false;
}

function date2Array (date) {
// Takes a date object and turns it into an array.
  var mon = date.getMonth();
  var day = date.getDate();
  var yr = date.getFullYear();
  var datea = new Array(mon, day, yr);
  return datea;
}

function vDate (mon, day, yr) {
// Returns a date object if the year,
// month and day passed represents a valid date.
  if (!validDate(mon, day, yr)) {
    alert("Invalid date " + mon + "-" + day + "-" + yr + " passed.");
    return;
  }
  return new Date(mon - 1, day, yr);
}

function getDate (f, id, combo) {
// Gets the date from the form.
  var mon, day, yr;
  if (combo) {
    var temp = f.elements[id].value.split("/");
    mon = temp[0];
	day = temp[1];
    yr = temp[2];
  } else {
    yr = f.elements[id + 'YY'].value;
    mon = f.elements[id + 'MM'].value;
  }
  var date = vDate(mon, day, yr);
  return date;
}

function setDate (f, id, datea, combo) {
// Sets the date in the form.
// datea is an array created by date2Array
// the month should be in the range 0..11
  var yr = datea[0];
  var mon = datea[1] + 1;
  if (mon < 10) {
    mon = '0' + mon;
  }
  var day = datea[2];
  if (day < 10) {
    day = '0' + day;
  }
  f.elements[id + 'DD'].value = day;
  if (combo) {
    datea = Array(yr, mon);
    f.elements[id + 'YM'].value = datea.join(":");
  } else {
    f.elements[id + 'MM'].value = mon;
    f.elements[id + 'YY'].value = yr;
  }
}

function returnStart (start, end, book) {
// If the start date is greater than the end date,
// return a date that is equal to the start date.
  var enda = false;
  if (start >= end) {
    var tmp = start.getTime();
    if (book) {
      tmp += (book * oneday);
    }
    end.setTime(tmp);
    enda = date2Array(end);
  }
  return enda;
}

function returnEnd (start, end, book) {
// If the start date is greater than the end date,
// return a date that is equal to the end date.
  var starta = false;
  if (start >= end) {
    var tmp = end.getTime();
    if (book) {
      tmp -= (book * oneday);
    }
    start.setTime(tmp);
    starta = date2Array(start);
  }
  return starta;
}

function adjustStartEnd (f, id, startx, endx, sid, eid, start, end, combo, book) {
// Checks the start and end date and adjusts as needed.
  var eym,ey,em,sym,sy,sm;
  var ed = f.elements[eid+'DD'];
  var sd = f.elements[sid+'DD'];
  if (combo) {
    eym = f.elements[eid+'YM'];
    sym = f.elements[sid+'YM'];
  } else {
    ey = f.elements[eid+'YY'];
    em = f.elements[eid+'MM'];
    sy = f.elements[sid+'YY'];
    sm = f.elements[sid+'MM'];
  }
  if (startx.test(id)) {
    var enda = returnStart(start, end, book);
    if (combo) {
      if (sym.selectedIndex == (sym.length - 1) &&
          sd.selectedIndex == (sd.length - 1)) {
        eym.selectedIndex = eym.length - 1;
        ed.selectedIndex = ed.length - 1;
        sd.selectedIndex = sd.length - 2;
        return;
      }
    } else {
      if (sy.selectedIndex == (sy.length - 1) &&
          sm.selectedIndex == (sm.length - 1) &&
          sd.selectedIndex == (sd.length - 1)) {
        ey.selectedIndex = ey.length - 1;
        em.selectedIndex = em.length - 1;
        ed.selectedIndex = ed.length - 1;
        sd.selectedIndex = sd.length - 2;
        return;
      }
    }
    if (enda) {
      setDate(f, eid, enda, combo);
    }
  } else if (endx.test(id)) {
    var starta = returnEnd(start, end, book);
    if (combo) {
      if (eym.selectedIndex == 0 &&
          ed.selectedIndex == 0) {
        sym.selectedIndex = 0;
        sd.selectedIndex = 0;
        ed.selectedIndex = 1;
        return;
      }
    } else {
      if (ey.selectedIndex == 0 &&
          em.selectedIndex == 0 &&
          ed.selectedIndex == 0) {
        sy.selectedIndex = 0;
        sm.selectedIndex = 0;
        sd.selectedIndex = 0;
        ed.selectedIndex = 1;
        return;
      }
    }
    if (starta) {
      setDate(f, sid, starta, combo);
    }
    if (combo) {
      if (sym.selectedIndex < 0) {
        sym.selectedIndex = 0;
        sd.selectedIndex = 0;
      }
    } else {
      if (sy.selectedIndex < 0 || sm.selectedIndex < 0) {
        sy.selectedIndex = 0;
        sm.selectedIndex = 0;
        sd.selectedIndex = 0;
      }
    }
  }
}

function checkStartEnd (f, id, combo, book) {
// Checks the start or end date and corrects
// the other as needed given:
  var sid, eid, start, end, startx, endx;
  if (combo) {
    startx = /^IN/;
    endx = /^OUT/;
    if (startx.test(id)) {
      sid = id;
      eid = 'OUT' + id.substr(2);
    } else if (endx.test(id)) {
      sid = 'IN' + id.substr(3);
      eid = id;
    } else {
      alert("Unknown id: " + id);
      return;
    }
  } else {
    startx = /^START/;
    endx = /^END/;
    if (startx.test(id)) {
      sid = id;
      eid = 'END' + id.substr(5);
    } else if (endx.test(id)) {
      sid = 'START' + id.substr(3);
      eid = id;
    } else {
      alert("Unknown id: " + id);
      return;
    }
  }
  start = getDate(f, sid, combo);
  end = getDate(f, eid, combo);

  adjustStartEnd (f, id, startx, endx, sid, eid, start, end, combo, book);
}

function swapStartEnd (f, id, combo) {
// Checks the start and end date and swaps them
// if the start date is later than the end date.
  var startx = /^START/;
  var endx = /^END/;
  var sid, eid, start, end;
  if (combo) {
    startx = /^IN/;
    endx = /^OUT/;
    if (startx.test(id)) {
      sid = id;
      eid = 'OUT' + id.substr(2);
    } else if (endx.test(id)) {
      sid = 'IN' + id.substr(3);
      eid = id;
    } else {
      alert("Unknown id: " + id);
      return;
    }
  } else {
    if (startx.test(id)) {
      sid = id;
      eid = 'END' + id.substr(5);
    } else if (endx.test(id)) {
      sid = 'START' + id.substr(3);
      eid = id;
    } else {
      alert("Unknown id: " + id);
      return;
    }
  }
  var start, end;
  start = getDate(f, sid, combo);
  end = getDate(f, eid, combo);
  if (start > end) {
    var temp = start;
    start = end;
    end = temp;
    var starta = date2array(start);
    var enda = date2array(end);
    setDate(f, sid, starta, combo);
    setDate(f, eid, enda, combo);
  } else if (start == end) {
    adjustStartEnd (f, id, startx, endx, sid, eid, start, end, combo);
  }
}

function fixDate (f, id, combo) {
// Fixes invalid dates like 31 Apr 2003 or 29 Feb 2003.
  if (combo) {
    // grabs the form values and sets the form elements later
    var day = f.elements[id + 'DD'].value;
    var temp = f.elements[id + 'YM'].value.split(":");
    var yr = temp[0];
    var mon = temp[1];
    while (!validDate(mon, day, yr)) {  // valid date yet?
      day--;  // no?  move back one day
    }
    if (day.length < 2 && day < 10) {
      day = '0' . day;
    }
    temp = Array(yr, mon);
    f.elements[id + 'DD'].value = day;
    f.elements[id + 'YM'].value = temp.join(":");
  } else {
    // works directly on the form elements
    var day = f.elements[id + 'DD'];
    var mon = f.elements[id + 'MM'];
    var yr = f.elements[id + 'YY'];
    while (!validDate(yr.value, mon.value, day.value)) {  // valid date yet?
      day.value--;  // no?  move back one day
    }
  }
}

function checkDates (e, pair, book) {
// This is the basic function to call when you want to check the dates.
// If you have two date selections (ie a start and end date), you need
// to pass in a true value for pair. (This is done automatically by
// start_end_select.)
// If book is set to 1, then the dates are for booking reservations
// and the start and end date cannot be the same.
  if (book) {
    var app = navigator.appName;
    var plat = navigator.platform;
    if (app != 'Microsoft Internet Explorer' || plat != 'Win32') {
      return;
    }
  }
  var f = e.form; // which form?
  var n = e.name; // name of this element?
  var id = n.substr(0, n.length - 2);
  var combo = 0;
  var inout = new RegExp("^IN|^OUT");
  if (inout.test(id)) {
    combo = 1;
  }
  fixDate(f, id, combo);
  if (pair) {
    checkStartEnd(f, id, combo, book);
  }
}

