createRequest();

function createRequest()
{
	try
	{
		request = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
				request = false;
			}
		}
	}
	if (!request)
	alert("Error initializing XMLHttpRequest!");
}

function vote(rate, id)
{
	var url = 'vote,'+escape(id)+','+escape(rate)+'.html';
	request.open("GET", url, true);
	request.onreadystatechange = someone_voted;
	request.send(null);
}

function someone_voted()
{
	if ((request.readyState == 4) && (request.status == 200))
	{
		var x = request.responseText.split("|");
		var info = "Nietypowe zdarzenie. W przypadku powtarzających się problemów prosimy o kontakt z administratorem serwisu.";
		if (x[0] == "ok") info = x[1];
		if (x[0] == "not_logged") info = x[1];
		if (x[0] == "already_voted") info = x[1];
		if (x[0] == "hacked_attempt") info = x[1];
		if (x[0] == "removed") info = x[1];
		if (x[0] == "not_good") info = x[1];
		document.getElementById('info').innerHTML = info;
	}
}

function vote2(rate, id)
{
	var url = 'vote,'+escape(id)+','+escape(rate)+'.html';
	request.open("GET", url, true);
	request.onreadystatechange = someone_voted2;
	request.send(null);
}

function someone_voted2()
{
	if ((request.readyState == 4) && (request.status == 200))
	{
		var x = request.responseText.split("|");
		var info = "Nietypowe zdarzenie. W przypadku powtarzających się problemów prosimy o kontakt z administratorem serwisu.";
		if (x[0] == "ok") info = x[1];
		if (x[0] == "not_logged") info = x[1];
		if (x[0] == "already_voted") info = x[1];
		if (x[0] == "hacked_attempt") info = x[1];
		if (x[0] == "removed") info = x[1];
		if (x[0] == "not_good") info = x[1];
		alert(info);
	}
}

function remove_favorite(id)
{
	var url = 'usuwanie_ulubionych.html';
	var postData = "id=" + id;

	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-2");
	request.setRequestHeader("Content-length", postData.length);
	request.setRequestHeader("Connection", "close");
	request.send(postData);
}

function favorite(id)
{
	var url = 'ulubione.html';
	var postData = "id=" + id;

	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-2");
	request.setRequestHeader("Content-length", postData.length);
	request.setRequestHeader("Connection", "close");

	request.onreadystatechange = add_favorite;
	request.send(postData);
}

function add_favorite()
{
	if (!(request.readyState == 4) || !(request.status == 200))
	{
		return false;
	}

	var x = request.responseText.split("|");
	document.getElementById('info').innerHTML = x[1];
	if ('not_logged' == x[0])
	{
		document.getElementById('info').innerHTML = x[1];
	} else if ('hacked_attempt' == x[0])
	{
		alert(x[1]);
	} else if ('already_added' == x[0])
	{
		document.getElementById('info').innerHTML = x[1];
	} else if ('ok' == x[0])
	{
		document.getElementById('info').innerHTML = x[1];
	} else if ('not_ok' == x[0])
	{
		document.getElementById('info').innerHTML = x[1];
	}
}


function favorite2(id)
{
	var url = 'ulubione.html';
	var postData = "id=" + id;

	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-2");
	request.setRequestHeader("Content-length", postData.length);
	request.setRequestHeader("Connection", "close");

	request.onreadystatechange = add_favorite2;
	request.send(postData);
}

function add_favorite2()
{
	if (!(request.readyState == 4) || !(request.status == 200))
	{
		return false;
	}

	var x = request.responseText.split("|");

	if ('not_logged' == x[0])
	{
		alert(x[1]);
	} else if ('hacked_attempt' == x[0])
	{
		alert(x[1]);
	} else if ('already_added' == x[0])
	{
		alert(x[1]);
	} else if ('ok' == x[0])
	{
		alert(x[1]);
	} else if ('not_ok' == x[0])
	{
		alert(x[1]);
	}
}

