/*
	General functions
	=================
*/

// IE or not.
var isIE = navigator.userAgent.match(/MSIE/) == "MSIE";

// "encrypted mailto:"
function level(_felhasznalo, _domen, _tema)
{
	if(_tema == undefined || _tema == null)
		window.top.location = "mailto:" + base64_decode(_felhasznalo) + "@" + base64_decode(_domen);
	else
		window.top.location = "mailto:" + base64_decode(_felhasznalo) + "@" + base64_decode(_domen) + "?subject=" + base64_decode(_tema);
}

/*
	Modules
	=======
*/
// Date selector
function updateDateTime(_id)
{
	fld = document.getElementById(_id);
	dt = document.getElementById(_id + "_date");
	tm = document.getElementById(_id + "_time");
	
	if(dt != null && tm != null && fld != null)
		fld.value = dt.value + " " + tm.value;
}

// Color selector
function set_color(_id, _value)
{
	t = document.getElementById(_id);
	s = document.getElementById(_id + "_sample");
	
	if(t != null && s != null)
	{
		t.value = _value;
		s.setAttribute("style", "border: solid 1px #000000; margin: 5px; width: 50px; height: 50px; background-color: "+_value+";");
	}
}

// Picture selector
function toggle_picture_browser_upload(_instance)
{
	i = document.getElementById(_instance + "_upload");
	
	if(i.className == "iframe-visible")
	{
		i.className = "iframe-hidden";
	}
	else
	{
		i.className = "iframe-visible";
	}
}


/*
	Links
	=====
*/
// Delayed jump on a link
function delayed_jump(_url, _delay)
{
	window.setTimeout("top.window.location=\"" + _url + "\";", _delay); 
}

// Confirmed jump for a link
function link_confirm(_url, _question)
{
	ans = confirm(_question);
	
	if(ans)
		location = _url;
}


// Trims spaces from edges :-)
function trim(s)
{
	var rs = "";
	
	for(i = 0; i < s.length; i++)
	{
		if(s.charAt(i) != " ")
			break;
	}

	for(i2 = s.length-1; i2 > 0; i2--)
	{
		if(s.charAt(i2) != " ")
			break;
	}
	i2 += 1;
	
	rs = s.substring(i, i2);
	
	return rs;
}


/*
	String functions
*/

// Adds slashes to ', ", \, 0, \n
function addslashes(str) 
{
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	str=str.replace(/\n/g,'\\n');
	return str;
}


// Removes added slahses
function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\n/g,'\n');
	return str;
}

// Gets a reference from the instance
function getReferenceOf(_instance)
{
	refof=eval(_instance);
	return refof;
}

/*
	Password Entropy
*/
// Logarithms based on 2
function log2(_arg)
{
	return Math.log(_arg) / Math.log(2);
}

// Statistical entropy
function entropy(_s)
{
	max = _s.length;
	chars = new Object();
	
	do
	{
		c = _s.charAt(0);
		if(chars.hasOwnProperty(c))
			chars[c]++;
		else
			chars[c] = 1;
		
		_s = _s.substr(1);
	}
	while(_s != "");

	e = 0;
	for(var c in chars)
	{
		r = chars[c] / max;
		e += r * log2(r);
	}
	
	e = -1 * e;
	
	return e;
}

// Password entropy
function password_entropy(_pass)
{
	return Math.round(100*entropy(_pass)*_pass.length/7)/100;
}

//	URL encode / decode
function url_encode(string)
{
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return escape(utftext);
}

function url_decode(string2)
{
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	utftext = unescape(string2);
	
	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}


// Base64
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function base64_encode(input)
{
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	
	do
	{
	  chr1 = input.charCodeAt(i++);
	  chr2 = input.charCodeAt(i++);
	  chr3 = input.charCodeAt(i++);
	
	  enc1 = chr1 >> 2;
	  enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	  enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	  enc4 = chr3 & 63;
	
	  if(isNaN(chr2))
	  {
	     enc3 = enc4 = 64;
	  }
	  else if(isNaN(chr3))
	  {
	     enc4 = 64;
	  }
	
	  output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
	}
	while(i < input.length);
	
	return output;
}

function base64_decode(input)
{
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	
	do
	{
	  enc1 = keyStr.indexOf(input.charAt(i++));
	  enc2 = keyStr.indexOf(input.charAt(i++));
	  enc3 = keyStr.indexOf(input.charAt(i++));
	  enc4 = keyStr.indexOf(input.charAt(i++));
	
	  chr1 = (enc1 << 2) | (enc2 >> 4);
	  chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	  chr3 = ((enc3 & 3) << 6) | enc4;
	
	  output = output + String.fromCharCode(chr1);
	
	  if(enc3 != 64)
	  {
	     output = output + String.fromCharCode(chr2);
	  }
	  if(enc4 != 64)
	  {
	     output = output + String.fromCharCode(chr3);
	  }
	}
	while(i < input.length);
	
	return output;
}

