function trim(input)
{
    var retValue = input.value; 
    var retName = input.name;   
    var alphaCharsUserid = "0123456789-~`$%#@!*^[]{}\|/?><'\"()&_+=.,";
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof input.value != "string")
    {
        alert("The " + retName + " should not be numeric");
        input.value = "";
        input.focus();
        return false;
    }
    else
    {
        var ch = retValue.substring(0, 1);
        while (ch == " ")
        { // Check for spaces at the beginning of the string
            retValue = retValue.substring(1, retValue.length);
            ch = retValue.substring(0, 1);
        }
    
        ch = retValue.substring(retValue.length-1, retValue.length);
        while (ch == " ")
        { // Check for spaces at the end of the string
            retValue = retValue.substring(0, retValue.length-1);
            ch = retValue.substring(retValue.length-1, retValue.length);
        }
    
        while (retValue.indexOf("  ") != -1)
        { // Note that there are two spaces in the string - look for multiple spaces within the string
            retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
        }
        if(retValue.length <= 0)
        {
            alert("The " + retName + " should not be empty");
            input.value = "";
            input.focus();
            return false;
        }
        
        else if(retValue.length < 3)
        {
            alert("The " + retName + " should be at least 3 characters long");
            input.focus();
            return false;
        }        
        else if (alphaCharsUserid.indexOf(retValue.charAt(0)) > 0)
        {            
            alert("The " + retName + " should not start with " + retValue.charAt(0));
            input.value = "";
            input.focus();
            return false;
        }           
    }
    input.value = retValue;
    return true; // Return the trimmed string back to the user
} // Ends the "trim" function

function trimValue(input, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = "Value";
    }

    var retValue = input.value; 
    if (typeof input.value != "string")
    {
        alert(errmsg + " should not be numeric");
        input.value = "";
        input.focus();
        return false;
    }
    else
    {
        var ch = retValue.substring(0, 1);
        while (ch == " ")
        { // Check for spaces at the beginning of the string
            retValue = retValue.substring(1, retValue.length);
            ch = retValue.substring(0, 1);
        }
    
        ch = retValue.substring(retValue.length-1, retValue.length);
        while (ch == " ")
        { // Check for spaces at the end of the string
            retValue = retValue.substring(0, retValue.length-1);
            ch = retValue.substring(retValue.length-1, retValue.length);
        }
    
        while (retValue.indexOf("  ") != -1)
        { // Note that there are two spaces in the string - look for multiple spaces within the string
            retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
        }
    }

    input.value = retValue;
    return true;
}

function isEmpty(input, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = "Value";
    }

    if ((input.value == null) || (input.value == ""))
    {
        alert(errmsg + " should not be empty");
        input.focus();
        return true;
    }

    return false;
}

function round(num, places)
{
    if (places > 0)
    {
        if ((num.toString().length - num.toString().lastIndexOf('.')) > (places + 1))
        {
            var rounder = Math.pow(10, places);
            return Math.round(num * rounder) / rounder;
        }
        else
        {
            return num;
        }
    }
    else
    {
        return Math.round(num);
    }
}

function isAlphaNumeric(input, errmsg, avoidChars, bcheckEmpty, bcheckNumeric)
{
    if (arguments.length < 2)
    {
        errmsg = "Value";
    }

    if (arguments.length < 3)
    {
        avoidChars = "";
    }

    if (arguments.length < 4)
    {
        bcheckEmpty = true;
    }

    if (arguments.length < 5)
    {
        bcheckNumeric = true;
    }

    if (!trimValue(input, errmsg))
    {
        return false;
    }

    if (bcheckEmpty && isEmpty(input, errmsg))
    {
        return false;
    }

    if (bcheckNumeric)
    {
        var numeric = "0123456789";
        if (numeric.indexOf(input.value.charAt(0)) > 0)
        {            
            alert(errmsg + " should not start with numeric (" + input.value.charAt(0) + ").");
            input.value = "";
            input.focus();
            return false;
        }
    }           

    var specialChars = "-~`$%#@!*^[]{}\|/?><'\"()&_+=.,";
    for (var i = 0; i < avoidChars.length; i++)
    {
        specialChars = specialChars.replace(avoidChars.charAt(i), "");
    }

    for (var i = 0; i < specialChars.length; i++)
    {
        if (input.value.indexOf(specialChars.charAt(i)) >= 0)
        {            
            alert(errmsg + " should not contain any special characters (" + specialChars.charAt(i) + ").");
            input.value = "";
            input.focus();
            return false;
        }
    }           

    return true;
}

