/**
* This file contains copyrighted cryptographic functions.
* Any distribution or modification of this code without permission is prohibited.
* It is not allowed to study this code for any kind of commercial use.
*
* (C) 2007-2008 Philipp Wollmann - www.softwaregarten.de
*/
	

/**
* Change the URL to the encoded link
*/
function w_link2EncodedLink(eclink, key, type) {
	location.href = w_decode(eclink, key, type);
}

function w_link2(link) {
	location.href = link;
}

/**
* Convert a char value from 0 to 63 into a printable ASCII character (a-z,A-Z,0-9,#,!)
*/
function w_mapChar(c) {
	if (c >= 0 && c <=25) { // A-Z
		return (65+c);
	} else if (c >= 26 && c <=51){ // a-z
		return (97-26+c);
	} else if (c >= 52 && c <=61){ // 0-9
		return (48-52+c);
	} else if (c == 62){ // #
		return 35;
	} else if (c == 63){ // !
		return 33;
	}
}


/**
* Demap an encoded char from its char to a int value from 0-63
*/
function w_deMapChar(c) {
	if (c >= 65 && c <= 90) { // A-Z
		return (c - 65);
	} else if (c >= 97 && c <=122){ // a-z
		return (26-97+c);
	} else if (c >= 48 && c <=57){ // 0-9
		return (52-48+c);
	} else if (c == 35){ // #
		return 62;
	} else if (c == 33){ // !
		return 63;
	}
}


/**
* Encrypts a code with a key with an encryption type
*/
function w_encode(code, key, type) {
	var ecode = ""; //decoded text
	
	if (type == 'WB64') {
		var len = code.length;
		var c;
		var c1;
		var c2;
		
		for(var i=0; i<len; i++) {
			c = code.charCodeAt(i);
			c1 = c & 0x3F; // bits 2-8 as 0-63 integer
			c2 = c>>2; // bits 0-6 as 0-63 integer
		
			ecode += String.fromCharCode(w_mapChar(c1))+String.fromCharCode(w_mapChar(c2)); // Merge bits 2-8 from c1 and bits 0-1 (alias 2,3) from c2
		}
	}
	
	return ecode;
}


/**
* Decrypts a code with a key with an encryption type
*/
function w_decode(code, key, type) {
	var dcode = ""; //decoded text
	
	if (type == 'WB64') {
		var len = code.length/2; // 2 chars become one ;-)
		var c1;
		var c2;
		
		for(var i=0; i<len; i++) {
			c1 = w_deMapChar(code.charCodeAt(i*2)); // get char from string
			c2 = w_deMapChar(code.charCodeAt(i*2+1)); // get char from string
			dcode += String.fromCharCode(c1 | c2<<2); // Merge bits 2-8 from c1 and bits 0-1 (alias 2,3) from c2
		}
	}
	
	return dcode;
}


/**
* Prints the parameter to the html file
*/
function writeMe(me) {
	document.write(me);
}