/**************************************
//	Modified By: Kurian V. T.
//	Modified On: March 08, 2004
//	Modified For: JavaScript validation
*************************************/



/**************************************************************
// Avaiable Functions be :
// -----------------------
// 	isRegExpSupported()
//	trim(pmStr)
//	isEmail(pmStr, pmMultiple)
//	isEmailOne(pmStr)
// 	isValidUrl(pmUrl)
//	isValidString(pmStr)
//	isValidName(pmStr)
//	isValidCharacter(pmStr)
//	isValidUSZip(pmZipCode)
//	isValidUSPhone(pmPhoneNumber)
//	allowedCharacter(vObject, vAllowedLength)
//	gNulAlNumSplKeys()
//	isAddress(string)
//	isProper(string)
//	isValidDomain(pmStr)
//	isPhoneNo(pmPhNo,pmintMinLen,pmintMaxLen)
//	isZipcode(pmStr)
//	isName(pmStr)
//	gfuncMaxLenChk(fstrValue,lintLength)
// checkEmail(pmStr)
**************************************************************/



/*******************************************************
// Function to Check whether regular expression supported
*******************************************************/
function isRegExpSupported()
{
	if (window.RegExp)	//#-- are regular expressions supported?
	{
		var vTempStr = /a/;	//#-- assign expression
		var vTempReg = new RegExp(vTempStr);
		return (vTempReg.test(vTempStr));	//#-- return status
	}
	return (false);	//#-- return status
} //#-- close of isRegExpSupported()



/************************************************
// Function to remove complete white spaces string
************************************************/
function trim(pmStr)
{
	/*****************************
	// @pmStr = String to be trimed
	*****************************/

	//#-- is regular expressions supported and return string with removed spaces
	if (isRegExpSupported()) return pmStr.replace( /^ +/, "").replace( / +$/, "");
	var vI = 0, vJ = pmStr.length - 1;	//#-- check all the string for white space	
	while (pmStr.charAt(vI) == ' ') vI++;
	while (pmStr.charAt(vJ) == ' ') vJ--;
	vJ++;
	return pmStr.substring(vI, vJ);	//#-- return string with removed spaces
} //#-- close of trim()



/**********************************************************************
// Function to Check for valid email. User can check multiple email ids
**********************************************************************/
function isEmail(pmStr, pmMultiple)
{
	/*********************************************************************************
	// @pmStr = String contain email
	// @pmMultiple = TURE or FALSE. Whether this email string contains multiple emails
	*********************************************************************************/
	vResValidate = false;
	if (pmMultiple)
	{
		aEmailIds = pmStr.split(",")
		vResValidate = true;
		for(vLoopEmail = 0; vLoopEmail < aEmailIds.length; vLoopEmail++)
		{
			if (!isEmailOne(aEmailIds[vLoopEmail]))
			{
				vResValidate = false;
				break ;
			}
		}
	}
	else vResValidate = isEmailOne(pmStr)	
	return vResValidate;
} //#-- close of isEmail()



	function isEmailOne(pmStr)
{
	/******************************
	// @pmStr = String contain email
	******************************/
	
	pmStr = trim(pmStr);	//#-- trim the string
	if (!isRegExpSupported()) return (pmStr.indexOf(".") > 2) && (pmStr.indexOf("@") > 0);	//#-- is regular expressions supported

	var vPattern = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";
	var vRegExp = new RegExp(vPattern);
	return (vRegExp.test(pmStr));
}



