
/*
  ED - Every day 
  EWD - Every week day
  EW_SAME_DAY - Every week on the same day
  A_DATE - Every month on the same day
  NTH_WEEK_A_DAY - Same day of 'nth' week of month
  LAST_WEEK_A_DAY - Same day of last week (such as last Monday)
  MORE - Look at subtype
  EVERY_WEEK
  OTHER_WEEK
  1st_WEEK
  2nd_WEEK
  3rd_WEEK
  4th_WEEK
  LAST_WEEK
  REPEAT_AFTER_DAYS
  REPEAT_AFTER_WEEKS
  REPEAT_AFTER_MONTHS_BY_DAY
  REPEAT__AFTER_MONTHS_BY_DATE
  REPEAT_AFTER_YEARS
  
*/
Af.DateUtil = Class.create();

Af.DateUtil.prototype = {

	initialize: function() {
	   this.days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
	   this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	   this.daysLong = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	   this.monthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
	   this.weeks = ["1st", "2nd", "3rd", "4th", "5th"];
	},
	
	getWeekOfMonth: function(date) {
	   var n = date.getDate();
	   var wk = -1;
	   while (n > 0) {
	     wk += 1;
	     n -= 7;
	   }
	   return wk;
	},
	
	isDayInFirstWeek: function(date) {
	   return date.getDate() < 7;
	},
	
	
	isDayInLastWeek: function(date) {
	   var dt2 = new Date(date.getTime());
	   dt2.setDate(dt2.getDate() + 7);
	   return date.getMonth() != dt2.getMonth();
	},

	
	getRepeatList: function(date) {
	   var dl = new Array();
	   
	   var obj = new Object();
	   obj["label"] = "Every Day";
	   obj["value"] = "ED";
	   dl.push(obj);
	   
	   
	   var n = date.getDay();
	   
	   if (n>=1 && n <=5) {
		   obj = new Object();
		   obj["label"] = "Every Weekday";
		   obj["value"] = "EWD";
		   dl.push(obj);
	   }
	   
	   
	   obj = new Object();
	   obj["label"] = "Every " + this.daysLong[date.getDay()];
	   obj["value"] = "EW_SAME_DAY";
	   dl.push(obj);
	   
	   obj = new Object();
	   obj["label"] = "-------";
	   obj["value"] = "NULL1";
	   dl.push(obj);
	   
	   obj = new Object();
	   obj["label"] = date.getDate() + " of every month";
	   obj["value"] = "A_DATE";
	   dl.push(obj);
	   
	   var n = this.getWeekOfMonth(date);
	   if (n < 4) {
		   obj = new Object();
		   obj["label"] = this.weeks[n] + " " + this.daysLong[date.getDay()] + " of every month";
		   obj["value"] = "NTH_WEEK_A_DAY";
		   dl.push(obj);
	   }
	   
	   if (this.isDayInLastWeek(date)) {
	       obj = new Object();
		   obj["label"] = "Last " + this.daysLong[date.getDay()] + " of every month";
		   obj["value"] = "LAST_WEEK_A_DAY";
		   dl.push(obj);
	   }
	   
	   obj = new Object();
	   obj["label"] = "-------";
	   obj["value"] = "NULL2";
	   dl.push(obj);
	   
	   obj = new Object();
	   obj["label"] = "More Choices";
	   obj["value"] = "MORE";
	   dl.push(obj);
	   
	   return dl;
	},
	
	getDaysAfter: function(dtA, dtB) {
	   var t1 = dtA.getTime();
	   var t2 = dtB.getTime();
 if ((dtA.getYear() <= dtB.getYear()) || (dtA.getMonth() <= dtB.getMonth()) || (dtA.getDate() <= dtB.getDate())) {
	//   if (t1 <= t2) {        // Change by Gabbar on 23 Oct 07, it was creating problem if time is included in date
	      n = 0;
	      var dt = new Date(t1);
	      while (dt.getTime() < t2) {
	        n++;
	        dt.setDate(dt.getDate() + 1);
	      }
	      return n;
	   }
	   return -1;
	}
	
}

