

/******************************************************************************
Function name:	fnIsInteger(strInteger,intRange)
Description  :  This  function is used for validating integer under given parameters.
				Maximum digit in the integer must be less than max length passed as
				parameter and integer must lie between the range specified in Third parameter.
				function used : 1) fnTrim(strValue)

Assumption	 :  1)There should be no space between sign and digit
				2)intRange should contain permissible value given below
				3)4.0 is not treated as interger with value 4

Input		 :	1) Pass value to be validated
				2) Pass valid range
					Valid Range are :
						1)	0  -> Any Input
						2)	2  -> only +ve ( *zero not included)
						3)	8  -> only  -ve ( * zero not included)
						4)	16 -> >= 0
						5)  32 -> <= 0
						6)  64 -> only 0

Output       :	If Valid Integer (Check Against each parameter)
					Return True
				Else
					Return False


*******************************************************************************/
function fnIsInteger(strInteger,intRange)
{
	if (!fnMandatory(strInteger)) //Mandatory check for parameters
	{
		return false;
	}

	/*Generate Regular expression which will check
	1) First character must be (+,- or any valid digit)
	2) Input value is a valid interger
	*/
	var regExp = new RegExp("\^\[\+\-\]\\d{1,999}\$\|\^\\d{1,999}\$");

	strInteger = fnTrim(strInteger); //Remove leading and trailing spaces
	if (regExp.test(strInteger)) //Validate Integer
	{
		strInteger = strInteger.valueOf(Number)//Convert to numeric value
		switch (parseInt(intRange,10)) //Match Range passed as Third parameter
		{
			case 0:
				return true;
			case 2:
				if (strInteger > 0)
				{
					return true;
				}
				return false;
			case 8:
				if (strInteger < 0)
				{
					return true;
				}
				return false;
			case 16:
				if (strInteger >= 0)
				{
					return true;
				}
				return false;
			case 32:
				if (strInteger <= 0)
				{
					return true;
				}
				return false;
			case 64:
				if (strInteger == 0)
				{
					return true;
				}
				return false;
			default:
				return false;	// Not a Valid Range
		}
	}
	return false; //Not a valid integer
}



/******************************************************************************
Function name:	fnIsNumeric(strInteger)
Description  :  This  function is used to check whether input parameter is a valid numeric
				value or not?
				function used : 1) fnTrim(strValue)

Assumption	 :  1)There should be no space between sign and digit
				2)2. is not a valid number
				3 .2 is a valid number

Input		 :	1) Pass any real number as a parameter

Output       :	If Valid Number
					Return True
				Else
					Return False


*******************************************************************************/
function fnIsNumeric(strInteger)
{
	/*Generate Regular expression which will check
	1) If (+,-) sign exist then  it must be the first character.
	2) Input value is a valid real number
	*/
	var regExp = /^[\-\+]{0,1}\d{0,100}\.{0,1}\d{1,100}$/
	strInteger = fnTrim(strInteger); //Remove leading and trailing spaces
	if (regExp.test(strInteger)) //Validate Integer
	{
		return true;
	}
	return false; //Not a valid integer
}



/******************************************************************************
Function name:	function fnIsReal(strInteger,intScale,intPrecision,intRange)
Description  :  This  function is used for validating integer under given parameters.
				Maximum digit in the integer must be less than max length passed as
				parameter and integer must lie between the range specified in Third parameter.
				function used : 1) fnTrim(strValue)

Assumption	 :  1)There should be no space between sign and digit
				2)intRange should contain permissible value given below
				3)4.0 is not treated as interger with value 4

Input		 :	1) Pass value to be validated
				2) Pass integer value.
				3) Pass valid range
					Valid Range are :
						1)	0  -> Any Input
						2)	2  -> only +ve ( *zero not included)
						3)	8  -> only  -ve ( * zero not included)
						4)	16 -> >= 0
						5)  32 -> <= 0
						6)  64 -> only 0

Output       :	If Valid Integer (Check Against each parameter)
					Return True
				Else
					Return False


*******************************************************************************/

