/* =============================================================================
* FUNCTIONS FOR USE WITH POPUP WINDOWS
-------------------------------------- */

///////////////
// popup_win //
///////////////
//
// Attributes:
// - required String url: the page to open in the popup
// - optional Object dict: a dictionary of other popup attributes
//   > optional String target: the target window name. predefined are:
//     -- '_blank': always a new window (default)
//     -- '_self': the originating window
//     -- '_top': the top level frame
//   > optional Object attrs: attributes of the new window
//     -- int/bool width: the width of the window (use false to omit)
//     -- int/bool height: the height of the window (use false to omit)
//     -- int/bool top: distance from the top of the screen (use false to omit)
//     -- int/bool left: distance from the left of the screen (use false to omit)
//     -- bool scrollbars: should scrollbars be used
//     -- bool resizable: should the window be resizable by the user
//     -- bool menubar: should the file/edit/view/etc menubar be displayed
//     -- bool toolbar: should the back/next/stop/etc toolbar be displayed
//     -- bool location: should the address bar be displayed
//     -- bool status: should the status bar be displayed
//
// Sample formats:
//   popup_win('http://www.imagescape.com/')
//   popup_win('http://www.imagescape.com/'
//             {'target':'named_win'})
//   popup_win('http://www.imagescape.com/'
//             {'attrs':{'width':400,'scrollbars':false}})
//   popup_win('http://www.imagescape.com/'
//             {'target':'named_win',
//              'attrs':{'width':false,'toolbar':false}})
////////////////////////////////////////////////////////////////////////////////
function popup_win(url, dict) {
  if(!url) {
    return false;
  }

  // default target and attributes
  var target = '_blank';
  var attrs = {
          'width' : 600,
         'height' : 400,
            'top' : 50,
           'left' : 50,
     'scrollbars' : true,
      'resizable' : true,
        'menubar' : true,
        'toolbar' : true,
    'directories' : false,
       'location' : true,
         'status' : true
  };

  if (dict) {
    if (dict.target && dict.target != '') {
      target = dict.target;
    }
    if (dict.attrs && dict.attrs == 'false') {
      attrs = {};
    } else if (dict.attrs) {
      for (var attr in dict.attrs) {
        attrs[attr] = dict.attrs[attr];
      }
    }
  }

  var attr_str = '';
  for (var attr in attrs) {
    if (attr_str != '') {
      attr_str += ',';
    }

    if (attrs[attr] && attrs[attr] != '') {
      if (attrs[attr] === true) {
        attrs[attr] = 1;
      } else if (attrs[attr] === false) {
        attrs[attr] = 0;
      }

      attr_str += attr + '=' + attrs[attr];
    }
  }

  return window.open(url, target, attr_str);
}
