/**
 * @author Michael.Paige
 */
String.repeat = function(chr,count) {
	var str = "";
	for(var x=0;x<count;x++) {str += chr};
	return str;
}

String.prototype.padL = function(width,pad) {
	if (!width || width<1)
		return this;

	if (!pad) pad = " ";
	var length = width - this.length;
	if (length < 1) return this.substr(0,width);

	return (String.repeat(pad,length) + this).substr(0,width);
}

String.prototype.padR = function(width,pad) {
	if (!width || width<1)
		return this;

	if (!pad) pad = " ";
	var length = width - this.length;
	if (length < 1) this.substr(0,width);

	return (this + String.repeat(pad,length)).substr(0,width);
}

Date.prototype.formatDate = function(format) {
	var date = this;
	var _monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	var _dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	var _year = date.getFullYear();
	var _month = date.getMonth();
	var _day = date.getDay();
	var _date = date.getDate();
	var _hours = date.getHours();
	var _minutes = date.getMinutes();
	var _seconds = date.getSeconds();
	
	if (!format) {
		format = "MM/dd/yyyy";
	}

	format = format.replace("yyyy", "{0}").replace("yy", "{1}").replace("y", "{2}")
			.replace("MMMM", "{3}").replace("MMM", "{4}").replace("MM", "{5}").replace("M", "{6}")
			.replace("dddd", "{7}").replace("ddd", "{8}").replace("dd", "{9}").replace("d", "{10}")
			.replace("hh", "{11}").replace("h", "{12}").replace("HH", "{13}").replace("H", "{14}")
			.replace("mm", "{15}").replace("m", "{16}")
			.replace("ss", "{17}").replace("s", "{18}")
			.replace("tt", "{19}").replace("t", "{20}");

	//year
	if (format.indexOf("{0}") > -1)
		format = format.replace("{0}", _year.toString());
	if (format.indexOf("{1}") > -1)
		format = format.replace("{1}", _year.toString().substr(2, 2));
	if (format.indexOf("{2}") > -1)
		format = format.replace("{2}", parseInt(_year.toString().substr(2, 2)).toString());

	//month
	if (format.indexOf("{3}") > -1)
		format = format.replace("{3}", _monthNames[_month]);
	if (format.indexOf("{4}") > -1)
		format = format.replace("{4}", _monthNames[_month].substr(0, 3));
	if (format.indexOf("{5}") > -1)
		format = format.replace("{5}", (_month + 1).toString().padL(2, "0"));
	if (format.indexOf("{6}") > -1)
		format = format.replace("{6}", (_month + 1).toString());

	//day
	if (format.indexOf("{7}") > -1)
		format = format.replace("{7}", _dayNames[_day]);
	if (format.indexOf("{8}") > -1)
		format = format.replace("{8}", _dayNames[_day].substr(0, 3));
	if (format.indexOf("{9}") > -1)
		format = format.replace("{9}", _date.toString().padL(2, "0"));
	if (format.indexOf("{10}") > -1)
		format = format.replace("{10}", _date.toString());

	//hours
	if (format.indexOf("{11}") > -1) {
		var _h = _hours;
		if (_hours > 12) _h -= 12;
		if (_hours == 0) _h = 12;
		format = format.replace("{11}", _h.toString().padL(2, "0"));
	}
	if (format.indexOf("{12}") > -1) {
		var _h = _hours;
		if (_hours > 12) _h -= 12;
		if (_hours == 0) _h = 12;
		format = format.replace("{12}", _h.toString());
	}
	if (format.indexOf("{13}") > -1)
		format = format.replace("{13}", _hours.toString().padL(2, "0"));
	if (format.indexOf("{14}") > -1)
		format = format.replace("{14}", _hours.toString());

	//minutes
	if (format.indexOf("{15}") > -1)
		format = format.replace("{15}", _minutes.toString().padL(2, "0"));
	if (format.indexOf("{16}") > -1)
		format = format.replace("{16}", _minutes.toString());

	//seconds
	if (format.indexOf("{17}") > -1)
		format = format.replace("{17}", _seconds.toString().padL(2, "0"));
	if (format.indexOf("{18}") > -1)
		format = format.replace("{18}", _seconds.toString());

	//am/pm
	if (format.indexOf("{19}") > -1) {
		if (_hours > 11) {
			format = format.replace("{19}", "pm")
		} else {
			format = format.replace("{19}", "am");
		}
		if (format.indexOf("{20}") > -1) {
			if (_hours > 11) {
				format = format.replace("{20}", "p")
			} else {
				format = format.replace("{20}", "a");
			}
		}
	}

	return format;
}			

String.prototype.formatDate = function(format) {
	var d = new Date();

    if (this.indexOf("/Date(") > -1) { //Check if MS formatted date string
		var reMsJSONDate = /^\/Date\((-?[0-9]+)(?:[a-zA-Z]|(?:\+|-)[0-9]{4})?\)\/$/;
		
		var epochDate = reMsJSONDate.exec(this)[1];
		if (epochDate ) {
			d = new Date( +epochDate );
		}
    } else {
		var str = this.toString();
        d = new Date(str);
    }
    return d.formatDate(format);
}

function parseMSJSONDateString(dateString) {
	var reMsJSONDate = /^\/Date\((-?[0-9]+)(?:[a-zA-Z]|(?:\+|-)[0-9]{4})?\)\/$/; //Discards any timezone adjuster
	
	var epochDate = reMsJSONDate.exec(dateString)[1];

	if (epochDate ) {
		return new Date( +epochDate );
	}
}

function dateSeeker(date, i) {
	var currentDate = new Date(date); //Set current date
	var interval = i; //Set the interval as a variable
	currentDate.setDate(currentDate.getDate() + interval);

	return currentDate;
}

function daysBetween(date1, date2) {
	var date1 = (typeof date1 === 'string') ? new Date(date1) : date1;
	var date2 = (typeof date2 === 'string') ? new Date(date2) : date2;

	// The number of milliseconds in one day
	var ONE_DAY = 86400000;
	
	// Convert both dates to milliseconds
	var date1_ms = date1.getTime();
	var date2_ms = date2.getTime();
	
	// Calculate the difference in milliseconds
	var difference_ms = Math.abs(date1_ms - date2_ms)
	
	// Convert back to days and return
	return Math.round(difference_ms/ONE_DAY)
}

function findSunday(date) {
	// Returns a date object that is either itself (if Sunday), or the previous Sunday that began that week.
	Date.prototype.getSunday = function() {
		return (this.getDay() > 0) ? new Date(this.setDate(this.getDate() - this.getDay())) : this;
	}

    var dateObj = (date !== undefined) ? new Date(date) : new Date();
    return dateObj.getSunday();
}

function totalWeeks(date1, date2) {
	// The number of milliseconds in one day
	var ONE_DAY = 86400000;

	// Convert both dates to their respective Sunday
	var Sunday1 = findSunday(date1);
	var Sunday2 = findSunday (date2);

	// Calculate the difference between dates
	var difference = Math.abs(Sunday1 - Sunday2);

	// Calculate the total number of weeks
	var totalWeeks = Math.floor((Math.round(difference / ONE_DAY) /7) + 1);

	// Return total number of weeks
	return totalWeeks;
}

function getDay( dateStr ) {
	return new Date(dateStr).getDay();
}