function fnIsReal(strInteger,intScale,intPrecision,intRange)
{

	//Mandatory check for parameters
	if ((!fnMandatory(strInteger))||(!fnMandatory(intScale))||(!fnMandatory(intPrecision)))
	{
		return false;
	}
	if (intPrecision == 0)
	{
		intPrecision = 999; //Assume that max. length of any number befor decimal is less than 999 digit
	}
	if (intScale == 0)
	{
		intScale = 999; //Assume that max. length of any number after decimal is less than 999 digit
	}
	/*Generate Regular expression which will check
	1) First character must be (+,- or any valid digit)
	2) Input value is a valid interger
	3) Reg. Exp checks No. of digit before decimal in first parameter must be less than equal
	to Scale
	4) Reg. Exp also checks No. of digit after decimal in first parameter must be less than equal
	to the precision.
	*/
	var regExp = new RegExp("\^\[\\-\\+\]{0,1}\\d{0," + intScale+ "}$\|\^\[\\-\\+\]{0,1}\\d{0," + intScale+ "}\\.{1,1}\\d{1," + intPrecision+ "}$");
	strInteger = fnTrim(strInteger); //Remove leading and trailing spaces
	if (regExp.test(strInteger)) //Validate Integer
	{
		strInteger = strInteger.valueOf(Number)//Convert to numeric value
		switch (parseInt(intRange,10)) //Match Range
		{
			case 0:
				return true;
			case 2:
				if (strInteger > 0)
				{
					return true;
				}
				return false;
			case 8:
				if (strInteger < 0)
				{
					return true;
				}
				return false;
			case 16:
				if (strInteger >= 0)
				{
					return true;
				}
				return false;
			case 32:
				if (strInteger <= 0)
				{
					return true;
				}
				return false;
			case 64:
				if (strInteger == 0)
				{
					return true;
				}
				return false;
			default:
				return false;	// Not a Valid Range
		}
	}
	return false; //Not a valid integer
}




/******************************************************************************
Function name:	function fnRound(intNumber)
Description  :  This  function is used for truncate real number upto 2 place
				of decimal.

Assumption	 :  1)Number should be valid real number

Input		 :	1) Pass Number

Output       :	Number rounded upto 2 place of decimal


*******************************************************************************/

function fnRound(intNumber)
{
	var arrSplitByDec;
	var intUptoTwoDec
	var intFinalNumber
	intFinalNumber = intNumber.toString();
	arrSplitByDec = intFinalNumber.split(".")
	if (arrSplitByDec.length == 2)
	{

		if (arrSplitByDec[1].length > 2)
		{

			if (parseInt(arrSplitByDec[1].charAt(2)) >= 5)
			{
				intUptoTwoDec = eval(arrSplitByDec[1].substr(0,2)) + 1;
				intFinalNumber = arrSplitByDec[0] + "." + intUptoTwoDec;
			}
			else
			{
				intUptoTwoDec = arrSplitByDec[1].substr(0,2);
				intFinalNumber = arrSplitByDec[0] + "." + intUptoTwoDec;
			}
		}
	}
	return intFinalNumber;
}







/*************************************************************************
'Function Name	: fnCheckTextAreaLength
'Description	: This function is used to check the maximum length of text area
'Return			: Boolean - True If no error Else False

'************************************************************************/

function fnCheckTextAreaLength(r_strMsg,r_varCtrl,r_length)
{
    if ( r_varCtrl.value.length > r_length )
    {
		var lc_strErr
		lc_strErr = r_strMsg + " cannot be greater than "  + r_length + " characters"
		alert(lc_strErr)
		r_varCtrl.select()
		r_varCtrl.focus()
		return false

    }
    return true

}

/******************************************************************************
Function name:	fnPinCheck
Description  :  This function will be used for Pin Number validation.The valid character
		character set includes : numbers only
function used : 1) fnTrim(strValue)
		2) fnMandatory(strString)

Input		 :	The form Control Value to be checked
Output       :	Boolean ( true/false)

*******************************************************************************/

