// JScript File

DatesHandler = {
    /**
	* String variable that holds the seperator
	* @property Seperator
	* @type String
	*/
    Seperator : "/",

    /**
	* Enum variable that holds the format
	* @property OutputCulture
	* @type Enum
	*/
    OutputCulture : { Israel : "dd/MM/yyyy", InternationalGMT : "MM/dd/yyyy", InternationalUTC : "dd MMM yyyy hh:mm:ss GMT" },

    /**
	* Enum variable that holds the format
	* @property InputCulture
	* @type Enum
	*/
    InputCulture : { Israel : "dd/MM/yyyy", International : "MM/dd/yyyy" },

    /**
	* Integer variable that holds the miliseconds in a day
	* @property MilisecondInADay
	* @type Integer
	*/
    MilisecondInADay : 1000*60*60*24,

    /**
	* Creates the desired date according to the specified date, time and culture.
	* @method CreateDate
	* @param {Date} date	    The desired date.
	* @param {String} time	    The desired time.
	* @param {Number} culture	The desired date format.
	* @return {Date} The resulting Date object
	*/
    CreateDate : function(date, time, outputCulture) {
        var datestr;
		var tempDate = new Date(this.ConvertToUTCCulture(date));

	    var hhhh, mnmn, sec;
	    if(typeof time != "undefined" && time != null) {
	        hhhh = time.substring(0, 2);
	        mnmn = time.substring(3, 2);
	        sec = time.substring(6, 2);
	    } else {
	        hhhh = tempDate.getHours().toString();
	        mnmn = tempDate.getMinutes().toString();
	        sec = tempDate.getSeconds().toString();
	    }

		if (hhhh.length == 1) hhhh = "0" + hhhh;
		if (mnmn.length == 1) mnmn = "0" + mnmn;
		if (sec.length == 1) sec = "0" + sec;

		var dddd = tempDate.getDate().toString();		
		var month = (tempDate.getMonth() + 1);		
		month = month.toString();
		var yyyy = tempDate.getFullYear().toString();
		if (dddd.length == 1) dddd = "0" + dddd;
		if (month.length == 1) month = "0" + month;
       
		switch(outputCulture) {
		    case this.OutputCulture.Israel:
		        datestr = dddd + "/" + month + "/" + yyyy;	
		        //datestr = month + "/" + dddd + "/" + yyyy;	        
		        break;

		    case this.OutputCulture.InternationalGMT:
		        datestr = month + "/" + dddd + "/" + yyyy;
		        break;

			case this.OutputCulture.InternationalUTC:
				datestr = this.ConvertDayToWord(dddd) + "," + dddd + this.ConvertMonthToWord(month) + yyyy + " " + hhhh + ":" + mnmn + ":" + sec + " GMT";
				break;

		}
		//alert(datestr);
		

		var returnDate = new Date(this.SetProperDateValue(datestr));		
		returnDate.setHours(hhhh);
		returnDate.setMinutes(mnmn);
		returnDate.setSeconds(sec);
		//alert(returnDate);
		return returnDate;
    },

    /**
	* Sets the format of the desired date to "MM/dd/yyyy" format.
	* @method SetProperDateValue
	* @param {Date} inputDate	The date to convert.
	* @return {Date} The resulting Date
	*/
    SetProperDateValue : function(inputDate) {
        if (typeof inputDate == "string") {
            var day = inputDate.substr(0, 2);
            var month = inputDate.substr(3, 2);
            var yyyy = inputDate.substr(6);
            inputDate = month + "/" + day + "/" + yyyy;
        }
        
        
        return inputDate;
    },

	/**
	* Converts the desired date into a UTC string.
	* @method ConvertToUTCCulture
	* @param {Date} inputDate	The date to convert.
	* @return {Date} The resulting UTC Date
	*/
	
	TrimLeadingZeros: function(inpstr) {
		var tempstr = inpstr;
		var finished = false;
		while (!finished) {
			if (tempstr.charAt(0) == "0") tempstr = tempstr.substr(1); else finished = true;
		}
		return tempstr;
	},
	
    ConvertToUTCCulture : function(inputDate) {
		var tempDate;
        if (typeof inputDate == "string") {
			var testSlashes = inputDate.split("/");
			if (testSlashes.length>1) {
				// Date with slashes
				var day = parseInt(this.TrimLeadingZeros(testSlashes[0]));
				var month = parseInt(this.TrimLeadingZeros(testSlashes[1]))-1;
				var yyyy = parseInt(testSlashes[2]);
				//alert(day + ":"+month+":"+yyyy);
			} else {
				// No slashes, try spaces
				var testSpaces = inputDate.split(" ");
				if (testSpaces.length>1) {
					// Spaces ok
					var day = parseInt(this.TrimLeadingZeros(testSpaces[0]));
					var m = testSpaces[1];
					var month = "";
					switch (m.toLowerCase()) {
						case "jan":
						case "january": month = 0; break;
						case "feb":
						case "february": month = 1; break;
						case "mar":
						case "march": month = 2; break;
						case "apr":
						case "april": month = 3; break;
						case "may":   month = 4; break;
						case "jun":
						case "june": month = 5; break;
						case "jul":
						case "july": month = 6; break;
						case "aug":
						case "august": month = 7; break;
						case "sep":
						case "september": month = 8; break;
						case "oct":
						case "october": month = 9; break;
						case "nov":
						case "november": month = 10; break;
						case "dec":
						case "december": month = 11; break;
					}
					var yyyy = parseInt(testSpaces[2]);
				}
			}
/*          var day = inputDate.substr(0, 2);
            var month = inputDate.substr(3, 3);
            var yyyy = inputDate.substr(7);*/
//          inputDate = month + "/" + day + "/" + yyyy;
		    tDate = new Date(yyyy,month,day,12,0,0,0);
			tempDate = tDate.toUTCString();
		} else {
			tempDate = inputDate.toUTCString();
		}
		return tempDate;
    },

    /**
	* Adds the specified amount of time to the this instance.
	* @method Add
	* @param {Date} date	    The JavaScript Date object to perform addition on
	* @param {String} field	    The field constant to be used for performing addition.
	* @param {Number} amount	The number of units (measured in the field constant) to add to the date.
	* @return {Date} The resulting Date object
	*/
	Add : function(date, field, amount) {
		var d = new Date(date.getTime());
		switch (field) {
		    case "Day":
				d.setDate(date.getDate() + amount);
				break;

			case "Month":
				var newMonth = date.getMonth() + amount;
				var years = 0;

				if (newMonth < 0) {
					while (newMonth < 0) {
						newMonth += 12;
						years -= 1;
					}
				} else if (newMonth > 11) {
					while (newMonth > 11) {
						newMonth -= 12;
						years += 1;
					}
				}

				d.setMonth(newMonth);
				d.setFullYear(date.getFullYear() + years);
				break;

			case "Year":
				d.setFullYear(date.getFullYear() + amount);
				break;
		}
		return d;
	},

	/**
	* Subtracts the specified amount of time from the this instance.
	* @method Subtract
	* @param {Date} date	The JavaScript Date object to perform subtraction on
	* @param {String} field	The this field constant to be used for performing subtraction.
	* @param {Number} amount	The number of units (measured in the field constant) to subtract from the date.
	* @return {Date} The resulting Date object
	*/
	Subtract : function(date, field, amount) {
		return this.Add(date, field, (amount*-1));
	},

	/**
	* Determines whether a given date is before another date on the calendar.
	* @method Before
	* @param {Date} date		The Date object to compare with the compare argument
	* @param {Date} compareTo	The Date object to use for the comparison
	* @return {Boolean} true if the date occurs before the compared date; false if not.
	*/
	Before : function(date, compareTo) {
		var ms = compareTo.getTime();
		if (date.getTime() < ms) {
			return true;
		}
		else {
			return false;
		}
	},

	/**
	* Determines whether a given date is after another date on the calendar.
	* @method After
	* @param {Date} date		The Date object to compare with the compare argument
	* @param {Date} compareTo	The Date object to use for the comparison
	* @return {Boolean} true if the date occurs after the compared date; false if not.
	*/
	After : function(date, compareTo) {
		var ms = compareTo.getTime();
		if (date.getTime() > ms) {
			return true;
		}
		else {
			return false;
		}
	},

	/**
	* Determines whether a given date is between two other dates on the calendar.
	* @method Between
	* @param {Date} date		The date to check for
	* @param {Date} dateBegin	The start of the range
	* @param {Date} dateEnd		The end of the range
	* @return {Boolean} true if the date occurs between the compared dates; false if not.
	*/
	Between : function(date, dateBegin, dateEnd) {
		if (this.After(date, dateBegin) && this.Before(date, dateEnd)) {
			return true;
		}
		else {
			return false;
		}
	},

	/**
	* Determines if a given week overlaps two different years.
	* @method IsYearOverlapWeek
	* @param {Date}	weekBeginDate	The JavaScript Date representing the first day of the week.
	* @return {Boolean}	true if the date overlaps two different years.
	*/
	IsYearOverlapWeek : function(weekBeginDate) {
		var overlaps = false;
		var nextWeek = this.Add(weekBeginDate, "Day", 6);
		if (nextWeek.getFullYear() != weekBeginDate.getFullYear()) {
			overlaps = true;
		}
		return overlaps;
	},

	/**
	* Determines if a given week overlaps two different months.
	* @method IsMonthOverlapWeek
	* @param {Date}	weekBeginDate	The JavaScript Date representing the first day of the week.
	* @return {Boolean}	true if the date overlaps two different months.
	*/
	IsMonthOverlapWeek : function(weekBeginDate) {
		var overlaps = false;
		var nextWeek = this.Add(weekBeginDate, "Day", 6);
		if (nextWeek.getMonth() != weekBeginDate.getMonth()) {
			overlaps = true;
		}
		return overlaps;
	},

	/**
	* Clears the time fields from a given date, effectively setting the time to 12 noon.
	* @method ClearTime
	* @param {Date}	date	The JavaScript Date for which the time fields will be cleared
	* @return {Date}		The JavaScript Date cleared of all time fields
	*/
	ClearTime : function(date) {
		date.setHours(12,0,0,0);
		return date;
	},

	/**
	* Compares two given dates with considiration to the given percision.
	* @method CompareDates
	* @param {Date}	date1	      The first date to compare
	* @param {Date}	date2	      The second date to compare
	* @param {Number} percision   The percision according by which to compare the dates
	** percision is 0 by default, marking best percision (down to millisecs)
	**  1 - disregard milliseconds
	** 2 - disregard seconds
	** 3 - disregard minutes
	** 4 - disregard hours
	** 5 - disregard days
	** 6 - disregard months
	* @return {Number} -1 when date1 < date2, 0 when date1 = date2 and 1 when date1 > date2
	*/
    CompareDates: function(date1,date2,percision) {
		if (typeof date1 == "undefined" || typeof date2 == "undefined") {
			return false;
		} else {
			if (typeof percision == "undefined" || percision == null) {
				var per = 0;
			} else {
				var per = percision;
			}
			switch (per) {
				case 0:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),date1.getSeconds(),date1.getMilliseconds());
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),date2.getSeconds(),date2.getMilliseconds());
					break;
				case 1:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),date1.getSeconds(),0);
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),date2.getSeconds(),0);
					break;
				case 2:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),0,0);
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),0,0);
					break;
				case 3:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate(),date1.getHours(),0,0,0);
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),date2.getDate(),date2.getHours(),0,0,0);
					break;
				case 4:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate(),0,0,0,0);
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),date2.getDate(),0,0,0,0);
					break;
				case 5:
					var d1 = new Date(date1.getFullYear(),date1.getMonth(),1,0,0,0,0);
					var d2 = new Date(date2.getFullYear(),date2.getMonth(),1,0,0,0,0);
					break;
				case 6:
					var d1 = new Date(date1.getFullYear(),0,1,0,0,0,0);
					var d2 = new Date(date2.getFullYear(),0,1,0,0,0,0);
					break;
			}
			if (d1.valueOf() == d2.valueOf()) {
				return 0;
			} else {
				if (d1.valueOf()>d2.valueOf()) {
					return 1;
				} else {
					return -1;
				}
			}
		}
	},

	/**
	* Compares two given dates with considiration to the given percision.
	* @method DistDates
	* @param {Date}	date1	      The first date to compare
	* @param {Date}	date2	      The second date to compare
	* @param {Number} distComponents   The percision according by which to compare the dates
	** distComponents can be one of the following:
	** 0 - Days (default)
	** 1 - Months
	** 2 - Years
	** 3 - Hours
	** 4 - Minutes
	** 5 - Seconds
	** 6 - Total milliseconds
	* @return {Number} The distance between two dates.
	*/
	DistDates: function(date1,date2,distComponents) {
		if (typeof date1 == "undefined" || typeof date2 == "undefined") {
			return false;
		}
		var msDist = (date1.valueOf() > date2.valueOf()) ? (date1.valueOf() - date2.valueOf()) : (date2.valueOf() - date1.valueOf());
		if (typeof distComponents == "undefined" || distComponents == null) {
			var dc = 0;
		} else {
			var dc = distComponents;
		}
		var msDate = new Date(msDist);
		switch (dc) {
			case 0:
				return (msDate.getDate()-1);
				break;
			case 1:
				return (msDate.getMonth());
				break;
			case 2:
				return (msDate.getFullYear()-1970);
				break;
			case 3:
				return (msDate.getHours()-2);
				break;
			case 4:
				return (msDate.getMinutes());
				break;
			case 5:
				return (msDate.getSeconds());
				break;
			default:
				return msDate.valueOf();
		}
	},

	/**
	* Converts the month from a number representation into a word.
	* @method ConvertMonthToWord
	* @param {String}	month	The month representation in numbers (e.g. "10")
	* @return {String}		    The month representation in word (e.g. "Oct")
	*/
	ConvertMonthToWord : function(month) {
		var returnMonth;
		if(month.length == 1) month = "0" + month;
	    switch(month) {
			case "01": returnMonth = "Jan"; break;
			case "02": returnMonth = "Feb"; break;
			case "03": returnMonth = "Mar"; break;
			case "04": returnMonth = "Apr"; break;
			case "05": returnMonth = "May"; break;
			case "06": returnMonth = "Jun"; break;
			case "07": returnMonth = "Jul"; break;
			case "08": returnMonth = "Aug"; break;
			case "09": returnMonth = "Sep"; break;
			case "10": returnMonth = "Oct"; break;
			case "11": returnMonth = "Nov"; break;
			case "12": returnMonth = "Dec"; break;
		}

		return returnMonth;
	},

	/**
	* Converts the month from a string representation into a number.
	* @method ConvertMonthToNumber
	* @param {String}	month	The month representation in word (e.g. "Jan")
	* @return {String}		    The month representation in numbers (e.g. "01")
	*/
	ConvertMonthToNumber : function(month) {
		var returnMonth;
        switch (month.toUpperCase().substring(0, 3)) {
			case "JAN": returnMonth = "01"; break;
			case "FEB": returnMonth = "02"; break;
			case "MAR": returnMonth = "03"; break;
			case "APR": returnMonth = "04"; break;
			case "MAY": returnMonth = "05"; break;
			case "JUN": returnMonth = "06"; break;
			case "JUL": returnMonth = "07"; break;
			case "AUG": returnMonth = "08"; break;
			case "SEP": returnMonth = "09"; break;
			case "OCT": returnMonth = "10"; break;
			case "NOV": returnMonth = "11"; break;
			case "DEC": returnMonth = "12"; break;
		}

		return returnMonth;
	},

	/**
	* Converts the day from a number representation into a word.
	* @method ConvertDayToWord
	* @param {String}	day	    The day representation in numbers (e.g. "03")
	* @return {String}		    The day representation in word (e.g. "Thu")
	*/
	ConvertDayToWord : function(day) {
		var returnDay;
		if(day.length == 1) day = "0" + day;
	    switch(day) {
			case "01": returnDay = "Sun"; break;
			case "02": returnDay = "Mon"; break;
			case "03": returnDay = "Tue"; break;
			case "04": returnDay = "Wed"; break;
			case "05": returnDay = "Thu"; break;
			case "06": returnDay = "Fri"; break;
			case "07": returnDay = "Sat"; break;
		}

		return returnDay;
	},

	/**
	* Converts the day from a string representation into a number.
	* @method ConvertDayToNumber
	* @param {String}	day	    The day representation in word (e.g. "Thu")
	* @return {String}		    The day representation in numbers (e.g. "03")
	*/
	ConvertDayToNumber : function(day) {
		var returnDay;
        switch (day.toUpperCase()) {
			case "SUN": returnDay = "01"; break;
			case "MON": returnDay = "02"; break;
			case "TUE": returnDay = "03"; break;
			case "WED": returnDay = "04"; break;
			case "THU": returnDay = "05"; break;
			case "FRI": returnDay = "06"; break;
			case "SAT": returnDay = "07"; break;
		}

		return returnDay;
	}
}

