
/*************************************************************************
'Function Name	: fnCheckGTCurrentDate
'Description	: This function is used to check the provided date is greater the current date
'Return			: -1 If Passed date is less tha CurrentDate
'			   0 If passed date is equal to current date
'			   1 If passed date is greater than current date
'Modification History
'Author						Date						Reference
'Kamlesh					16-08-2001					Created
'************************************************************************/



function fnCompareWithCurrentDate(strInputDate)
{
	var regExp = /\s+|[\s\,\-\.]/g; //Reg Exp Use to identify Date seperator.
	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	var strOutputDate //Use to store Date in specified format
	var chrDateSeperator  //Store  Date seperator 
	strInputDate = fnTrim(strInputDate); // Remove Leading and Trailing Spaces
	chrDateSeperator = strInputDate.charAt(strInputDate.length - 5); //Get Date Seperator
	strInputDate = strInputDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strInputDate.split("/"); //Split date into Day, Month and Year

	var dtmTempDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	
	var dtmCurTempDate = new Date();
	dtmCurTempDate = new Date(dtmCurTempDate.getYear(), dtmCurTempDate.getMonth(), dtmCurTempDate.getDate());
	if(dtmCurTempDate < dtmTempDate)
	{
		return 1;
	}
	else if(dtmCurTempDate == dtmTempDate)
	{
		return 0;
	}
	else if(dtmCurTempDate > dtmTempDate)
	{
		return -1;
	}

}



/*************************************************************************
'Function Name	: fnCheckGTCurrentDate
'Description	: This function is used to check the provided date is greater the current date
'Return			: Boolean - True If no error Else False
'Modification History
'Author						Date						Reference
'rajesh						08-07-2001					Created
'************************************************************************/



function fnCheckGTCurrentDate(strInputDate)
{
	var regExp = /\s+|[\s\,\-\.]/g; //Reg Exp Use to identify Date seperator.
	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	var strOutputDate //Use to store Date in specified format
	var chrDateSeperator  //Store  Date seperator 
	strInputDate = fnTrim(strInputDate); // Remove Leading and Trailing Spaces
	chrDateSeperator = strInputDate.charAt(strInputDate.length - 5); //Get Date Seperator
	strInputDate = strInputDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strInputDate.split("/"); //Split date into Day, Month and Year

	var dtmTempDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	
	var dtmCurTempDate = new Date()
	dtmCurTempDate.getDate()
	if(dtmCurTempDate < dtmTempDate)
	{
		return false
	}
	return true
}


/******************************************************************************
Function name:	fnAddDate(strInputDate, chrIntervalType, intInterval)
Description  :  This  function is used to Add or Subtract Day,Month ,Year from given Date. 
				Input Date will be in dd/mm/yyyy( ., - ,blank space can also be used as 
				date seperator). 
				function used : fnTrim(strYear)

Input		 :	1) Pass Valid Date in (dd/mm/yyyy) Format(.,-,space are allowed as 
				  Date seperator) as first Paramater.
				2) ChrIntervalType can Have only 3 values
					a) D = Day
					b) M = Month
					c) Y = Year
				3) intInterval represent the inteval to be added or subtracated from the given
					date. If intInterval is negative then inteval is subtracted form given date
					D in second parameter represent No. of Days to be added/subtrcated from given date.
					M in second parameter represent No. of Months to be added/subtrcated from given date.
					Y in second parameter represent No. of Years to be added/subtrcated from given date.
					
				
Output       :	If Error : Return False		
				Else Return The New Date after manipulation

Modification History:
Author			Date				Version				Reference
Kamlesh			13-02-2001			1.0					
*******************************************************************************/
function fnAddDate(strInputDate, chrIntervalType, intInterval)
{
	var regExp = /\s+|[\s\,\-\.]/g; //Reg Exp Use to identify Date seperator.
	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	var strOutputDate //Use to store Date in specified format
	var chrDateSeperator  //Store  Date seperator 
	strInputDate = fnTrim(strInputDate); // Remove Leading and Trailing Spaces
	chrDateSeperator = strInputDate.charAt(strInputDate.length - 5); //Get Date Seperator
	strInputDate = strInputDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strInputDate.split("/"); //Split date into Day, Month and Year

	var dtmTempDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	
	switch (chrIntervalType) //Check which type of date unit is passed
	{
		case "D": //If Days to be added
				var intTimeInMS=dtmTempDate.getTime()+(intInterval*24*60*60*1000);
				dtmTempDate.setTime(intTimeInMS);
				break;
		case "M": //If months to be added
				var intNewMonth = dtmTempDate.getMonth() + eval(intInterval);
				dtmTempDate.setMonth(intNewMonth);
				break;
		case "Y": //if Year to be added
				var intNewYear = dtmTempDate.getFullYear() + eval(intInterval);
				dtmTempDate.setFullYear(intNewYear);
				break;
		default:
				alert("Interval Type is not correct")
				return false
	}

	// Generate new date with same Date seperator passed originally.
	if (dtmTempDate.getDate() < 10) 
	{
		strOutputDate = "0" + dtmTempDate.getDate() + chrDateSeperator; //Convert Day in DD format
	}
	else
	{
		strOutputDate = dtmTempDate.getDate() + chrDateSeperator;
	}
	if ((dtmTempDate.getMonth()+1) < 10)
	{
		// Convert Month in MM Format
		strOutputDate = strOutputDate + "0" + (dtmTempDate.getMonth()+1) + chrDateSeperator;
	}
	else
	{
		strOutputDate = strOutputDate + (dtmTempDate.getMonth()+1) + chrDateSeperator;
	}
	
	strOutputDate = strOutputDate + dtmTempDate.getFullYear();
	return strOutputDate //Return new date
}


