/*
SilasDateCheck Javascript Function

Initialization:

  Create the date check object:
  
    // Default internal date to today's date and time.
    // Use this for live site
    check = new SilasDateCheck() 
  
        OR
  
    // For testing, set internate date to a specific date.
    // Note that 0=jan, 1=feb, 2=mar, etc...
    check = new SilasDateCheck([YEAR], [MONTH], [DAY], [HOUR], [MINUTE]) 

  Add new date ranges to date check object:

    check.addNewRange([WEEKDAY], [STARTHOUR], [STOPHOUR]);
    
    Where [WEEKDAY] is the day of the week, starting with sunday=0.
    [STARTHOUR] and [STOPHOUR] are 24 hour times along with minutes. 
    
    example:
      check.addNewRange(0, 1000, 1300); // sunday, from 10am to 1pm.
      
Usage:

  Test if date check object's internal date is within one of the ranges:
  if (check.isValidDateTime()) {
      // date and time are valid and within one of the ranges

      // do something
  } else {
      // date and time are outside range

      // do something else
  }


EXAMPLE IMPLEMENTATION:
check = new SilasDateCheck()
check.setRangeTimezone(-8); // following ranges will be Pacific time (-8)
check.addNewRange(0, 1000, 1300); // sunday, from 10am to 1pm
check.addNewRange(1, 1500, 1600); // monday, from 3pm to 4pm
check.addNewRange(2, 1700, 2000); // tues, from 5pm to 8pm


if (check.isValidDateTime() {
    document.write("right now is a valid date range")
}
      
      
ISSUES:
 - will not work for date ranges, ie sunday 10pm to monday 3am
*/
function SilasDateCheck (year, month, day, hour, minute) {
    if (year && month && day) { // used for testing purposes
        if (hour && minute) {
            this.now = new Date(year, month, day, hour, minute, 0);
        } else {
            this.now = new Date(year, month, day);
        }
    } else {
        this.now = new Date();
    }        
    this.dates = Array()
    this.isValidDateTime = function () {
        var now = this.getNow();
        for (var i=0; i<this.dates.length; i++) {
            if (now[0] == this.dates[i][0]) { // valid weekday
                if ((now[1] >= this.dates[i][1]) && (now[1] <= this.dates[i][2])) {
                    return true;
                }
            }
        }
        return false;
    }
    // return current date time, adjusted for timezone
    this.getNow = function () { 
        var hour = this.now.getHours()
        var minute = this.now.getMinutes()
        var day = this.now.getDay();
        var timezone = (this.now.getTimezoneOffset() / 60) + this.timezone;
        var time = ((hour+timezone) * 100) + minute
        return new Array(day, time);
    }
    // set the range's timezone
    this.setRangeTimezone = function(zone) { this.timezone = parseInt(zone) }
    this.addNewRange =      function (weekday, startHour, stopHour) { this.dates[this.dates.length] = [weekday, startHour, stopHour] }
}

