// Enhanced Date Functions
// Author:  Chester Mealer Jr
// Date:    12-22-2009
// Version: 0.5
/*
 	These functions add more formatting options for input and output of dates and add
	date manipulation functions.
	Functions:
		fromString(dateSTR)		- allows dates to be input as Y-m-d[ g:iA]
		justDate() 				- allows dates to be output as Y-m-d
		toString2()				- outputs as Y-m-d g:iA
		getWeek()				- outputs 1 - 5 based on week of the month
		addDays( numDays )		- adds the specified number of days to the date
		addMonths( numMonths )	- adds the specified number of months to the date
		enhancedDate()			- outputs true, used to check to see if my functions are present
		typeOf()				- outputs "date" will be used in future for meeting checks
		getAM()					- returns "AM" or "PM"
		justTime()				- returns just the time as "g:iA"
*/

function enhancedDate() {
	return true;
}

function fromString( dateSTR ) {
	var hour = 0;	
	var minute = 0;
	if (dateSTR.search(/\d{4}-\d{2}-\d{2}/) > -1) {
		var year = Number(dateSTR.slice(0,4));
		var month = Number(dateSTR.slice(5,7)) - 1;
		var day = Number(dateSTR.slice(8,10));
		var hour = 0;	
		var minute = 0;
		var time_pat = new RegExp("\d+:\d{2}");
		with(this) {
			setFullYear(year);
			setMonth(month);
			setDate(day);
			setHours(hour);
			setMinutes(minute);
			setSeconds(0);
			setMilliseconds(0);
		};
	};
	var time = dateSTR.match(time_pat);
	if (dateSTR.search(/\s\d+:\d{2}/) > -1 ) {
		time = dateSTR.match(/\d+:\d{2}/)[0];
		if (time) {
			var separator = time.search(":");
			hour = Number(time.slice(0,separator));
			minute = Number(time.slice(separator + 1));
			with (this) { 
				setHours(hour);
				setMinutes(minute);
				setSeconds(0);
				setMilliseconds(0);
			};
		};
	};
	if (dateSTR.search(/\s\d+[AM|PM]/) > -1) {
		time = dateSTR.match(/\d+[AM|PM]/)[0];
		var cutoff = time.search(/[A|P]/);
		hour = Number(time.slice(0,cutoff));
		with (this) { 
			setHours(hour);
			setMinutes(minute);
			setSeconds(0);
			setMilliseconds(0);
		};
	};
	if ((dateSTR.search("PM") > -1) || (dateSTR.search("pm") > -1)) {
		if (hour < 12) { hour = hour + 12 };
	};
 };
 function myToString () {
	with(this) {
		var retval = String(getFullYear())+"-"+(((getMonth() + 1) < 10)?("0"+(getMonth()+1)):(getMonth()+1))+"-"+((getDate() < 10)?("0"+getDate()):getDate())+" "+((getHours() > 12)? (getHours() - 12):getHours())+":"+((getMinutes() < 10)?("0"+getMinutes()):getMinutes())+((getHours() > 12)?"PM":"AM");
		return retval;
	};
 };
 function justDate() {
 	with(this) {
		var retval = String(getFullYear())+"-"+(((getMonth() + 1) < 10)?("0"+(getMonth()+1)):(getMonth()+1))+"-"+((getDate() < 10)?("0"+getDate()):getDate());
		return retval;
	};
  };
 function getOccurence () {
 	with(this) {
		var occurence = Math.floor((getDate() - 1)/7) + 1;
		return occurence;
	};
};
function addDays( numDays ) {
	with(this){
		var curdate = getDate();
		curdate += numDays;
		setDate(curdate);
	};
};	
function addMonths( numMonths ) {
	with(this){
		var curmonth = getMonth();	
		curmonth += numMonths;
		setMonth( curmonth);
	};
};
function dateTypeOf() {
	return "date";
}
function dateGetAM() {
	var retval = "AM";
	with(this){
		if(getHours() > 12) {
			retval = "PM";
		};
	};
	return retval;
};
function dateJustTime() {
	var hours = 0;
	var retvalue = " ";
	with(this){
		hours = getHours();
		if(hours > 12){ hours -= 12; };
		retvalue = String(hours).concat(":",( (getMinutes() > 10)?getMinutes():"0".concat(getMinutes()) ),getAM());
	};
	return retvalue;
};
Date.prototype.justTime = dateJustTime;
Date.prototype.getAM = dateGetAM;
Date.prototype.typeOf = dateTypeOf;
Date.prototype.addMonths = addMonths;
Date.prototype.addDays = addDays;
Date.prototype.getWeek = getOccurence; 
Date.prototype.justDate = justDate;
Date.prototype.fromString = fromString;
Date.prototype.toString2 = myToString;