/******************************************************************************
Function name:	fnDateDiff(strFromDate,strToDate)
Description  :  This  function is used to find out Number of days between two given Date.
				Number of Days = strToDate - strFromDate
				function used : fnTrim(strYear)

Input		 :	1) Pass Valid Date in (dd/mm/yyyy) Format(.,-,space are allowed as 
				  Date seperator) as first and second Paramater.
				
Output       :	Return Number of Days
				Number of Days > 0 if strToDate > strFromDate
				Number of Days < 0 if strToDate < strFromDate
				Number of Days = 0 if strToDate = strFromDate

Modification History:
Author			Date				Version				Reference
Kamlesh			13-02-2001			1.0					
*******************************************************************************/
function  fnDateDiff(strFromDate,strToDate)
{
	var dtmFromDate,dtmToDate //Store From and To Date
	var intDateDiff; // Variable is used to store diffrence between given date
	var regExp = /\s+|[\s\,\-\.]/g; //Reg Exp Use to identify Date seperator.
	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	var chrDateSeperator  //Store  Date seperator 
	strFromDate = fnTrim(strFromDate); // Remove Leading and Trailing Spaces
	strToDate = fnTrim(strToDate); // Remove Leading and Trailing Spaces
	chrDateSeperator = strFromDate.charAt(strFromDate.length - 5); //Get Date Seperator
	strFromDate = strFromDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strFromDate.split("/"); //Split date into Day, Month and Year
	dtmFromDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	strToDate = strToDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strToDate.split("/"); //Split date into Day, Month and Year
	dtmToDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter	

	var intDateDiff = dtmToDate.getTime() - dtmFromDate.getTime(); //Get Date diff in Millisecond
	intDateDiff = Math.floor(intDateDiff / (1000 * 60 * 60 * 24)); //Convert diff in days
	
	return intDateDiff; //Retrun Number of days
}



/******************************************************************************
Function name:	fnDateCompare(strFromDate,strToDate)
Description  :  This  function is used to compare Dates as passed as parameter
				function used : fnTrim(strYear)

Input		 :	1) Pass Valid Date in (dd/mm/yyyy) Format(.,-,space are allowed as 
				  Date seperator) as first and second Paramater.
				
Output       :	Return 1 if strToDate > strFromDate
				Return -1 if strToDate < strFromDate
				Return 0 if strToDate = strFromDate

Modification History:
Author			Date				Version				Reference
Kamlesh			13-02-2001			1.0					
*******************************************************************************/

function fnDateCompare(strFromDate,strToDate)
{
	
	var regExp = /\s+|[\s\,\-\.]/g; //Reg Exp Use to identify Date seperator.
	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	strFromDate = fnTrim(strFromDate); // Remove Leading and Trailing Spaces
	strToDate = fnTrim(strToDate); // Remove Leading and Trailing Spaces
	strFromDate = strFromDate.replace(regExp,"/"); //Replace Date seperator by "/"
	strToDate = strToDate.replace(regExp,"/"); //Replace Date seperator by "/"
	arrDate = strFromDate.split("/");
	var dtmFromDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	arrDate = strToDate.split("/");
	var dtmToDate = new Date(arrDate[2],eval(arrDate[1])-1,arrDate[0]) //Create Date object with input Date as parameter
	if (dtmFromDate < dtmToDate) 
	{
		return 1
	}
	else if (dtmFromDate > dtmToDate) 
	{
		return -1
	}
	else
	{
		return 0
	}
}