function fnPinCheck(strTxtCtrlValue)
{
	var regExp
	if (fnMandatory(strTxtCtrlValue) == false) // This includes Null and Space check
		return false;
	strTxtCtrlValue = fnTrim(strTxtCtrlValue);
	regExp = new RegExp("\[\^0-9\]");
	if (regExp.test(strTxtCtrlValue))
		return false;
	return true;
}


/******************************************************************************
'Function name: fnPhoneNumberCheck
'Description  :  This function will be used for the validating phone number
'                Functions Used
'Input        :  The form Control Value to be checked
'Output       :  Boolean ( true/false)

'*******************************************************************************/
function fnPhoneNumberCheck(strPhoneNumber)
{
    var regEx;
    regEx = new RegExp("\^\[\^\\d\]|\[\^\\-\\ \\d\]|\[\^\\d\]\$|\\--");
    if (regEx.test(strPhoneNumber)) //Validate Phone Number
    {
		return false;
	}
	else
	{
		return true;
	}
}


/******************************************************************************
Function name:	fnEmailValidation
Description  :  This function will be used for email validation.The valid character
		character set includes : Alphabets, Numbers, _,.,-
function used : 1) fnTrim(strValue)
		2) fnMandatory(strString)
		3) fnIsAlphaNumeric(strString,strType)

Input		 :	The form Control Value to be checked
Output       :	Boolean ( true/false)

*******************************************************************************/
function fnEmailValidation(strTxtCtrlValue)
{
	var strAtSym
	var strPeriod
	var strSpace
	var strLength

	if (fnMandatory(strTxtCtrlValue) == false) // This includes Null and Space check
		return false;
	strTxtCtrlValue = fnTrim(strTxtCtrlValue);
	if (strTxtCtrlValue.length == 0 )			 // Spaces Check
		return false;
	var regExpression = /[^0-9a-zA-Z][^0-9a-zA-Z]/;
	if (regExpression.test(strTxtCtrlValue))
		return false;
	if (strTxtCtrlValue.search(/\s+/) != -1 ) // checking space in between email id
		return false;
	var arrSplit = new Array();
	arrSplit = 	strTxtCtrlValue.split("@");
	if ( arrSplit.length != 2) 				 // Only one @ is allowed in an email id
		return false;
	// Call Function for Alphanumeric Check along with special character (. - _)
	// for the string before @ and pass 0 in maxlength as maxlength check not required

	if (fnIsAlphaNumeric(arrSplit[0],"E") == false)
		return false;

	//Check whether first charater is in a-z /A-Z
	var strValidchar = arrSplit[0].substring(0,1)
	if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
		return false
	// Call Function for IsAlpha Check along with special character (.)
	// for the string after @ and pass 0 in maxlength as maxlength check not required
	if (fnIsAlphaNumeric(arrSplit[1],"E")== false)
		return false;

	//Check whether first charater is in a-z /A-Z
	strValidchar = arrSplit[1].substring(0,1)
	if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
		return false
	//Check whether last character is in a-z /A-Z
	strValidchar = arrSplit[1].substring(arrSplit[1].length-1,arrSplit[1].length)

	if (!((strValidchar >= "A") && (strValidchar <= "Z")) &&  !((strValidchar >= "a") && (strValidchar <= "z")))
		return false

	//Every portion of the email address after @ must be of <= 2 characters
	var arrEmails
	arrEmails = arrSplit[1].split(".");
	for (intm = 0;intm < arrEmails.length;intm++ )
	{
		if (arrEmails[intm].length < 2)
			return false;
	}
	strAtSym=strTxtCtrlValue.indexOf('@')
	strPeriod=strTxtCtrlValue.lastIndexOf('.')
	strSpace=strTxtCtrlValue.indexOf(' ')
	strLength=strTxtCtrlValue.length-1
	if ((strAtSym < 1) ||(strPeriod <= strAtSym+1)|| (strPeriod==strLength) ||(strSpace!=-1))
		return false;
	return true;
}


