//Konstruktor

var rc4 = {
    s: Array(),
    i: 0,
    j: 0,
    _key: "",

    key: function (key) {
        var len = key.length;
        for (this.i = 0; this.i < 256; this.i++) {
            this.s[this.i] = this.i;
        }

        this.j = 0;
        for (this.i = 0; this.i < 256; this.i++) {
            this.j = (this.j + this.s[this.i] + key.charCodeAt(this.i % len)) % 256;
            var t = this.s[this.i];
            this.s[this.i] = this.s[this.j];
            this.s[this.j] = t;
        }
        this.i = this.j = 0;
    },

    myunescape: function (instr) {
         // escape stuff, fix for Netscape unescape bug
        var reg = new RegExp("%([0-9abcdef]){2}", "gi");    
        var matches = instr.match(reg);
        if(!matches) 
            return instr;   
        for (var i = 0; i  < matches.length; i++){
           var match =  matches[i].substr(1);
           var replace = String.fromCharCode(new Number('0x'+match));
           instr = instr.replace(new RegExp('%'+match,'i'), replace);         
        } // end of Nescape stuff   
        return instr;  //unescape(instr);
    },

    decrypt: function (paramstr) {

        paramstr = this.myunescape(paramstr);
        this.key(this._key);
        var len = paramstr.length;
        var returnstr = "";
        for (var c = 0; c < len; c++) {    
            this.i = (this.i + 1) % 256;    
            this.j = (this.j + this.s[this.i]) % 256;

            var t = this.s[this.i];
            this.s[this.i] = this.s[this.j];
            this.s[this.j] = t;
            t = (this.s[this.i] + this.s[this.j]) % 256;
	    returnstr += String.fromCharCode(paramstr.charCodeAt(c) ^ this.s[t]);        
        }
        return returnstr;
    },

    crypt: function(input) {
          return this.decrypt(input);
    },
    
    mailto: function (href, ob, key) {
    	this.setKey(key);
    	var plain_href = this.decrypt(href);    
    	ob.href = "mailto:" + plain_href;
     	ob.innerHTML = plain_href;
    },

    mailtodbg: function (href, ob, key) {
    	this.setKey(key);
        alert(key);
        alert(href)
        alert (myunescape(href));
        var enchref = this.decrypt(href);    
        ob.href = "mailto:"+enchref;
        ob.innerHTML = enchref;
    },
	
    start: function () {
            
        for (var i = 0; i < document.links.length; i++){
    
            try {
                var node = document.links[i].getAttributeNode('pt:onload');
                var str = node.value.replace(/this/, "document.links[i]" );
                eval(str);			   
            } catch (e) {
              //alert(e);			
            }
        }
    },
	
    setKey: function (key)  {
        this._key = key;
    }
}



