/**
* phpBB3 forum functions
*/

/**
* Window popup
*/
function popup(url, width, height, name)
{
	if (!name)
	{
		name = '_popup';
	}

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes, width=' + width);
	return false;
}

/**
* Jump to page
*/
function jumpto()
{
	var page = prompt(jump_page, on_page);

	if (page !== null && !isNaN(page) && page > 0)
	{
		document.location.href = base_url.replace(/&amp;/g, '&') + '&start=' + ((page - 1) * per_page);
	}
}

/**
* Mark/unmark checklist
* id = ID of parent container, name = name prefix, state = state [true/false]
*/
function marklist(id, name, state)
{
	var parent = document.getElementById(id);
	if (!parent)
	{
		eval('parent = document.' + id);
	}

	if (!parent)
	{
		return;
	}

	var rb = parent.getElementsByTagName('input');

	for (var r = 0; r < rb.length; r++)
	{
		if (rb[r].name.substr(0, name.length) == name)
		{
			rb[r].checked = state;
		}
	}
}

/**
* Resize viewable area for attached image or topic review panel (possibly others to come)
* e = element
*/
function viewableArea(e, itself)
{
	if (!e) return;
	if (!itself)
	{
		e = e.parentNode;
	}

	if (!e.vaHeight)
	{
		// Store viewable area height before changing style to auto
		e.vaHeight = e.offsetHeight;
		e.vaMaxHeight = e.style.maxHeight;
		e.style.height = 'auto';
		e.style.maxHeight = 'none';
		e.style.overflow = 'visible';
	}
	else
	{
		// Restore viewable area height to the default
		e.style.height = e.vaHeight + 'px';
		e.style.overflow = 'auto';
		e.style.maxHeight = e.vaMaxHeight;
		e.vaHeight = false;
	}
}

/**
* Set display of page element
* s[-1,0,1] = hide,toggle display,show
*/
function dE(n, s)
{
	var e = document.getElementById(n);

	if (!s)
	{
		s = (e.style.display == '' || e.style.display == 'block') ? -1 : 1;
	}
	e.style.display = (s == 1) ? 'block' : 'none';
}

/**
* Alternate display of subPanels
*/
function subPanels(p)
{
	var i, e, t;

	if (typeof(p) == 'string')
	{
		show_panel = p;
	}

	for (i = 0; i < panels.length; i++)
	{
		e = document.getElementById(panels[i]);
		t = document.getElementById(panels[i] + '-tab');

		if (e)
		{
			if (panels[i] == show_panel)
			{
				e.style.display = 'block';
				if (t)
				{
					t.className = 'activetab';
				}
			}
			else
			{
				e.style.display = 'none';
				if (t)
				{
					t.className = '';
				}
			}
		}
	}
}

/**
* Call print preview
*/
function printPage()
{
	if (is_ie)
	{
		printPreview();
	}
	else
	{
		window.print();
	}
}

/**
* Show/hide groups of blocks
* c = CSS style name
* e = checkbox element
* t = toggle dispay state (used to show 'grip-show' image in the profile block when hiding the profiles)
*/
function displayBlocks(c, e, t)
{
	var s = (e.checked == true) ?  1 : -1;

	if (t)
	{
		s *= -1;
	}

	var divs = document.getElementsByTagName("DIV");

	for (var d = 0; d < divs.length; d++)
	{
		if (divs[d].className.indexOf(c) == 0)
		{
			divs[d].style.display = (s == 1) ? 'none' : 'block';
		}
	}
}

function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}

/**
* Play quicktime file by determining it's width/height
* from the displayed rectangle area
*/
function play_qt_file(obj)
{
	var rectangle = obj.GetRectangle();

	if (rectangle)
	{
		rectangle = rectangle.split(',');
		var x1 = parseInt(rectangle[0]);
		var x2 = parseInt(rectangle[2]);
		var y1 = parseInt(rectangle[1]);
		var y2 = parseInt(rectangle[3]);

		var width = (x1 < 0) ? (x1 * -1) + x2 : x2 - x1;
		var height = (y1 < 0) ? (y1 * -1) + y2 : y2 - y1;
	}
	else
	{
		var width = 200;
		var height = 0;
	}

	obj.width = width;
	obj.height = height + 16;

	obj.SetControllerVisible(true);
	obj.Play();
}

/**
* @return Eine Initialisierte XmlHttpRequest Instanz
*/
function xml_http_request() {
	var xmlHttp = null;

	// Mozilla, Opera, Safari sowie Internet Explorer 7
	if (typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}

	if (!xmlHttp) {
		// Internet Explorer 6 und älter
		try {
			xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlHttp  = null;
			}
		}
	}

	return xmlHttp;
}


