function isEmpty(m) {
	if ((m.length == 0) || (m == null)) { return true; }
	else { return false; }
}

function emailCheck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at) == -1) { return false; }
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) { return false; }
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) { return false; }
	if (str.indexOf(at,(lat+1)) != -1) { return false; }
	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) { return false; }
	if (str.indexOf(dot,(lat+2)) == -1) { return false; }
	if (str.indexOf(" ") != -1) { return false; }
	return true;				
}

function checkGuestbook() {
	mehet = 1;
	if (mehet == 1 && isEmpty(document.msg.name.value)) {
		mehet = 0;
		document.msg.name.focus();
		alert('A név mező kitöltése kötelező!');
	}
	if (mehet == 1 && !emailCheck(document.msg.email.value)) {
		mehet = 0;
		document.msg.email.focus();
		alert('Helytelen e-mail cím!');
	}
	if (mehet == 1 && isEmpty(document.msg.msg.value)) {
		mehet = 0;
		document.msg.msg.focus();
		alert('Írjon be üzenetet is!');
	}
	if (mehet == 1 && parseInt(document.msg.num.value) != (parseInt(document.msg.num1.value) + parseInt(document.msg.num2.value))) {
		mehet = 0;
		document.msg.num.focus();
		alert('Helytelen eredmény!');
	}
	if (mehet == 1) {
		document.msg.submit();
	}
}

function codeSimple (txtId, code, defText) {
  codeInsert (txtId, "<" + code + ">", "</" + code + ">", defText);
}

function codeParam (txtId, code, param, defText) {
  codeInsert (txtId, "<" + (code == 'a' ? 'a href' : 'img src') + "=\"" + param + "\"" + (code == 'a' ? ' target="_blank"' : ' /') + ">", (code == 'a' ? '</a>' : null), defText);
}

function codeGetURL (txtId, code, msg) {
  var url = prompt (msg, "http://");
  if (!url) {
    document.getElementById(txtId).focus();
    return;
  }
  url = encodeURI(url).replace(/%25[0-9A-F][0-9A-F]/gi, decodeURIComponent);
  codeParam (txtId, code, url, (code == 'a' ? '[link]' : null));
}

function codeSmiley (txtId, code) {
  codeInsert (txtId, " " + code + " ", null, null);
}

function codeInsert (txtId, oTag, cTag, defText) {
  var txt = document.getElementById(txtId);
  txt.focus();
  if (txt.selectionStart != undefined) {
    // -- Firefox, Opera, Safari
    var sel_start = txt.selectionStart;
    var sel_end   = txt.selectionEnd;
    if (!cTag) {
      txt.value = txt.value.substr(0, sel_start) + oTag + txt.value.substr(sel_end);
      txt.selectionStart = sel_start + oTag.length;
      txt.selectionEnd   = sel_start + oTag.length;
    }
    else {
      var selText = txt.value.substr(sel_start, sel_end - sel_start);
      if (!selText && defText) selText = defText;

      txt.value = txt.value.substr(0, sel_start) + oTag + selText + cTag + txt.value.substr(sel_end);
      txt.selectionStart = sel_start + oTag.length;
      txt.selectionEnd   = sel_start + oTag.length + selText.length;
    }
  }
  else if (document.selection) {
    // -- IE
    var sel = document.selection.createRange();
    if (sel) {
      if (!cTag) sel.text = oTag;
      else {
        var selText = sel.text;
        if (!selText && defText) selText = defText;
        var selLength = selText.replace(/\r\n/g, "\n").length;

        sel.text = oTag + selText + cTag;
        sel.moveStart ("character", -cTag.length - selLength);
        sel.moveEnd   ("character", -cTag.length);
      }
      sel.select();
    }
  }
}
