/**
 * Extensions to the String object
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @version 1.0
 * @package Resource it Modules
 * @copyright Resource it ApS
 **/
 
    /**
     * htmlentites almost equivalent to the php function of same name
     * The function takes an abitrary amount of arguments used to exclude some
     * characters from beeing replaced with their html entity equivalent
     * @access public
     **/
    String.prototype.htmlentities = function() {
       
        var str = this;

        var entities = {"&":'amp',"¡":'iexcl',"¢":'cent',"£":'pound',"¤":'curren',"¥":'yen',"¦":'brvbar',
            "§":'sect',"¨":'uml',"©":'copy',"ª":'ordf',"«":'laquo',"¬":'not',"­":'shy',"®":'reg',
            "¯":'macr',"°":'deg',"±":'plusmn',"²":'sup2',"³":'sup3',"´":'acute',"µ":'micro',"¶":'para',
            "·":'middot',"¸":'cedil',"¹":'sup1',"º":'ordm',"»":'raquo',"¼":'frac14',"½":'frac12',
            "¾":'frac34',"¿":'iquest',"×":'times',"÷":'divide',"À":'Agrave',"Á":'Aacute',"Â":'Acirc',
            "Ã":'Atilde',"Ä":'Auml',"Å":'Aring',"Æ":'AElig',"Ç":'Ccedil',"È":'Egrave',"É":'Eacute',"Ê":'Ecirc',
            "Ë":'Euml',"Ì":'Igrave',"Í":'Iacute',"Î":'Icirc',"Ï":'Iuml',"Ð":'ETH',"Ñ":'Ntilde',"Ò":'Ograve',
            "Ó":'Oacute',"Ô":'Ocirc',"Õ":'Otilde',"Ö":'Ouml',"Ø":'Oslash',"Ù":'Ugrave',"Ú":'Uacute',
            "Û":'Ucirc',"Ü":'Uuml',"Ý":'Yacute',"Þ":'THORN',"ß":'szlig',"à":'agrave',"á":'aacute',"â":'acirc',
            "ã":'atilde',"ä":'auml',"å":'aring',"æ":'aelig',"ç":'ccedil',"è":'egrave',"é":'eacute',"ê":'ecirc',
            "ë":'euml',"ì":'igrave',"í":'iacute',"î":'icirc',"ï":'iuml',"ð":'eth',"ñ":'ntilde',"ò":'ograve',
            "ó":'oacute',"ô":'ocirc',"õ":'otilde',"ö":'ouml',"ø":'oslash',"ù":'ugrave',"ú":'uacute',"û":'ucirc',
            "ü":'uuml',"ý":'yacute',"þ":'thorn',"ÿ":'yuml',"\"":"quot"}

        for ( var c = 0; c < arguments.length; c++ ) {
            if ( entities[arguments[c]] != undefined ) delete entities[arguments[c]]; 
        }//for

        for ( var entity in entities ) {
            var reg = new RegExp(entity,"g");
            str = str.replace(reg, "&" + entities[entity] + ";");
        }//for

        return str;

    }//htmlentities


    /**
     * ucfirst almost equivalent to the php function of same name
     * The function converts the first character in a string to upper case
     * @access public
     **/
    String.prototype.ucfirst = function() {
        return this.charAt(0).toUpperCase() + this.substr(1);
    }//ucfirst


    /**
     * base64encode
     * Encode a string to base64 for safe url- transfer
     * Base64 encode / decode
     * http://www.webtoolkit.info/
     *
     **/
    String.prototype.base64encode = function() {
        var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        // public method for encoding
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
 
        //input = Base64._utf8_encode(input);
 
        while (i < this.length) {
 
            chr1 = this.charCodeAt(i++);
            chr2 = this.charCodeAt(i++);
            chr3 = this.charCodeAt(i++);
 
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
 
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
 
            output = output +
            _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
            _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
 
        }//while
 
        return output;
    }//base64encode
    
