var result;
var linkText;
var key = "BAD4@.56CEGFHIJKLVWdfTUhijXYZbacemngMNOPQRSopqrstuvz018923klwxy7";
var base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@.0123456789";

function calculate()
{
	result = encode(document.forms['change'].original.value);
	linkText = document.forms['change'].linkText.value;
	document.forms['change'].encrypted.value = result;
	document.forms['change'].decrypted.value = decode(result);
	document.getElementById('codeSample').innerHTML = "<code>&lt;script&gt;generateMailLink(\""+result+"\",\""+linkText+"\");&lt;/script&gt;</code>";
	document.getElementById('sample').style.visibility = "visible";
	return false;
}

function generateMailLink(encoded, linkText)
{
  document.write("<a hr"+"ef=\"ma"+"ilto"+":"+decode(encoded)+"\">"+linkText+"</"+"a>");
}

function generateMailLink(encoded, linkText, extraHrefAttributes)
{
  document.write("<a hr"+"ef=\"ma"+"ilto"+":"+decode(encoded)+"\""+extraHrefAttributes+">"+linkText+"</"+"a>");
}

function printKey()
{
  document.write("\""+key+"\"");
}

function printBase()
{
  document.write("\""+base+"\"");
}

function encode(str)
{
	return codec(base, key, str);
}
 
function decode(str)
{
	return codec(key, base, str);
}

/**
 * Encrypt/Decrypt a string using a simple substitution cypher
 */
function codec(from, to, str)
{
  var codedResult = "";                                      
  for (i = 0; i < str.length; i++) {     
		current = str.charAt(i);                  
		idx = from.indexOf(current);             
nextVal =  (idx == -1) ? current : to.charAt(idx);
		codedResult += nextVal;
	}
	return codedResult;
}



