var lapplet_launch_node = "";
var lapplet_launch_label = "";
var lapplet_launch_domain = "";
var lapplet_launch_username = "";
var lapplet_editurl = "";
var lapplet_deleteurl = "";
var lapplet_ackurl = "";

function launch_tool(node, label, domain, user, toolindex)
{
 // Some hacks to make empty string args in konq java.
 if (domain == "") {
  domain = "!@#NODOMAIN#@!";
 }
 if (label == "") {
  label = "!@#NOLABEL#@!";
 }
 if (node == "") {
  node = "!@#NONODE#@!";
 }
 if (user == "") {
  user = "!@#NOUSER#@!";
 }	
 try {
  // specifik test to satisfy 
  var isrun = is_lapplet_running();
  if (isrun > 1) {
   var result = document.getElementById('lapplet').launchtool(node,label,domain,user,toolindex);
  } else if (isrun == 0) {
// alert_no_java();
  } else {
   // retry the launch tool
   setTimeout(launch_tool, 1000, node, label, domain, user, toolindex);
  }
 } catch (a) {
  alert_no_java();
 }
}

function addTool(tab_id, tooldef, i)
{
 var row = getById(tab_id).insertRow(i);
 var launch = "<a href='javascript:launch_tool(\"" +  
  lapplet_launch_node  + "\", \"" + 
  lapplet_launch_label + "\", \"" + 
  lapplet_launch_domain + "\", \"" + 
  lapplet_launch_username + "\", " + i + ");'>" + tooldef[0] + "</a>";
 var acklaunch = "<a href='" + lapplet_ackurl + "&toolidx=" + i + "'>" + tooldef[0] + "</a>";
 row.insertCell(0).innerHTML = tooldef[2] == 1 ? acklaunch : launch;
 row.insertCell(1).innerHTML = "<a href='" + lapplet_editurl + "&toolidx=" + i + "'>[Edit]</a>";
 row.insertCell(2).innerHTML = "<a href='" + lapplet_deleteurl + "&toolidx=" + i + "'>[Delete]</a>";
}

function add_tools_from_applet(tab_id)
{
 try {
  // specific test to satisfy 
  var isrun = is_lapplet_running();
  if (isrun > 1) {
   var i;
   var MAX_TOOLS = 50;
   for (i = 0; i < MAX_TOOLS; i++) {
    var result = document.getElementById('lapplet').gettoollabel(i);
    if (result == "") {
     break;
    }
    var tooldef = tokenize(result, ":");
    addTool(tab_id, tooldef, i);
   }
  } else if (isrun == 0) {
// alert_no_java();
  } else {
   setTimeout(add_tools_from_applet, 1000, tab_id);
  }
 } catch (a) {
  alert_no_java();
 }
}

function fill_tool_form(toolindex)
{
 var isrun = is_lapplet_running();
 if (isrun == 0) {
  return;
 } else if (isrun == 1) {
  setTimeout(fill_tool_form, 1000, toolindex);
  return;
 }
 var applet = document.getElementById('lapplet');
 var result = applet.gettoollabel(toolindex);
 if (result == "") {
  alert("No applet running");
  return;
 }
 getById("label").value = result;
 getById("command").value = applet.gettoolcommand(toolindex);
 getById("confirm").checked = applet.gettoolconfirm(toolindex);
 getById("console").checked = applet.gettoolconsole(toolindex);
}

function save_tool_form(toolindex) 
{
 var label = getById("label").value;
 var command = getById("command").value;
 var confirm = getById("confirm").checked != "" ? 1 : 0;
 var console = getById("console").checked != "" ? 1 : 0;
 var idx = parseInt(toolindex);
 // Some hacks to make empty string args in konq java.
 if (command == "") {
  command = "!@#NOCOMMAND#@!";
 }
 if (label == "") {
  label = "!@#NOLABEL#@!";
 }
 var isrun = is_lapplet_running();
 if (isrun == 0) {
// alert_no_java();
  return;
 } else if (isrun == 1) {
  setTimeout(fill_tool_form, 1000, toolindex);
  return;
 } 
 // if idx is < 0 it means that a new tool should be created.
 var result = document.getElementById('lapplet').settoolinfo(idx, label, command, confirm, console);
 if (result) {
  alert("Error saving tool.");
  return;
 }
}

function delete_tool(toolindex, next_url)
{
 var isrun = is_lapplet_running();
 if (isrun > 1) {
  document.getElementById('lapplet').deletetool(toolindex);
  document.location = next_url;
 } else if (isrun == 0) {
// alert_no_java();
 } else {
  setTimeout(function() { delete_tool(toolindex, next_url) }, 1000);
 }
}

