/**
 *
 * @access public
 * @return void
 **/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 *
 * @access public
 * @return void
 **/
function email_decryptor(controllerName)
{
    this.encrypt = true;
    if (controllerName == "") {
        controllerName = 'contact';
    }
    set_formController(controllerName);

    this.rot13 = new rot13();

     // Check for browser support
	if (!document.getElementsByTagName) {
        return false;
	}

    var links = document.getElementsByTagName('a'); // Get all anchors

	// Loop through the anchors
	for (var l=0; l<links.length; l++) {
        if (links[l].href.indexOf(this.formController)>-1) {
    		ed_decrypt(links[l]);
		}
	}

    // function to recompose the orginal address
    /**
     *
     * @access public
     * @return void
     **/
    function ed_decrypt(anchor) {
		var href     = anchor.getAttribute('href');
		var regExp   = new RegExp(".*"+this.formController+"\\/([a-z0-9._%-]+)\\+([a-z0-9._%-]+)\\+([a-z.]+)", 'i');
		var address  = href.replace(regExp, '$1' + '@' + '$2' + '.' + '$3');
		var linktext = anchor.innerHTML.replace(regExp, '$1' + '@' + '$2' + '.' + '$3');
		if (linktext != anchor.innerHTML) {
		    linktext = (this.encrypt ? this.rot13.str_rot13(linktext) : linktext);
		}
        if (href != address) {
			anchor.setAttribute('href','mailto:' + (this.encrypt ? rot13.str_rot13(address) : address)); // Add mailto link
			anchor.innerHTML = linktext;
		}
	}

	/**
	 *
	 * @access public
	 * @return void
	 **/
	function set_formController(controller)
    {
	    this.formController = controller;
	}

}

/**
 * Rot13 class
 * @access public
 * @return void
 **/
var rot13 = function() {
    this.map = initialise_map(); //array containing the rot13 map

    /**
     * rot13.initialise_map()
     * initialise the rot map
     * @access public
     * @return array
     **/
    function initialise_map(){
       	var map = new Array();
    	var s = "abcdefghijklmnopqrstuvwxyz";
    	for (var i=0; i<s.length; i++) {
    		map[s.charAt(i)] = s.charAt((i+13)%26);
    	}
    	for (var i=0; i<s.length; i++) {
    		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
    	}
    	return map;
    };

    /**
     * rot13.str_rot13()
     * Apply rot13 to a string
     * @access public
     * @return string
     **/
    //function str_rot13(a){
    this.str_rot13 = function(a) {
    	var s = "";
    	for (var i=0; i<a.length; i++) {
    		var b = a.charAt(i);
    		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? this.map[b] : b);
    	}
    	return s;
    }
}
