function copyAction() {
  document.formtext.text.value = getApplet().copyText();
  document.formtext.text.select();
  document.formtext.text.focus();
  if (document.execCommand) document.execCommand("Copy");
  else updateMessageWindow('document.execCommand("Copy") not supported.\nPress Ctrl-C after using the copy button.');
  updateMessageWindow();
}
function clearAction() {
   getApplet().clearText();
   document.formtext.text.value = "";
   updateMessageWindow();
}
var buttonsRemoved = false;
function removeButtons() {
  if (buttonsRemoved) return;
  //setTimeout("removeButtons()",500); // retry after 0.5 sec
  //  if (!getApplet().noButtons) return;
  getApplet().noButtons();
  buttonsRemoved = true;
  if (document.formtext) document.formtext.text.disabled = false;
  document.form.copy.disabled = false;
  if (document.formclear) document.formclear.clear.disabled = false;
  document.form3.showdoc.disabled = false;
  document.form4.showdefdoc.disabled = false;
  if (document.formloadconfig) document.formloadconfig.loadconfig.disabled = false;
  if (document.formloadconfig) document.formloadconfig.configname.disabled = false;
  //document.formmirror.mirror.disabled = false;
  parseQueryString();
  updateMessageWindow("Initialization completed.");
}
function showCurrentDocumentation() {
  var copydoc = window.parent.frames['copy'].document;
  if (copydoc) {
    var docu = getApplet().getCurrentDocumentation();
    copydoc.open();
    copydoc.write(docu);
    copydoc.close();
  } else {
    updateMessageWindow("Can't display documentation: No frame called 'copy'");
  }
  updateMessageWindow();
}
function showDefaultDocumentation() {
  showDocumentation("default");
  updateMessageWindow();
}
function showAppletDocumentation() {
  window.parent.frames['copy'].location = "documentation.html";
  updateMessageWindow();
}
function showDocumentation(group) {
  var copydoc = window.parent.frames['copy'].document;
  if (copydoc) {
    var docu = getApplet().getDocumentation(group);
    copydoc.open();
    copydoc.write(docu);
    copydoc.close();
  } else {
    updateMessageWindow("Can't display documentation: No frame called 'copy'");
  }
}
/*var mirrorDoc = false;
function _mirrorDocument() {
  if (mirrorDoc==false) return;
  setTimeout("_mirrorDocument()",250); // redo after 1/4 sec
  if (getApplet().hasChangedSinceMirror())
    document.formtext.text.value = getApplet().mirror();
}
function toggleMirroring() {
  if (mirrorDoc) {
    mirrorDoc = false;
    document.formmirror.mirror.value = "Mirror";
  } else {
    mirrorDoc = true;
    _mirrorDocument();
    document.formmirror.mirror.value = "Stop Mirroring";
  }
}*/
var applet;
var getAppletWarned;
function getApplet() {
  if (document.applets) if (document.applets['intinp'])
    return document.applets['intinp'];
  if (document.embeds) if (document.embeds['intinp'])
    return document.embeds['intinp'];
  if (document.objects) if (document.objects['intinp'])
    return document.objects['intinp'];
  if (!getAppletWarned) 
    updateMessageWindow
      ("Can't find applet on page.\nTry one of the OBJECT/EMBED/APPLET links above.");
  getAppletWarned = 1;
  return 0;
}
function resizeStuff(factor,mod_w1,mod_h1,mod_w2,mod_h2) {
  var app = getApplet();
  var txt = document.formtext.text;

  var w1 = app.width;
  var w2 = txt.cols;
  var w1n = Math.ceil(w1*factor);
  var w2n = Math.ceil(w2*factor);
  if (w1n==w1 && factor<1) w1n--;
  if (w1n==w1 && factor>1) w1n++;
  if (w2n==w2 && factor<1) w2n--;
  if (w2n==w2 && factor>1) w2n++;
  if (w1n<1) w1n = 1;
  if (w2n<1) w2n = 1;

  var h1 = app.height;
  var h2 = txt.rows;
  var h1n = Math.ceil(h1*factor);
  var h2n = Math.ceil(h2*factor);
  if (h1n==h1 && factor<1) h1n--;
  if (h1n==h1 && factor>1) h1n++;
  if (h2n==h2 && factor<1) h2n--;
  if (h2n==h2 && factor>1) h2n++;
  if (h1n<1) h1n = 1;
  if (h2n<1) h2n = 1;
//  alert("h:  "+h1+"->"+h1n+"  /  "+h2+" -> "+h2n);

  if (mod_w1) app.width = w1n;
  if (mod_w2) txt.cols = w2n;
  if (mod_h1) app.height = h1n;
  if (mod_h2) txt.rows = h2n;
}
function loadConfig(url) {
  getApplet().loadConfig(url);
  updateMessageWindow();
}
function parseQueryString() {
  var qs = document.URL;
  var idx = qs.indexOf('?');
  if (idx==-1 || idx==qs.length) {
    qs = parent.document.URL;
    idx = qs.indexOf('?');
    if (idx==-1) return;
  }
  qs = qs.substr(idx+1);
  if (qs=='') return;
  qs = qs.split("&");
  for (var i=0; i<qs.length; i++) {
    var p = qs[i];
    idx = p.indexOf('=');
    if (idx==-1) continue;
    var val = p.substr(idx+1);
    val = unescape(val); // why not decodeURIComponent (in IE)?
    p = p.substr(0,idx);
    if (p=='config') {
      getApplet().loadConfig(val);
    } else if (p=='commands') {
      getApplet().loadConfigString(val);
    } else if (p=='input') {
      getApplet().setClipboardContent(val);
      getApplet().pasteText(true);
    } else if (p=='content') {
      getApplet().setClipboardContent(val);
      getApplet().pasteText(false);
    } else {
      updateMessageWindow("Unknown query string keyword '"+p+
			  "' (value: "+val+")");
    }
  }
}
function initFormLoadConfig() {
  var t = document.formloadconfig.configname.value;
  var i = t.indexOf('://');
  if (i==-1 || i==t.length)
    document.formloadconfig.configname.value = 
      document.URL.substring(0,document.URL.lastIndexOf("/"))+"/"+t;
}
function updateMessageWindow(text) {
  var newtext = "";
  if (buttonsRemoved) newtext = getApplet().getLog()+"";
  //var oldtext = document.formmessagewindow.messagewindow.value;
  if (!text) text = ""; else text += "\n";
  if (!text && newtext=="") return;
  if (newtext!="") parent.frames['messagewindow'].document.write(quoteHTML(newtext)+"<hr noshade size=1>");
  if (text) parent.frames['messagewindow'].document.write(quoteHTML(text)+"<hr noshade size=1>");
  //if (oldtext.length>1000) 
  //oldtext = ".............."+oldtext.substring(oldtext.length-1000);
  //document.formmessagewindow.messagewindow.value = oldtext + newtext + text;
  parent.frames['messagewindow'].scrollBy(0,1000);
}
function quoteHTML(str) {
  var newstr = "";
  for (var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    switch (c) {
    case '&': newstr += "&amp;"; break;
    case '<': newstr += "&lt;"; break;
    case '>': newstr += "&gt;"; break;
//    case ' ': newstr += " "; break;
    case '\n': newstr += "<br>"; break;
    default: newstr += c;
    }
  }
  return newstr;
}
function clearMessageWindow() {
  //document.formmessagewindow.messagewindow.value = "";
  parent.frames['messagewindow'].document.close();
  parent.frames['messagewindow'].document.open();
  updateMessageWindow();
}
function copyAreaChanged() {
  getApplet().setClipboardContent(document.formtext.text.value);
}
function initialize() {
  //document.formmessagewindow.messagewindow.value = "";
  updateMessageWindow("International Input. (C) by Dominique Unruh, 2003, GPL.");
  initFormLoadConfig();
  setInterval("daemonicAction()",500);
}
function daemonicAction() {
  if (!buttonsRemoved)
    removeButtons();
  else
    updateMessageWindow();
}