function report_spam(id)
{
	var url = 'comment.html';
	var postData = "spam=1&id=" + id;

	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-2");
	request.setRequestHeader("Content-length", postData.length);
	request.setRequestHeader("Connection", "close");

	request.onreadystatechange = function()
	{
		if (!(request.readyState == 4) || !(request.status == 200))
		{
			return false;
		}
		var x = request.responseText.split("|");

		if ('not_ok' == x[0])
		{
			alert(x[1]);
		}
		else if ('ok' == x[0])
		{
			alert(x[1]);
		}
		else if ('not_logged' == x[0])
		{
			alert(x[1]);
		}
		else if ('already_report' == x[0])
		{
			alert(x[1]);
		}
	};
	request.send(postData);
}
function add_comment(comment, id)
{
	var url = 'comment.html';
	var postData = "comment=" + encodeURIComponent(comment) + "&id=" + id;
	document.getElementById('comment').disabled = 'disabled';
	document.getElementById('submit_comment').value='Trwa dodawanie...';
	document.getElementById('new_comment').innerHTML = '<p> <img src="http://www.jeja.pl/img/ajax-loader.gif" /> Trwa dodawanie komentarza... </p>';
	document.getElementById('comments_form').style.display = 'none';
	document.getElementById('link_zrezygnuj').style.display = 'none';
	request.open("POST", url, true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-2");
	request.setRequestHeader("Content-length", postData.length);
	request.setRequestHeader("Connection", "close");
	request.onreadystatechange = function() 
	{
		if ((request.readyState == 4) && (request.status == 200))
		{
			var x = request.responseText.split("|");
			if (x[0] == 'ok' || x[0] == 'ok2')
			{
				var num_comments = document.getElementById('num_comments').innerHTML;
				document.getElementById('comments_form').style.display = 'none';
				document.getElementById('comment').value = '';
				document.getElementById('link_napisz').style.display = '';
				document.getElementById('link_zrezygnuj').style.display = 'none';
				document.getElementById('num_comments').innerHTML = ++num_comments;
				//document.getElementById('comments').innerHTML = '';
			}
			if (x[0] == 'not_logged') {}
			else if (x[0] == 'url')
			{
				document.getElementById('link_napisz').style.display = '';
			}
			else if (x[0] == 'hacked_attempt') {}
			else if (x[0] == 'banned') {}
			else if (x[0] == 'not_good')
			{
				document.getElementById('link_napisz').style.display = '';
			}
			else if (x[0] == 'too_long')
			{
				document.getElementById('link_napisz').style.display = '';
			}
			else if (x[0] == 'too_fast')
			{
				document.getElementById('link_napisz').style.display = '';
			}
			document.getElementById('new_comment').innerHTML = x[1];
			document.getElementById('comment').disabled = false;
			document.getElementById('submit_comment').value = 'Dodaj komentarz';
			if (x[0] == 'ok')
			{
				get_comments(id, 0);
			}
		};
	};
	request.send(postData);
}

function show_comments_form()
{
	document.getElementById('comments_form').style.display = '';
	document.getElementById('link_napisz').style.display = 'none';
	document.getElementById('link_zrezygnuj').style.display = '';
	document.getElementById('new_comment').innerHTML = '';
}

function hid_comments_form()
{
	document.getElementById('comments_form').style.display = 'none';
	document.getElementById('link_napisz').style.display = '';
	document.getElementById('link_zrezygnuj').style.display = 'none';
}

function get_comments(id, page)
{
	document.getElementById('new_comment').innerHTML = '<p> <img src="http://www.jeja.pl/img/ajax-loader.gif" /> Trwa pobieranie nowych komentarzy... </p>';
	var url = 'comment,' + id + ',' + page + '.html';
	request.open("GET", url, true);
	request.onreadystatechange = display_comments;
	request.send(null);
}

function display_comments()
{
	if (!(request.readyState == 4) || !(request.status == 200))
	{
		return false;
	}

	var x = request.responseText.split('<-[.]->');

	if ('none' == x[0])
	{
		document.getElementById('comments').innerHTML = 'Brak komentarzy dla tego materiału';
	}
	else if ('hacked_attempt' == x[0])
	{
		alert('Please, don\'t try it again');
	}
	else if ('ok' == x[0])
	{
		var comments = '';
		for( var i = 1; i < x.length; i++ )
		{
			comments = comments + x[i];
		}
		document.getElementById('comments').innerHTML = comments;
	}
	document.getElementById('new_comment').innerHTML = '';
}

function share(id)
{
	window.open('/share,' + id + '.html', 'Wyslij', 'width=580,height=480,resizable=yes,scrollbars=yes,status=0');
}

function blog(id)
{
	window.open('/blog,' + id + '.html', 'Na bloga', 'width=580,height=480,resizable=yes,scrollbars=yes,status=0');
}

function do_nothing()
{

}

function rozmiar(action, width, height)
{
	var swf = document.getElementById('swf');
	var obj = document.getElementById('game');
	if(swf) {
		var new_height = swf.height;
		var new_width  = swf.width;
	} else {
		var new_height = obj.height;
		var new_width  = obj.width;
	}
	if(1 == action)
	{
		new_height = parseInt(new_height * 0.9);
		new_width  = parseInt(new_width * 0.9);
	}
	else if(2 == action)
	{
		new_height = parseInt(new_height * 1.1);
		new_width  = parseInt(new_width * 1.1);
	}
	else if(3 == action)
	{
		new_height = height;
		new_width  = width;
	}
	if(swf) {
		swf.height = new_height;
		swf.width  = new_width;
	}
	obj.height = new_height;
	obj.width  = new_width;
}
function maxlength(obj, mlength)
{
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}

function new_sec_code(url)
{
	document.getElementById('sec_code').src = url + '?' + (new Date()).getTime();
}

//if (self!=top) top.location=self.document.location;

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			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 +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._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);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (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 utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		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;
	}

}