
var bd_DaysShort = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
var bd_DaysMed = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var bd_DaysLong = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
var bd_MonthsShort = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var bd_MonthsMed = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
var bd_MonthsLong = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
 
var bd_DefaultSeparator = " ";    // common values would be "/" or "."
var bd_DefaultFormat = "dmmmy";   // valid values are "mdy", "dmy", and "ymd"

Date.prototype.Copy = function() {
  var i_Date = new Date();
  i_Date.setTime(this.getTime());
  if (typeof this.Format != "undefined") {
    i_Date.SetFormat(this.Format); }
  if (typeof this.Separator != "undefined") {
    i_Date.SetSeparator(this.Separator); }
  return i_Date;
}
Date.prototype.AddDays = function(p_Days) {
  this.setDate(this.getDate() + p_Days);
}
Date.prototype.AddMonths = function(p_Months) {
  this.setMonth(this.getMonth() + p_Months);
}
Date.prototype.DaysDiff = function(p_Date) {
  var i_Day = 1000 * 60 * 60 * 24;
  var i_V1 = this.getTime();
  var i_V2 = p_Date.getTime();
  var i_V3 = i_V2 - i_V1;
  var i_Days = Math.round(i_V3 / i_Day);
  return i_Days;
}
Date.prototype.FromID = function(p_ID) {
  try {
    if (p_ID.length != 8) { return; }
    var i_Year = parseInt(p_ID.substr(0,4),10);
    var i_Month = parseInt(p_ID.substr(4,2),10);
    var i_Day = parseInt(p_ID.substr(6,2),10);
    var i_Date = new Date(i_Year, i_Month - 1, i_Day);
    this.setTime(i_Date.getTime()); }
  catch(i_Err) { }
}
Date.prototype.ToID = function() {
  var i_Year = "S0000" + this.getFullYear();
  var i_Month = "S00" + (this.getMonth() + 1);
  var i_Date = "S00" + this.getDate();
  var i_ID = i_Year.substr(i_Year.length - 4) + i_Month.substr(i_Month.length - 2) + i_Date.substr(i_Date.length - 2);
  return i_ID;
}
Date.prototype.SetFormat = function(p_Format) {
  this.Format = p_Format;
}
Date.prototype.GetFormat = function() {
  if (typeof this.Format == "undefined") {
    return "dmmmy"; }
  return this.Format;
}
Date.prototype.SetSeparator = function(p_Separator) {
  this.Separator = p_Separator;
}
Date.prototype.GetSeparator = function() {
  if (typeof this.Separator == "undefined") {
    return " "; }
  return this.Separator;
}
Date.prototype.FromText = function(p_Text) {
  var i_Format = this.GetFormat();
  var i_Separator = this.GetSeparator();
  var i_Date = new Date();
  var i_Today = new Date(i_Date.getFullYear(), i_Date.getMonth(), i_Date.getDate());
  var i_Items = p_Text.split(i_Separator);
  var i_Day = -1;
  var i_Month = -1;
  var i_MonthX = "";
  var i_Year = -1;
  var i_Set = false;
  var i_Sub = 0;

  try {
    switch (i_Format) {
        case "dmmmy":
          if (i_Items.length == 3) {
            i_Day = parseInt(i_Items[0], 10);
            i_MonthX = i_Items[1];
            i_Year = parseInt(i_Items[2], 10);
            i_Set = "X"; }
          break; }

    if (i_Set == false) {
      this.setTime(i_Today.getTime());
      return; }

    if (i_Set == "X") {
      i_Month = -1;
      for (i_Sub = 0; i_Sub < 12; i_Sub++) {
        if (i_MonthX.toLowerCase() == bd_MonthsLong[i_Sub].toLowerCase()) {
          i_Month = i_Sub + 1; } }
      if (i_Month == -1) {
        this.setTime(i_Today.getTime());
        return; } }

    i_Month = i_Month - 1;
    i_Date = new Date(i_Year, i_Month, i_Day);
    this.setTime(i_Date.getTime()); }
  catch(i_Err) {
    this.setTime(i_Today.getTime()); }
}
Date.prototype.ToText = function() {
  var i_Format = this.GetFormat();
  var i_Separator = this.GetSeparator();
  var i_Day = this.getDate();
  var i_Text = (i_Day < 10 ? "0" + i_Day: i_Day) + i_Separator + bd_MonthsLong[this.getMonth()] + i_Separator + this.getFullYear();
  
  return i_Text;
}
Date.prototype.SetToMidnight = function() {
  this.setHours(0, 0, 0, 0);
}

