function addText(elname, wrap1, wrap2) {
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		document.forms['inputform'].elements[elname].focus();
		var sel = document.selection.createRange();
		sel.text = wrap1 + str + wrap2;
		return;
	} else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var txtarea = document.forms['inputform'].elements[elname];
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		//if (selEnd == 1 || selEnd == 2)
		//selEnd = selLength;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(elname, wrap1 + wrap2);
	}
}

function insertText(elname, what) {
	if (document.forms['inputform'].elements[elname].createTextRange) {
		document.forms['inputform'].elements[elname].focus();
		document.selection.createRange().duplicate().text = what;
	} else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
		var tarea = document.forms['inputform'].elements[elname];
		var selEnd = tarea.selectionEnd;
		var txtLen = tarea.value.length;
		var txtbefore = tarea.value.substring(0,selEnd);
		var txtafter =  tarea.value.substring(selEnd, txtLen);
		var oldScrollTop = tarea.scrollTop;
		tarea.value = txtbefore + what + txtafter;
		tarea.selectionStart = txtbefore.length + what.length;
		tarea.selectionEnd = txtbefore.length + what.length;
		tarea.scrollTop = oldScrollTop;
		tarea.focus();
	} else {
		document.forms['inputform'].elements[elname].value += what;
		document.forms['inputform'].elements[elname].focus();
	}
}

function getCookie(cookie_name) {

    var dc = document.cookie;
    var prefix = cookie_name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;

    var end = document.cookie.indexOf(";", begin);

    if (end == -1)
        end = dc.length;

    return unescape(dc.substring(begin + prefix.length, end));

}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// 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;
	}
}

function forum_fill_post_form() {
	if ($('forum_username'))
	{
		username = Url.decode(getCookie('anonym_username'));
		username = username.replace(/\+/g, ' ');
		if (username != 'null')
			$('forum_username').value = username;
	}
}


function forum_create_quote(post_id)
{
	curr_post_id = post_id;
	new Ajax(site_url+'forum_action.php?action=get_post&id='+post_id, { method: 'get', onComplete: forum_insert_quote }).request();
}


function forum_insert_quote(text)
{
	user_html = '[b]'+$('post_user_'+curr_post_id).innerHTML+_lang_has_written+'[/b]\n';
	if ($('message').value != '')
		$('message').value += '\n';
	$('message').value += '[quote]'+user_html+text+'[/quote]\n';
	location.href='#form_post';
	$('message').focus();
}
function forum_create_reply(post_id)
{
	$('preview').style.display='none';
	if (curr_post_top == '')
		curr_post_top = getStyle($('form_post'), 'top');
	$('form_post').style.top = (fcurY+40)+"px";
	//$('form_post').style.top = fcurY+"px";
	$('replyto_id').value = post_id;
	$('remove_reply').style.display = 'block';
	$('message').focus();
}
function forum_remove_reply()
{
	$('remove_reply').style.display = 'none';
	$('form_post').style.top = curr_post_top;
	$('replyto_id').value = 0;
}
function forum_get_post(post_id, requestor)
{
	curr_post_id = requestor;
	new Ajax(site_url+'forum_action.php?action=get_post&format=1&id='+post_id, { method: 'get', onComplete: forum_show_post }).request();
}
function forum_show_post(text)
{
	$('preview_content').innerHTML = text;
	$('preview').style.top=fcurY+'px';
	$('preview').style.display='block';
}
function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
function forum_preview_move()
{
	if (preview_move)
		$('preview').style.top = (fcurY-20)+"px";
}


/**
 * Check if user is logged in and if not show login box
 * @author Anze Robida
 */
function forum_check_user_loggedin(redirect,type)
{
	
	var username = Cookie.get('user');

	if (username == false) {
		login_show();
	} else {
		if (type == 'get')
			window.location.href = redirect;
		else
			$(redirect).submit();
	}
		
}

