// American Format: 12/31/2000 5:00 pm
// Thanks, Wes Hays

Date.prototype.toFormattedString = function(include_time){
	str = Date.padded2(this.getMonth() + 1) + '/' +Date.padded2(this.getDate()) + '/' + this.getFullYear();

	if (include_time) { hour=this.getHours(); str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() + " " + this.getAMPM() }
	return str;
}
/*
Date.parseFormattedString = function (string) {
	// Test these with and without the time
	// 11/11/1111 12pm
	// 11/11/1111 1pm
	// 1/11/1111 10:10pm
	// 11/1/1111 01pm
	// 1/1/1111 01:11pm
	// 1/1/1111 1:11pm
	var regexp = "(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4}) *([0-9]{1,2}(:[0-9]{2})? *(am|pm))?";
	var d = string.match(new RegExp(regexp, "i"));
	if (d==null) {
		return Date.parse(string); // Give javascript a chance to parse it.
	}
	

	mdy = d[1].split('/');
	hrs = 0;
	mts = 0;
	if(d[3] != null) {
		hrs = parseInt(d[3].split('')[0], 10);
		if(d[5].toLowerCase() == 'pm') { hrs += 12; } // Add 12 more to hrs
		mts = d[4].split(':')[1];
	}

	return new Date(mdy[2], parseInt(mdy[0], 10)-1, mdy[1], hrs, mts, 0);
}
*/

Date.parseFormattedString = function (string) {  
	var regexp = "(([0-1]?[0-9])[-\/]([0-3]?[0-9])([-\/]([0-9]{2,4}))?)? *(([0-9]{1,2})(:([0-9]{2}))? *(am|pm)?)?";
	// E.g. for "11/25/2007 12:34 pm":
	// d[0]  = "11/25/2007 12:34 pm" // entire string
	// d[1]  = "11/25/2007"          // entire date
	// d[2]  = "11"                  // month
	// d[3]  = "25"                  // day
	// d[4]  = "/2007"               // slash and year (not directly used)
	// d[5]  = "2007"                // year
	// d[6]  = "12:34 pm"            // entire time
	// d[7]  = "12"                  // hours
	// d[8]  = ":34"                 // colon and minutes (not directly used)
	// d[9]  = "34"                  // minutes
	// d[10] = "pm"                  // am/pm

	var d = string.match(new RegExp(regexp, "i"));
	if (d == null) {
		return Date.parse(string); // Give javascript a chance to parse it.
	}

	var today = new Date();


	// Date
	var month;
	var day;
	var year;
	if (d[1] == null) {
		// No date given. Use today.
		year  = today.getFullYear();
		month = today.getMonth();
		day   = today.getDate(); 
	} else {
		month = parseInt(d[2], 10)-1;
		day   = parseInt(d[3], 10);
		if (d[5] == null) {
			// They didn't specify the year. Use this year.
			year = today.getFullYear();
		} else {
			year = parseInt(d[5], 10);
			// Check for 2-digit years
			if (year >= 0 && year < 50) {
				year += 2000;
			} else if (year >= 50 && year < 100) {
				year += 1900;
			}
		}  
	}

	// Time
	var hours;
	var minutes;
	if(d[6] == null) {
		// No time given. Set to midnight.
		hours = 0;
		minutes = 0;
	} else {
		// Hours
		hours = parseInt(d[7], 10);
		if(d[10] != null) {
			// Convert am/pm to 24-hour notation
			if(d[10].toLowerCase() == 'pm' && hours != 12) { 
				hours += 12; 
			} else if (d[10].toLowerCase() == 'am' && hours == 12) {
				hours = 0;
			}
		}
		// Minutes
		if(d[9] == null) {
			minutes = 0;
		} else {
			minutes = parseInt(d[9], 10);
		}
	}

	return new Date(year, month, day, hours, minutes, 0);
}