/******************************************************************************
Function name:	fnLeapYear(intYear)
Description  :  This  function is used to find out whether the Year passed as a parameter
				is a leap year or not?
				If year passed as a parameter lies between 0 and 99 then 
				convert year into 2000 - 2099 
				function used : fnTrim(strYear)
								fnMandatory(strYear)

Input		 :	1) Input will be a valid year
				
Output       :	If LeapYear then return True
				Else False		

Modification History:
Author			Date				Version				Reference
Kamlesh			13-02-2001			1.0					
*******************************************************************************/

function fnLeapYear(strYear)
{
	var intYear;
	if (!fnMandatory(strYear)) //Mandatory check for parameters
	{
		return false;
	}
	
	strYear = fnTrim(strYear); //Remove leading and trailing spaces
	intYear = Math.abs(strYear); //Cast Year into numerci value.
	if ((intYear >=0) && (intYear <= 99))
	{
	//If year passed as a parameter lies between 0 and 99 then convert year into 2000 - 2099 
		intYear = 2000 + intYear;
	}
	if ((intYear % 400 == 0) || (intYear % 100 != 0 && intYear % 4 == 0))
	{	
		return true; //Given Year is a leap year
	}
	else
	{
		return false; //Given Year is not leap year
	}
}

/******************************************************************************
Function name:	fnGetDaysInMonth(intMonth, intYear) 
Description  :  This  function is used to get number of days in a month for a given 
				month and year passed as parameter.
				function used : 1)fnLeapYear(strYear)

Input		 :	1) Input will be a valid month and year
				
Output       :	If Valid Month then
					Return Number of days in a month paseed as parameter
				Else
					return -1

Modification History:
Author			Date				Version				Reference
Kamlesh			13-02-2001			1.0					
*******************************************************************************/
function fnGetDaysInMonth(strMonth, strYear) 
{
	if ((!fnMandatory(strYear)) || (!fnMandatory(strMonth))) //Mandatory check for parameters
	{
		return false;
	}

	switch (Math.abs(strMonth)) //Convert month into interger type and then match
	{
		case 1:
			return 31; 
		case 2:
			if (fnLeapYear(strYear)) //Check for leap year
			{
				return 29; //If Leap year
			}
			else 
			{
				return 28;
			}
		case 3:
			return 31;
		case 4:
			return 30;
		case 5:
			return 31;
		case 6:
			return 30;
		case 7:
			return 31;
		case 8:
			return 31;
		case 9:
			return 30;
		case 10:
			return 31;
		case 11:
			return 30;
		case 12:
			return 31;
		default :
			return -1;
	}
}



/******************************************************************************
Function name:	fnValidDate(strInputDate,strFormat)
Description  :  This  function is used to Validate Date with the format passed as parameter. 
				Input Date will be in any permissible format.Format allowed is wriiten in 
				Input section.
				This function calls 
					fnTrim(strValue) ,
					fnGetDaysInMonth(strMonth, strYear) ,

Input		 :	1) Pass  Date 
				
Output       :	If Valid Date and format then return True
				Else return False
				
Assumption	 : Valid Year Range is 1900 To 2099.

Modification History:
Author			Date				Version				Reference
Kamlesh			14-02-2001			1.0					
*******************************************************************************/

function fnValidDate(strInputDate)
{

	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	var blnValidFormat = true //check for valid format

	strInputDate = fnTrim(strInputDate); // Remove Leading and Trailing Spaces
	arrDate = strInputDate.split("/"); //Split date into Day, Month and Year
	if (arrDate.length < 3) //at least one of the component is missing  from the date
	{
		return false
	}
	if ((arrDate[2] >= 1900) && (arrDate[2] <= 2099)) //Year(YYYY) must lies between 1900 to 2099
	{
		if ((arrDate[1] >=1) && (arrDate[1] <= 12)) //Month must lies between 1 to 12
		{
			//Validate Date for given month and year
			if ((arrDate[0] <= fnGetDaysInMonth(arrDate[1], arrDate[2])) && (arrDate[0] >= 1))
			{
				return true;
			}
		}
	}
	return false; //Invalid date
}


