//alert ("Activated date.js")

// This js file is to create my own DATE object because the one that comes with
// javascript is not very usefull for me.

// Useful to be Global Variables
var monthList = new Array ("", "January", "February", "March", "April", "May",
                           "June", "July", "August", "September", "October",
                           "November", "December")
var daysInMonth = new Array ("", 31, 28, 31, 30, 31,
                            30, 31, 31, 30, 31,
                            30, 31)
function isLeapYear(year) {
  var diff = 1
  diff = year - (Math.round(year / 4) * 4)
  if ( diff == 0 ) {
    daysInMonth[2] = 29
  } else {
    daysInMonth[2] = 28
  }
}

function myDate (year, month, day) {
//alert ("Entering myDate("+year+", "+month+", "+day+")")
  if ( year == undefined && month == undefined && day == undefined) {
//alert ("Setting default date of Today")
    var today = new Date()
    year = today.getFullYear()
    month = today.getMonth() + 1
    day = today.getDate()
  }
  if ( typeof year == "string" ) {
//alert("Setting offset of date")
    //offset from todays date accordingly
    if ( month > 28 ) { 
      alert ("Can only offset todays date by 28 days max")
      return
    }
    if ( year == "add" ) {
      var offset = month
    } else if ( year == "sub" ) {
      var offset = -month
    }
    var today = new Date()
    year = today.getFullYear()
    month = today.getMonth() + 1
    day = today.getDate() + offset
    isLeapYear(year)
    if ( day > daysInMonth[month] ) {
      day = day - daysInMonth[month]
      ++month
      if ( month > 12 )  {
        month = 1
        ++year
      }
    } else if ( day < 1 ) {
      --month
      if ( month < 1 ) {
        --year
        month = 12
      }
      isLeapYear(year)
      day = daysInMonth[month] + day
    }
  }
//alert("Setting Date")
  this.year = year  // a number
  this.month = month  // a number 1-12
  this.day = day  // a number
  this.asJSdate = new Date(year, (month-1), day)
  this.newJSdate = getNewDate  // Be sure to run this method before using the 
                               // asJSdate property to be sure the correct date                                // is in the date object
  this.monthName = getMonth
  this.daysInMonth = getNumDays
  this.print = print
//alert("Returning:  "+this.print())
}

function getMonth() {
var name = monthList[this.month]
return name
}

function getNumDays() {
isLeapYear(this.year)
var num = daysInMonth[this.month]
return num
}

function print() {
  return this.monthName()+" "+this.day+", "+this.year
}

function getNewDate() {
  this.asJSdate = new Date(this.year, (this.month-1), this.day)
}
