// www.warmland.ru
function insert(textAreaId, insertText) {
	text = document.getElementById(textAreaId);
	selstart = text.selectionStart;
	
	text.value = text.value.slice(0, selstart) + insertText +
			text.value.slice(text.selectionEnd);
	text.selectionStart = selstart + insertText.length;
	text.selectionEnd = text.selectionStart;
    text.focus()
}

function append(textAreaId, insertText) {
	text = document.getElementById(textAreaId);
	text.value = text.value + insertText;
}

function ctrl_enter(e, form)
{
	if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) form.submit();
}

function show(id) {
	var el = document.getElementById(id);
	if (el.style.display == 'none' || el.style.display == '') document.getElementById(id).style.display = 'block'; else document.getElementById(id).style.display = 'none';
	
}

function stripslashes (str) {    
	return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':                
            	return '\u0000';
            case '':
                return '';
            default:
                return n1;        }
    });
}

jQuery(function () {
	
	$('#addtag').click(function (e) {
	
		var tag = jQuery('#tagSelect').val();
		var tags = jQuery('#tagsField').val();
		
		if (tags) {
			if (!tags.match(tag))
				jQuery('#tagsField').val(tags + ',' + tag);
		} else {
			jQuery('#tagsField').val(tag);
		}
		
		e.preventDefault();
	
	});

	$('#preview').jqm(
		{
			onShow: function (hash) {
				var c = jQuery('#comment').val();
				
				
				
				c = c.replace(/\</, "&lt;")
					.replace(/\>/, "&gt;")
					.replace(/\[b\](.*)\[\/b\]/gm, "<b>$1</b>")
					.replace(/\[url=(.*)\](.*)\[\/url\]/gm, "<a href='$1'>$2</a>")
					.replace(/\[u\](.*)\[\/u\]/gm, "<u>$1</u>")
					.replace(/\[i\](.*)\[\/i\]/gm, "<i>$1</i>")
					.replace(/\n\n/gm, "<p>")
					.replace(/\n/gm, "<br />");
					
				
			
				
				hash.w.html(stripslashes(c)).show();
			},
			onHide: function (hash) {
				jQuery('#preview').text('Подгружаем данные с тормозного сервера...');
				hash.w.hide();
				hash.o.remove();
			},
			trigger: 'a.preview'
		}
	);

	
});


