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;
}

/* trim a string  - remove leading and trailing spaces */
function trim(strValue)
{
    strValue = strValue.replace(/^ +/,'');
    strValue = strValue.replace(/ +$/,'');
    return strValue;
}

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 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;
}
