<!-- $Id: dojo_inline_forms.html 5542 2007-12-09 14:03:37Z httpd $ -->

if (typeof rename_inline_nodes == 'undefined') { // only load once!
  var inline_form_pane;
  var inline_form_idnum;
  var dlg_width_conn;
  var dlg_height_conn;
  var DEBUG_INLINE_FORMS = false;

  function pb_debug(_msg, _type) {
    if (! DEBUG_INLINE_FORMS) {
      // DEBUG var not set, check if debug param on query string
      
      var u = new dojo._Url(document.location);
      if (! u.query) { // no queries
        return;
      }
        
      var q = dojo.queryToObject(u.query);
        
      if ((! q.pb_debug_dojo) && (! q.debug_js)) {
        // debug queries not set
        return;
      }
    }

    if (_type == 'dir') {
      console.dir(_msg);
    }
    else {
      console.log(_msg);
    }
  }
  
  function getWindowSize() {
    // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
      myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
    //window.alert( 'Width = ' + myWidth );
    //window.alert( 'Height = ' + myHeight );
  }
  
  function isInteger(s) {
    return (s.toString().search(/^-?[0-9]+$/) == 0);
  }
  
  function set_inline_message(message) {
    isError = ((set_inline_message.arguments.length == 2) || (message.toLowerCase().indexOf('error') != -1));
    pb_debug('"' + message + '": error check: ' + message.toLowerCase().indexOf('error') + ' isError: ' + isError);
    
    message = decodeURIComponent(message);
    pb_debug("set_inline_message " + message + " for inline_form_error_msg_" + inline_form_idnum);
    try {
      node = document.getElementById('inline_form_error_msg_' + inline_form_idnum);
      var newnode = document.createElement("div");
      newnode.id = 'inline_form_error_msg_' + inline_form_idnum;
      
      if (isError) {
        pb_debug('Setting node style for Error');
        newnode.style.color = 'red';
      }
      else {
        newnode.style.color = '';
      }

      var msg_node = document.createTextNode(message);
      newnode.appendChild(msg_node);
      node.parentNode.replaceChild(newnode, node);
    
      // remove all child nodes - this way has unexpected results
      //while (node.hasChildNodes()) {
      //  node.removeChild(node.firstChild);
      //}
    
      // var txtNode = document.createTextNode(message);
      // node.appendChild(txtNode);
    }
    catch (e) {
      //alert ('Warning!  Please define inline_form_error_msg_' + inline_form_idnum + ' for debugging!');
      console.debug(e);
    }
    pb_debug("done set_inline_message " + message + " for inline_form_error_msg_" + inline_form_idnum);
  }

  function handle_inline_submit(domNode, parent_commtypeid, commtypeid, link_fieldid, link_itemid, template, inline_form) {
    // inline child form submit - currently only supports adding a new item, not editing one.
    
    if (link_fieldid) {
      inline_form_idnum = link_fieldid;
    }
    else{ 
      inline_form_idnum = commtypeid;
    }
    set_inline_message("Saving...");

    form_content = {};

    // TODO: get rid of some of the mess here
    form_content['pb_edit_submit'] = 1;
    form_content['pb_inline_form'] = 1;
    form_content['item_id'] = 'new';
    form_content['commtypeid'] = commtypeid;
    form_content['template'] = template;
    form_content['pb_link_fieldid'] = link_fieldid;
    form_content['pb_link_itemid'] = link_itemid;
    form_content['pb_parent_inline_form'] = inline_form;
    form_content['pb_inline_parent_commtypeid'] = parent_commtypeid;
    
    // get all nodes in the domNode container starting with pb_inline_ so we can post their 
    // content via XHR below
    //dojo.query('*[name^="pb_INLINE_"]', domNode).forEach( // this didn't work for ie - grrr....
    dojo.query('[id^="pb_INLINE_field_"]', domNode).forEach(
      function (domItem) {
        // sigh - some dijits replace out the damn form widget with a hidden sibling widget to carry the 
        // actual data:
        if (! domItem.name) {
          try {
            domItem = domItem.nextSibling;
          }
          catch (e) {
            //console.log ('Cannot find dom item for id: ' + domItem.id);
          }
        }
        
        k = domItem.name.replace(/^pb_INLINE_/, '');
        k = k.replace(/_displayed_$/, '');
        try {
          d = dijit.getEnclosingWidget(domItem);
          val = d.getValue();
          //console.log ('v1: ' + val + ' v2: ' + d.value);
        }
        catch (e) {
          val = domItem.value;
          //console.log('e: ' + e.message + ' val: ' + val);
        }
        
        form_content[k] = val;
      }
    );
    //dojo.query('*', domNode).forEach(
    //  function (domItem) {
    //    console.log('2 id: ' + domItem.id + ' name: ' + domItem.name + ' val: ' + domItem.value);
    //  }
    //);

    //console.debug(form_content);

    // submit the blank form
    try {
      refresh_id_save = dojo.byId('pb_refresh_list_id').value;
      dojo.byId('pb_refresh_list_id').value = '';
    } catch (e) { pb_debug('Warning, no pb_refresh_list_id detected'); }
    
    save_inline_form(form_content, false);

    try {
      dojo.byId('pb_refresh_list_id').value = refresh_id_save;
    } catch (e) { } 
    
    set_inline_message("");
    
    //handle_inline_submit_old(domNode, parent_commtypeid, commtypeid, link_fieldid, link_itemid, template, inline_form)
  }
    
  function save_inline_form() {
    PB_Dialog.setContent("Please Wait While Saving Form...");
    PB_Dialog.titleNode.innerHTML = 'Please Wait...';
    PB_Dialog.show();
    
    if (DEBUG_INLINE_FORMS) {
      console.log('save_inline_form args:');
      console.debug(save_inline_form.arguments);
    }

    var no_args = save_inline_form.arguments.length;
    formNode = save_inline_form.arguments[0];
  
    hide_form = true;
    if (no_args >= 2) {
      hide_form = save_inline_form.arguments[1];
    }

    save_callback = null;
    if (no_args >= 3) { // a callback was passed
      save_callback = save_inline_form.arguments[2];
    }
  
    // asynch form submit
    set_inline_message("Saving.....");
    
    refresh_id = '';
    try {
      refresh_id = dojo.byId('pb_refresh_list_id').value;
      pb_debug('got refresh_id ' + refresh_id + ' from pb_refresh_list_id');
    }
    catch (e) { }
    
    if (! isInteger(refresh_id)) {
      refresh_id = inline_form_idnum;
      pb_debug('got refresh_id ' + refresh_id + ' from inline_form_idnum');
    }
    
    pb_debug('running xhr for ' + inline_form_idnum + ' refresh: ' + refresh_id);
    
    function _handle_inline_form_post(response, ioArgs) { 
      //console.debug(response, ioArgs);
      
      if ((response.error) || (typeof response == "Error")) {
        set_inline_message('Save Form XHR Error: ' + response.message, true);
        alert('Save Form XHR Error: ' + response.message);
        PB_Dialog.hide();
        return response;
      }

      pb_debug('resp: ' + response + ' refresh: ' + refresh_id);
      if (response == "ok") {
        // check if an override set for the refresh id via a hidden form field
        try {
          refresh_id = ioArgs.args.form.pb_refresh_list_id.value;
          pb_debug('override refresh id to ' + refresh_id);
        }
        catch (e) { }
        
        handle_data_refresh(refresh_id);
        
        dojo.publish("refresh_inline_data", [{refresh_id: refresh_id, form_id: inline_form_idnum}]);
        
        if (hide_form) {
          pb_debug("calling hide_inline_form");
          hide_inline_form();
        }
        
        if (save_callback) {
          pb_debug('running callback: ' + save_callback);
          // TODO: check if the save_callback returns a dojo.Deferred?  rename to postSave?
          save_callback();
        }
      }
      else if ((response == "new") || (isInteger(response))) {
        // redisplay form as an edit now
        itemid = response.toString();

        handle_data_refresh(refresh_id);

        try {
          document.getElementById('item_id_' + refresh_id).value = itemid;
        } catch (e) { }

        //refresh_edit_form(itemid);
        
        if (save_callback) {
          pb_debug('running callback, itemid: ' + itemid);
          save_callback(itemid);
        }
        else {
          pb_debug('refresh_edit_form, itemid: ' + itemid);
          refresh_edit_form(itemid);
        }
      }
      else {
        //alert('Unexpected Server Response: ' + response);
        set_inline_message(response, true);
      }
      PB_Dialog.hide();
      return response;
    }
    
    // if formNode is a domNode, use that to post, otherwise submit to plugin.php with 
    // formNode as content array
    if (formNode.nodeType) {
      dojo.xhrPost({
        form: formNode,
        handleAs: "text",
        preventCache: true,
        handle: _handle_inline_form_post,
        timeout: 100000
      });
    }
    else {
      dojo.xhrPost({
        url: '/pb/plugin/plugin.php',
        content: formNode,
        handleAs: "text",
        preventCache: true,
        handle: _handle_inline_form_post,
        timeout: 100000
      });
    }
  }
  
  function refresh_edit_form(item_id) {
    var _href = inline_form_pane.href;
  
    // clean up the URL from previous additions
    _href = _href.replace('&new_plugin_item=1', '');
    _href = _href.replace(/\&edit_item_id=\d+/g, '');
  
    // passing in a item to edit
    _href = _href + '&edit_item_id=' + item_id.toString();
  
    //alert (_href);
    inline_form_pane.loadingMessage = 'Saving...';
    //inline_form_pane.attr('href', _href);
    inline_form_pane.setHref(_href);
  }

  function handle_data_refresh(refresh_id) {
    try {
      dijit.byId("InlineRefreshPane" + refresh_id).refresh();
    }
    catch (e) { 
      pb_debug("refresh failed!  (id=" + refresh_id + ") trying to refresh all panes"); 

      dojo.query('div[id^="InlineRefreshPane"]').forEach(function(node, i) {
        try {
          d = dijit.getEnclosingWidget(node);
          d.refresh();
          //console.debug(d);
        }
        catch (e) {
          pb_debug("getEnclosingWidget failed - node id: " + node.id);
          console.debug(node);
          console.debug(e);
        }
      });
    }
    
    try {
      eval("grid_store_" + refresh_id + ".fetch()");
      eval("grid_model_" + refresh_id + ".requestRows()");
      dijit.byId("grid_" + refresh_id).refresh();
    } catch (e) { }
  }
  
  function refresh_inline_data(refresh_info) {
    console.log("refresh_inline_data!");
    console.debug(refresh_info);
  }
    
  function change_inline_list_template(fieldid, cur_template, new_template, link_text) {
    var inline_list = dijit.byId("InlineRefreshPane" + fieldid);
    
    var _href = inline_list.href;

    //<a id="change_inline_list_template_461" href="javascript:change_inline_list_template('461', 'list', 'list_all', 'List All Orders');" class="us_b1"><img src="/app/filter.gif" border="0" align="absmiddle"> <span id="change_inline_list_link_text_461">List All Orders<span></a>&nbsp;&nbsp;
  
    // flip flop templates
    
    match_data = _href.match(/\?template=([^\&]+)/);
    cur = match_data[1];
    pb_debug(match_data, 'dir');
    
    if (cur == cur_template) {
      _href = _href.replace(/\?template=[^\&]+/g, '?template=' + new_template);
      document.getElementById("change_inline_list_link_text_461").innerHTML = "Restore Filter";
    }
    else {
      _href = _href.replace(/\?template=[^\&]+/g, '?template=' + cur_template);
      document.getElementById("change_inline_list_link_text_461").innerHTML = link_text;
    }
      
    pb_debug('t: ' + new_template + ' new url: ' + _href);
  
    inline_list.attr('href', _href);
    inline_list.setHref(_href);
  }

  function change_inline_list_sort(list_id, sort_field) {
    inline_list = dijit.byId("InlineRefreshPane" + list_id);
  
    try {
      var _href = inline_list.href;
    } 
    catch (e) {
      alert ('Error Sorting!  Contact Site Administrator!');
      return;
    }
  
    // changing sort starts from page one
    _href = _href.replace(/\&pb_page=\d+/g, '');
  
    // save the current sort on the list
    match_data = _href.match(/[\?|\&]sort=(\-?)([^\&]+)/);
    cur_sort_dir = '-';
    cur_sort = 'id';
  
    if (match_data) {
      cur_sort_dir = match_data[1];
      cur_sort = match_data[2];
    }
    
    // remove previous sort
    _href = _href.replace(/\&sort=[^\&]+/g, '');
    
    if (cur_sort == sort_field) {
      if (cur_sort_dir == '') {
        // changing current sort field to descending
        sort_field = '-' + sort_field;
      }
    }
    
    _href = _href + '&sort=' + sort_field;
  
    inline_list.attr('href', _href);
  }
  
  function change_content_pane_param(pane, param, val, toggle, callback, no_refresh) {
    var _href = pane.href;

    var u = new dojo._Url(_href);
    if (! u.query) { // no query string - wtf
      return;
    }
    var q = dojo.queryToObject(u.query);
    
    if (toggle) {
      if (q[param] && q[param] == '1') {
        val = 0;
      }
      else {
        val = 1;
      }
    }

    q[param] = val;
    var _href = u.path + '?' + dojo.objectToQuery(q);
    
    //console.debug(q);
    //console.log(_href);
    
    if (no_refresh) {
      pane.href = _href;
      pane._hrefChanged = true;
    }
    else {
      //pane.attr('href', _href);
      pane.setHref(_href);
    }
    
    // allow running a callback if sent, nice to do stuff like changing link text 
    if (dojo.isFunction(callback)) {
      dojo.partial(callback, val)();
    }
  }

  function change_inline_list_param(listid, param, val, toggle, callback, no_refresh) {
    var inline_list = dijit.byId("InlineRefreshPane" + listid);
    change_content_pane_param(inline_list, param, val, toggle, callback, no_refresh);
  }

  function change_inline_form_param(param, val, toggle, callback, no_refresh) {
    change_content_pane_param(inline_form_pane, param, val, toggle, callback, no_refresh);
    // update the URL on the dialog stack
    if (dialog_info_stack.count > 0) {
      prev_dlg_info = dialog_info_stack.pop();
      prev_dlg_info['_href'] = inline_form_pane.href;
      pb_debug(prev_dlg_info, 'dir');
      dialog_info_stack.push(prev_dlg_info);
    }
  }
  
  function change_inline_list_page(listid, page) {
    var inline_list = dijit.byId("InlineRefreshPane" + listid);
    
    var _href = inline_list.href;
    _href = _href.replace(/\&pb_page=\d*/g, '');
    _href = _href + '&pb_page=' + page;
    
    pb_debug('change_inline_list_page href: ' + _href);
  
    //inline_list.attr('href', _href);
    inline_list.setHref(_href);
  }

  function remove_restore_inline_list_filter(fieldid, link_text, restore_text) {
    var inline_list = dijit.byId("InlineRefreshPane" + fieldid);
    
    var _href = inline_list.href;

    // remove any pagination when flipping  
    _href = _href.replace(/\&pb_page=\d*/g, '');

    // flip flop templates    
    match_data = _href.match(/\&pb_ignore_filter=1/);
    if (match_data) {
      _href = _href.replace(/\&pb_ignore_filter=1/, '');
      document.getElementById("remove_restore_filter_link_text_" + fieldid).innerHTML = link_text;
    }
    else {
      _href = _href + '&pb_ignore_filter=1';
      document.getElementById("remove_restore_filter_link_text_" + fieldid).innerHTML = restore_text;
    }
  
    pb_debug('remove_restore_filter href: ' + _href);
    inline_list.setHref(_href);
  }
    
  function copy_then_edit_plugin_item(fieldid, copy_id, copy_commtypeid, parent_itemid, extra) {
    //var curdate = new Date();
  
    // run a callback
    var URL = "/pb/callbacks/dojo_copy_item.php" +
           "?copy_id=" + copy_id +
           "&copy_commtypeid=" + copy_commtypeid +
           "&parent_itemid=" + parent_itemid +
           "&fieldid=" + fieldid + extra; // +
    //       "&no_cache=" + curdate.getMilliseconds();
    
    //alert ('Page URL: ' + URL);
  
    dojo.xhrGet({
      url: URL,
      handleAs: "text",
      load: function(response, ioArgs) {
        if (isInteger(response)) {
          try {
            // immediately edit the item after the copy completes
            // TODO: prompt them with the new item id?
            show_inline_form(fieldid, response, '', '');
            // refresh the list too
            dijit.byId("InlineRefreshPane" + fieldid).refresh();
            // TODO: what if grid?
          }
          catch (e) { }
        }
        else {
          alert(response);
        }
        return response;
      },
      error: function(response, ioArgs) {
        alert('XHR Copy Error: ' + response.message);
        return response;
      }, 
      preventCache: true,
      timeout: 20000
    }); 
  }
  
  function handle_dlg_dims(dlg_width, dlg_height) {
    if (dlg_width > 0) {
      //alert ('connect w');
      dlg_width_conn = dojo.connect(inline_form_pane, 'onDownloadEnd', function() {
        pb_debug('setting width to ' + dlg_width);
        inline_form_pane.containerNode.style.width = dlg_width + 'px';
        inline_form_pane.containerNode.style.overflowX = 'auto';
        dojo.disconnect(dlg_width_conn);
      });
    }
    if (dlg_height > 0) {
      //alert ('connect h');
      dlg_height_conn = dojo.connect(inline_form_pane, 'onDownloadEnd', function() {
        pb_debug('setting height to ' + dlg_height);
        inline_form_pane.containerNode.style.height = dlg_height + 'px';
        inline_form_pane.containerNode.style.overflowY = 'auto';
        dojo.disconnect(dlg_height_conn);
      });
    }
  }
  
  function restore_inline_form() {
    prev_dlg_info = dialog_info_stack.peek()
    
    _url = prev_dlg_info['_href'];
    
    // restore dialog dimensions
    //handle_dlg_dims(prev_dlg_info['_dlg_width'], prev_dlg_info['_dlg_height'])
    
    // main thing here is to restore the inline_form_idnum
    if (_url.match(/\&pb_link_fieldid=(\d+)/g, '')) {
      inline_form_idnum = _url.replace(/^.*\&pb_link_fieldid=(\d+).*$/g, '$1');
    }
    else if (_url.match(/\&commtypeid=(\d+)/g, '')) {
      inline_form_idnum = _url.replace(/^.*\&commtypeid=(\d+).*$/g, '$1');
    }
    
    inline_form_pane.titleNode.innerHTML = prev_dlg_info['_title'] + '&nbsp;&nbsp;&nbsp;';
  
    pb_debug('restoring form url for inline_form_idnum: ' + inline_form_idnum + ': ' + _url);

    dijit.byId('PB_Dialogx').setHref(_url);
    dijit.byId('PB_Dialogx').show();
    dijit.byId('PB_Dialogx').layout();
  }
  
  function show_inline_form() {
    var no_args = show_inline_form.arguments.length;
    inline_form_idnum = show_inline_form.arguments[0];
    //console.debug(show_inline_form.arguments);
    
    pb_debug('inline_form_idnum: ' + inline_form_idnum);
    if (inline_form_idnum == '') {
      PB_Dialog.titleNode.innerHTML = 'Error!';
      PB_Dialog.setContent("Error!  Empty Inline Form Id!  Contact Administrator");
      PB_Dialog.show();
      return;
    }
    //inline_form_pane = dijit.byId("ModalInlineFormPane" + inline_form_idnum);
    inline_form_pane = dijit.byId("PB_Dialogx");
    inline_form_pane.loadingMessage = 'Loading...';
  
    cont_node = dojo.byId('dojo_inline_form_container_' + inline_form_idnum);
    title_node = dojo.byId('dojo_inline_form_title_' + inline_form_idnum);
    
    if ((cont_node == null) || (title_node == null)) {
      PB_Dialog.titleNode.innerHTML = 'Error!';
      PB_Dialog.setContent("Error!  Could not find inline form (" + inline_form_idnum + ")!  Contact Administrator");
      PB_Dialog.show();
      return;
    }
    
    _href = decodeURIComponent(cont_node.innerHTML);
    title = decodeURIComponent(title_node.innerHTML);

    pb_debug('URL pre: ' + _href);

    //try {
    //  var _href = inline_form_pane.href;
    //} 
    //catch (e) {
    //  alert ('Error Showing Inline Form id: ' + inline_form_idnum + '!  Contact Site Administrator!');
    //  return;
    //}
  
    // clean up the URL from previous additions
    //_href = _href.replace('&pb_initialize=1', '');
    //_href = _href.replace('&new_plugin_item=1', '');
    //_href = _href.replace(/\&edit_item_id=\d+/g, '');
  
    if (show_inline_form.arguments[1] != '' && show_inline_form.arguments[1] != undefined) {
      // passing in a item to edit
      _href = _href + '&edit_item_id=' + show_inline_form.arguments[1];
      //alert (show_inline_form.arguments[1] + ' url: ' + _href);
    }
    else {
      // indicate to back end that we are adding an item, not editting one
      _href = _href + '&new_plugin_item=1';
    }
  
    if (show_inline_form.arguments[2] != '' && show_inline_form.arguments[2] != undefined) {
      // passing in a parent item id
      _href = _href.replace(/\&parent_item_id=\d+/g, '');
      _href = _href + '&parent_item_id=' + show_inline_form.arguments[2];
    }
  
    if (show_inline_form.arguments[3] != '' && show_inline_form.arguments[3] != undefined) {
      // passing in arbitrary extra params
      _href = _href + show_inline_form.arguments[3];
    }
    
    if (show_inline_form.arguments[4] != '' && show_inline_form.arguments[4] != undefined) {
      // passing in a plugin id
      _href = _href.replace(/\&commtypeid=\d+/g, '');
      _href = _href + '&commtypeid=' + show_inline_form.arguments[4];
    }
    
    if (show_inline_form.arguments[5] != '' && show_inline_form.arguments[5] != undefined) {
      // passing in a title
      title = show_inline_form.arguments[5];
    }
    
    pb_debug('URL post: ' + _href);

    // some width / height checking
    dlg_width = 0;
    dlg_height = 0;
    
    dlg_max_width = dojo.byId('pb_dialog_max_width_' + inline_form_idnum).innerHTML;
    dlg_max_height = dojo.byId('pb_dialog_max_height_' + inline_form_idnum).innerHTML;
    dlg_min_height = dojo.byId('pb_dialog_min_height_' + inline_form_idnum).innerHTML;
    
    dlg_set_width = dojo.byId('pb_dialog_width_' + inline_form_idnum).innerHTML;
    dlg_desired_width = dojo.byId('pb_dialog_desired_width_' + inline_form_idnum).innerHTML;
    dlg_set_height = dojo.byId('pb_dialog_height_' + inline_form_idnum).innerHTML;

    if (dlg_desired_width > 0) {
      //console.log(dlg_desired_width);
      try {
        inline_form_pane.desiredWidth = dlg_desired_width;
      }
      catch (e) { console.debug(e); }
    }

    pb_debug('max w: ' + dlg_max_width);
    pb_debug('set w: ' + dlg_set_width);
    
    if (dojo.byId('pb_dialog_autosize_' + inline_form_idnum).innerHTML >= 1) {
      percentage = .85;
      if (dojo.byId('pb_dialog_autosize_' + inline_form_idnum).innerHTML > 1) {
        percentage = dojo.byId('pb_dialog_autosize_' + inline_form_idnum).innerHTML / 100;
      }
      
      // try to get viewport width/height and figure out what 85% (or passed percentage)
      // of viewport is - if viewport dims not available use sensible default for 800x600 screen.
      viewport = dijit.getViewport();
      
      pb_debug(viewport, 'dir');
      
      dlg_width = viewport.w ? viewport.w * percentage : 750;
      dlg_height = viewport.h ? viewport.h * percentage - 30 : 550; // subtract 30 for dialog title bar, not part of containerNode below
      //dlg_width = dojo.cookie('pb_viewport_w') ? dojo.cookie('pb_viewport_w') * .85 : 750;
      //dlg_height = dojo.cookie('pb_viewport_h') ? dojo.cookie('pb_viewport_h') * .85 : 550;

      if ((dlg_max_width > 0) && (dlg_width > dlg_max_width)) {
        dlg_width = dlg_max_width;
      }
      if ((dlg_max_height > 0) && (dlg_height > dlg_max_height)) {
        dlg_height = dlg_max_height;
      }
    }

    // if a set width/height is passed in use that (note, this can be used in conjunction with autosize to
    // autosize only one of the dimensions.
    if (dlg_set_width > 0) {
      dlg_width = dlg_set_width;
    }
    if (dlg_set_height > 0) {
      dlg_height = dlg_set_height;
    }
    
    if (dlg_min_height > 0) {
      console.log('min: ' + dlg_min_height);
      inline_form_pane.minHeight = dlg_min_height;
    }
    
    pb_debug('h: ' + dlg_height + ' w: ' + dlg_width);
    //inline_form_pane.containerNode.style.width = '';
    //inline_form_pane.containerNode.style.height = '';
    //inline_form_pane.containerNode.style.overflowX = '';
    //inline_form_pane.containerNode.style.overflowY = '';
    
    var width_conn;
    var height_conn;
    
    handle_dlg_dims(dlg_width, dlg_height);
    
    // save information on this dialog to a stack in case we have multiple dialogs
    dialog_info = {
      '_href':       _href,
      '_title':      title,
      '_dlg_width':  dlg_width,
      '_dlg_height': dlg_height
    };

    pb_debug(dialog_info, 'dir');
    
    try {
      dialog_info_stack.push(dialog_info); // save the URL on a stack in case we show multiple dialogs
    }
    catch (e) { console.debug(e); }
    
    pb_debug('inline_form href: ' + _href);

    inline_form_pane.setHref(_href);
    //inline_form_pane.titleNode.innerHTML = title + '&nbsp;&nbsp;&nbsp;';
    
    inline_form_pane.show();
    inline_form_pane.layout();
  }
  
  function change_inline_list(fieldid, itemid) {
    if (! itemid) {
      console.log('change_inline_list empty item id: ' + fieldid);
      return;
    }

    inline_list = dijit.byId("InlineRefreshPane" + fieldid);
  
    try {
      var _href = inline_list.href;
    } 
    catch (e) {
      alert ('Error Changing Inline List!  Contact Site Administrator!');
      return;
    }
  
    // clean up the URL from previous changes
    // Decoupled: item_id was productId
    _href = _href.replace(/\&item_id=\d+/g, '');
  
    // change the itemid
    _href = _href + '&item_id=' + itemid.toString();
  
    //alert (_href);
    inline_list.setHref(_href);
  }
  
  function change_inline_list_filter(list_id, search_id) {
    inline_list = dijit.byId("InlineRefreshPane" + list_id);
  
    try {
      var _href = inline_list.href;
    } 
    catch (e) {
      alert ('Error Changing Inline List Filter!  Contact Site Administrator!');
      return;
    }
  
    // clean up the URL from previous changes
    // Decoupled: item_id was productId
    _href = _href.replace(/\&pb_filter_search_id=\d+/g, '');
    // changing filter starts over from page one
    _href = _href.replace(/\&pb_page=\d+/g, '');
  
    // set the search results id to use
    try {
      _href = _href + '&pb_filter_search_id=' + search_id.toString();
    }
    catch (e) {
      alert ('Error Changing Inline List Filter - Bad Search ID!  Contact Site Administrator!');
      return;
    }
  
    // console.log(_href);
    inline_list.setHref(_href);
  }
  
  function hide_inline_form() {
    pb_debug('hiding inline_form_pane');
    console.debug(inline_form_pane);
    try {
      inline_form_pane.hide();
      console.log("hidden!");
    }
    catch (e) { 
      console.log('Warning!  Could not hide inline form');
    }
  }
  
  function delete_inline_item(itemid, pluginid, fieldid, hashid, redirect, callback) {
    var answer = confirm ("Are you sure you want to delete this item?");
    if (! answer) { return; }

    // run a callback
    var URL = "/pb/callbacks/dojo_item_delete.php" +
           "?itemid=" + itemid +
           "&pluginid=" + pluginid +
           "&key=" + hashid; // +

    dojo.xhrGet({
      url: URL,
      handleAs: "text",
      timeout: 20000,
      preventCache: true,
      load: function(response, ioArgs) {
        if (response == "ok") {
          if (callback) {
            callback();
          }
          if (redirect) {
            window.location = redirect;
          } else {
            handle_data_refresh(fieldid);
          }
        }
        else {
          console.log('Unexpected response: ' + response);
          set_inline_message(response, true);
          show_pb_dialog(response, 'Delete Failed');
        }
        return response;
      },
      error: function(response, ioArgs) {
        if (response.dojoType != "cancel") {
          alert('Delete XHR Error! ' + response.message);
        }
        return response;
      }
    });
  }
}

