/**************************************
//	Modified By: Kurian V. T.
//	Modified On: March 08, 2004
//	Modified For: JavaScript validation
*************************************/

/**************************************************************
// Avaiable Functions be :
// -----------------------
// 	isRegExpSupported()
//	trim(pmStr)
//	isEmail(pmStr)
// 	isValidUrl(pmUrl)
//  checkPassword(pmStr)
//  checkStr(pmStr)
//  checkEmail(pmStr)
//  checkName(pmStr)

**************************************************************/

/*******************************************************
// Function to Check whether regular expression supported
*******************************************************/
	function isRegExpSupported()
	{
		//#-- are regular expressions supported?
			if (window.RegExp)
			{
				//#-- assign expression
					var vTempStr = "a";
					var vTempReg = new RegExp(vTempStr);
				
				//#-- return status
					return (vTempReg.test(vTempStr));
			}
		
		//#-- return status
			return (false);
	} //#-- 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( / +$/, "");
		
		//#-- check all the string for white space	
			var vI = 0, vJ = pmStr.length - 1;
			while (pmStr.charAt(vI) == ' ') vI++;
			while (pmStr.charAt(vJ) == ' ') vJ--;
			vJ++;
		
		//#-- return string with removed spaces
			return pmStr.substring(vI, vJ);
	} //#-- close of trim()

	
//#--	Starting the isEmail() Checking Function
function isEmail(pmEmail)
{
	/*****************************
		This function checks valid strings for given type.
		Arguments:	
			pmEmail - This is a Email ID that to be validated
		Return:
			function returns TRUE if valid Email otherwise returns FALSE
	*****************************/
	//#--	Remove the White Space
	pmEmail=trim(pmEmail);
	//#--	Assign the Regular Expression for  Email Checking
	var vPattern = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-\\_]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$";
	//#--	Creating teh Object for Regular Expression
	var vRegExp = new RegExp(vPattern);
	
	//#--	Checking the given Email is Corect or Not
	return (vRegExp.test(pmEmail));
}
//#--	End of the isEmail() Function
	
	
	function isEmailOne(pmStr)
	{
		/******************************
		// @pmStr = String contain email
		******************************/
		//#-- trim the string
			pmStr = trim(pmStr);
		
		//#-- is regular expressions supported
			if (!isRegExpSupported()) 
				return (pmStr.indexOf(".") > 2) && (pmStr.indexOf("@") > 0);
	
		//#-- if regular expresson supported
			var vR1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
			var vR2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		
		//#-- return staus
			return (!vR1.test(pmStr) && vR2.test(pmStr));
	}



	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 to check for valid website
************************************/
	function isValidUrl(pmUrl)
	{
		/****************************
		// @pmUrl = Url for validation
		****************************/

		//#-- trim the url
			pmUrl = trim(pmUrl);
	
		//#-- is regular expression supported
			if (isRegExpSupported())
			{
				//#-- 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 status
					return (vRegExp.test(pmUrl));
			}
		
		//#-- assign allowable character variable
			var vStr = new string();
			vStr = "1234567890qwertyuiopasdfghjklzxcvbnm_./-";
		
		//#-- make lower case the url
			pmUrl = pmUrl.toLowerCase();
	
		
		//#-- is give url string character matches with our character
			for (vLoop=0; vLoop <= pmUrl.length; vLoop++)
			{
				//#-- get each character of url
					vSymbol =  pmUrl.substr(vLoop,1);
				
				//#-- is url character found in allowable character
					if (vStr.indexOf(vSymbol,1) < 0)
					{
						//#-- return status and have a break point
							return (false);
							break;
					}
			}
		
		//#-- return status
			return (true);
	} //#-- close of isValidUrl()