/**
* Sendet eine Bewertung ab und gibt die Antwort der Forensoftware dem Benutzer
* bekannt. Jegliche Ueberpruefung ob das Rating gerechtfertigt ist wird von der
* Forensoftware durchgefuehrt
*/
function commit_rating(action, post_id, rating) {
	var MAXIMUM_RATING = 5;
	var MINIMUM_RATING = -2;

	var xmlHttp = xml_http_request();
	if (!xmlHttp) {
		return false;
	}

	var seperator = -1 == action.indexOf('?') ? '?' : '&';
	xmlHttp.open('GET', action + seperator +'p='+ post_id +'&r='+ rating, true);
	xmlHttp.onreadystatechange = function() {
		if (4 == xmlHttp.readyState) {
			var response = xmlHttp.responseText.split("\n");
			var thanks = document.getElementById('thanks_'+ post_id);

			switch (response[0]) {

				/* Fehlermeldung ausgeben
				 */
				case 'ERROR':
					thanks.style.color = 'red';
					thanks.innerHTML = response[1];
					break;

				case 'SUCCESS':
					thanks.style.color = 'green';
					thanks.innerHTML = response[1];

					/* Bewertungsbalken aktualisieren
					 */
					var rating = response[3];
					var positive = document.getElementById('rater_positive_'+ post_id);
					var neutral = document.getElementById('rater_neutral_'+ post_id);
					var negative = document.getElementById('rater_negative_'+ post_id);
					var lib_positive = document.getElementById('rater_positive_lib_'+ post_id);
					var lib_neutral = document.getElementById('rater_neutral_lib_'+ post_id);
					var lib_negative = document.getElementById('rater_negative_lib_'+ post_id);
					var rating_display = document.getElementById('rating_display_'+ post_id +'_background');

					var total_votes = document.getElementById('post_'+ post_id +'_total_votes');
					var total_rating = document.getElementById('post_'+ post_id +'_total_rating');
					total_votes.innerHTML = response[2];
					total_rating.innerHTML = response[4];

					/* Bewertung befindet sich unterhalb von 0 (negativ)
					 */
					if (rating < 0) {
						lib_neutral.style.width = '0px';
						lib_positive.style.width = '0px';

						var width = negative.style.width.replace(/px/, '');
						lib_width = (width / MINIMUM_RATING) * rating;
						lib_negative.style.width = lib_width +'px';
						lib_negative.style.left = (width - lib_width) +'px';

						rating_display.style.width = lib_width +'px';
						rating_display.className = 'rating-display-background-negative';
					}

					/* Bewertung befindet sich zwischen 0 und 1 (neutral)
					 */
					if ((rating >= 0) && (rating < 1)) {
						lib_negative.style.left = negative.style.width;
						lib_negative.style.width = '0px';
						lib_positive.style.width = '0px';

						lib_neutral.style.width = neutral.style.width;
						rating_display.style.width = neutral.style.width;
						rating_display.className = 'rating-display-background-neutral';
					}

					/* Bewertung ist groesser 1
					 */
					if (rating >= 1) {

						/* Werte von 0 bis 1 sind als neutral zusammengefasst
						 */

						lib_negative.style.left = negative.style.width;
						lib_negative.style.width = '0px';
						lib_neutral.style.width = '0px';

						var width = positive.style.width.replace(/px/, '');
						width = (width / MAXIMUM_RATING) * rating;
						lib_positive.style.width = width +'px';
						rating_display.style.width = width +'px';
						rating_display.className = 'rating-display-background-positive';
					}

					break;
			}
		}
	};
	xmlHttp.send(null);

	return true;
}



/**
* Blendet die Kommentierfunktion ein fuer ein bestimmtes Posting ein
*
* @param post_id ID des Postings
*/
function show_comment(post_id) {
	document.getElementById('post_'+ post_id +'_comments').style.display = 'block';
	document.getElementById('post_'+ post_id +'_new_comment').style.display = 'inline';

	/* Link nicht verfolgen
	 */
	return false;
}

/**
* Sendet ein Kommentar ab
*
* @param action Seite an welche das Kommentar gesendet werden soll
* @param post_id ID des Postings
* @param comment Kommentar
*/
function send_comment(action, post_id, comment, show_username) {
	var xmlHttp = xml_http_request();
	if (!xmlHttp) {
		return false;
	}

	var seperator = -1 == action.indexOf('?') ? '?' : '&';
	var arguments = 'c='+ encodeURI(comment);
	xmlHttp.open('POST', action + seperator +'p='+ post_id +'&a='+ (show_username ? '0' : '1'), true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	xmlHttp.setRequestHeader('Content-Length', arguments.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.onreadystatechange = function() {
		if (4 == xmlHttp.readyState) {
			var response = xmlHttp.responseText.split("\n");
			var comments = document.getElementById('post_'+ post_id +'_comments_text');

			switch (response[0]) {
				case 'ERROR':
					comments.innerHTML += '<div><strong style="color: red">'+ response[1] +'</strong></div>';
					break;

				case 'SUCCESS':
					deltaHTML = '<div class="comment_message">'+"\n";
					deltaHTML += '<span class="comment_message_author">'+ response[1] +'</span>:'+"\n";
					deltaHTML += '<span class="comment_message_message">'+ response[2] +'</span>'+"\n";
					deltaHTML += '</div>'+"\n";

					comments.innerHTML += deltaHTML;
					break;
			}

			document.getElementById('post_'+ post_id +'_new_comment').style.display = 'none';
		}
	};
	xmlHttp.send(arguments);
}


function swap_password_input(that) {
}