// old school cookie functions grabbed off the web
var Cookies = {};
Cookies.set = function(name, value){
     document.cookie = name + "=" + escape(value);
}

Cookies.get = function(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;
	while(i < clen){
		j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return Cookies.getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
			break;
	}
	return null;
}

Cookies.clear = function(name) {
  if(Cookies.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

Cookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
}

function zeroPad(num, width) {
  num = num.toString();
  while (num.length < width)
    num = "0" + num;
  return num;
}

// clone the current date object and return a different object with identical value
Date.prototype.clone = function () {
  return new Date(this.getTime());
}
// clear the time information from this date and return it
Date.prototype.clearTime = function () {
  this.setHours(0); this.setMinutes(0);
  this.setSeconds(0); this.setMilliseconds(0);
  return this;
}
// return the last day of this month
Date.prototype.lastDay = function () {
  var tempDate = this.clone();
  tempDate.setMonth(tempDate.getMonth()+1);
  tempDate.setDate(0);
  return tempDate.getDate();
}
// return number of days since start of year
Date.prototype.getYearDay = function () {
  var today = new Date(this);
  today.setHours(0); today.setMinutes(0); today.setSeconds(0);
  var tempDate = new Date(today);
  // set start of year
  tempDate.setDate(1);
  tempDate.setMonth(0);
  return Math.round(
    (today.getTime() - tempDate.getTime())
    / 86400 / 1000) + 1; // Jan/1 is day 1
}
// add format() to Date
Date.prototype.format = function(formatString) {
  var out = new String();
  var token = "";
  for (var i = 0; i < formatString.length; i++) {
    if(formatString.charAt(i) == token.charAt(0)) {
      token = token.concat(formatString.charAt(i));
      continue;
    }
    out = out.concat(this.convertToken(token));
    token = formatString.charAt(i);
  }
  return out + this.convertToken(token);
}
Date.prototype.convertToken = function (str) {
  switch(str.charAt(0)) {
    case 'y': // set year
      if (str.length > 2)
        return this.getFullYear();
      return this.getFullYear().toString().substring(2);
    case 'd': // set date
      return zeroPad(this.getDate(),str.length);
    case 'D': // set day in year
      return this.getYearDay();
    case 'a':
      return this.getHours() > 11 ? "PM" : "AM";
    case 'H': // set hours
      return zeroPad(this.getHours(),str.length);
    case 'h':
      return zeroPad(this.get12Hours(),str.length);
    case 'm': // set minutes
      return zeroPad(this.getMinutes(),2);
    case 's': // set secondes
      return zeroPad(this.getSeconds(),2);
    case 'S': // set milisecondes
      return zeroPad(this.getMilliseconds(),str.length);
    case 'x': // set epoch time
      return this.getTime();
    case 'Z': // set time zone
      return (this.getTimezoneOffset() / 60) + ":" +
        zeroPad(this.getTimezoneOffset() % 60,2);
    case 'M': // set month
      if (str.length > 3) return this.getFullMonthName();
      if (str.length > 2) return this.getShortMonthName();
      return zeroPad(this.getMonth()+1,str.length);
    case 'E': // set dow
      if (str.length > 3) return this.getDOWName();
      if (str.length > 1) return this.getShortDOWName();
      return this.getDay();
    default:
      return str;
  }
}

Date.MONTH_NAMES = [
    "January", "February", "March",
    "April", "May", "June",
    "July", "August", "September",
    "October", "November", "December" ];

Date.prototype.getFullMonthName = function() {
  return Date.MONTH_NAMES[this.getMonth()];
}

Date.prototype.getShortMonthName = function() {
  return Date.MONTH_NAMES[this.getMonth()].substring(0,3);
}

Date.WEEKDAY_NAMES = [
    "Sunday", "Monday", "Tuesday",
	"Wednesday", "Thursday", "Friday",
	"Saturday" ];

Date.prototype.getDOWName = function () {
  return Date.WEEKDAY_NAMES[this.getDay()];
}

Date.prototype.getShortDOWName = function () {
  return Date.WEEKDAY_NAMES[this.getDay()].substring(0,3);
}

Date.prototype.get12Hours = function () {
  return this.getHours() == 0 ? 12 :
    (this.getHours() > 12 ? this.getHours() - 12 : this.getHours());
}

Date.MILLI = 'ms';
Date.SECOND = 's';
Date.MINUTE = 'mi';
Date.HOUR = 'h';
Date.DAY = 'd';
Date.MONTH = 'mo';
Date.YEAR = 'y';

Date.prototype.add = function(interval, value){
  var d = this.clone();
  if (!interval || value === 0) return d;
  switch(interval.toLowerCase()){
    case Date.MILLI:
      d.setMilliseconds(this.getMilliseconds() + value);
      break;
    case Date.SECOND:
      d.setSeconds(this.getSeconds() + value);
      break;
    case Date.MINUTE:
      d.setMinutes(this.getMinutes() + value);
      break;
    case Date.HOUR:
      d.setHours(this.getHours() + value);
      break;
    case Date.DAY:
      d.setDate(this.getDate() + value);
      break;
    case Date.MONTH:
      d.setMonth(this.getMonth() + value);
      break;
    case Date.YEAR:
      d.setFullYear(this.getFullYear() + value);
      break;
  }
  return d;
}
