
var separator = 'a@a';

function GetRandom(start,end) { 
    var range = end - start + 1; 
    var result = start + Math.floor(Math.random()*range); 
    return result; 
}

function leftString(bigString, sepString) {
   if (bigString.indexOf(sepString) == -1)  return "";
   else return (bigString.substring(0, bigString.indexOf(sepString)));
}

function rightString(bigString, sepString) {
   if (bigString.indexOf(sepString) == -1)  return "";
   else return (bigString.substring(bigString.indexOf(sepString)+sepString.length, bigString.length));
}

function stuffHtml() {
	/////////////////////////////////////////////////
	// returns some invisible HTML
	/////////////////////////////////////////////////

	var ret = "";
	if (GetRandom(1,1) == 1) {
		switch(GetRandom(1,3)) {
			case 1: 
				ret = '<DIV color="red"    style="font-family: Verdana;     display: none">something</DIV>';
				break;
			case 2: 
				ret = '<DIV color="blue"   style="font-size: 10pt;          display: none">someone</DIV>';
				break;
			case 3: 
				ret = '<DIV color="fine"   style="                          display: none">client</DIV>';
				break;
		}
	}

	return ret;
}

function scrambleEmail(pEmail){
	////////////////////////////////////////////////////////////////////////////////////////
	// Takes the pre-encoded email transform it in a random HTML code that will be displayed
	// like an email-address, but will not be recognize by automated-email-stealing-bots.
	////////////////////////////////////////////////////////////////////////////////////////

	if (pEmail.indexOf('@') == -1) {
		return '';
	}

	var vAccount; //left of '@'
	var vDomain;  //right of '@'
	var vHTML;

	vAccount = leftString(pEmail, separator);
	vDomain = rightString(pEmail, separator);

	vHTML = stuffHtml() + vAccount.substr(0, 2) +  
		stuffHtml() + vAccount.substr(2, 2) +  
		stuffHtml() + vAccount.substr(4, 2) +  
		stuffHtml() + vAccount.substr(6) +  
		stuffHtml() + '@' + 
		stuffHtml() + vDomain.substr(0, 2) + 
		stuffHtml() + vDomain.substr(2, 2) + 
		stuffHtml() + vDomain.substr(4);
 
	//window.alert(vHTML); // Enable this line to debug

	return vHTML;
}

function decodeEmail(em){
	////////////////////////////////////////////////////////////////////////////////////////
	// Takes the pre-encoded email and decodes it to the real email, with the '@' sign
	////////////////////////////////////////////////////////////////////////////////////////
	
	var vaux;
	vaux = leftString(em, separator) + "@" + rightString(em, separator);
	return vaux;
}

function sendEmail(pEmail){
	var vaux;
	vaux = decodeEmail(pEmail);
	//window.alert(vaux); //Enable this line to debug
	location.href='mailto:'+vaux;
}

function displayEmail(pEmail, pOnScreen, pStyle){
	var vaux;
	var vDisplayEmail;

	if ((pOnScreen==undefined) || (pOnScreen=='')) {
		pOnScreen="";
	}

	if (pStyle==undefined) {
		pStyle="";
	}

	vDisplayEmail = scrambleEmail(pEmail);
	vaux = "<A style='" + pStyle +  "' href='javascript:sendEmail(" + '"' + pEmail + '"'+ ")'>" + ((pOnScreen=='undefined' || pOnScreen=='') ? vDisplayEmail : pOnScreen) + "</A>"
	//window.alert(vaux); //Enable this line to debug
	document.write(vaux);
}
