/*
	JavaScript Scriplets for common form field validations

	Started On	: 03.apr.2003
	Author		: Guru
*/



function checkInCharSet(txt, charset)
{
	/*
		This function checks if the characters in a given text are part of a given character set.

		INPUT:	Text ti be verified [txt]
					String of character that forms the reference [charset]

		OUTPUT: Returns true if all of the characters in txt are part of charset, else false.

		USAGE:
					for example:

						checkInCharSet( "guru", "aeiouAEIOU" ) this fucntion returns false as "guru" contains 'g' and 'r'
						whcih are not part of "aeiouAEIOU".

						checkInCharSet( "abC", "abcdefABCDEF" ) this statement returns true as all "abC" contains characters
						that are present in "abcdefABCDEF"
	*/

	var b = true;

	for(i = 0; i < txt.length; i++ )
	{
		if(charset.indexOf(txt.charAt(i)) == -1 )
		{
			b = false;
		}
	}

	return b;
}



/**
 FUNCTION FOR CHECKING THE FIELD CONTAINS BLANK VALUES ISBLANK(Element.value)
 **/
//To check if trim(value) is blank
function isBlank(txt, minlen)
{
	/*
		This fucntion can be used to check if a given text contains only spaces or 0 in length.

		INPUT: Text [txt]
					Minimum Length [minlen] optional
					Indicates that the text should be atleast 'minlen' in length

		OUTPUT: returns true if blank else false
	*/

	if( txt.length == getCountOf('\n', txt) )
	{
		/*
			This condition avoids the entry of just newlines in text areas.
		*/
		return true;
	}

	if( txt.length == getCountOf(' ', txt) || txt.length == 0 )
	{
		return true;
	}
	else if( minlen > 0 )
	{
		if( txt.length < minlen )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

//This can be used for any character validation.
//For example in a valid date the count of - or / should not be more than 2
//Likewise in a valid numer there should be only one .
function getCountOf(vChr, txt)
{
	var i = 0;
	var iCount = 0;

	for( i=0; i < txt.length; i++ )
	{
		if( txt.charAt(i) == vChr )
		{
			iCount++;
		}
	}
	return iCount;
}

/**
 FUNCTION NUMVALIDATION(element,message,spl,onlynum) 
 **/
function NumValidation(Element,MessageLen0,spl,OnlyNum) {
	
	if(MessageLen0.length != 0) {
		if(isBlank(Element.value) || Element.value.length == 0)  {
			alert("Please enter the "+ MessageLen0);
			Element.focus();
			return 0;
		}
	}
	
	if(OnlyNum == "num") {
		if(isNaN(Element.value))  {
			alert("Please enter only Numeric Data");
			Element.focus();
			return 0;
		}
		if(parseInt(Element.value) < 0)  {
			alert("Negative values are not allowed for this field.");
			Element.focus();
			return 0;
		}
		
	}
			
	if(spl == "spl" && OnlyNum != "num") {
		if(SplNumbers(Element) == 0)
		return 0;
	}	


} // closing the function NumValidation()