function isNumeric(input, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = ".";
    }

    if (isNaN(input.value))
    {
        alert("Please enter a numeric value" + errmsg);
        input.value = "";
        input.focus();
        return false;
    }

    input.value = input.value.replace(/\s/gi, "");
    /*if ((input.value != null) && (input.value.length != 0))
    {
        input.value = eval(input.value);
    }*/

    return true;
}

function isPositive(input, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = ".";
    }

    if (!isNumeric(input, errmsg))
    {
        return false;
    }

    if (input.value < 0)
    {
        alert("Please enter a positive value" + errmsg);
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}

function isNaturalNumber(input, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = ".";
    }

    if (!isNumeric(input, errmsg))
    {
        return false;
    }

    if (input.value < 1)
    {
        alert("Please enter a value greater than 0" + errmsg);
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}

function isNegative(input)
{
    if (arguments.length < 2)
    {
        errmsg = ".";
    }

    if (!isNumeric(input, errmsg))
    {
        return false;
    }

    if (input.value >= 0)
    {
        alert("Please enter a negative value" + errmsg);
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}

function isValueInRange(input, min, max, errmsg)
{
    if (arguments.length < 2)
    {
        errmsg = ".";
    }

    if (!isNumeric(input, errmsg))
    {
        return false;
    }

    if ((input.value < min) || (input.value > max))
    {
        alert("Please enter a numeric value between " + min + " and " + max + " " + errmsg + ".");
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}

function isValidEmail(input, checkempty, errmsg)
{
    if (arguments.length < 2)
    {
        checkempty = false;
    }

    if (arguments.length < 3)
    {
        errmsg = "Value";
    }

    if (checkempty)
    {
        if (isEmpty(input, errmsg))
        {
            return false;
        }
    }

    if ((input.value.indexOf("@") == -1) || (input.value.indexOf(".") == -1) || (input.value.indexOf("@") > input.value.lastIndexOf(".")))
    {
        alert("Invalid Mail Id.");
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}

function isAlpha(input, errmsg)
{
	var strval = input.value;
	for (j=0; j < strval.length; j++)
	{
        if ((input.value.charCodeAt(j) >= 65 && input.value.charCodeAt(j) <= 123))
        {
			if ((input.value.charCodeAt(j) > 91 && input.value.charCodeAt(j) < 97))
			{
				alert(errmsg + " should not contain any numeric / special characters");
	            input.value = "";
	            input.focus();
	            return false;
			}
        }
		else
		{
			alert(errmsg + " should not contain any numeric / special characters");
			input.value = "";
			input.focus();
			return false;
		}
	}
    return true;
}

function isBlank(val)
{
	if(val==null)
    {
        return true;
    }
	for(var i=0;i<val.length;i++) 
    {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r"))
        {
            return false;
        }
    }
	return true;
}

function isDigit(num)
{
	if (num.length>1)
    {
        return false;
    }
	var string="1234567890";
	if (string.indexOf(num)!=-1)
    {
        return true;
    }
	return false;
}

function isInteger(input, errmsg)
{
	var val = input.value;
	if (isBlank(val))
    {
        return false;
    }
	for(var i=0;i<val.length;i++)
    {
		if(!isDigit(val.charAt(i)))
        {
			alert(errmsg + " should not contain decimal values");
			input.value = "";
			input.focus();
            return false;
        }
    }
	return true;
}

function isZero(input, errmsg)
{
    if (!isNumeric(input, errmsg))
    {
        return false;
    }

    if (input.value == 0)
    {
        alert(errmsg + " should be greater than zero");
        input.value = "";
        input.focus();
        return false;
    }

    return true;
}
