///////////////////////////////////////////////////////////////////////////////////////////////
//INC_DATE.JS VERSIONE 1.1/////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

var theDay = 0;
var theMonth = 0;   
var theYear = 0;
var theDate = "";

///////////////////////////////////////////////////////////////////////////////////////////////
//CHECK DAY////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function isDay() {	
	if(theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7	|| theMonth == 8 || theMonth == 10 || theMonth == 12) {
		if (theDay >= 1 && theDay <= 31)
			return true;
		else
			return false;
	}
	else if (theMonth == 2) {
		if (theDay >= 1 && theDay <= 28)
			return true;
		else if (theDay == 29 && isYearLeap(theYear))
			return true;
		else
			return false;
	}
	else {
		if (theDay >= 1 && theDay <= 30)
			return true;
		else
			return false;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////
//CHECK MONTH//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function isMonth() {
	if(theMonth <= 12 && theMonth != 0)
		return true;
	else
		return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//CHECK YEAR///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function isYear() {
   if (theYear >= 1900 && theYear <= 2010)
	  return true;
   else
	  return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//CHECK LEAP YEAR//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function isYearLeap() {
	if (((theYear % 4) == 0 && (theYear % 100) != 0) || (theYear % 400) == 0)
		return true;
	else
		return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//EXTRACT DAY, MONTH AND YEAR VALUES///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function separateDateString(dateString) {
	var retVal = 0;
	var slash1 = 0;
	var slash2 = 0;
	var numSlashes = 0;
	
	for (slash1; slash1 < dateString.length && numSlashes == 0; slash1++) {
		if (dateString.charAt(slash1) == "/" || dateString.charAt(slash1) == "-")
			numSlashes++;
	}	
	for (slash2 = slash1; slash2 < dateString.length && numSlashes == 1; slash2++) {
		if (dateString.charAt(slash2) == "/" || dateString.charAt(slash2) == "-")
			numSlashes++;
	}
	
	if (numSlashes == 2) {	
		theDay   = dateString.substring(0, slash1 - 1);
		theMonth = dateString.substring(slash1, slash2 - 1);
		theYear = dateString.substring(slash2, dateString.length);
		
		retVal = isDate(theDay, theMonth, theYear);
	}
	else
		retVal = 4;
	
	if (theMonth < 10) theMonth = "0" + theMonth;		
	if (theDay < 10) theDay = "0" + theDay;
	theDate = theDay + "/" + theMonth + "/" + theYear;
	
	return retVal;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//CHECK DATE///////////////////////////////////////////////////////////////////////////////////
//retVal = 0 - VALID DATE//////////////////////////////////////////////////////////////////////
//retVal = 1 - NOT VALID MONTH/////////////////////////////////////////////////////////////////
//retVal = 2 - NOT VALID DAY///////////////////////////////////////////////////////////////////
//retVal = 3 - NOT VALID YEAR//////////////////////////////////////////////////////////////////
//retVal = 4 - NOT VALID DATE//////////////////////////////////////////////////////////////////		
///////////////////////////////////////////////////////////////////////////////////////////////
function isDate(enteredDay, enteredMonth, enteredYear) {
	var retVal = 0;
		
	theDay = enteredDay;
	theMonth = enteredMonth;
	theYear = enteredYear;
	
	if(enteredMonth == null && enteredYear == null)
		retVal = separateDateString(enteredDay);
	else if (!isMonth())
		retVal = 1;
	else if (!isDay())
		retVal = 2;
	else if (!isYear())
		retVal = 3;
	
	return retVal;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//FORMAT DATE IN DIFFERENT STRING FORMATS//////////////////////////////////////////////////////
//dateString = null - CURRENT DATE/////////////////////////////////////////////////////////////
//formatNumber = 0 - DD/MM/YYYY////////////////////////////////////////////////////////////////
//formatNumber = 1 - MM/DD/YYYY////////////////////////////////////////////////////////////////
//formatNumber = 2 - YYYYMMDD//////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function formatDateString(dateString, formatNumber) {
	if (dateString <= 2 && formatNumber == null) {
		theDate = new Date();
		theDay = theDate.getDate();
		theMonth = theDate.getMonth() + 1;
		theYear = theDate.getFullYear();
	}
	
	separateDateString(dateString);	
	
	if (formatNumber == 0)
		theDate = theDay + "/" + theMonth + "/" + theYear;
	else if (formatNumber == 1)
		theDate = theMonth + "/" + theDay + "/" + theYear;
	else if (formatNumber == 2)
		theDate = theYear + theMonth + theDay;
	
	return theDate;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//COMPARE TWO DATES////////////////////////////////////////////////////////////////////////////
//retVal = 0 - DATE1 = DATE2///////////////////////////////////////////////////////////////////
//retVal = 1 - DATE1 > DATE2///////////////////////////////////////////////////////////////////
//retVal = 2 - DATE1 < DATE2///////////////////////////////////////////////////////////////////
//retVal = 3 - DATE1 NOT VALID/////////////////////////////////////////////////////////////////
//retVal = 4 - DATE2 NOT VALID/////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function compareDates(dateString1, dateString2) {
	var retVal = 0;
	var theDate1 = formatDateString(dateString1, 2);
	var theDate2 = formatDateString(dateString2, 2);
	
	if (isDate(dateString1) != 0)
		retVal = 3;
	else if (isDate(dateString2) != 0)
		retVal = 4;
	else if (theDate1 == theDate2)
		retVal = 0;
	else if (theDate1 > theDate2)
		retVal = 1;
	else if (theDate1 < theDate2)
		retVal = 2;
		
	return retVal;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//ADD OR SUBSTRACT INTERVAL TIME TO A DATE/////////////////////////////////////////////////////
//intervalString = d - DAY/////////////////////////////////////////////////////////////////////
//intervalString = m - MONTH///////////////////////////////////////////////////////////////////
//intervalString = y - YEAR////////////////////////////////////////////////////////////////////
//intervalNumber = POSITIVE VALUE TO ADD, NEGATIVE VALUES TO SUBSTRACT/////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
function dateAdd(dateString, intervalString, intervalNumber) {
	theDate = new Date(formatDateString(dateString, 1));
	
	if (intervalString == 'd')
		theDate = new Date(theDate.setDate(theDate.getDate() + intervalNumber));
	else if (intervalString == 'm')
		theDate = new Date(theDate.setMonth(theDate.getMonth() + intervalNumber));
	else if (intervalString == 'y')
		theDate = new Date(theDate.setFullYear(theDate.getFullYear() + intervalNumber));
	
	theDay = theDate.getDate();
	theMonth = theDate.getMonth() + 1;
	theYear = theDate.getFullYear();
	theDate =  formatDateString(theDay + '/' + theMonth  + '/' + theYear, 0);
	
	return theDate;
}
///////////////////////////////////////////////////////////////////////////////////////////////