// AddScr4Im.js
// (c) 2004, Joachim Kaiser, Germany
// This Java script file is a variant designed to
// work with procedures of the Open Source Project:

//   addressScrambler.js version 0.2.2
//   copyright 2001, 2002, Josiah Q. Hamilton
//   This software is provided under the Artistic license of the Open 
//   Source Initiative, as it exists on 2001-12-19, including the optional
//   provision regarding aggregation with a commercial distribution.
//   This notice must be included with any distribution.

// The following procedures are originally taken
// from addressScrambler.js version 0.2.2
// copyright 2001, 2002, Josiah Q. Hamilton:

// function _scramble(inText,inverse)
// function descrambleText(inText)

// -----------------------------------------------------------------------

// The following string is used for scrambling/descrambling
// and can be chosen freely. However, it must be used both
// for scrambling and descrambling:
var scrambleString = "2hEll.with.y0u.nastY.SPAMeR";


// This "private" function holds the scrambling algorithm
// The first argument is the text to be scrambled or unscrambled
// The second argument is boolean -- false if the text is to be
// scrambled or true if it is to be unscrambled 
function _scramble(inText,inverse) {
	var outText = "";
	var scrambleLen = scrambleString.length;
	for (var i = 0; i < inText.length; ++i) {
		var currentCode = inText.charCodeAt(i);
		var offset = scrambleString.charCodeAt(i % scrambleLen);
		if (inverse) {
			// offset should be non-negative, hence the 10*26 below
			offset = 10 * 26 - offset;
		}

		var newCode = currentCode;
		// caveat: the following lines assume ASCII encoding
		if (currentCode == 46) {
			newCode = 64;  // replace '.' by '@'
		} else if (currentCode == 64) {
			newCode = 46;  // replace '@' by '.'
		} else if (65 <= currentCode && currentCode <= 90) {
			newCode = (currentCode - 65 + offset) % 26 + 65;
		} else if (97 <= currentCode && currentCode <= 122) {
			newCode = (currentCode - 97 + offset) % 26 + 97;
		}

		outText += String.fromCharCode(newCode);
	}
	return outText;
}


// This function returns unscrambled text
// It takes scrambled text as an input
function descrambleText(inText) {
	var outText = _scramble(inText,true);
	return outText;
}


// This "public" function works like
// "writeMailToWithClearDisplayText" of the
// original addressScrambler, but skips writing 
// the mailto and </a> tag. Hence, an image (e.g. button)
// can be used instead of a display text. Note that the
// </a> tag must be manually added to finish the anchor
// definition.
function writeMailToWithImage(scrambledAddress) {
	document.open();
	document.write("<a href=\"mailto:" + descrambleText(scrambledAddress) + "\">");
	document.close();
}

