// IE only - wraps selected text with lft and rgt
function WrapIE(lft, rgt) {
strSelection = document.selection.createRange().text;
if (strSelection!="") {
document.selection.createRange().text = lft + strSelection + rgt;
}
}

// Moz only - wraps selected text with lft and rgt
function wrapMoz(txtarea, lft, rgt) {
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
if (selEnd==1 || selEnd==2) selEnd=selLength;
var s1 = (txtarea.value).substring(0,selStart);
var s2 = (txtarea.value).substring(selStart, selEnd)
var s3 = (txtarea.value).substring(selEnd, selLength);
txtarea.value = s1 + lft + s2 + rgt + s3;
}

// Chooses technique based on browser
function wrapTag(txtarea, lft, rgt) {
lft = unescape(lft);
rgt = unescape(rgt);
if (document.all) {
WrapIE(lft, rgt);
}
else if (document.getElementById) {
wrapMoz(txtarea, lft, rgt);
}
}  

// IE only - Insert text at caret position or at start of selected text
function insertIE (txtarea, text) {
if (txtarea.createTextRange && txtarea.caretPos) { 
var caretPos = txtarea.caretPos; 
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text+caretPos.text + ' ' : text+caretPos.text;
} else {
txtarea.value = txtarea.value + text; 
}
return;
} 

// Moz only - Insert text at caret position or at start of selected text
function insertMoz(txtarea , lft) {
var rgt="";
wrapTag(txtarea, lft, rgt);
return;
}

// Switch function based on browser - Insert text at caret position or at start of selected text
function insertTag(txtarea , lft) {
if (document.all) {
insertIE(txtarea, lft);
}
else if (document.getElementById) {
insertMoz(txtarea, lft);
}
}

// IE only - stores the current cursor position on any textarea activity
function storeCaret (txtarea) { 
if (txtarea.createTextRange) { 
txtarea.caretPos = document.selection.createRange().duplicate();
} 
}


function smajliky(what)
{
  insertTag (document.zapis.text, what);
}

function demoMatchClick() {
  var re = new RegExp(document.demoMatch.regex.value);
  if (document.demoMatch.subject.value.match(re)) {
    alert("Successful match");
  } else {
    alert("No match");
  }
}

function kontrola(){
  if(document.zapis.name.value=="" || document.zapis.text.value==""){alert("Je nutno vyplnit JMENO a VZKAZ!");document.zapis.name.focus();return false;} 
  var re = new RegExp("^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*");
  if(document.zapis.web.value!="" && document.zapis.web.value!="http://" && !document.zapis.web.value.match(re)) {alert("Adresa webu neni spravna!");document.zapis.web.focus();return false;} 
  var re = new RegExp("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
  if(document.zapis.email.value!="" && document.zapis.email.value!="@" && !document.zapis.email.value.match(re)) {alert("Emailova Adresa neni spravna!");document.zapis.email.focus();return false;} 
}

function ConfirmDelete(id) {
  answer = confirm("Opravdu chces smazat tento prispevek?");
  if (answer !="0") {
    location = "guestbookadmin.php?delete=" + id;
  }
}

function ConfirmDeleteAnswer(id) {
  answer = confirm("Opravdu chces smazat tuto odpoved?");
  if (answer !="0") {
    location = "guestbookadmin.php?deleteAnswer=" + id;
  }
}