/************************************
// 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 to allow only letters and numbers in password and in phone or zipcode

function checkStr(pmStr)
{

    var illegalChars = /[\W_]/; 
   	if (illegalChars.test(pmStr)) {
      return false
    }
	else
		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
}

function checkName(pmStr) {

    var illegalChars =/[\(\)\<\>\;\@\%\^\&\_\~\#\+\{\}\=\|\?\*\'\$\:\!\\\/\"\[\]]/; 
//	str=pmStr.substring(0,1)
   	if (illegalChars.test(pmStr)){//|| pmStr.indexOf("_")==0)  {
      return false
    }
	else
		return true
}
 
function checkPhone(pmStr) {

    var illegalChars =/[\(\)\<\>\;\@\%\^\&\_\~\#\+\=\|\*\.\'\$\:\!\\\/\"\[\]]/; 
//	str=pmStr.substring(0,1)
   	if (illegalChars.test(pmStr)|| pmStr.indexOf("-")==0)  {
      return false
    }
	else
		return true
}

function checkSubject(pmStr) {

    var illegalChars =/[\(\)\<\>\;\@\%\^\_\~\#\+\=\|\*\'\$\\\/\"]/; 
//	str=pmStr.substring(0,1)
   	if (illegalChars.test(pmStr)|| pmStr.indexOf("-")==0)  {
      return false
    }
	else
		return true
}



function checkAlbum(pmStr) {

    var illegalChars =/[\(\)\<\>\;\@\%\^\&\~\#\+\=\|\{\}\*\'\$\!\\\/\"\[\]]/; 
   	if (illegalChars.test(pmStr) || pmStr.indexOf("_")==0)  {
      return false
    }
	else
		return true
}


function findEscape(pmStr)
{
	var illegalChars = /[\\]/; 
	if ( illegalChars.test(pmStr))
	{
		return false
	}
	else
		return true;
}




function gAlphaKeys()
{
	// 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 ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}
	else
	{	
		window.status = "Invalid Character. Only Characters Are Allowed.";
		window.event.keyCode = 0;
	}

}

function gNulNumKeys()
{
	/*Function that allows only Numbers. This is to be 
	 called in the "KeyPress" event of a Text control. 
	 "this" reference is to be passed.*/
	// Variable to store the "KeyCode".
	var pNumKeyCode = window.event.keyCode;
	// Check what is the value of "KeyCode". If its valid
	// allow the value to be displayed, otherwise prompt.
 
	if(pNumKeyCode > 47 && pNumKeyCode < 58	|| pNumKeyCode==13) 
	{
		window.status = "Done";
		window.event.keyCode = pNumKeyCode;	
	}
	else
	{	
		window.status = "Invalid Character. Only Numbers Are Allowed.";
		window.event.keyCode = 0;
	}
} 

