/****************************************************************
Available functions in this file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	isStartSpace(pmString)
	trim(pmString) 
	isValidPersonName(pmString)
	isMinCharacters(pmString,pmMinLength)
	isValidCity(pmString)
	isValidZibcode(pmString)
	isValidPhoneNo(pmPhoNo)
	isValidPassword(pmString)
	isEmail(pmString)
****************************************************************/
	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 isStartSpace(pmString)
	{
		/*******************************
			@pmString - A string value that to be checked
				This function is used to check if the string started with space
		*******************************/
		if(pmString.charAt(0) == " ") //#-- test and return srtatus
			return (false);
		return (true);	
	}
		
	function trim(pmString)
	{
		/*****************************
			This function trims starting and ending spaces in passed string.
			Arguments:	
				pmStr - A string value that to be trimmed
			Return:
				function returns a trimmed string value 
		*****************************/
		var vPattern, vRegExpFind, vRegExpReplace;
		var vReplacedString;
	
		//#--Assign Regular Expression string value for starting and Ending space replace.
		vRegExpFindStart = /^ +/;
		vRegExpFindEnd = / +$/;
		
		//#--Replace the space value with empty string  
		vReplacedString = pmString.replace(vRegExpFindStart, "").replace(vRegExpFindEnd, "");
		
		//#--Return trimmed value
		return vReplacedString;
	
	} //#-- close of trim()

	function isValidPersonName(pmString)
	{
		/*************************************************
			@pmString = to be tested
				checks allowed string characters "A" to "Z", "a" to "z", "spaces" and "'"
		************************************************/
		//#-- trim starting and ending white spaces 
		var vPersonName = trim(pmString);
		
		//#-- check if the string is empty
		if(vPersonName == "")
			return false;
		//#-- check if the browser support regular expersion	
		if(isRegExpSupported()) //#-- is regular experssion supported
		{	
			//#-- build the allowed character pattern
			var vPattern = "(^([a-zA-Z ']+)?)$";	//#-- set the pattern
			var vRegExp = new RegExp(vPattern);		//#-- create a new regular experson object
			return (vRegExp.test(vPersonName));		//#-- test and return status
		} //#-- end of regular experssion support
		
		//#-- build the character patern
		var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '";
		
		for(var vI = 0; vI < vPattern.length ; vI ++)
			if(vPattern.indexOf(vPersonName.charAt(vI)) == -1) //#-- test and return status
				return (false);
		return (true);
				
	}//#-- End of the isValidPersonName() function

	function isMinCharacters(pmString,pmMinLength)
	{
		/***********************************************
			@pmString = to be tested
			@pmMinLength = Minimum characters of the string
				check the string length not less than the minimum length
		***********************************************/
		var vMinChars = trim(pmString).length; //#-- get the charcter length
		
		return(vMinChars < pmMinLength)?true:false; //#-- test and return status
		
	} //#-- end of the isMinCharacters() function
	
	function isValidCity(pmString)
	{
		/*************************************************
			@pmString = to be tested
				checks allowed string characters "A" to "Z", "a" to "z" and  "spaces"
		************************************************/
		//#-- trim starting and ending white spaces 
		var vCity = trim(pmString);
		
		//#-- check if the string is empty
		if(vCity == "")
			return (false);
		//#-- check if the browser support regular expersion	
		if(isRegExpSupported()) //#-- is regular experssion supported
		{	
			//#-- build the allowed character pattern
			var vPattern = "(^([a-zA-Z ]+)?)$";	//#-- set the pattern
			var vRegExp = new RegExp(vPattern);		//#-- create a new regular experson object
			return (vRegExp.test(vCity));		//#-- test and return status
		} //#-- end of regular experssion support
		
		//#-- build the character patern
		var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		
		for(var vI = 0; vI < vPattern.length ; vI ++)
			if(vPattern.indexOf(vCity.charAt(vI)) == -1) //#-- test and return status
				return (false);
		return (true);
	}
	
	function isValidZipcode(pmString)
	{
		/***************************************************
			@pmString = to be tested
				checks allowed characters "A" to "Z", "a" to "z" and "-"				
		***************************************************/
		
		var vZipcode = trim(pmString);//#--  trim the string
		var vNumFound = 0; 		//#-- initialize the flag value
		
		if(vZipcode == "") 		//#-- check empty and return value
			return (false);
		
		if(vZipcode.length < 6) //#-- check length and return value
			return (false);
		if(isRegExpSupported()) //#-- is regular experssion supported
		{
				
			//#-- loop check interger found in  the string
			for(var i=0; i < vZipcode.length; i++)
			{
				var vPattern = "(^([0-9]+)?)$";
				var vRegExp = new RegExp(vPattern);
				if(vRegExp.test(vZipcode.charAt(i)))
				{
					var vNumFound = 1; //#-- set flag value 
					break;
				}
			}
			if(vNumFound == 1)//#-- check the flag value
			{
				var vPattern = "(^([a-zA-Z0-9-]+)?)$"; //#-- set the pattern
				var vRegExp = new RegExp(vPattern); 	//#-- create a new regular exp
				return (vRegExp.test(vZipcode))			//#-- test and return value
			}
			else
				return (false); //#-- interger not found return false
		}
		//#-- regular experssion not supported
		vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxys0123456789-"; //#-- set the pattern
		for(var vI = 0; vI < vPattern.length; vI++)
		{
			if(parseInt(vZipcode.indexOf(vI)) != -1) //#-- integer found 
				var vNumFound = 1; //#-- set flag value 
			if(vPattern.indexOf(vZipcode.charAt(vI)) == -1 )//#-- parse the string and return value
				return false
		}
		return(vNumFound)?true:false; //#-- check the flag status and return value
				
	} //#-- end of isValidZibcode() function
	
	function isValidPhoneNo(pmPhoNo)
	{
		/***************************************************
		@pmPhoNo = to be tested
			checks allowed characters 0 to 9 ,"space","-", amd "()"
		***************************************************/
		var vPhoneNo = trim(pmPhoNo); //#-- trim spaces 
	
		//#-- initialize variables
		var vPlusFound = 0;
		var vOpenBraceFound = 0;
		var vCloseBraceFound = 0;
		
		if(vPhoneNo == "" || vPhoneNo.charAt(0) == "-") //#-- checks empty and first character not a hyphen
			return false;
	
		for(var vI = 0; vI < vPhoneNo.length; vI++)
		{
			
			if(vPhoneNo.charAt(vI) == "+" && vI != 0) //#-- set flag for plus sign not in starting position.
				vPlusFound = 1;
			
			if(vPhoneNo.charAt(vI) == "(")
				vOpenBraceFound = vOpenBraceFound + 1; //#-- set flag for more than 1 open brace occur
			
			if(vPhoneNo.charAt(vI) == ")")
				vCloseBraceFound = vCloseBraceFound + 1; //#-- set flag for more then one close brace occur
		}
		
		if(vPlusFound == 1 || vOpenBraceFound > 1 || vCloseBraceFound > 1) //#-- test the flag and return value
			return (false);
		
		if(isRegExpSupported()) //#-- check regula expression supprot
		{	
			var vPattern = "(^([0-9 -()+]+)?)$"; //#-- set the pattern
			var vRegExp = new RegExp(vPattern);  //#-- create a new regular expression
			return(vRegExp.test(vPhoneNo));		//#-- test and return value
		}
		var vPattern = "0123456789+ -()"; //#-- set pattern for regular exp not support
		for(var vJ=0; vJ < vPattern.length; vJ++)
		{
			if(vPattern.indexOf(vPhoneNo.charAt(vJ)) == -1) //#-- parse the number and return value
				return (false);
		}
		return (true);
	} //#-- end of  isValidPhoneNo() function

	function isValidPassword(pmString)
	{
		/************************************************
			@pmString = to be tested
				checks allowed characters "a" to "z", "A" to "Z" and 0 to 9
		************************************************/
		var vPassword = trim(pmString); //#-- trim spaces
	
		if(vPassword == "")		//#-- check empty and return vlaue
			return (false);
			
		//#-- check if regular experssion supported
		if(isRegExpSupported())
		{
			var vPattern = "(^([a-zA-Z0-9]+)?)$"; //#-- set the pattern
			var vRegExp = new RegExp(vPattern); //#-- create a new regular experssion
			return (vRegExp.test(vPassword)); //#-- test and return value
		}
		
		//#-- regular exp not supported
		var vPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; //#-- set the pattern
		for(var vI=0; vI < vPattern.length; vI++)
		{
			if(vPattern.indexOf(vPassword,charAt(vI)) == -1) //#-- parse the string and return value
				return (false);
		} 
		return (true);
		
	} //#-- end of isValidPassword() function
	
	
	function isEmail(pmString)
	{
		/******************************
		// @pmString = String contain email
		******************************/
		pmString  = trim(pmString);	//#-- trim the string
		pmString  = pmString.replace(/\r\n|\r|\n/g, ''); 
		pmString  = pmString.replace(/\r\n/g,'');
		pmString  = pmString.replace(/\r/g,'');
		pmString  = pmString.replace(/\n/g,''); 
		
		//#-- check empty and return value
		if (pmString == "") return false;
		
		if (pmString.length > 65) return false;
		//#-- if the email does not have @ and . then return false
		if(pmString.indexOf('@') == -1 || pmString.indexOf('.') == -1 || pmString.indexOf(' ') != -1)
			return false;
		if (!isRegExpSupported()) return (pmString.indexOf(".") > 2) && (pmString.indexOf("@") > 0);	//#-- is regular expressions supported
		
		
		vSubStr = pmString.split('@'); //#-- split the email 
		vDomain = vSubStr[1].split('.'); //#-- split the domain extension 
		
		//#-- check the length of the array and return value
		if(vDomain.length > 4)
			return false;
		
		//#-- check if repetation occur
		for(vI = 0; vI < vDomain.length ; vI++)
		{
			for(vJ = 0; vJ < vDomain.length ; vJ++)
			{
				if(vDomain[vI] == vDomain[vJ] && vI != vJ)
				{
					return (false) //#-- test and return value
				}
			}
		}
		var vPattern = "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9_]+)*)@([A-Za-z0-9]+)(([\\_.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$"; //#-- set pattern
		var vRegExp = new RegExp(vPattern); //#-- create a new regular exp
		return (vRegExp.test(pmString)); //#-- test and return value
	}
	//#--- function use to product space for entery in text fields
	function funNotAllowSpace(e)
	{
		var pNumKeyCode = (window.event) ? event.keyCode : e.keyCode;
		if (pNumKeyCode==32)
		{ //if the key isn't the backspace key (which we should allow)
			window.event.keyCode=0;
			return false //disable key press
		}
	}



//#--- function use to set decimal value from the given number
	function format(pmNumber, pmDecimals)
	{
		var i,d;
		if (pmNumber == "") pmNumber = parseInt("0");
		if (pmDecimals == "") pmDecimals = parseInt("2");
		pmNumber = "" + Math.round(pmNumber * Math.pow(10,pmDecimals)) * Math.pow(10,-pmDecimals);

		d = pmNumber.indexOf(".");
		
		if (d == -1)
		{
			pmNumber = pmNumber + ".";
			for(i = 0; i < pmDecimals; i++)
				pmNumber = pmNumber + "0";
			return pmNumber;
		} 

		if (d == 0)
		{
			pmNumber = "0" + pmNumber;
			d++;
		} 

		if(d == 1 && pmNumber.substring(0,1) == "-")
		{
			pmNumber = "-0" + pmNumber.substring(1,pmNumber.length);
			d++;
		} 

		pmNumber = pmNumber.substring(0,d+pmDecimals+1);

		while (pmNumber.length <= d+pmDecimals)
			pmNumber = pmNumber + "0";
		return pmNumber;
	}


