//sifra
function Sifra(){
	//korenova trida pro sifry
	this.strABC = this.createInSet();
}

Sifra.prototype.createABC = function(){
	var start = 'a';
	var end = 'z';
	var abc = '';
	for(var i = start.charCodeAt(0); i<=end.charCodeAt(0); i++){
		//var s ='';
		abc = abc + String.fromCharCode(i);
	}
	return abc;
}
Sifra.prototype.create09 = function(){
	var cisla = '';
	for (var i=0; i<=9; i++){
		cisla = cisla + i.toString();
	}
	return cisla;
}

Sifra.prototype.createInSet = function(){ // mnozina znaku jez bude tvorit abecedu pro kryptovani
	var out = ''
	out = out + this.createABC() + this.create09() + '._-@:';
	return out;
}



//-----------

function Rotor(strKey){
	this.strABC = this.createInSet(); //set znaku, ktere je mozne zasifrovat
	this.strKey = this.checkKey(strKey); //heslo
	this.strCABC = this.createSubstitutionAlphabet(this.strKey, this.strABC); //transponovaný set znaku

}

Rotor.prototype = new Sifra(); //dedeni ze obj Sifra

Rotor.prototype.setKey = function(strKey){
	this.strKey = this.checkKey(strKey); //heslo
	this.strCABC = this.createSubstitutionAlphabet(this.strKey, this.strABC); //transponovaný set znaku
}

Rotor.prototype.checkKey = function(strKey){
	var strNewKey = '';
	for(var i = 0; i < strKey.length; i++){
		/*if((pos = strKey.indexOf(strKey.charAt(i), i+1) >= 0)){
			var newtext = strKey.substr(0,pos) + strKey.substr(pos+1);
		}*/
		if(!(strNewKey.indexOf(strKey.charAt(i)) >= 0)){
			strNewKey = strNewKey + strKey.charAt(i);
		}
	}
	return strNewKey;
}

Rotor.prototype.createSubstitutionAlphabet = function(strKey, strABC){
	//strKey - klíč
	// strABC - výchozí abeceda- co lze zasifrovat
	var strKeyLen = strKey.length;
	var strABCLen = strABC.length;
	var strKeyText = '';
	for(var i = 0; i <= strABCLen; i++){
		if(strKey.indexOf(strABC.charAt(i)) == -1){
			/* znak na pozici i neni v strKey */
			strKeyText = strKeyText + strABC.charAt(i);
		}
	}
	strKeyText = strKeyText + strKey;
	return strKeyText;	
}


Rotor.prototype.shift = function(text, len){
	var part = '';
	if ((len == 0) || (len == text.length)){
		return text;
	}
	if (len > 0){
		part = text.substr(0,len);
		text = text.substr(len) + part;
	}else{
		part = text.substr(text.length + len);
		text = part + text.substr(0, text.length + len);
	}
	return text;
}

Rotor.prototype.encode = function(text){
	var strCABC = this.strCABC;
	var strABC = this.strABC;
	var textLen = text.length;
	var offset = 0;
	var code = '';
	
	var sss = strCABC.length;
	
	for (var i = 0; i < textLen; i++){
		var pos = strABC.indexOf(text.charAt(i));
		var cpos = (pos + offset) % strCABC.length;
		code = code + strCABC.charAt(cpos);
		offset=(offset + pos+1) % strABC.length;
	}
	return code;
}

Rotor.prototype.decode = function(text){
	var strCABC = this.strCABC;
	var strABC = this.strABC;
	var textLen = text.length;
	var offset = 0;
	var code = '';
	for (var i = 0; i < textLen; i++){
		var pos = strCABC.indexOf(text.charAt(i));
		var cpos = (strCABC.length + pos - offset) % strCABC.length;
		code = code + strABC.charAt(cpos);
		offset= (strCABC.length+cpos+offset+1)%strCABC.length;
	}
	return code;
}
//--------end rotor


function Vernam(strKey){
	this.strKey = strKey;
	this.strABC = this.createInSet();
}

Vernam.prototype = new Sifra();

Vernam.prototype.verEncode = function(text){
	var strABC = this.strABC;
	var strKey = this.strKey;
	var code = '';
	for(var i =0 ; i < text.length; i++){
		var keyIndex = i % strKey.length;
		var offset = strABC.indexOf(strKey.charAt(keyIndex)) + 1;
		var charIndex = strABC.indexOf(text.charAt(i));
		var codeIndex = (charIndex + offset) % strABC.length;
		code = code + strABC.charAt(codeIndex);
	}
	return code;
} 

Vernam.prototype.verDecode = function(text){
	var strABC = this.strABC;
	var strKey = this.strKey;
	var code = '';
	
	for(var i =0 ; i < text.length; i++){
		var keyIndex = i % strKey.length;
		var offset = strABC.indexOf(strKey.charAt(keyIndex)) + 1;
		var charIndex = strABC.indexOf(text.charAt(i));
		var codeIndex = (strABC.length + charIndex - offset) % strABC.length;
		code = code + strABC.charAt(codeIndex);
	}
	return code;
}

function rozsifruj(text, heslo){
	var s = new Rotor(heslo);
	return s.decode(text);
}

function zasifruj(text, heslo){
	var s = new Rotor(heslo);
	return s.encode(text);
}


//---------------
function replace_ep(){
	var eps = document.getElementsByName("ep");
	var at;
	for(var i=0; i< eps.length; i++){
		var ep = eps[i];
		if(at = AnchorText(ep.innerHTML)){
			if(at.length > 0){
				var atext = split(at);
				if(atext.length == 2){
					try{
						var posta = rozsifruj(atext[0],atext[1]);
						//var posta = koko();
						ep.innerHTML = posta;
						ep.href = "mai" + "lto" + ":" + posta;
						
					}
					catch(err){
					}
				}
			}
		}
		
	}
}

function replace_a(){
	var aa = document.links;
	var oRE = new RegExp("^<!--(.+?)-->$");
	for(var i = 0; i < aa.length; i++){
		var a = aa[i];
		var inner = a.innerHTML;
		if(inner.length > 0){
			if(oRE.test(inner)){
				//yes, we can continue!!!! GREAT!!! Happy we :))
				// obsah je skremblovaný comment
				var crypttext = RegExp.lastParen;
				var aDeFeed = split(crypttext);
				if(aDeFeed.length == 2){
					var posta = rozsifruj(aDeFeed[0], aDeFeed[1]);
					a.innerHTML = posta;
					a.href = "ma" + "ilto" + ":" + posta;
				}
			}
		}
	}
}



function AnchorText(text){
	var start = text.indexOf('<!--');
	var end = text.indexOf('-->');
	var innerText = '';
	if (start >= 0){
		if(end >= (start + 4)){
			innerText = text.substr(start+4,(end - start - 4));
	
		}
	}
	return innerText;
}
function split(text){
	var arr = new Array();
	var pos = text.indexOf('/');
	if(pos > 0){
		arr[0] = text.substr(0, pos);
		arr[1] = text.substr( pos+1 );
	}
	return arr;
}