/************************* DATE FUNCTIONS *************************************************
****************	<Date> = Date.dateAdd(<nodays>)	**************************************
****************	<String> = Date.dateAddWithFormat(<nodays>)	**************************
****************	<Date> = Date.dateSub(<nodays>)	**************************************
****************	<String> = Date.dateSubWithFormat(<nodays>)	**************************
****************	<String> = Date.formatDate(<format>) *********************************
*****************************************************************************************/

//////////////////////// (add number of days with the current date) ///////////////////////////////////////
if (!Date.prototype.dateAdd) {
	Date.prototype.dateAdd = function(nodays) {
		var dateinms = Date.parse(this.toString());
		var noofdaysinms = nodays * 24 * 60 * 60 * 1000;
		tmpDate = new Date(dateinms + noofdaysinms);
		return(tmpDate);
	};
}

//////////////////////// (add number of days with the current date and returns as specified format) //////////
if (!Date.prototype.dateAddWithFormat) {
	Date.prototype.dateAddWithFormat = function(nodays, formatstring) {
		var dateinms = Date.parse(this.toString());
		var noofdaysinms = nodays * 24 * 60 * 60 * 1000;
		tmpDate = new Date(dateinms + noofdaysinms);
		var regValid = new RegExp(/^([dd]{1,2}[\-|\/|\.][mm]{1,2}[\-|\/|\.][yyyy]{4})|([mm]{1,2}[\-|\/|\.][dd]{1,2}[\-|\/|\.][yyyy]{4})|([yyyy]{4}[\-|\/|\.][mm]{1,2}[\-|\/|\.][dd]{1,2})$/i);
		if(!regValid.test(formatstring))
			formatstring = "yyyy-mm-dd";	//if not matches with the above expression, setting the default format
		formatstring = formatstring.toLowerCase();
		intDate = tmpDate.getDate();
		intMonth = tmpDate.getMonth() + 1;
		formatstring = formatstring.replace("dd", (intDate < 10) ? '0' + intDate : intDate);
		formatstring = formatstring.replace("d", intDate);
		formatstring = formatstring.replace("mm", (intMonth < 10) ? '0' + intMonth : intMonth);
		formatstring = formatstring.replace("m", intMonth);
		formatstring = formatstring.replace("yyyy", tmpDate.getFullYear());
		return(formatstring);
	};
}


//////////////////////// (subtract number of days with the current date) ///////////////////////////////////////
if (!Date.prototype.dateSub) {
	Date.prototype.dateSub = function(nodays) {
		var dateinms = Date.parse(this.toString());
		var noofdaysinms = nodays * 24 * 60 * 60 * 1000;
		if((dateinms - noofdaysinms) >= 0){
			return(new Date(dateinms - noofdaysinms));
		}
		return(new Date());
	};
}

//////////////////////// (subtract number of days with the current date and returns as specified format) //////////
if (!Date.prototype.dateSubWithFormat) {
	Date.prototype.dateSubWithFormat = function(nodays, formatstring) {
		var dateinms = Date.parse(this.toString());
		var noofdaysinms = nodays * 24 * 60 * 60 * 1000;
		if((dateinms - noofdaysinms) >= 0){
			tmpDate = new Date(dateinms - noofdaysinms);
		}else{
			tmpDate = new Date(Date.parse(new Date().toString()) - noofdaysinms);
		}
		var regValid = new RegExp(/^([dd]{1,2}[\-|\/|\.][mm]{1,2}[\-|\/|\.][yyyy]{4})|([mm]{1,2}[\-|\/|\.][dd]{1,2}[\-|\/|\.][yyyy]{4})|([yyyy]{4}[\-|\/|\.][mm]{1,2}[\-|\/|\.][dd]{1,2})$/i);
		if(!regValid.test(formatstring))
			formatstring = "yyyy-mm-dd";	//if not matches with the above expression, setting the default format
		formatstring = formatstring.toLowerCase();
		intDate = tmpDate.getDate();
		intMonth = tmpDate.getMonth() + 1;
		formatstring = formatstring.replace("dd", (intDate < 10) ? '0' + intDate : intDate);
		formatstring = formatstring.replace("d", intDate);
		formatstring = formatstring.replace("mm", (intMonth < 10) ? '0' + intMonth : intMonth);
		formatstring = formatstring.replace("m", intMonth);
		formatstring = formatstring.replace("yyyy", tmpDate.getFullYear());
		return(formatstring);
	};
}

//////////////////////// (Formatting the date and returns as specified format) //////////
if (!Date.prototype.formatDate) {
	Date.prototype.formatDate = function(formatstring) {
		var regValid = new RegExp(/^([dd]{1,2}[\-|\/|\.][mm]{1,2}[\-|\/|\.][yyyy]{4})|([mm]{1,2}[\-|\/|\.][dd]{1,2}[\-|\/|\.][yyyy]{4})|([yyyy]{4}[\-|\/|\.][mm]{1,2}[\-|\/|\.][dd]{1,2})$/i);
		if(!regValid.test(formatstring))
			formatstring = "yyyy-mm-dd";	//if not matches with the above expression, setting the default format
		formatstring = formatstring.toLowerCase();
		intDate = this.getDate();
		intMonth = this.getMonth() + 1;
		formatstring = formatstring.replace("dd", (intDate < 10) ? '0' + intDate : intDate);
		formatstring = formatstring.replace("d", intDate);
		formatstring = formatstring.replace("mm", (intMonth < 10) ? '0' + intMonth : intMonth);
		formatstring = formatstring.replace("m", intMonth);
		formatstring = formatstring.replace("yyyy", this.getFullYear());
		return(formatstring);
	};
}

///////////////////////// Number of days between two dates /////////////////////////////
if (!Date.prototype.getDays) {
	Date.prototype.getDays = function(anotherDate) {
		var dateinms = Date.parse(this.toString());
		var adateinmns = Date.parse(anotherDate.toString());
		return(Math.ceil((dateinms - adateinmns) / (24 * 60 * 60 * 1000)));
	};
}

///////////////////////// One date is greater than another date /////////////////////////////
if (!Date.prototype.isGreater) {
	Date.prototype.isGreater = function(anotherDate) {
		var dateinms = Date.parse(this.toString());
		var adateinmns = Date.parse(anotherDate.toString());
		return(dateinms >= adateinmns);
	};
}