function gNulAlNumSplKeys()
{
	/*Function that allows only Alpahabets,Numbers and some
	special charactes like   "_", "-", 
	"'" . 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 ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "0" to "9".
	else if(pNumKeyCode > 47 && pNumKeyCode < 58)  
	{
		window.status ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is anyother character, mentioned above.
	else 
	{
		switch(pNumKeyCode)
		{	
			case 48:
			case 57:
			case 95:
			case 45:
			case 13:
			case 32:	//Space charactre alllow
			
 		//	case 40:
		//	case 41:
			// commented by ramki as single quotes causes problem with DB.
			window.status = "Done";
			window.event.keyCode = pNumKeyCode ;
			break;
			default:
				window.status = "Invalid Character "
				window.event.keyCode = 0; 
		}
	}
}

// Function is used for textarea
function gNulAddressKeys()
{
	/*Function that allows only Alpahabets,Numbers and some
	special charactes like   "_", "-", 
	"'" . 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 ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "0" to "9".
	else if(pNumKeyCode > 47 && pNumKeyCode < 58)  
	{
		window.status ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is anyother character, mentioned above.
	else 
	{
		switch(pNumKeyCode)
		{	
				//space -32, comma-44 , exclamation-33 colon-58  underscore-95 minus-45 dot- 46  /-47
			case 13:
			case 32:
			case 33:
			case 39:
			case 44:
			case 45:
			case 46:
			case 47:
			case 58:
			case 95:
			  // commented by ramki as single quotes causes problem with DB.
				window.status = "Done";
				window.event.keyCode = pNumKeyCode ;
				break;
			default:
				window.status = "Invalid Character "
				window.event.keyCode = 0; 
		}
	}
}



function gNulAlNumEmailKeys()
{
	/* For Email - Function that allows only Alpahabets,Numbers and some
	special charactes like   "@", ".", 
	"'" . 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 ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "0" to "9".
	else if(pNumKeyCode > 47 && pNumKeyCode < 58)  
	{
		window.status ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is anyother character, mentioned above.
	else 
	{
		switch(pNumKeyCode)
		{	
			case 95:
			case 64:
			case 46:
			case 13:
			case 44:
		  // commented by ramki as single quotes causes problem with DB.
				window.status = "Done";
				window.event.keyCode = pNumKeyCode ;
				break;
			default:
				window.status = "Invalid Character "
				window.event.keyCode = 0; 
		}
	}
}



function gNulAlNumKeys()
{
	/*Function that allows only Alpahabets,Numbers 
	. 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 || pNumKeyCode==13 || pNumKeyCode==45)
	{
		window.status ="";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "a" to "z".
	else if(pNumKeyCode > 96 && pNumKeyCode < 123 || pNumKeyCode==13 || pNumKeyCode==45)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}
	// Check if the "KeyCode" is from "0" to "9".
	else if(pNumKeyCode > 47 && pNumKeyCode < 58 || pNumKeyCode==13 || pNumKeyCode==45)  
	{
		window.status ="";
		window.event.keyCode = pNumKeyCode; 
	}
	else
	{	
		window.status = "Invalid Character. Only Characters and Numbers Are Allowed.";
		window.event.keyCode = 0;
	}
}


/* ***   FUNCTION THAT ALLOWS ONLY NUMBERS (CURRENCY) INSIDE A TEXTBOX   *** */
function gNulCurKeys(pCtlTextBox)
{
	/*Function that allows only Numbers one Period and 
	two decimal places only. This is to be called in 
	the "KeyPress" event of a Text control. "this" 
	reference is to be passed.*/
	// Variable to store the "KeyCode".
	var pNumKeyCode = window.event.keyCode;
	// Variable to store the control's value.
	var pStrValue = pCtlTextBox.value;
	// Variable to store the length of the content.
	// -1 since 0 based.
	var pNumLength = pStrValue.length - 1; 
	// Variable to store the place where "." occurs.
	var pNumDeciPosi = pStrValue.indexOf(".");
	// To find if "." is the first character typed.	
	if (pNumLength < 0 && pNumKeyCode == 46)
	{
		window.status = "Period Not allowed In First Position.";
		window.event.keyCode = 0;			
	}
	/* To find if user is trying to type more than two 
	 numbers after the decimal point.
	else if (pNumDeciPosi > 0 && pNumDeciPosi+2 == pNumLength)
	{	
		window.status = "After Period Only Two Numbers are Allowed.";
		window.event.keyCode = 0;
	}
*/
	// To find if user is typing valid Numbers.
	else if(pNumKeyCode > 47 && pNumKeyCode < 58 || pNumKeyCode==13)
	{
		window.status = "";
		window.event.keyCode = pNumKeyCode;
	}

	// To find if user is typing "." for the first time.
	else if(pNumKeyCode == 46 && pNumDeciPosi < 0)
	{	
		window.status = "";
		window.event.keyCode = pNumKeyCode; 
	}

	// If "KeyCode" is any other thing its Invalid.
	else
	{	
		if(pNumKeyCode==46)
		{
			window.status = "Invalid Character. Dot already Exists";
		}
		else
		{
			window.status = "Invalid Character. Only Numbers Are Allowed.";
		}
		window.event.keyCode = 0;
	}
} // END FUNCTION gNulCurKeys


function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}




/*				if (pmProdObject.value != "" && (isNaN(pmProdObject.value)) || (!isNaN(pmProdObject.value) && pmProdObject.value < 0)|| 
					pmProdObject.value.indexOf(".")!= -1 ||	pmProdObject.value.indexOf("+",0) != -1)
				{
					alert (cMsgAddProdText);
					pmProdObject.value = "";
					pmProdObject.focus();
					return false;
				} */

 
