// charset=utf-8
// $Id: mcmEmailDecoder.js 2376 2008-07-01 14:33:32Z dierker $
// +----------------------------------------------------------------------+
// | mcm                                                                  |
// | version 5.5                                                          |
// | (c) 2002-2008 monsun media (http://www.monsun-media.com)             |
// +----------------------------------------------------------------------+


/**
* mcmEmailDecoder
*
* class which decodes encoded email-address in a complete document
* tested in 
*   - Mozilla Firefox v1.5 (Windows)
*   - Mozilla Firefox v2.0 (Windows)
*   - Mozilla Firefox v3.0 Beta 4 (Windows)
*   - Internet Explorer 5.5 SP2 (Windows)
*   - Internet Explorer 6 (Windows)
*   - Internet Explorer 7 (Windows)
*   - Opera 9.21 (Windows)
*   - Safari 2 (Mac)
*   - Safari 3 Beta (Windows)
*
* @note		the a-tags containing an encoded/decoded email-address aren't marked
*			as visited by any of the browsers, so you can't style "visited"-links
*			different from unvisted links
* @usage	just include the script on a XHTML-page
* @author	dierker (monsun-media.com)
*/
var mcmEmailDecoder = {
	/**
	* @var	RegExp
	*/
	encodedEmailRegEx : /##(([a-z0-9-]+(\.[a-z0-9-]+)*)+@([a-z0-9]+))##/i
	,
	/**
	* @var	RegExp
	*/
	allEncodedEmailRegEx : /##(([a-z0-9-]+(\.[a-z0-9-]+)*)+@([a-z0-9]+))##/gi
	,
	/**
	* search the whole document for encoded email adresse and try to decode them
	* 
	* @return	void
	*/
	decodeDocument : function(){
		try {
			var body = document.getElementsByTagName('body')[0];
			var level = 1;
			mcmEmailDecoder.processNode(body,level);
		}catch( e ){
			// console.log('Exception caught: ' + e);
		};
	}
	,
	/**
	* process a DOM-node
	*
	* @param	DomNode	node
	* @param	int		level
	* @return	void
	*/
	processNode : function(node,level){
		if( node.nodeType==1 ){
			if( node.nodeName.toUpperCase()=='A' ){
				mcmEmailDecoder.processLink(node);
			}else if( node.nodeName.toUpperCase()=='TEXTAREA' || (node.nodeName.toUpperCase()=='INPUT' && (node.type.toUpperCase()=='TEXT'||node.type.toUpperCase()=='HIDDEN')) ){
				mcmEmailDecoder.processTextInput(node);
			};
		}else if( node.nodeType==3 ){
			mcmEmailDecoder.processTextNode(node);
			return;
		}
		var i;
		for( i=0; i<node.childNodes.length; i++ ){
			mcmEmailDecoder.processNode(node.childNodes.item(i),(level+1));
		}
	}
	,
	/**
	* process the properties of a <a>-tag
	*
	* @param	DomNode	node
	* @return	void
	*/
	processLink : function(node){
		var href = node.getAttribute('href');
		if( !(mcmEmailDecoder.encodedEmailRegEx.exec(href)==null) ){
			// temporary store the values of the innerHTML-property, because
			// the IE will overwrite the property with the content
			// of the href-attribute !!!
			var innerHTML = node.innerHTML;
			var matches = href.match(mcmEmailDecoder.encodedEmailRegEx);
			var newHref = href.replace(matches[0],mcmEmailDecoder.decodeEmail(matches[1]));
			node.setAttribute('href',newHref);
			node.innerHTML = innerHTML;
		}
	}
	,
	/**
	* process text-input-fields
	*
	* @param	DomNode	node
	* @return	void
	*/
	processTextInput : function(node){
		var inputVal = node.value;
		if( !(mcmEmailDecoder.encodedEmailRegEx.exec(inputVal)==null) ){
			var matches = inputVal.match(mcmEmailDecoder.encodedEmailRegEx);
			var newVal = inputVal.replace(matches[0],mcmEmailDecoder.decodeEmail(matches[1]));
			node.value = newVal;
		};
	}
	,
	/**
	* process a single text-node
	*
	* @param	DomNode	textNode
	* @return	void
	*/
	processTextNode : function(textNode){
		// search for an encoded email in the text-node 
		var atPos = textNode.nodeValue.indexOf('##');
		if( atPos==-1 ){
			return false;
		};
		var resultAry = textNode.nodeValue.match(mcmEmailDecoder.allEncodedEmailRegEx);
		for( i=0; i<resultAry.length; i++ ){
			// remove the ## from the email-string
			var encodedEmail = resultAry[i].replace(/##(.+)##/,'$1');
			textNode.nodeValue = textNode.nodeValue.replace(resultAry[i],mcmEmailDecoder.decodeEmail(encodedEmail));
		};
		return true;
	}
	,
	/**
	* decode an encoded email-address string
	*
	* @param	string	str
	* @return	string
	*/
	decodeEmail : function(str){
		if( str==undefined ){
			return;
		};
		var i=0;
		var newStr='';
		for( i=(str.length-1); i>=0; i-- ){
			newStr += str.charAt(i);
		};
		return newStr;
	}
}


// initialize the mcmEmailDecoder
if( window.addEventListener ){
	window.addEventListener("load",function(){ mcmEmailDecoder.decodeDocument(); },false);
}else if( window.attachEvent ){
	window.attachEvent("onload",function(){ mcmEmailDecoder.decodeDocument(); });
};
