function insertSmilie(element, smilie) {
  insert(element, '', ' ' + smilie);
}
function insert(element, start, eind) {
  element = document.getElementById(element);
  if (document.selection) {
    element.focus();
    sel = document.selection.createRange();
    sel.text = start + sel.text + eind;
  } else if (element.selectionStart || element.selectionStart == '0') {
    element.focus();
    var startPos = element.selectionStart;
    var endPos = element.selectionEnd;
    element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + eind + element.value.substring(endPos, element.value.length);
  } else {
    element.value += start + eind;
  }
}

function insert_url(element) {
	var adres = prompt("Wpisz adres URL","http://");
	if(validURL(adres)) {
		insert(element,'<a href="'+adres+'">','</a>');
	} else {
		alert("Wpisałeś nieprawidłowy adres URL!");
	}
}

function validURL(url){
  if(url.match(/^(http|https|ftp)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
     url.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i)){
    return true;
  } else {
    return false;
  }

} 