/**
* Script contains all functions for dealing with strings. 
*/

/**
 * Creates a class called StringUtils. 
 */
var StringUtils = {
    version : "1.0.1",
    defaultEmptyOK : false
};

/**
 * Defines a trim function for strings. Usage: string.trim().
 *
 * @return trimmed string
 */
String.prototype.trim = function() {
 return this.replace(/^\s+|\s+$/g,"");
};

/**
 * Defines a left trim function for strings. Usage: string.leftTrim().
 *
 * @return left trimmed string
 */
String.prototype.leftTrim = function() {
    return this.replace(/^\s+/,"");
};

/**
 * Defines a right trim function for strings. Usage: string.rightTrim().
 *
 * @return right trimmed string
 */
String.prototype.rightTrim = function() {
    return this.replace(/\s+$/,"");
};

/**
 * Defines a startsWith function for strings. Usage: string.startsWith("test");
 *
 * @param str to check
 */
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)};

/**
 * Defines a endsWith function for strings. Usage: string.startsWith("test");
 *
 * @param str to check
 */
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}

/**
 * Checks if the given value is empty.
 * 
 * @param value value to check if empty.
 * @return true if value is emtpy, false otherwise.
 */
StringUtils.isWhitespace = function(value) {
    return StringUtils.isEmpty(value);
};

/**
 * Removes the characters that are in the given bag string from the 
 * passed in value.
 * 
 * @param value value to remove characters from.
 * @param bag string containing characters to remove.
 * @return value of string with characters removed.
 */
StringUtils.stripCharsInBag = function(value, bag) {
  
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < value.length; i=i+1)
    {
        // Check that current character isn't whitespace.
        var c = value.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
};

/**
 * Removes the characters that are NOT in the given bag string from the 
 * passed in value.
 * 
 * @param value value to remove characters from.
 * @param bag string containing characters to NOT remove.
 * @return value of string with characters that are NOT in bag removed.
 */
StringUtils.removeCharactersInBag = function(value, bag) {

    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < value.length; i=i+1)
    {
        // Check that current character isn't whitespace.
        var c = value.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
};

/**
 * Checks whether an string is empty.
 *
 * @param stringValue to check if empty
 * @return true if string is empty; false otherwise.
 */
StringUtils.isEmpty = function(stringValue) {
    if (stringValue ==null) {
        return true;
    }
    
    stringValue = stringValue.trim();
    return stringValue.length == 0;
};


/**
 * Returns true if the character c is a letter
 *
 * @param c to check
 * @return true if a character is a letter; false, otherwise.
 */
StringUtils.isLetter = function(c) {
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
};


/**
 * Returns true if string s consist of letters only.
 *
 * @param s string value to check
 * @return true if the string is alphabetic; false, otherwise.
 */
StringUtils.isAlphabetic = function(s) {
    var i;

    if (StringUtils.isEmpty(s))
       if (StringUtils.isAlphabetic.arguments.length == 1) return StringUtils.defaultEmptyOK;
       else return (StringUtils.isAlphabetic.arguments[1]);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!StringUtils.isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
};


/**
 * Returns true if string s consist of letters and numbers only.
 *
 * @param s to check
 * @return true if the string is alphanumeric; false, otherwise
 */
StringUtils.isAlphanumeric = function(s) {
    var i;

    if (StringUtils.isEmpty(s))
       if (StringUtils.isAlphanumeric.arguments.length == 1) return StringUtils.defaultEmptyOK;
       else return (StringUtils.isAlphanumeric.arguments[1]);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (StringUtils.isLetter(c) || StringUtils.isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
};

StringUtils.lengthOfTrimmedString = function(stringValue) {
    if (stringValue ==null) {
        return 0;
    }

    stringValue = stringValue.trim();
    return stringValue.length;
};


StringUtils.hasWhiteSpace = function(s) {
	  return /\s/g.test(s);
};

StringUtils.containsSpecialCharacters = function(s) {
    return /[$#%@&^!@\(\)\*]/g.test(s);
}

StringUtils.isDigit = function(character) {
    return ((character >= "0") && (character <= "9"));
};
