function emoticon(text) {
  var txtarea = document.getElementById("themessage");
  text = ' ' + text + ' ';
  if (txtarea.createTextRange && txtarea.caretPos) {
    var caretPos = txtarea.caretPos;
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
    txtarea.focus();
  } else {
    txtarea.value  += text;
    txtarea.focus();
  }
}

function filterMessage(obj)
{
  var v = obj.value;
  v = v.replace(/[^\*\<\>]+/, '');
  obj.value = v;
}


function isFieldEmpty(id, fieldName)
{
  var t = document.getElementById(id).value;
  t = t.replace(/^\s+|\s+$/g, '');
  document.getElementById(id).value = t;

  if (t == '') {
    document.getElementById(id).focus();
    document.getElementById(id).select();
    alert("Silakan mengisi " + fieldName + ".");
    return true;
  }
  return false;
}

function allValidChars(email)
{
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function isValidEmail(email)
{
  if (email.length==0)
    return false;
  if (!allValidChars(email)) // check to make sure all characters are valid
    return false;
  if (email.indexOf("@") < 1) //  must contain @, and it must not be the first character
    return false;
  else if (email.lastIndexOf(".") <= email.indexOf("@"))  // last dot must be after the @
    return false;
  else if (email.indexOf("@") == email.length) // @ must not be the last character
    return false;
  else if (email.indexOf("..") >=0) // two periods in a row is not valid
    return false;
  else if (email.indexOf(".") == email.length)  // . must not be the last character
    return false;
  return true;
}

function validate_form()
{
  if (isFieldEmpty("thename", "nama")) return false;
  var n1 = document.getElementById("thename").value;
  var n2 = n1.toLowerCase();

  if (n2 == 'author' || n2 == 'erosario' || n2 == 'admin') {
    alert('Maaf, nama \"'+ n1 +'\" sudah di-reserved.\nSilakan gunakan nama lain.');
    document.getElementById("thename").focus();
    return false;
  }

  if (isFieldEmpty("alamat", "email")) return false;

  if (!isValidEmail(document.getElementById("alamat").value)) {
    alert("Alamat email tidak valid.");
    document.getElementById("alamat").focus();
    return false;
  }

  if (isFieldEmpty("themessage", "komentar,saran atau pesan")) return false;

  var m1 = document.getElementById("themessage").value;
  if (m1.length < 15) {
    alert("Pesan terlalu singkat.");
    document.getElementById("themessage").focus();
    return false;
  }
  return true;
}

function autoResizeFrame() {
  try{
    frame = document.getElementById('theiframe');
    innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
    objToResize = (frame.style) ? frame.style : frame;
    objToResize.height = 10;
    objToResize.height = innerDoc.body.scrollHeight + 10;
  }
    catch(err){
    //window.status = err.message;
  }
}
