$(document).ready(function() {
$("#as").click(function () {
text = $("#text").val(); 
size = $("#size option:selected").val();
encoded = strtr(base64_encode(encodeURIComponent(text)), '+/=', '-_,');

$("#pic").html('<img src="http://www.cy-pr.com/antispam/'+encoded+'/'+size+'.gif" />');
$("#blog").val('<a href="http://www.cy-pr.com/tools/antispam/"><img src="http://www.cy-pr.com/antispam/'+encoded+'/'+size+'.gif" alt="Антиспам" /></a>');
$("#forum").val('[url=http://www.cy-pr.com/tools/antispam/][img]http://www.cy-pr.com/antispam/'+encoded+'/'+size+'.gif[/img][/url]');
$("#res").slideDown();

return false;
});
});

function base64_encode(data) {

var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);

bits = o1<<16 | o2<<8 | o3;

h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;

// use hexets to index into b64, and append result to encoded string
enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);

switch( data.length % 3 ){
case 1:
enc = enc.slice(0, -2) + '==';
break;
case 2:
enc = enc.slice(0, -1) + '=';
}

return enc;
}


function strtr (str, from, to) {
    // Translates characters in str using given translation tables  
    // 
    // version: 912.1315
    // discuss at: http://phpjs.org/functions/strtr    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +      input by: uestla
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Alan C
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +      input by: Taras Bogach
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: jpfle
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // -   depends on: krsort    // -   depends on: ini_set
    // *     example 1: $trans = {'hello' : 'hi', 'hi' : 'hello'};
    // *     example 1: strtr('hi all, I said hello', $trans)
    // *     returns 1: 'hello all, I said hi'
    // *     example 2: strtr('a"aabaa*ccasdeo"oo', 'a"a*o"','aao');    // *     returns 2: 'aaabaaccasdeooo'
    // *     example 3: strtr('a"a"a"a"a"a"a"a"', 'a"', 'a');
    // *     returns 3: 'aaaaaaaa'
    // *     example 4: strtr('http', 'pthxyz','xyzpth');
    // *     returns 4: 'zyyx'    // *     example 5: strtr('zyyx', 'pthxyz','xyzpth');
    // *     returns 5: 'http'
    // *     example 6: strtr('aa', {'a':1,'aa':2});
    // *     returns 6: '2'
    var fr = '', i = 0, j = 0, lenStr = 0, lenFrom = 0, tmpStrictForIn = false, fromTypeStr = '', toTypeStr = '', istr = '';    var tmpFrom = [];
    var tmpTo = [];
    var ret = '';
    var match = false;
     // Received replace_pairs?
    // Convert to normal from->to chars
    if (typeof from === 'object') {
        tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
        from = this.krsort(from);        this.ini_set('phpjs.strictForIn', tmpStrictForIn);
 
        for (fr in from) {
            if (from.hasOwnProperty(fr)) {
                tmpFrom.push(fr);                tmpTo.push(from[fr]);
            }
        }
 
        from = tmpFrom;        to = tmpTo;
    }
    
    // Walk through subject and replace chars when needed
    lenStr  = str.length;    lenFrom = from.length;
    fromTypeStr = typeof from === 'string';
    toTypeStr = typeof to === 'string';
 
    for (i = 0; i < lenStr; i++) {        match = false;
        if (fromTypeStr) {
            istr = str.charAt(i);
            for (j = 0; j < lenFrom; j++) {
                if (istr == from.charAt(j)) {                    match = true;
                    break;
                }
            }
        }        else {
            for (j = 0; j < lenFrom; j++) {
                if (str.substr(i, from[j].length) == from[j]) {
                    match = true;
                    // Fast forward                    i = (i + from[j].length)-1;
                    break;
                }
            }
        }        if (match) {
            ret += toTypeStr ? to.charAt(j) : to[j];
        } else {
            ret += str.charAt(i);
        }    }
 
    return ret;
}