/*###################################################
Use : Removes unnecessary spaces from beginning and end of a string
Arguments : str - String to be trimmed
####################################################*/

function stringTrim(str)
{
    if(str.length > 0)
        while(str.indexOf(' ') == 0)
            str = str.substr(1);
    if (str.length > 0)
        while(str.lastIndexOf(' ') == str.length-1)
            str = str.substr(0, str.length-1);
    return str;
}

/*################################################################################
Use : this function allow to insert only digits into the text box
Arguments : event on the text box element of the form to which this function is associated
#################################################################################*/

function onlyDigits(e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if(String.fromCharCode(whichCode) != ' ' && String.fromCharCode(whichCode) >= 0 && String.fromCharCode(whichCode) <= 9)
		return true;
	return false;
}

/*#####################################
Use : this function checks if given pattern is an integer 
Arguments : pattern to match
#####################################*/
function isInteger(pattern)
{
	var match = pattern.match(/\d{1,}/);
	if(pattern==match)
		return true;
	else
		return false;
}

/*#######################################
Use : this function does not allow to insert any special character 
Arguments : event on the text box element of the form 
#####################################*/
function noSpecialChar(e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;

	//if ((String.fromCharCode(whichCode)!=' ' && String.fromCharCode(whichCode)>='a' && String.fromCharCode(whichCode)<='z') || (String.fromCharCode(whichCode)>='A' && String.fromCharCode(whichCode)<='Z') || (String.fromCharCode(whichCode)>=0 && String.fromCharCode(whichCode)<=9) || (String.fromCharCode(whichCode)=='_') || (String.fromCharCode(whichCode)=='-'))
	if ((String.fromCharCode(whichCode)!=' ' && String.fromCharCode(whichCode)>='a' && String.fromCharCode(whichCode)<='z') || (String.fromCharCode(whichCode)>='A' && String.fromCharCode(whichCode)<='Z') || (String.fromCharCode(whichCode)>=0 && String.fromCharCode(whichCode)<=9))
	{
		return true;
	}
	return false;
}


function validateEmail(mailId)
{
    var ref=document.getElementById(mailId);
	var s=ref.value;
	if(stringTrim(s)== '')
		return true;
	a = s.match(/\S+@([-\w]+\.)+\w+/g);
	if(a != null && a.length)
		return true;
	else
		return false;
}

function getNumberOfDigits(str)
{
	var count=0;
	for(i=0; i<str.length; i++)
	{
		if(str.charAt(i)=='0' || str.charAt(i)=='1' || str.charAt(i)=='2' || str.charAt(i)=='3' || str.charAt(i)=='4' || str.charAt(i)=='5' || str.charAt(i)=='6' || str.charAt(i)=='7' || str.charAt(i)=='8' || str.charAt(i)=='9')
			count++;
	}
return count;
}

/*######################################################
Use : this function allow to insert only digits and decimal point into the text box
Arguments : event on the text box element of the form to which this function is associated
#######################################################*/
function onlyDigitsAndDot(e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if((String.fromCharCode(whichCode)!=' ' && String.fromCharCode(whichCode)>=0 && String.fromCharCode(whichCode)<=9) || (String.fromCharCode(whichCode)=='.'))
		return true;
	return false;
}


function ValidPhone(e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if((String.fromCharCode(whichCode)>=0 && String.fromCharCode(whichCode)<=9) || (String.fromCharCode(whichCode)=='.') || (String.fromCharCode(whichCode)=='-') || (String.fromCharCode(whichCode)=='(') || (String.fromCharCode(whichCode)==')') || (String.fromCharCode(whichCode)=='+'))
		return true;
	return false;
}