//-----------------------------------------------------------//
//     Iminet stílusú biztonságos e-mail cím generálása      //         
//-----------------------------------------------------------//

function safemail(name, domain, beszopato, tld, display) 
{
	// Ehhez tartozik egy PHP-s függvény is!
	domain+=tld;
	displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
	document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
}


//-----------------------------------------------------------//
//                     Base64-style link functions           //
//-----------------------------------------------------------//

// Create link to Base64 address
function safelink(cEmailAddress, cText, cProtocol, cTitle)
{
	var cEmailAddress = b64_Decode(cEmailAddress);
  	cTitle = (cTitle ? ' title="' + cTitle + '"' : '');
  	cProtocol = (cProtocol ? cProtocol : 'mailto:');
  	if(!cText) cText = cEmailAddress;
	document.write('<a href="' + cProtocol + cEmailAddress + '"' + cTitle + '>' + cText + '</a>');
}

// Base64 decode function for the safe e-mail links

var b64_ValueChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var b64_CharValues = [];
for(var i = 0; i <= b64_ValueChars.length; i++) b64_CharValues[b64_ValueChars.charCodeAt(i)] = i;

// Base64 and UTF8 decode
function b64_Decode(data)
{
	  data = data.replace(/[^A-Za-z0-9\+\/=]/g, '');
	  while((data.length % 4) != 0) data += '=';
	
	  var ch1, ch2, ch3, ch4, byte1, byte2, byte3;
	  var charIdx = 0, decoded = [], byteIdx = 0;
	  while(charIdx < data.length) 
	  {
		ch1 = b64_CharValues[data.charCodeAt(charIdx++)];
		ch2 = b64_CharValues[data.charCodeAt(charIdx++)];
		ch3 = b64_CharValues[data.charCodeAt(charIdx++)];
		ch4 = b64_CharValues[data.charCodeAt(charIdx++)];
		byte1 = (ch1 << 2) | (ch2 >> 4);
		byte2 = ((ch2 & 15) << 4) | (ch3 >> 2);
		byte3 = ((ch3 & 3) << 6) | ch4;
		decoded[byteIdx++] = byte1;
		if(ch3 != 64) decoded[byteIdx++] = byte2;
		if(ch4 != 64) decoded[byteIdx++] = byte3;
	}

	byteIdx = 0;
	var charCode, seqLen, retStr = '';
	while(byteIdx < decoded.length) 
	{
		charCode = decoded[byteIdx++]; seqLen = 1;
		if(charCode >= 192) seqLen++;
		if(charCode >= 224) seqLen++;
		if(charCode >= 240) seqLen++;
		if(seqLen > 1) 
		{
			charCode = (charCode & (255 >> (seqLen + 1)));
			while(seqLen-- > 1) charCode = ((charCode << 6) | (decoded[byteIdx++] & 63));
		}
		retStr += String.fromCharCode(charCode);
	}
	return(retStr);
}

