/**
 * Collection of functions used for string information/manipulation
 *  usage : str.replace( search, replace, string )
 *  usage : str.contains( haystack, needle )
 *  usage : str.is_empty( string, ignoreSpaces[true/false] )
 *  usage : str.is_integer( string )
 *
 * Version: 1.02.13
 */
var str = {
    /**
     * replaces the old characters from the string with the new characters
     */
    replace : function( search, replace, string ) {
        string      = string.split( search );
        var output  = string[0];
        
        for ( i = 1; i < string.length; i++ ) {
            output += replace + string[i];
        }
        
        return output;
    },
    
    /**
     * checks to see if a string contains another string
     */
    contains : function( haystack, needle ) {
        var strPos = haystack.match( needle );
        
        if ( strPos != null ) {
            return true;
        }
        
        return false;
    },
    
    /**
     * checks to see if a string is empty or not
     */
    is_empty : function( string, ignoreSpaces ) {
        if ( ignoreSpaces == undefined || ignoreSpaces == 'undefined' ) {
            ignoreSpaces = false;
        }
        
        if ( string == undefined || string == 'undefined' || string == null ) {
            return true;
        }
        
        if ( ignoreSpaces != true && ignoreSpaces != false ) {
            ignoreSpaces = false;
        }
        
        if ( !ignoreSpaces ) {
            string = str.replace( " ", "", string );
        }
        
        if ( string == "" || string == undefined || string == 'undefined' ) {
            return true;
        }
        
        return false;
    },
    
    /**
     * checks to see if all the characters in a string is numbers
     */
    is_integer : function( string ) {
        var integerCharacters   = "0123456789";
        var stringIsInteger     = true;
        var subjectCharacter;


        for (i = 0; i < string.length && stringIsInteger == true; i++) { 
            subjectCharacter = string.charAt( i ); 
            if ( integerCharacters.indexOf( subjectCharacter ) == -1 ) {
                stringIsInteger = false;
            }
        }
        return stringIsInteger;        
    },
    
    /**
     * takes the provided string and truncates it to the specified length if the string length is too long
     */
    truncate : function( string, size, suffix ) {
        if ( str.is_empty( string ) ) {
            return string;
        }
        
        if ( string.length <= size ) {
            return string;
        }
        
        if ( !str.is_integer( size ) || typeof size != 'number' ) {
            return string;
        }
        
        if ( str.is_empty( suffix ) ) {
            suffix = "...";
        }
        
        return string.substring( 0, size ) + suffix;
    }
}

String.prototype.empty = function( string ) {
    if ( typeof string == 'undefined' || string == 'null' ) {
        string = this;
    }
    
    string = string.replace( ' ', '' );
    
    if ( string == '' ) {
        return true;
    }
    
    return false;
}
    
String.prototype.truncate = function( size, suffix, string ) {
    if ( typeof string == 'undefined' ) {
        string = this;
    }
    
    if ( typeof size != 'number' ) {
        return string;
    }
    
    if ( string.length <= size ) {
        return string;
    }
    
    if ( typeof suffix != 'string' || suffix.empty( ) ) {
        suffix = '...';
    }
    
    return string.substring( 0, size ) + suffix;
}

String.prototype.reverse = function( ) {
    string = '';
    
    for ( i = this.length - 1; i >= 0; i-- ) {
        string += this.charAt( i );
    }
    
    return string;
}

String.prototype.stripHTML = function( ) {
    return this.replace( /<(?:.|\s)*?>/g, '' );
}

String.prototype.contains = function( key ) {
    return this.match( key ) !== null;
}

String.prototype.randomString = function( length ) {
    var chars  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
    var string = '';
    
    if ( typeof length != 'number' ) {
        length = 8;
    }
    
    for ( i = 0; i < length; i++ ) {
        var rnum = Math.floor( Math.random( ) * chars.length );
        string += chars.substring( rnum, rnum + 1 )
    }
    
    return string;
}

String.prototype.is_integer = function( ) {
    var integerCharacters = '0123456789';
    var stringIsInteger   = true;
    var subjectCharacter;
    
    if ( this.empty( ) ) {
        return false;
    }
    
    for ( i = 0; i < this.length && stringIsInteger == true; i++ ) {
        subjectCharacter = this.charAt( i );
        
        if ( integerCharacters.indexOf( subjectCharacter ) == -1 ) {
            stringIsInteger = false;
        }
    }
    
    return stringIsInteger;
}

String.prototype.is_float = function( ) {
    var integerCharacters = '0123456789.';
    var stringIsFloat     = true;
    var subjectCharacter;
    
    if ( this.empty( ) ) {
        return false;
    }
    
    for ( i = 0; i < this.length && stringIsFloat == true; i++ ) {
        subjectCharacter = this.charAt( i );
        
        if ( integerCharacters.indexOf( subjectCharacter ) == -1 ) {
            stringIsFloat = false;
        }
    }
    
    var value = this.split( '.' );
    if ( value.length != 2 ) {
        stringIsFloat = false;
    }
    
    return stringIsFloat;
}

