<?xml version="1.0"?>
<!DOCTYPE window>
<!-- DO NOT LOCALIZE: this file is source documentation; not part of the build -->
<!-- dialog containing a control requiring initial setup, expecting
     initial settings to be passed in as parameters -->
<xul:window
    xmlns:html="http://www.w3.org/1999/xhtml"
    xmlns:xul ="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    onload = "SetFromParams()"
    title = "Things to do"
    height = "200" width = "300">

  <script>
    <![CDATA[
    var debug = true;
    // Initialize controls from parameters sent through the URL
    function SetFromURL() {
      // dump a bunch of diagnostics
      if (debug) {
        dump("In SetFromURL...\n");
        dump("param string is '" + location.search + "', length " +
            location.search.length + "\n");
        var debugSetting = GetNamedParam(location.search.substring(1,location.search.length), "remind");
        if (debugSetting) {
          dump("'remind' parameter = '" + debugSetting + "'\n");
        } else
          dump("'remind' setting not found\n");
        if (document.getElementById("remind"))
          dump("found 'remind' element in document\n");
        else
          dump("'remind' element missing from document\n");
        dump("Finishing SetFromURL...\n");
      }

      // set checkbox from "remind=xxx" name=value pair
      var params = location.search.substring(1, location.search.length);
      var setting = GetNamedParam(params, "remind");
      if (setting)
        setting = setting.toLowerCase() == "true";
      else
        setting = false;
      var checkbox = document.getElementById("remind");
      if (checkbox)
        checkbox.checked = setting;

      // set prompt text from "prompt=xxx" name=value pair
      var setting = GetNamedParam(params, "prompt");
      control = document.getElementById("prompt");
      if (control && setting) {
        control = control.firstChild;
        if (control && control.nodeType == 3) // TEXT_NODE
          control.data = setting;
      }
    }

    // Initialize controls from parameters sent through openDialog
    function SetFromParams() {
      // dump a bunch of diagnostics
      if (debug) {
        var debugSetting;
        var debugControl;
        dump("In SetFromParams...\n");
        if (window.arguments) {
          dump("arguments exist\n");
          if (window.arguments[0]) {
            dump("  arguments[0] exists\n");
            if (window.arguments[0].remind) {
              dump("  arguments[0].remind exists\n");
              debugSetting = window.arguments[0].remind;
              dump("  it's " + debugSetting +
                ", type " + typeof debugSetting + "\n");
            } else
              dump("arguments[0].remind does not exist\n");
            if (window.arguments[0].prompt) {
              dump("  arguments[0].prompt exists\n");
              debugSetting = window.arguments[0].prompt;
              dump("  it's " + debugSetting +
                ", type " + typeof debugSetting + "\n");
            } else
              dump("arguments[0].prompt does not exist (or it's just false)\n");
          } else
            dump("arguments[0] does not exist\n");
        } else
          dump("no arguments\n");
        debugControl = document.getElementById("remind");
        if (debugControl)
          dump("found 'remind' element in document "+
            typeof debugControl+"\n");
        else
          dump("'remind' element missing from document\n");
        debugControl = document.getElementById("prompt");
        if (debugControl) {
          dump("found 'prompt' element in document "+
            typeof debugControl+"\n");
          debugControl = debugControl.firstChild;
          if (debugControl) {
            dump("found prompt's first child. type " + debugControl.nodeName + "\n");
          } else
            dump("couldn't find prompt's first child\n");
        } else
          dump("'prompt' element missing from document\n");
        dump("Finishing SetFromParams...\n");
      }

      // look in arguments[0] for an object with interesting properties and values
      // set checkbox from its value, if present
      if (window.arguments && window.arguments[0]) {
        var setting;
        var control;
        // set checkbox from the value of argment[0]'s "value" property
        setting = window.arguments[0].remind;
        if (setting) { // (exists and true)
          control = document.getElementById("remind");
          if (control)
            control.checked = setting;
        }
        // set prompt from the value of argment[0]'s "prompt" property
        setting = window.arguments[0].prompt;
        if (setting) {
          if (typeof setting == "string") {
            control = document.getElementById("prompt");
            if (control) {
              control = control.firstChild;
              if (control && control.nodeType == 3) // TEXT_NODE
                control.data = setting;
            }
          }
        }
      }
    }

    // OK button handler
    // return the setting of the "remind" checkbox in two equivalent ways
    // and then close the window (disabled for now, since that crashes)
    function DoOK() {
      var checkbox = document.getElementById("remind");
      if (checkbox) {

        // attach a property to ourselves, which can be queried from outside
        window.returnArguments = checkbox.checked;

        // additionally, if we were given an openDialog parameter, set its value
        if (window.arguments && window.arguments[0])
          window.arguments[0].remind = checkbox.checked;
      }
//    var toolkitCore = GetToolkitCore();
//    if (toolkitCore)
//      toolkitCore.CloseWindow(window);
      window.close();
    }

    function GetToolkitCore() {
      var toolkitCore = XPAppCoresManager.Find("ToolkitCore");
      if (!toolkitCore) {
        toolkitCore = new ToolkitCore();
        if (toolkitCore)
          toolkitCore.Init("ToolkitCore");
      }
      return toolkitCore;
    }

    // params is a list of name=value parameters separated by semicolons
    // or ampersands. To pass parameters containing either character,
    // string together parameters escape()d separately, separated by ;.
    // this function returns the value of the named pair, or null
    // if it found nothing.
    function GetNamedParam(params, name) {
      if (debug)
        dump("GetNamedParam looking for '" + name + "' in '" + params + "'\n");
      var re = new RegExp(name+" *=([^&;]+)");
      var match = re(params);
      if (match) {
        if (debug)
          dump("  matched regexp. found '" + match[1] + "'\n");
        return unescape(match[1]);
      }
      return null;
    }

    function DumpWindow() {
      dump("**********************************************\n");
      for (prop in window)
        dump(prop + "\n");
      dump("----------------------------------------------\n");
    }
    ]]>
  </script>

  <html:table>
    <html:tr>
      <html:td id="prompt">Give me your money</html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <!-- note the html namespace on the id attribute, which
             seems at this time to be required by getAttribute() -->
        <html:input type="checkbox" id="remind"/>Remind me
      </html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <html:button onclick="DoOK()">OK</html:button>
      </html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <html:button onclick="SetFromURL()">Startup from URL</html:button>
      </html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <html:button onclick="SetFromParams()">Startup from Params</html:button>
      </html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <html:button onclick="sizeToContent()">Size To Content</html:button>
      </html:td>
    </html:tr>
    <html:tr>
      <html:td>
        <html:button onclick="DumpWindow()">Dump Window</html:button>
      </html:td>
    </html:tr>
  </html:table>

</xul:window>
