﻿ function replaceSelection (input_new_post, replaceString) {
		if (input_new_post.setSelectionRange) {
				var selectionStart = input_new_post.selectionStart;
				var selectionEnd = input_new_post.selectionEnd;
				input_new_post.value = input_new_post.value.substring(0, selectionStart)
								+ replaceString
								+ input_new_post.value.substring(selectionEnd);
				if (selectionStart != selectionEnd) // has there been a selection
					setSelectionRange(input_new_post, selectionStart, selectionStart + 
							replaceString.length);
				else // set caret
					setCaretToPos(input_new_post, selectionStart + replaceString.length);
		} else if (document.selection) {
				if(input_new_post.caretPos) {
					var caretPos = input_new_post.caretPos;
					caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : replaceString;

					if(caretPos.text!='') {
							caretPos.moveStart('character', -replaceString.length);
							caretPos.select();
					}
				} else {
					input_new_post.value += replaceString;
				}
		} else {
				input_new_post.value += replaceString;
				input_new_post.focus();
		}
		
		 $get('charCount').innerHTML = 4000 - input_new_post.value.length;
	}
 
 function createUrl(){
       
        var url = prompt('Unesi link:','http://');
					var target = 'target=_blank';
					if(url!='' && url!=null) {
							if((url.indexOf("http://") == -1) || (url.indexOf("@") == -1)) target = '';
							if(getSelection())
								if(target == '')
									wrapSelection('[url="'+url+'"]','[/url]');
								else
									wrapSelection('[url="'+url+'" '+target+']','[/url]');
							else
								replaceSelection(input_new_post,'[url]'+url+'[/url]');
					}
 }
 
 function setCaretToPos (pos) {
		setSelectionRange(pos, pos);
	}
	
	function setSelectionRange(selectionStart, selectionEnd) {
   
		if (input_new_post.setSelectionRange) {
				input_new_post.focus();
				input_new_post.setSelectionRange(selectionStart, selectionEnd);
		} else if(input_new_post.createTextRange) {
				var range = input_new_post.createTextRange();
				range.collapse(true);
				range.moveEnd('character', selectionEnd);
				range.moveStart('character', selectionStart);
				range.select();
		}
    }
 
 function wrapSelection (preString, postString) {
	
	
	
		if (input_new_post.setSelectionRange) {
				var selectionStart = input_new_post.selectionStart;
				var selectionEnd = input_new_post.selectionEnd;

				input_new_post.value = input_new_post.value.substring(0, selectionStart)
								+ preString
								+ input_new_post.value.substring(selectionStart,selectionEnd)
								+ postString
								+ input_new_post.value.substring(selectionEnd);

				if (selectionStart != selectionEnd) // has there been a selection
					setSelectionRange(input_new_post, selectionStart, preString.length + postString.length + selectionEnd);
				else // set caret
					setCaretToPos(selectionStart + (preString+postString).length);
		    } 
		else if (document.selection) 
		{
				var sel = document.selection.createRange().text;
				if(sel) {
					document.selection.createRange().text = preString + sel + postString;
					input_new_post.focus();
				} else {
					input_new_post.value += preString + postString;
					input_new_post.focus();
				}
		} 
		else 
		{
				input_new_post.value += preString + postString;
				input_new_post.focus();
		}

        $get('charCount').innerHTML = 4000 - input_new_post.value.length;
	}

	function getSelection() {
		if(input_new_post.setSelectionRange) {
				return input_new_post.selectionStart != input_new_post.selectionEnd;
		} else if(document.selection) {
				var range = document.selection.createRange();
				return range.parentElement()==input_new_post && range.text!='';
		} else {
				return false;
		}
	}
	
	
	if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
