/***************************************************************************
 *                                show.js
 *                            -------------------
 *   copyright            : (C) 2005 Panayiotis P. Gotsis
 *   email                  : g_panayiotis@hotmail.com
 *
 *   The source code contained herein is considered trivial. However I decide to
 *   release it under GPLv2 cause there are some people who seem to even steal 10
 *   lines of code instead of typing something on their own, *forgeting* to mention
 *   where they got this code from.
 * 
 *   These functions would not have been conceived of, if it wasn't for the inspiration
 *   offered by a similar feature on the website of the Management School of the
 *   University of Lancaster.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of version 2 of the GNU General Public License as published by
 *   the Free Software Foundation.
 *
 ***************************************************************************/


function showEmail(el,caption,cipher)
{
	document.getElementById(el).innerHTML="<a href=\"mailto:" + decodeEmail(cipher) + "\">"+caption+"</a>";
}

function computeCipher(fromEl,toEl)
{
	document.getElementById(toEl).value=encodeEmail(document.getElementById(fromEl).value);
}

function decodeEmail(txtIn)
{
	var s;
	
	s=xorChar(hex2bin(txtIn));
//	s=hex2bin(txtIn);

	return s;
}

function encodeEmail(txtIn)
{
	var s;
	
	s=bin2hex(xorChar(txtIn));
//	s=bin2hex(txtIn);
	
	return s;
}

function xorChar(txtIn)
{
	var txtLen;
	var key;
	var i;
	var el;
	var s;

	key=189;
	
	txtLen=txtIn.length;
	s='';
	for (i=0;i<txtLen;i++)
	{
		el=txtIn.charCodeAt(i) ^ key;
		s=s+String.fromCharCode(el);
	}

	return s;
}

function bin2hex(txtIn)
{
	var txtLen;
	var i;
	var el;
	var s;

	txtLen=txtIn.length;
	s='';
	for (i=0;i<txtLen;i++)
	{
		el=(txtIn.charCodeAt(i) & 240) >>> 4;
		if (el==10) { el='a' }
		else if (el==11) { el='b' }
		else if (el==12) { el='c' }
		else if (el==13) { el='d' }
		else if (el==14) { el='e' }
		else if (el==15) { el='f' }
		s=s + el;

		el=(txtIn.charCodeAt(i) & 15);
		if (el==10) { el='a' }
		else if (el==11) { el='b' }
		else if (el==12) { el='c' }
		else if (el==13) { el='d' }
		else if (el==14) { el='e' }
		else if (el==15) { el='f' }
		s=s + el;
	}
	
	return s;
}

function hex2bin(txtIn)
{
	var txtLen;
	var i;
	var el,out;
	var s;

	txtLen=txtIn.length;
	s='';
	for (i=0;i<txtLen;i=i+2)
	{
		el=txtIn.charAt(i);
		if (el=='a') { el=10 }
		else if (el=='b') { el=11 }
		else if (el=='c') { el=12 }
		else if (el=='d') { el=13 }
		else if (el=='e') { el=14 }
		else if (el=='f') { el=15 }
		out=el*16;

		el=txtIn.charAt(i+1);
		if (el=='a') { el=10 }
		else if (el=='b') { el=11 }
		else if (el=='c') { el=12 }
		else if (el=='d') { el=13 }
		else if (el=='e') { el=14 }
		else if (el=='f') { el=15 }
	
		s=s + String.fromCharCode(out*1+el*1);
	}
	
	return s;
}
