/**
 * string.lib.js
 *
 * Copyright (c) 2004 Webparation Technologies
 */

/* White space characters */
var whitespace = " \t\n\r";

/**
 * Trims all white spaces from a given string
 *
 * @param str
 * @return String without leading and trailing whitespaces
 */
function trimWhitespaces(str){
    
    str = trimLeadingWhitespaces(str);
    str = trimTrailingWhitespaces(str);

    return str;
}

/**
 * Trims leading whitespaces from a given string
 *
 * @param str
 * @return String without leading whitespaces
 */
function trimLeadingWhitespaces(str){

    /* Trim leading whitespaces */
    while (str.substring(0,1) == ' '){
        str = str.substring(1, str.length);
    }
    
    return str;
}

/**
 * Trims trailing whitespaces from a given string
 *
 * @param str
 * @return String without trailing whitespaces
 */
function trimTrailingWhitespaces(str){

    /* Trim trailing whitespaces */
    while (str.substring(str.length-1, str.length) == ' '){
        str = str.substring(0, str.length-1);
    }

    return str;
}

/**
 * Checks if a string is empty (i.e. if a string doesn't exist or has a length
 * of zero)
 *
 * @param str String to check
 * @return True if string is empty, otherwise false
 */
function isEmpty(str){
    return ((str == null) || (str.length == 0));
}

/**
 * Checks if a string is empty or is only white spaces
 *
 * @param str String to check
 * @param True if string is empty or whitespaces only, otherwise false
 * @see isEmpty
 */
function isWhitespace(str){
    
    var charPos;

    /* 1. Check if the string is empty */
    if (isEmpty(str)){
        return true;
    }


    /* 2. Check if each character position is empty */
    for (charPos=0; charPos<str.length; charPos++){

        var currentChar = str.charAt(charPos);

        if (whitespace.indexOf(currentChar) == -1){
            return false;
        }
    }

    return true;
}

/**
 * Checks if a given string contains whitespaces
 *
 * @param str String to check
 * @param True if the given string contains any whitespaces, otherwise false
 */
function containsWhitespaces(str){
    
    var charPos;

    for (charPos=0; charPos<str.length; charPos++){

        var currentChar = str.charAt(charPos);

        if (whitespace.indexOf(currentChar) != -1){
            return true;
        }
    }

    return false;
}

/**
 * Checks if a given string is a valid e-mail address
 *
 * @param str String to check
 * @param True if given string is a valid e-mail address, otherwise false
 */
function isEmail(str){

    /* Check if the string is empty or just white spaces */
    if (isWhitespace(str)){
        return false;
    }

    /* Check if the string contains any white spaces */
    if (containsWhitespaces(str)){
        return false;
    }

    /* A valid e-mail address:
     *  (1) must have an @
     *  (2) must have one or more characters before @
     *  (3) after the @ there should be at least one .
     *  (4) after the . there must be at least one character
     */
     
    /* Position we should start at - Rule (2) */
    var charPos = 1;

    /* Length of the string to validate */
    var strLength = str.length;

    /* Rule (1) - Look for @ */
    while ((charPos < strLength) && (str.charAt(charPos) != "@")){
        charPos++;
    }

    /* If we have run out of characters then the string is not valid */
    if ((charPos >= strLength) || (str.charAt(charPos) != "@")){
        return false;
    }
    else{
        /* Increase character positioning if more characters in string */
        charPos += 2;
    }

    /* Rule (3) - Looking for . */
    while ((charPos < strLength) && (str.charAt(charPos) != ".")){
        charPos++;
    }
   
    /* Rule (4) - Looking for characters after . */
    if ((charPos >= strLength - 1) || (str.charAt(charPos) != ".")){
        return false;
    }
    else{
        return true;
    }
}
