/**
 * import jbon.javascript.Math;
 */

function Calendar() {
	var _daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var _currentDate = new Date();
	
	var checkLeapYear = function() {
		_daysInMonth[1] = Calendar.isLeapYear(_currentDate.getFullYear()) ? 29 : 28;
	};
	
	this.setDay = function(newDay) {
		_currentDate.setDate(newDay);
		Calendar.isLeapYear(_currentDate.getYear());
	};
	
	this.getDay = function() {
		return _currentDate.getDate();
	};
	
	this.getDayName = function() {
		return Calendar.daysOfWeek[this.getDayOfWeek()];
	};
	
	this.getDayOfWeek = function() {
		return _currentDate.getDay();
	};
	
	this.getDaysInMonth = function() {
		return _daysInMonth;
	};
	
	this.getDaysOfWeek = function() {
		return Calendar.daysOfWeek;
	};
	
	this.setMonth = function(newMonth) {
		if (1 <= newMonth && newMonth <= 12) {
			_currentDate.setMonth(newMonth - 1);
			checkLeapYear();
		} else {
			//window.alert();
		}
	};
	
	this.getMonth = function() {
		return _currentDate.getMonth()+1;
	};
	
	this.setYear = function(newYear) {
		if (1900 <= newYear) {
			_currentDate.setYear(newYear);
			checkLeapYear();
		} else {
			//window.alert();
		}
	};
	
	this.getYear = function() {
		return _currentDate.getFullYear();
	};
	
	this.setDate =  function(newDate) {
		/*
		var arrNewDate = newDate.split("\/");
		*/
		_currentDate = new Date(newDate);
		/*
		_currentDate.setFullYear(arrNewDate[2],arrNewDate[1] - 1,arrNewDate[0])
		*/
		checkLeapYear();
	};
	
	this.getDate = function() {
		return this.getMonth() + "/" + this.getDay() + "/" + this.getYear();
	};
	
	
	/**
	 * void nextDay()
	 * @return null
	 */
	this.nextDay = function() {
		var maxDaysInMonth = this.getTotalDays();
		if (this.getDay() == maxDaysInMonth) {
			this.nextMonth();
			this.setDay(1);
		}
	};
	
	/**
	 * void previousDay()
	 * @return null
	 */
	this.previousDay = function() {
		if (this.getDay() == 1) {
			this.previousMonth();
			this.setDay(this.getTotalDays());
		} else {
			this.setDay(this.getDay() - 1);
		}
	};
	
	/**
	 * void nextMonth()
	 * @return null
	 */
	this.nextMonth = function() {
		if (this.getMonth() == 12) {
			this.nextYear();
			this.setMonth(1);
		} else {
			this.setMonth(this.getMonth() + 1);
		}
	};
	
	/**
	 * void previousMonth()
	 * @return null
	 */
	this.previousMonth = function() {
		if (this.getMonth() == 1) {
			this.previousYear();
			this.setMonth(12);
		} else {
			this.setMonth(this.getMonth() - 1);
		}
	};
	
	/**
	 * void nextYear()
	 * @return null
	 */
	this.nextYear = function() {
		_currentDate.setYear(this.getYear() + 1);
	};
	
	/**
	 * void previousYear()
	 * @return null
	 */
	this.previousYear = function() {
		var curDate = new Date();
		
		if (curDate.getFullYear() - 100 < this.getYear() < curDate.getFullYear() + 100) {
			_currentDate.setYear(this.getYear() - 1);
		}
	};
	
	/**
	 * int getLastDayInMonth()
	 * @return total days in current month
	 */
	this.getLastDayInMonth = function() {
		return _daysInMonth[this.getMonth() - 1];
	};	
	
	this.compareTo = function(toCalendar) {
		return this.getDay() == toCalendar.getDay() &&
						this.getMonth() == toCalendar.getMonth() &&
						this.getYear() == toCalendar.getYear();
	};
	
};	



Calendar.isDate = function(sDate) {
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) {
		var dArr = sDate.split("/");
		var d = new Date(sDate);
		return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2];
	} else {
		return false;
	}
}

Calendar.en_daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Satuday');
Calendar.en_monthsOfYear = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
Calendar.en_shortDaysOfWeek = new Array('Sun', 'Mon', 'Tues', 'Wed', 'Thu', 'Fri', 'Sat');
Calendar.en_shortMonthsOfYear = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

Calendar.vn_daysOfWeek = new Array('Chủ nhật', 'Thứ Hai', 'Thứ Ba', 'Thứ Tư', 'Thứ Năm', 'Thứ Sáu', 'Thứ Bẩy');
Calendar.vn_monthsOfYear = new Array('Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6', 'Tháng 7', 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12');
Calendar.vn_shortDaysOfWeek = new Array('Cn', 'Hai', 'Ba', 'Tư', 'Năm', 'Sáu', 'Bẩy');
Calendar.vn_shortMonthsOfYear = new Array('T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12');

Calendar.daysOfWeek = new Array('*', '2', '3', '4', '5', '6', '7');
Calendar.monthsOfYear = new Array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
/**
 * int getCurrent()
 * @return total days in current month
 */
Calendar.getDay = function() {
	return (new Date()).getDate();
};

Calendar.getMonth = function() {
	return (new Date()).getMonth() + 1;
};

Calendar.getYear = function() {
	return (new Date()).getFullYear();
};

Calendar.isLeapYear = function(yyyy) {
	return (yyyy % 4000 == 0 || yyyy % 4 == 0);
};

