/*
 * Web Widgets Toolkit
 * Copyright (c) 2005 Marco Maccaferri and Others. All rights reserved.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   IBM Corporation  - original SWT API, implementation and comments
 *   Marco Maccaferri - initial API and implementation
 */

function call_remote_scripting()
{
  window.focus();

  var IFrameObj = get_object('_dummy');
  if (IFrameObj.contentDocument)
  {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument;
  }
  else if (IFrameObj.contentWindow)
  {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  }
  else if (IFrameObj.document)
  {
    // For IE5
    IFrameDoc = IFrameObj.document;
  }
  else
  {
    return;
  }
  if (document.location.href.indexOf('?') > -1)
    qoe = '&';
  else
    qoe = '?';
  var qstr = document.location.href + qoe + 'rpccall=1' + '&funct=' + escape(arguments[0]);
  for (var i = 1; i < arguments.length; i++)
  {
    if (arguments[i] == '')
      qstr += '&prms[' + (i - 1) + ']=__rpc_empty_value__';
    else
      qstr += '&prms[' + (i - 1) + ']=' + escape(arguments[i]);
  }
  IFrameDoc.location.replace(qstr);
}

function table_doubleclick(source,selection,item)
{
  if (window.getSelection) window.getSelection().removeAllRanges();
  if (document.selection) document.selection.empty();
  document.forms[0].button.value = source;
  selection.value = item;
  document.forms[0].submit();
}

function button_click(form, id)
{
  // Clear the text selection (a side effect of double clicking)
  if (window.getSelection) window.getSelection().removeAllRanges();
  if (document.selection) document.selection.empty();

  // Walk back the DOM tree until the FORM tag is found  
  while(form.tagName != "FORM" && form.tagName != "form")
    form = form.parentNode;

  // Set the button id value  
  form.button.value = id;
  // Process the form
  form.submit();
}

function confirm_button_click(form, id, message)
{
  // Clear the text selection (a side effect of double clicking)
  if (window.getSelection) window.getSelection().removeAllRanges();
  if (document.selection) document.selection.empty();
  
  // Walk back the DOM tree until the FORM tag is found  
  while(form.tagName != "FORM" && form.tagName != "form")
    form = form.parentNode;

  // Ask for confirmation
  if (confirm(message))
  {
    // Set the button id value  
    form.button.value = id;
    // Process the form
    form.submit();
  }
}

function get_object(id)
{
  var elements = document.getElementsByName(id);
  if (elements != null && elements.length > 0)
    obj = elements[elements.length - 1];
  else
    obj = document.getElementById(id);
  return obj;
}

function main()
{
  call_remote_scripting('main', document.body.clientWidth, document.body.clientHeight);
}

function toggle_checked_state(id)
{
  var elements = document.getElementsByName(id);
  if (elements != null && elements.length > 0)
    obj = elements[elements.length - 1];
  else
    obj = document.getElementById(id);
  if (obj.checked)
    obj.checked = 0;
  else
    obj.checked = 1;
}

function select_radio_item(id, index)
{
  var elements = document.getElementsByName(id);
  if (elements != null && elements.length > 0)
  {
    obj = elements[index];
    if (obj.checked)
      obj.checked = 0;
    else
      obj.checked = 1;
  }
}