function execCmd(cmd, acmd) {
  var f = document.form1;
  if (isClear()) {
    f.cmd.value = cmd;
    f.acmd.value = acmd;
    f.submit();
  }
}

function isClear() {
  var f = document.form1;
  len = f.length;
  for (i=0; i < len; i++) {
    var isCheck = false;
    if ((f.elements[i].name == "to" || f.elements[i].name == "cc" || f.elements[i].name == "subject" || f.elements[i].name == "text") && f.elements[i].value.length != 0) {
      isCheck = true;
    }
    if(f.defaultBcc != null){
	    if (f.elements[i].name == "bcc") {
	      var bcc = f.elements[i].value;
	      var defaultBcc = f.defaultBcc.value;
	      if (defaultBcc == 0 && bcc.length != 0) {
	        isCheck = true;
	      } else if (bcc != defaultBcc) {
	        isCheck = true;
	      }
	    }
	}
	if(f.defaultReplyTo != null){
	    if (f.elements[i].name == "replyto") {
	      var replyto = f.elements[i].value
	      var defaultReplyTo = f.defaultReplyTo.value;
	      if (defaultReplyTo == 0 && replyto.length != 0) {
	        isCheck = true;
	      } else if (replyto != defaultReplyTo) {
	        isCheck = true;
	      }
	    }
	}
    if (isCheck) { 
      return confirm("The content you composed will be lost. Are you sure?");
    }
  }
  return true;
}

function cmdLogout() {
  var f = document.form1;
  if (confirm("Are you sure you want to logout?")) {
    f.cmd.value = "LOGOUT";
    f.submit();
    return;
  }
}

function checkAddress(val) {
  var f = document.form1;
  cnt = 0;
  for (i=0; i<val.length; i++) {
    if (val.charAt(i).match("\"") != null) {
      cnt++;
    }
  }
  if ((cnt % 2) != 0) {
    return false;
  } else {
    return true;
  }
}

function cmdAdr(cmd, acmd) {
  var f = document.form1;
  val = f.to.value;
  if (!checkAddress(val)) {
    alert("Double quotation is used incorrectly in recipient address.");
    return;
  }
  if (val.length != 0 && val.search("%") > -1) {
    alert("Please input the recipient address without using %");
    return;
  }
  val = f.cc.value;
  if (!checkAddress(val)) {
    alert("Double quotation is used incorrectly in cc address.");
    return;
  }
  if (val.length != 0 && val.search("%") > -1) {
    alert("Please input the cc address without using %");
    return;
  }

  if(f.bcc != null){ // 詳細表示のときだけ存在するので、nullチェックで存在の有無を確認
	  val = f.bcc.value;
	  if (!checkAddress(val)) {
	    alert("Double quotation is used incorrectly in bcc address.");
	    return;
	  }
	  if (val.length != 0 && val.search("%") > -1) {
	    alert("Please input the bcc address without using %");
	    return;
	  }
  }
  if(f.replyto != null){ // 詳細表示のときだけ存在するので、nullチェックで存在の有無を確認
	  val = f.replyto.value;
	  if (val.length != 0) {
	    if (!isAscii(val)) {
	      alert("Please input only single-byte character in Return-Path.");
	      return;
	    }
	    if (val.search("%") > -1) {
	      alert("Please input the Return-Path without using %");
	      return;
	    }
	  }
  }

  f.cmd.value = cmd;
  f.acmd.value = acmd;
  f.submit();
}

function set(adr) {
  var f = document.form1;
  f.cmd.value = 'SME';
  f.select_adr.value = adr;
  f.submit();
}

function isAscii(value) {
  for (n = 0; n < value.length; n++) {
    if (value.charCodeAt(n) >= 0x80) {
      return false;
    }
  }
  return true;
}

function setAdr(cmd, acmd) {
  var f = document.form1;
  len = f.length;
  for (i=0; i < len; i++) {
    if ((f.elements[i].name == "adr_to" || f.elements[i].name == "adr_cc" || f.elements[i].name == "adr_bcc") && (f.elements[i].checked)) {
      f.cmd.value = cmd;
      f.acmd.value = acmd;
      f.set_adr.value = "TRUE";
      f.submit();
      return;
    }
  }
  alert("Please select address to configure.");
}

function changeView(cmd, acmd) {
  var f = document.form1;
  f.cmd.value = cmd;
  f.acmd.value = acmd;
  f.submit();
}

function openWindow(value) {
  window.open(value);
}
