function ctrlenter(k)
	{
	if (k)
		{
		ctrl=k.ctrlKey;
		k=k.which;
		}
		else
		{
		k=event.keyCode;
		ctrl=event.ctrlKey;
		}

	if ((k==13 && ctrl) || (k==10) && sent==0) 
		{
		sF(true);
		sent=1;
		}
	
	}
	
	
function sF(s)
	{
	if (s) $('fs').submit();
	}
	
function storeCaret(text) {
	if (typeof(text.createTextRange) != "undefined")
		text.caretPos = document.selection.createRange().duplicate();
}

function surroundText(text1, text2, textarea) {
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0) {
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

function tag_url(textarea)
{
var FoundErrors = '';
var enterURL   = prompt("Введіть адресу", "http://");

if (!enterURL || enterURL=='http://') {FoundErrors = 1;}
if (FoundErrors) {return;}

surroundText('[url=' + enterURL + ']', '[/url]',textarea);
}

function http(id,act)
	{
	if (act == 'click')
		{
		if ($(id).value == 'http://' || $(id).value == 'ftp://')
			{
			$(id).value = "";
			$(id).style.color = "#000000";
			}
		}
	
	if (act == 'blur')
		{
		if (($(id).value == 'http://') || ($(id).value == 'ftp://') || ($(id).value == ''))
			{
			if ($(id).value == '') 
				$(id).value = "http://";
				
			$(id).style.color = "#B6B5B4";
			}
			else
			{			
			x_value = $(id).value;

			if ( (x_value.indexOf("http://")==-1) && (x_value.indexOf("https://")==-1) && (x_value.indexOf("ftp://")==-1))
				{
				$(id).value = 'http://'+x_value;
				}			
			}
		}
	}

function insert_link(textarea)
 {
 window_create('alert_x',350,1);
 window_setupcontent('alert_x','Вставка посилання');
	
 document.getElementById("alert_x_mf").innerHTML = document.getElementById("alert_x_mf").innerHTML + ""
	  +"<div id='forchange'><form action='' method='post'>"
		+"<table cellspacing='0' cellpadding='0' border='0' width='100%' style='border-collapse: collapse;'>"
			+"<tr style='height: 90px;' valign='top'>"
				+"<td style='width: 70px;' align='center'><img src='http://sarny.info/themes/beta/confirm_ico.gif' width='37' height='34' alt='' title='' border='0' style='margin-top: 20px;'></td>"
				+"<td style='color: #424242; font-size: 12px;'><div style='margin-top: 25px;'><input type='text' id='link' name='link' style='width: 300px; color: #B6B5B4; font-size: 14px;' value='http://' onClick=\"http('link','click');\" onBlur=\"http('link','blur');\" onFocus=\"http('link','click');\" /></div></td>"
			+"</tr>"
			+"<tr style='height: 40px;'>"
				+"<td align='center' style='white-space: nowrap; border-top: 1px solid #D2D1CB;' colspan='2'>"
					+"<div style='margin-top: 15px;'>"
						+"<input id='reg_enter_button' style='width: 68px; height: 16px' type='image' src='http://sarny.info/themes/beta/button_yes.gif' alt='Так' onclick='doInsertLink(this.form.link,\""+textarea+"\"); return false;' title='Так'>&nbsp;"
						+"<img style='width: 68px; height: 16px; cursor: pointer' width='68' height='16' src='http://sarny.info/themes/beta/cancel.gif' onClick=\"window_close(0);\" alt='Скасувати' title='Скасувати'>"
					+"</div>"
				+"</td>"
			+"</tr>"
		+"</table>"
	   +"</form></div><br>";
 $('link').focus();
 }
 
function doInsertLink(value,textarea)
	{
	surroundText('[url='+value.value+']', '[/url]', $(textarea));
	window_close(0);
	}
