/* <![CDATA[ */

/***********************************************
* Javascript © Felix Mau (www.mx-forums.de)
* This notice must stay intact for legal use
***********************************************/

// Browser compatibility
function getElementById(ElementId) {
	return (document.documentElement) ? document.getElementById(ElementId) : document.all[ElementId];
}

// Insert BB-Codes
function insert(aTag, eTag, obj) {
	var input = getElementById(obj);
	var scrollTop = (typeof(input.scrollTop) == 'number') ? input.scrollTop : -1;
	input.focus();
	if (typeof document.selection != 'undefined')
	{
		var range = document.selection.createRange();
		var insText = range.text;
    	range.text = aTag + insText + eTag;
    	range = document.selection.createRange();
    	(insText.length == 0) ? range.move('character', -eTag.length) : range.moveStart('character', aTag.length + insText.length + eTag.length);      
    	range.select();
	}
	else
	{
		var start = input.selectionStart;
    	var end = input.selectionEnd;
    	var insText = input.value.substring(start, end);
    	input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    	var pos;
    	(insText.length == 0) ? pos = start + aTag.length : pos = start + aTag.length + insText.length + eTag.length;
    	input.selectionStart = pos;
    	input.selectionEnd = pos;
	}
	if (scrollTop >= 0)
	{
		input.scrollTop = scrollTop;
	}
}

// Insert List
function BBClist(obj) {
	insert('[list]', '[/list]', obj);
	var count = 1;
	do {
		var enterLIST = prompt('Text eingeben:', ''+count+++'. Punkt');
		(enterLIST != null) ? insert('[*]'+enterLIST, '', obj) : '';
	}
	while (enterLIST != null);
}

// Insert Image
function BBCimg(obj) {
	var enterIMG = prompt('Internetadresse für das Bild eigeben:', 'http://');
	(enterIMG != 'http://') ? insert('[img]'+enterIMG+'[/img]', '', obj) : '';
}

// Insert URL
function BBCurl(obj) {
	var enterURL = prompt('Internetadresse eingeben:', 'http://');
	var enterTITLE = prompt('Namen für die Internetadresse eingeben:', 'Beschreibung');
	(enterURL != 'http://' && enterTITLE != 'Beschreibung') ? insert('[url='+enterURL+']'+enterTITLE+'[/url]', '', obj) : '';

}

// Show without scroll
function show(obj) {
	getElementById(obj).style.display = 'block';
}

// Hide without scroll
function hide(obj) {
	getElementById(obj).style.display = 'none';
}

// Determine whether we should hide or show the object
function show_hide_content(object, object_height) {
	interval = (parseInt(document.getElementById(object).style.height) >= object_height) ? window.setInterval("hide_content('" + object + "')", 1) : window.setInterval("show_content('" + object + "', '" + object_height + "')", 1);
}

// Show object by scrolling
function show_content(object, object_height) {   
	new_object_height = parseInt(document.getElementById(object).style.height);
	(new_object_height < object_height) ? document.getElementById(object).style.height = new_object_height + 10 + 'px' : window.clearInterval(interval);

}

// Hide object by scrolling
function hide_content(object) {   
	object_height = parseInt(document.getElementById(object).style.height);
	(object_height > 0) ? document.getElementById(object).style.height = object_height - 10 + 'px' : window.clearInterval(interval);
} 

// Show and hide the code-blocks
function showcode(linkobj) {
	blockobj = linkobj.parentNode.parentNode;
	if (blockobj.style.height > '50px' || blockobj.getElementsByTagName('div')[0].offsetHeight <= 30)
	{
		blockobj.style.height = '50px';
		linkobj.innerHTML = 'Erweitern';
	}
	else
	{
		blockobj.style.height = 'auto';
		linkobj.innerHTML = 'Verkleinern';
	}
}

// Used for downloading the codes
function download_code(linkobj) {
	form = linkobj.parentNode.parentNode.parentNode;
	code_content = form.getElementsByTagName('div')[0].innerHTML;
	code_content = code_content.split('<br />').join(''); // Strip tags
	code_content = code_content.split('<br>').join(''); // Strip tags
	form.getElementsByTagName('input')[0].value = code_content;
	form.submit();
}

// Check comments
function check_fields(url, value)	{
	var username = getElementById('username');
	var comment = getElementById('comment_content');
				
	if (username.value == '')
	{
		alert('Bitte gebe einen gültigen Namen an!');
		username.focus();
	}
	else if (comment.value == '')
	{
		alert('Bitte schreibe einen gültigen Kommentar!');
		comment.focus();
	}
	else
	{
		sendRequest(url, 'ajax_content', 'add_comment', value, username.value, comment.value);
	}
	return false;
}

// Send AJAX-Request and get result ..
function sendRequest(url, obj, method) {
 	if (obj = getElementById(obj))
	{
		// Determine whether AJAX is available
		request = (window.XMLHttpRequest) ? new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') :  alert('Fehler: XMLHTTP-Instanz kann nicht erzeugt werden!'));

	 	// Show object
	 	obj.style.display = 'block';

		// Set timeout after 5 seconds
		timeout = window.setTimeout('request.abort(); alert(\'Fehler: Die Anfrage dauerte zu lange!\');', 5000);
		
		// Get parameter
		parameters = '';
		for (var i = 3; i <= sendRequest.arguments.length-1; i++)
		{
			parameters += '&ajax' + i + '=' + encodeURIComponent(sendRequest.arguments[i]);
		}
		
		request.open('post', url, true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('method=' + method + parameters);
		request.onreadystatechange = function() {
			if (request.readyState == 4 && request.status != 0)
			{
			 	// Reset timeout and check result
			 	window.clearTimeout(timeout);
			 	(request.status == 200) ? obj.innerHTML = request.responseText : alert('Fehler: HTTP-Statuscode ' + request.status);
			}
		};
	}
	return false;
}
/* ]]> */