/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * File:	date.js															   *
 * Author:	Keith Borgmann													   *
 * Date:	2009-05-06														   *
 * Version:	1.0.1															   *
 * Description:																   *
 *	Extensions to the js Date object										   *
 *																			   *
 * Dependancies:															   *
 *	string.js (1.0.3+)														   *
 *																			   *
 * Extension List:															   *
 *		.toIEEEDate()	//"yyyy-mm-dd".										   *
 *		.addDays(d);	//Adds d days to the date.							   *
 *		.addMonths(m);	//Adds m months to the date.						   *
 *		.addYears(y);	//Adds y years to the date.							   *
 *		.getMonthName();//using MONTHS, returns the name of the date's month.  *
 *		.parseIEEEDate(date);	//Parse a date of format "yyyy-mm-dd"		   *
 *		.firstDayOfMonth();		//return a date for the first date of the month*
 *		.lastDayOfMonth();		//return a date for the last date of the month.*
 *		.sameMonthAs(m);		//returns True if same year/month as m.		   *
 *																			   *
 * Static:																	   *
 *		.MONTHS			//Array of month names.								   *
 *		.WEEKDAYS		//Array of weekday names.							   *
 *		.parseIEEEDate(date);	//convert the date from "yyyy-mm-dd" to ms	   *
 *																			   *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


//STATIC-------------------------------
Date.MONTHS_EN = ["January", "February", "March", "April", "May", "June",
				"July", "August", "September", "October", "November", "December"];
Date.MONTHS_FR = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
				"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
Date.WEEKDAYS_EN = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
Date.WEEKDAYS_FR = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];
Date.MONTHS = Date.MONTHS_EN;
Date.WEEKDAYS = Date.WEEKDAYS_EN;

Date.setLocale = function(locale) {
	if (locale=="fr") {
		Date.MONTHS = Date.MONTHS_FR;
		Date.WEEKDAYS = Date.WEEKDAYS_FR;
	} else {	//en
		Date.MONTHS = Date.MONTHS_EN;
		Date.WEEKDAYS = Date.WEEKDAYS_EN;
	}
}

//Requires format yyyy-mm-dd
Date.parseIEEEDate = function(date) {
	if (typeof(date)!="string")
		return null;
	var t = null;

	var split = date.split('-');
	if (split.length==3 && split[0].length==4 && split[1].length==2 && split[2].length==2 ) {
		var y = parseInt(split[0]);
		var m = parseInt( split[1].replace(/^0+/g, '') );	//08, 09 are octal
		var d = parseInt( split[2].replace(/^0+/g, '') );	//08, 09 are octal
		var ms = (new Date(y,(m-1),d)).getTime();
		if (!isNaN(ms))
			t = ms;
	}
	return (t==null) ? null : new Date(t);
};

//Requires format mm/dd/yyyy or dd/mm/yyyy
Date.parseTraditionalDate = function(date, dayFirst) {
	if (typeof(date)!="string")
		return null;
	var t = null;

	var split = date.split('/');
	if (split.length==3 && split[0].length==2 && split[1].length==2 && split[2].length==4 ) {
		var m = parseInt( split[0].replace(/^0+/g, '') );	//08, 09 are octal
		var d = parseInt( split[1].replace(/^0+/g, '') );	//08, 09 are octal
		var y = parseInt(split[2]);
		if (dayFirst)
			var ms = (new Date(y,(d-1),m)).getTime();
		else
			var ms = (new Date(y,(m-1),d)).getTime();
		if (!isNaN(ms))
			t = ms;
	}
	return (t==null) ? null : new Date(t);
};


//METHODS-------------------------------
Date.prototype.toIEEEDate = function() {
	var m = (""+(this.getMonth()+1)).pad(-2,'0');
	var d = (""+this.getDate()).pad(-2,'0');
	return this.getFullYear()+"-"+m+"-"+d;
};
Date.prototype.toMDY = function() {
	var m = (""+(this.getMonth()+1)).pad(-2,'0');
	var d = (""+this.getDate()).pad(-2,'0');
	return m+"/"+d+"/"+this.getFullYear()
};
Date.prototype.toDMY = function() {
	var m = (""+(this.getMonth()+1)).pad(-2,'0');
	var d = (""+this.getDate()).pad(-2,'0');
	return d+"/"+m+"/"+this.getFullYear()
};


Date.prototype.addDays = function(d) {
	this.setDate(this.getDate()+d);
};

Date.prototype.addMonths = function(m) {
	this.setMonth(this.getMonth()+m);
};

Date.prototype.addYears = function(y) {
	this.setFullYear(this.getFullYear()+y);
};

Date.prototype.getMonthName = function(y) {
	return Date.MONTHS[this.getMonth()];
};

//Requires format dd/mm/yyyy
Date.prototype.parseMDY = function(date) {
	var ms = Date.parseTraditionalDate(date, true);
	if (ms!=null)
		this.setTime(ms.getTime());
};
//Requires format mm/dd/yyyy
Date.prototype.parseMDY = function(date) {
	var ms = Date.parseTraditionalDate(date, false);
	if (ms!=null)
		this.setTime(ms.getTime());
};

//Requires format yyyy-mm-dd
Date.prototype.parseIEEEDate = function(date) {
	var ms = Date.parseIEEEDate(date);
	if (ms!=null)
		this.setTime(ms.getTime());
};


Date.prototype.firstDayOfMonth = function() {
	return new Date(this.getFullYear(), this.getMonth(), 1);
};

Date.prototype.lastDayOfMonth = function() {
	return new Date(this.getFullYear(), this.getMonth()+1, 0);
}

Date.prototype.sameMonthAs = function(m) {
	return (this.getFullYear()==m.getFullYear() && this.getMonth()==m.getMonth());
}