/************************************
// Function to check for valid website
************************************/
function isValidUrl(pmUrl)
{
	/****************************
	// @pmUrl = Url for validation
	****************************/

	pmUrl = trim(pmUrl);	//#-- trim the url
	if (isRegExpSupported())	//#-- is regular expression supported
	{
		//#-- assing expression
		var vRegExp = new RegExp("^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
		return (vRegExp.test(pmUrl));	//#-- return status
	} //#-- close of if (isRegExpSupported())
	var vStr = new string();	//#-- assing allowable character variable
	vStr = "1234567890qwertyuiopasdfghjklzxcvbnm_./-";
	pmUrl = pmUrl.toLowerCase();	//#-- make lower case the url
	for (vLoop=0; vLoop <= pmUrl.length; vLoop++)	//#-- is give url string character matches with our character
	{
		vSymbol =  pmUrl.substr(vLoop,1);	//#-- get each character of url
		if (vStr.indexOf(vSymbol,1) < 0)	//#-- is url character found in allowable character
		{
					return (false);	//#-- return status and have a break point
					break;
		} //#-- close of if (vStr.indexOf(vSymbol,1) < 0)
	} //#-- close of for (vLoop=0; vLoop <= pmUrl.length; vLoop++)
	return (true);	//#-- return status
} //#-- close of isValidUrl()



/*************************************************
// Function to Check whether given string is valid
*************************************************/
function isValidString(pmStr)
{
	/*************************************************************************************************************
	// @pmStr = String to be tested
	// Checks starting character is alphabet and the allowed string be "A to Z", "a to z", "space" and "'-:;/@?,."
	*************************************************************************************************************/

	pmStr = trim(pmStr);
	
	if (isRegExpSupported())	//#-- is regular expression supported
	{
		var vPattern = /(^[a-zA-Z]([a-zA-Z\s-':;\/\\@?()&*=+<>\.,]*)?)$/;	//#-- set the pattern
		var vRegExp = new RegExp(vPattern);				//#-- create regular expression object
		return (vRegExp.test(pmStr));					//#-- test and return status
	} //#-- close of if (isRegExpSupported())
	   
	var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '-:;/\\@?()&*=+<>.,";
	var vAlphaPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if (!pmStr.length) return false;	//#-- check for empty string
	if (pmStr.length) if (vAlphaPattern.indexOf(pmStr.charAt(0)) == -1) return false;	//#-- check for starting character
	for (var i = 0; i < pmStr.length; i++) if (vPattern.indexOf(pmStr.charAt(i)) == -1) return false;
	return true;
} //#-- close of isValidString(pmStr)



/*************************************************
// Function to Check whether given string is valid
*************************************************/
function isValidName(pmStr)
{
	/*************************************************************************************************************
	// @pmStr = String to be tested
	// Checks starting character is alphabet and the allowed string be "A to Z", "a to z", "space" and "'-:;,."
	*************************************************************************************************************/

	pmStr = trim(pmStr);

	if (isRegExpSupported())	//#-- is regular expression supported
	{
		var vPattern = /(^[a-zA-Z]([a-zA-Z0-9\s-':;\.,]+)?)$/;	//#-- set the pattern
		var vRegExp = new RegExp(vPattern);									//#-- create regular expression object
		return (vRegExp.test(pmStr));										//#-- test and return status
	} //#-- close of if (isRegExpSupported())

	var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '-:;\.,";
	var vAlphaPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if (!pmStr.length) return false;	//#-- check for empty string
	if (pmStr.length) if (vAlphaPattern.indexOf(pmStr.charAt(0)) == -1) return false;	//#-- check for starting character
	for (var i = 0; i < pmStr.length; i++) if (vPattern.indexOf(pmStr.charAt(i)) == -1) return false;

	return true;
} //#-- close of isValidName(pmStr)



/*************************************************
// Function to Check whether given string is valid
*************************************************/
function isValidCharacter(pmStr)
{
	/*********************************************************************************
	// @pmStr = String to be tested
	// Checks and allowed string be "A to Z", "a to z", "0 to 9", "space" and "'-:;/\@?()&*=+<>,."
	*********************************************************************************/

	pmStr = trim(pmStr);
	
	if (isRegExpSupported())	//#-- is regular expression supported
	{
		var vPattern = /(^[a-zA-Z0-9\s-':;\/\\@?()&*=+<>\.,]([a-zA-Z0-9\s-':;\/\\@?()&*=+<>\.,]*)?)$/;	//#-- set the pattern
		var vRegExp = new RegExp(vPattern);									//#-- create regular expression object
		return (vRegExp.test(pmStr));										//#-- test and return status
	} //#-- close of if (isRegExpSupported())
	   
	var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '-:;/\\@?()&*=+<>.,";
	if (!pmStr.length) return false;	//#-- check for empty string
	for (var i = 0; i < pmStr.length; i++) if (vPattern.indexOf(pmStr.charAt(i)) == -1) return false;
	return true;
} //#-- close of isValidCharacter(pmStr)



/****************************************************
// Function to check for valid United States Zip Code
****************************************************/
function isValidUSZip(pmZipCode)
{
	/****************************
	// pmZipCode = Zip Code that to validated
	****************************/
	
	//#--Write Regular expression to check valid US Zip Code
	var vRegExp = new RegExp("^[0-9]{5}([- /]?[0-9]{4})?$");
	
	//#-- return true if it is valid zip or false for invalid zip code
	return (vRegExp.test(pmZipCode));
}



/************************************
// Function to check for valid US Phone number format with extension 
************************************/
function isValidUSPhone(pmPhoneNumber)
{
	/****************************
	// pmPhoneNumber = Phone Number that to be validated
	****************************/
	
	//#--Write Regular expression to check valid US Phone number
	var vPattern = '^(\\(?\\d\\d\\d\\)?)?( |-|\\.)?\\d\\d\\d( |-|\\.)?\\d{4,4}(( |-|\\.)?[ext\\.]+ ?\\d+)?$';
	var vRegExp = new RegExp(vPattern);

	//#-- return true if it is valid Phone number or false for invalid Phone number
	return (vRegExp.test(pmPhoneNumber));
}



/************************************
// function to allow the character up to the given character
************************************/
function allowedCharacter(vObject, vAllowedLength)
{
	/****************************
	// vObject 			= text area control name
	// vAllowedLength 	= Length of character should be in text area
	****************************/
	
	// Check whether the character entered in the text area is greater then the given Allowed length
	if (vObject.value.length > vAllowedLength)
	{
		// Cut the remaining character which exceeds the Allowed length
		vObject.value = vObject.value.substring(0,(vAllowedLength));
		
		// return value
		return false;
	}
	
	// return value
	return true;
}



function gNulAlNumSplKeys()
{
	/*Function that allows only Alpahabets,Numbers and some
  	"'" . This is to be called 
	in the "KeyPress" event of a Text control."this" 
	reference is to be passed.	*/
	
	// Variable to store the value of "KeyCode".
	pNumKeyCode = window.event.keyCode;
	// Check if the "KeyCode" is from "A" to "Z".
	if(pNumKeyCode > 64 && pNumKeyCode < 91)
	{
		window.status ="Done";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123)
	{
		window.status = "Done";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "0" to "9".
	else if(pNumKeyCode > 47 && pNumKeyCode < 58)
	{
		window.status ="Done";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is anyother character, mentioned above.
	else 
	{
		switch(pNumKeyCode)
		{	
			case 48:
			case 57:
			//case 46:			
			//case 95:
			//case 45:
			//case 47:
			//case 92:
			//case 39: // commented by ramki as single quotes causes problem with DB.
				window.status = "";
				window.event.keyCode = pNumKeyCode ;
				break;
			default:
				window.status = "Invalid Character."
				window.event.keyCode = 0; 
		}
	}
}



//#--	these function checking the Calid Addess or Not
function isAddress(string)
{
   string=trim(string);
   if (!string) return false;
   var ichars = "<>{}";

   for (var i = 0; i < string.length; i++) {
	  if (ichars.indexOf(string.charAt(i)) != -1)
		 return false;
   }
   return true;
}



//#-- these function Checking the Valid String or not
function isProper(string)
{
   string=trim(string);
   if (!string) return false;
   var ichars = "<>{}";

   for (var i = 0; i < string.length; i++) {
	  if (ichars.indexOf(string.charAt(i)) != -1)
		 return false;
   }
   return true;
}



function isValidDomain(pmStr)
{
	/******************************
	// @pmStr = String contain email
	******************************/
	//#-- trim the string
		pmStr = trim(pmStr);
		
	  var reg1 = /(\.\.)|(^\.)|(^\.^\.^)/; // not valid
	  var reg2 = /^([a-zA-Z]{2,3}|[0-9]{1,3}).+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	  if (!reg1.test(pmStr) && reg2.test(pmStr))  // if syntax is valid			
		return true
	  else
		return false

	//#-- return staus
}



function isPhoneNo(pmPhNo,pmintMinLen,pmintMaxLen)
{   var i;
	var returnString = "";
	var phoneNumberDelimiters = "0123456789()-, ";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < pmPhNo.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = pmPhNo.charAt(i);
		if (phoneNumberDelimiters.indexOf(c) == -1) 
			return false;
	}
	if(pmPhNo.length>pmintMaxLen || pmPhNo.length<pmintMinLen)
		return false;
	else	
		return true;
}

function isValidNumber(pmNumber, pmMaxDecimal, pmAllowNegative, pmZeroInvalid)
{
	/*****************************
		This function checks for valid Number.
		Arguments:	
			pmNumber - This is a Number value that to be validated
			pmMaxDecimal - Default is 2 but user can assign maximum size of decimal
			pmAllowNegative - Default is FALSE if user can change to TRUE
			pmZeroInvalid - Default is TRUE, whether ZERO is consider as invalid
		Return:
			function returns TRUE if valid string otherwise returns FALSE
	*****************************/
	var vPattern, vRegExp, vRegExpValue
	var vDecFrom, vDecTo, vDecAllow
	var vNegPattern
	
	//#--Checking for decimals
	if (pmMaxDecimal == 0 || pmMaxDecimal == "")
	{	//#--Valud should not accept decimal points, If MaxDecimal is 0
		vDecFrom = 0	//#--Assinging 0 for Min decimal points
		vDecTo = 0		//#--Assinging 0 for Max decimal points
		vDecAllow = false	//#--Assign FALSE since decimal is not accepted
	}
	else
	{	//#--
		vDecFrom = 1	//#--Assign 1 for Min decimal points 
		vDecTo = (pmMaxDecimal <= 2?2:pmMaxDecimal)	//#--Default is 2 or assign max decimal points 
		vDecAllow = true	//#--Assign TRUE since decimal is accepted
	}
	//#--End of Checking decimals
	
	vRegExpValue = false;	//#--By default Regular Expression value is FALSE

	//#--Pattern build for Negative value, If pmAllowNegative is TRUE otherwise pattern is empty 
	vNegPattern = (pmAllowNegative?"-?":"")	//#--Pattern to check negative values
	//#--
	
	pmNumber = trim(pmNumber);		//#--Trim the Number value

	if (pmZeroInvalid == true)
	{	//#--This pattern is built If, Zero is consider as invalid 
		if (vDecAllow)	//#-- If decimal is allowed
			vPattern = "^(" + vNegPattern + "[1-9]{1}[0-9]{0,}(\\.[0-9]{" + vDecFrom + "," + vDecTo + "})?|(" + vNegPattern + "0\\.([0-9]{" + vDecFrom + "}[1-9]{" + (vDecTo - 1) + "}))|(" + vNegPattern + "0\\.([1-9]{" + vDecFrom + "}[0-9]{" + (vDecTo - 1) + "})))$";
		else	//#-- If decimal is not allowed
			vPattern = "^(" + vNegPattern + "[1-9]{1}[0-9]{0,})$";
	}
	else
	{	//#--This pattern is built, If Zero is consider as valid 
		if (vDecAllow)	//#-- If decimal is allowed
			vPattern = "^(([0]{1})|(" + vNegPattern + "[1-9]{1}[0-9]{0,}(\\.[0-9]{" + vDecFrom + "," + vDecTo + "})?|0\\.([0-9]{" + vDecFrom + "," + vDecTo + "})?|(" + vNegPattern + "0\\.([0-9]{" + vDecFrom + "}[1-9]{" + (vDecTo - 1 )+ "}))|(" + vNegPattern + "0\\.([1-9]{" + vDecFrom + "}[0-9]{" + (vDecTo - 1)+ "}))))$";		//#-- set the pattern
		else	//#-- If decimal is not allowed
			vPattern = "^(([0]{1})|(" + vNegPattern + "[1-9]{1}[0-9]{0,}))$";
	}

	vRegExp = new RegExp(vPattern);		//#-- create regular expression object

	vRegExpValue = vRegExp.test(pmNumber);	//#-- test the number for given pattern
	   
	return vRegExpValue;	//#--Return True for valid number, False for invalid.
	
	//#--End of Validation checking
}



function isZipcode(pmStr)
{
	string=trim(pmStr);
	if (!string) return false;
		//var ichars = "*|,\":<>[]{}`\';()@&$#%.";
	   var ichars = "-*|,\":<>[]{}`\';()@&$#%._?+=/";
	for (var i = 0; i < string.length; i++) 
	{
		if (ichars.indexOf(string.charAt(i)) != -1)
		return false;
	}
	return true;
}



function isName(pmStr)
{
	string=trim(pmStr);
	if (!string) return false;
		 var ichars = "`!*|<>[]{}$#%+=^";
		 
	for (var i = 0; i < string.length; i++) 
	{
		if (ichars.indexOf(string.charAt(i)) != -1)
		{
			return false;
		}

	}
	return true;
}



function  gfuncMaxLenChk(fstrValue,lintLength)
{
	var lstrValue = fstrValue.value.length;
	window.status='Text Length-->' + lstrValue+ ' / ' + lintLength;
	if (lstrValue>=lintLength)
	{
		window.status = "Text cannot exceed " + lintLength + " characters";
		window.event.keyCode=0;
		return false;			
	}
	return true;
}

//# function not to allow these charaters in email : ( ) < > [ ] , ; : \ / "
function checkEmail(pmStr)
{
    var illegalChars = /[\(\)\<\>\;\^\=\!\~\|\&\-\{\}\'\`\%\$\:\\\/\"\[\]\#\*\+]/;
//	str=pmStr.substring(0,1)
   	if (illegalChars.test(pmStr) || pmStr.indexOf("_")==0){
      return false
    }
	else
		return true
}

//###### Functions added by Suresh on 1 st May 2005 ################################

// Function to Check whether the entered value is numeric 

function IsNumeric(strString)
   //  check for valid numeric strings	
   {

   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {

      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
	  if(blnResult==false)
	  {
		 alert("Enter only numbers ");
	  }
   return blnResult;
   }
   
   
function fnCheckillegalChars(strString)
{
 var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\/]/
     if (strString.match(illegalChars)) 
	 {
		// alert("the string contains illegal characters ");
		 return false;
	 }
	 else
	 {
		 return true;
	 }
	 
}

function isStringValid(pmStr)
{
	/*************************************************************************************************************
	// @pmStr = String to be tested
	// Checks starting character is alphabet and the allowed string be "A to Z", "a to z", "space" and "'-:;/,."
	*************************************************************************************************************/

	pmStr = trim(pmStr);
	
	if (isRegExpSupported())	//#-- is regular expression supported
	{
		var vPattern = /(^[a-zA-Z]([a-zA-Z0-9\s-':;\/\()&\.,]*)?)$/;	//#-- set the pattern
		var vRegExp = new RegExp(vPattern);				//#-- create regular expression object
		return (vRegExp.test(pmStr));					//#-- test and return status
	} //#-- close of if (isRegExpSupported())
	   
	var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '-:;/?().,0-9";
	var vAlphaPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	if (!pmStr.length) return false;	//#-- check for empty string
	if (pmStr.length) if (vAlphaPattern.indexOf(pmStr.charAt(0)) == -1) return false;	//#-- check for starting character
	for (var i = 0; i < pmStr.length; i++) if (vPattern.indexOf(pmStr.charAt(i)) == -1) return false;
	return true;
} //#-- close of isValidString(pmStr)


function checkName(pmStr) {

    var illegalChars =/[\<\>\;\`\@\%\^\&\_\~\,\#\+\{\}\=\|\?\*\:\!\\\/\"\[\]]/; 
   	if (illegalChars.test(pmStr)){
      return false
    }
	else
		return true
}