/******************************************************************************
Function name:	fnIsCharcter(strString,chrFormat,intMaxLength,strSpecialChar)
Description  :  This  function is used for alphabets including some special characters.
		Input string will also be validate against Format and max Length.
function used : 1) fnTrim(strValue)
2) fnMandatory(strString)

Input		 :	1) Pass string to be validated
			2) Contain string of special character

Output       :	If Valid charcter (Check Against each parameter)
					Return True
				Else
					Return False

*******************************************************************************/
function fnIsCharacter(strString,strSpecialChar)
{
	var regExp;
	// Check for mandatory parameters
	if (!fnMandatory(strString)) //Mandatory check for parameters
	{
		return false;
	}
	strString = fnTrim(strString); //Remove leading and trailing spaces
	var regSpChar = /([\$\@\#\%\^\&\*\(\)\[\]\+\_\{\}\`\|\-\\])/g;
	strSpecialChar = strSpecialChar.replace(regSpChar,"\\$1") //Take care of special character in RegExp
	regExp = new RegExp("\[\^a-zA-Z" + strSpecialChar + "\]");
	if (regExp.test(strString))
	{
		return false;
	}
	return true; //Valid character
}



/******************************************************************************
Function name:	fnIsAlphaNumeric(strString,strSpecialChar)
Description  :  This  function is used for alphabets,digits including some special
		characters.	Input string will also be validate against Format and max Length.
function used : 1) fnTrim(strValue)
		2) fnMandatory(strString)

Input		 :	1) Pass string to be validated
			2) Contain string of special character
Output       :	If Valid charcter (Check Against each parameter)
					Return True
				Else
					Return False

*******************************************************************************/
function fnIsAlphaNumeric(strString,strType)
{
	var regExp;
	// Check for mandatory parameters
	if (!fnMandatory(strString)) //Mandatory check for parameters
	{
		return false;
	}
	var strSpecialChar;
	if (strType == "N")
	{
		strSpecialChar = "'._,- \""
	}
	else if (strType == "U")
	{
		strSpecialChar = "'.- \""
	}
	else if (strType == "E")
	{
		strSpecialChar = ".-_"
	}

	strString = fnTrim(strString); //Remove leading and trailing spaces
	var regSpChar = /([\$\@\#\%\^\&\*\(\)\[\]\+\_\{\}\`\|\-\\])/g;
	strSpecialChar = strSpecialChar.replace(regSpChar,"\\$1") //Take care of special character in RegExp
	regExp = new RegExp("\[\^a-zA-Z0-9" + strSpecialChar + "\]");
	if (regExp.test(strString))
	{
		return false;
	}
	return true; //Valid Character
}


/******************************************************************************
Function name:	fnErrorMessage
Description  :  This function will be used to display a error message corresponding to an error code
Input		 :	Error Code
			Form Field Name

Output       :	Boolean ( true/false)

*******************************************************************************/

function fnErrorMessage(intErrorCode,strFieldName)
{

	switch (intErrorCode) //Pass Errorcode
	{
		case 1:
			alert(strFieldName +  " must be entered") //Mandatory check
			return true;
		case 2:
			alert("Invalid " + strFieldName) //For Phone number, contactno,Faxno or any general error
			return true;
		case 3:
			alert("Invalid " + strFieldName + " Address") //For Email Address
			return true;
		case 4:
			alert(strFieldName + " can have [A-Z],[a-z],[0-9], spaces and [\" _ - , . '] only") // for alphanumeris values like password
			return true;
		case 5:
			alert(strFieldName + " can have [A-Z],[a-z], spaces and [' -] only") // for pure alphabets like Name
			return true;
		case 6:
			var arrSplit = new Array();
			arrSplit = 	strFieldName.split(",");
			alert(arrSplit[0] + " must be less than " + arrSplit[1]) //Date compare
			return true;
		case 7:
			alert("Invalid " + strFieldName)             // Invalid date
			return true;
		case 8:
			alert(strFieldName + " must be a positive number")  // For integer eg Quantity, Lineunits
			return true;
		case 9:
			alert("Invalid " + strFieldName) // For real values eg Price
			return true;
		case 10:
			alert(strFieldName + " can have [A-Z],[a-z],[0-9], spaces and [\" - . '] only") // for alphanumeris values like password
			return true;
		case 11:
			var blnAns;
			blnAns = confirm("Are you sure you want to delete the record(s)?");
			return blnAns;

		case 12:
			alert(strFieldName + " must be selected") // for drop down , list boxes
			return true;

		case 13:
			alert("Atleast one " + strFieldName + " must be selected for deletion") // for drop down , list boxes
			return true;
		case 14:
			alert("The passwords you typed do not match. Type the same password in both text boxes") // for drop down , list boxes
			return true;
		case 15:
			alert("Total Number of character exceeded in " + strFieldName) // for text area
			return true;
		case 16:
			alert(strFieldName + " cannot be less than Current Date") // for text area
			return true;


	}

}



/******************************************************************************
Function name:	fnMandatory
Description  :  This function will be used for the manadatory  text and
				text area controls of the form
Functions Used
		1. fnTrim(strValue)

Input		 :	The form Control Value to be checked
Output       :	Boolean ( true/false)

*******************************************************************************/

function fnMandatory(strTxtCtrlValue)
{
	if (strTxtCtrlValue == "")
        return false

    strTxtCtrlValue = fnTrim(strTxtCtrlValue)
    if (strTxtCtrlValue.length == 0)
       return false

	return true
}



/******************************************************************************
Function name:	fnTrim(strString)
Description  :  This function is used to remove leading  and trailing spaces
		from the input string.

Input		 :	Pass string as input.

Output       :	String with the leading & trailing whitespaces being chopped off.


*******************************************************************************/
function fnTrim(strString)
{
	// white space consist of (blank,tab,newline)
	var intLeftIndex = 0; //Store position of first non-white space from leftmost side
	var intRightIndex = 0; //Store position of first non-white space from rightmost side
	var blnFound = false; //Check for any non-white space character
	var regExp = /\S+/;
	var intCount;
	if (strString.search(regExp) == -1) //Check for non-white space character
	{
		strString = ""; //Valid character not found then return empty string
		return(strString);
	}

	//If  atleast one non-white space character found.
	for (intCount=0;intCount < strString.length; intCount++)
	{
		if (strString.charAt(intCount) != " ") // Checking for first non-white spaces
											// from left side
		{
			intLeftIndex = intCount - 1;
			break;
		}
	}
	for (intCount=strString.length - 1;intCount >= 0; intCount--)
	{
		if (strString.charAt(intCount) != " ") // Checking for first non-white spaces
											// from Right most side
		{
			intRightIndex = intCount + 1;
			break;
		}
	}

	strString=strString.substring(intLeftIndex+1,intRightIndex); //Remove leading and trailing
														// spaces
	return (strString);
}


/******************************************************************************
Function name:	fnCancel(strURL)
Description  :  This function is used to redirect user on click of cancel button

Input	     :	Pass URL as input.

*******************************************************************************/

function fnCancel(strFormName,strURL)
{
	strFormName.action = strURL;
	strFormName.submit();
}



/*************************************************************************
'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
'************************************************************************/


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

'************************************************************************/



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

*******************************************************************************/
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

*******************************************************************************/
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

*******************************************************************************/

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

*******************************************************************************/

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

*******************************************************************************/
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.


*******************************************************************************/

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
}

function fngetOracleDate(strInputDate)
{

	var arrDate  // This variable is used to store Date(dd,mm,yyyy) as an array element
	strInputDate = fnTrim(strInputDate); // Remove Leading and Trailing Spaces
	arrDate = strInputDate.split("/"); //Split date into Day, Month and Year

	return arrDate[0] + "-" + fngetMonthName(parseFloat(arrDate[1])) + "-" + arrDate[2]
}




function fngetMonthName(monthCode)
{
	switch(monthCode)
	{
		case 1:
			return "jan";
		case 2:
			return "feb";
		case 3:
			return "mar";
		case 4:
			return "apr";
		case 5:
			return "may";
		case 6:
			return "june";
		case 7:
			return "july";
		case 8:
			return "aug";
		case 9:
			return "sep";
		case 10:
			return "oct";
		case 11:
			return "nov";
		case 12:
			return "dec";
	}
}


























