function storeCaret(contentArea)
{
	if (contentArea.createTextRange)
	{
		contentArea.caretPos = document.selection.createRange().duplicate();
	}
}

function insertText(fmlText)
{
	var contentArea = document.editPostForm.content;
	fmlText = fmlText.replace(/<br>/g, "\n");

	if (contentArea.createTextRange && contentArea.caretPos)
	{
		var caretPos = contentArea.caretPos;
		caretPos.text = fmlText;
	}
	else
	{
		contentArea.value += fmlText;
	}
}

function insertTextAtEnd(fmlText)
{
	var contentArea = document.editPostForm.content;
	fmlText = fmlText.replace(/<br>/g, "\n");
	fmlText = fmlText.replace(/&#39;/g, "'");

	contentArea.value += fmlText;
}

function formatText(format)
{
	var formattedText;
	var formatLabel = new Array();
	formatLabel['bold'] = labels['FORUM_info_input_bold_text']; //'félkövér';
	formatLabel['italic'] = labels['FORUM_info_input_italic_text']; //'dõlt';
	formatLabel['underline'] = labels['FORUM_info_input_underlined_text']; //'aláhúzott';

	if (formattedText = prompt('' + formatLabel[format] + ':', ''))
	{
		insertText('[' + format + ']' + formattedText + '[/' + format + ']');
	}
}

function insertImage_OLD()
{
	var imageSrc;

	if (imageSrc = prompt(labels['FORUM_info_input_image_url'] + ':', 'http://'))
	{
		insertText('[image src="' + imageSrc + '"]');
	}
}

function insertImage()
{

	document.getElementById('insertImageDiv').style.display = 'block';
	document.getElementById('insertImageFrame').setAttribute('src', '/forum/upload');
}

function insertImageCallback(imageSrc)
{
	if (imageSrc)
	{
		insertText('[image src="' + imageSrc + '"]');
	}
	document.getElementById('insertImageDiv').style.display = 'none';
	
}

function preview(textArea)
{
	contentText = textArea.value;
	oPreview = document.getElementById('previewScreen');

	contentText = fml2html(contentText);

	oPreview.innerHTML = contentText;
}

function insertEmoticon(icon)
{
	insertText(':' + icon + ':');
}

function fml2html(fmlText)
{
	htmlText = fmlText.replace(/<[^>]*>/g, '');
	htmlText = htmlText.replace(/</g, '&lt;');
	htmlText = htmlText.replace(/>/g, '&gt;');
	htmlText = htmlText.replace(/[^@\s]+@([^@.\s]+\.)+[^@.\s]{2,3}/gi, "<a href=\"mailto: $&\">$&</a>");
	htmlText = htmlText.replace(/(^|[\s])(http[s]?:\/\/|www\.)([\w]+\.[^\s\[]+)/gi, '$1<a href="$2$3" target="_blank">$2$3</a>');
	htmlText = htmlText.replace(/\n/g, '<br>');
	htmlText = htmlText.replace(/\[(\/?b)old\]/gi, "<$1>");
	htmlText = htmlText.replace(/\[(\/?i)talic\]/gi, "<$1>");
	htmlText = htmlText.replace(/\[(\/?u)nderline\]/gi, "<$1>");
	htmlText = htmlText.replace(/\[image src=\"([^\"]*)\"\]/gi, '<img src=\"$1\" border="0">');
	htmlText = htmlText.replace(/\[quote\]/gi, "<div class=\"quote\">");
	htmlText = htmlText.replace(/\[\/quote\]/gi, "</div>");
	htmlText = htmlText.replace(/:([a-zA-Z]*):/g, checkEmoticons);
	htmlText = htmlText.replace(/href="(www[^"]*)"/gi, 'href="http://$1"');
	htmlText = htmlText.replace(/[^\s]{50,}/g, checkLongText);
	htmlText = htmlText.replace(/[ ]{2}/g, '&nbsp;&nbsp;');
	htmlText = htmlText.replace(/((&nbsp;){50})/g, '$1 ');

	return htmlText;
}

function checkEmoticons(full, emo)
{
	for (i = 0; i < emoticons.length; i++)
	{
		if (emo == emoticons[i])
		{
			return '<img src="/dl/images/forum/emoticons/' + emo + '.gif" border="0" name="emo">';
		}
	}
	return full;
}

function checkLongText(full)
{
	if (full.indexOf('"http') != -1)
	{
		return full;
	}

	return full.replace(/([^\s]{50})/g, '$1 ');
}

function checkEmail()
{
	var thisForm = document.sendEmailForm;

	if ((thisForm.toName.value == '') || (thisForm.toName.value == ' '))
	{
		alert(labels['FORUM_error_recipient_name_empty']);
		return;
	}

	if ((thisForm.toEmail.value == '') || (thisForm.toEmail.value == ' ') || (!/^([^@.]+\.)*[^@.]+@([^@.]+\.)+[^@.]{2,4}$/.test(thisForm.toEmail.value)))
	{
		alert(labels['FORUM_error_recipient_email_address_invalid']);
		return;
	}

	thisForm.submit();
}

function printSize(infoId,whatObj,maxSize)
{
	spacer='';
	whatLength=whatObj.value.length;
	if (String(whatLength).length<String(maxSize).length)
	{
		for($i=1;$i<=String(maxSize).length-String(whatLength).length;$i++)
			spacer+='&nbsp;&nbsp;';
	}
	document.getElementById(infoId).innerHTML='&nbsp;[<span'+(whatLength>maxSize?' class="SizeInfoBad"':'')+'>'+spacer+whatLength+'</span>/'+maxSize+']';
}
