// Opens an EQS popup window
//
// params:
//   url      - the url to open
//   name     - the id of the window - optional
//   width    - width of window - optional
//   height   - height of window - optional
//   controls - show all controls in the window - optional
//
function eqsPopupWindow(url) {
  var argv = arguments;
  var argc = arguments.length;
  var name     = (argc > 1) ? "eqsPopup" + argv[1] : "eqsPopupWindow";
  var width    = (argc > 2) ? argv[2] : 500;
  var height   = (argc > 3) ? argv[3] : 450;
  var controls = (argc > 4) ? argv[4] : false;

  var controls_text = (controls) ? 'yes' : 'no';

  var popupWin = window.open(url, name, "toolbar=" + controls_text + ",status=" + controls_text + ",scrollbars=yes,location=" 
			     + controls_text + ",resizable=" + controls_text + ",width=" + width + ",height=" + height);
  popupWin.focus();

  return popupWin;
}

// Open an EQS help window
function eqsPopupHelp(label) {
  var url = eqs_base_url + 'cgi-bin/admin/edit_helptext.pl?pid=' + eqs_pid + '&label_name=' + label;

  if( eqs_cgisessid ) {
    url += "&CGISESSID=" + eqs_cgisessid;
  }

  eqsPopupWindow(url, 'Help');
}

// Display a link in the main frame
function lenke(ev, link) {
  if (!ev) ev = window.event;

  var body_frame_name = 'qabody_' + eqs_username + '_' + eqs_pid;
  
  var found = false;

  if (top.frames[body_frame_name]) {
    top.frames[body_frame_name].location.href=link;
    found = true;
  } 

  if ( ! found ) {
    var RegularExpression  =  /^qabody/;
    var num_frames = top.frames.length;
    for (var i = 0; i < num_frames; i++) {
      if (top.frames[i].name.search(RegularExpression) == 0) {
        top.frames[i].location.href=link;
        found = true;
      }
    }
  }
  
  if ( ! found ) {
    var num_frames = top.frames.length;
    top.frames[num_frames - 1].location.href=link;
    found = true;
    
  }
  
  stop_event(ev);
}


/**
 * Go to a new page based on input from a <select>.
 * The typical case for this is a list of objects where 
 * each objects has a list of possible actions in a
 * select. 
 *
 * The url for the new page will have the same path
 * as the current page ("window.location.pathname") and a 'pid' 
 * parameter.
 * In addition, the 'do' parameter will be the value of the
 * currently selected option, and the ID parameter will be the
 * value of the HTML ID of the select element.
 *
 * NB! If the value from the option is "none", no action will be taken.
 *
 * @param ID The HTML ID of the active select element.
 */
function runCommand(ID) {
    var select  = document.getElementById(ID);
    var index   = select.selectedIndex;
    var opt     = select.options[index]; 
    var command = opt.value;
    var url = window.location.pathname + "?pid=" + eqs_pid;

    if( eqs_cgisessid ) {
      url += "&CGISESSID=" + eqs_cgisessid;
    }
    
    if (command != "none") {
        url += ("&do=" + command + "&ID=" + ID);
        window.location = url;
    }
    
    return true;
}


function runCommandSelect(select, ID) {
    var command = select.options[select.selectedIndex].value;
    if (command != "none") {
      var url = window.location.pathname + "?pid=" + eqs_pid + "&do=" + command + "&ID=" + ID;

      if( eqs_cgisessid ) {
        url += "&CGISESSID=" + eqs_cgisessid;
      }
      
      window.location = url;
    }
}

/**
 * Set the cursor style of 'obj' to 'pointer'.
 * Use this instead of obj.style.cursor directly 
 * to prevent an exception on IE 5.5.
 *
 * @param obj The element to set the cursor for.
 */
function setPointer(obj) {
    try {
        obj.style.cursor = 'pointer';
    }
    catch (e) { 
        // Throws an error on IE 5.5, even though the property
        // seems to be available. Is it read-only?
    }
}

function precedingZero(number) {

  if (number < 10) {
    return "0" + number;
  }
  return number;

}

