/****************************************************************

  Traction Software, Inc. Confidential and Proprietary Information

  Copyright (c) 1996-2007 Traction Software, Inc.
  All rights reserved.

****************************************************************/

// PLEASE DO NOT DELETE THIS LINE -- make copyright depends on it.
// requires /images/modern/js/shared.js or /html/shared.js

var mouseScreenX = -1;
var mouseScreenY = -1;

var FORM_ACTION_READ_ONLY  = "/traction/read";
var FORM_ACTION_READ_WRITE = "/traction/post";

// stick this function in the onMouseDown= handler for the image or
// button that launches a dialog.  this will ensure that the dialog is
// positioned correctly -- ajm
//
// ex. onMouseDown="grabMousePos(event)"
function grabMousePos(e) {
  mouseScreenX = e.screenX;
  mouseScreenY = e.screenY;

  //  alert("grabMousePos: "+mouseScreenX+","+mouseScreenY);
}

function grabCenterWindow() {

  mouseScreenX = (window.screenX ? window.screenX : window.screenLeft) + ((window.innerWidth ? window.innerWidth : document.body.clientWidth) / 2);
  mouseScreenY = (window.screenY ? window.screenY : window.screenTop) + ((window.innerHeight ? window.innerHeight : document.body.clientHeight) / 2);
}

function grabCenterScreen() {
  mouseScreenX = window.screen.availWidth / 2;
  mouseScreenY = window.screen.availHeight / 2;
}

// --------------------------------------------------------------------------------
// Insert Label Dialog - a dialog for selecting a set of labels from
// the list of all labels.
//

var INSERT_LABEL_URL = FORM_ACTION_READ_ONLY + "?type=inslabel";
var INSERT_LABEL_NAME = "";
var INSERT_LABEL_WIDTH  = 480;
var INSERT_LABEL_HEIGHT = 350;
var INSERT_LABEL_FEATURES = "scrollbars=no, titlebar=no, resizable=yes, alwaysRaised=yes, menubar=no, location=no, status=no, toolbar=no, directories=no";

var insertLabelWin = new Array();

// this function is only here for backward compatibility -- the width
// and height should be provided by the UserAgent properties so that
// it is correct for each browser -- ajm 09.07.01
function openInsertLabelDialog(project) {
  openInsertLabelDialog(INSERT_LABEL_FEATURES, INSERT_LABEL_WIDTH, INSERT_LABEL_HEIGHT, project);
}


// project: the current project (will be selected in the label
// chooser).
//
// labels: comma separated list of labels in rapid selector format.
//
// dialogname: a name that can be used for this dialog.  this
// guarantees that only a single dialog be opened with the same name
// and is returned in the closeInsertLabelDialog function.
//
// extras: extra parameters (in url format) that should appended to
// the url to open the InsertLabel view.  currently supported is
// "&fqlabels=[true]/false".
//

function openInsertLabelDialogSimple(project, labels, dialogname, extras) {
  return openInsertLabelDialog2(ua("modern_insertlabel_window_features"),
				ua("modern_insertlabel_window_width"),
				ua("modern_insertlabel_window_height"),
				project,
				labels,
				dialogname,
				extras);
}

function openInsertLabelDialog(features, width, height, project, labeledit, dialogname, extras) {

    labels = (labeledit) ? labeledit.value : document.fm.labelnames.value;
    return openInsertLabelDialog2(features, width, height, project, labels, dialogname, extras);
}


// we need this function because + is not escaped by the escape()
// function.  bogus, but well documented.
function escapeplus(str) {
  str = escape(str);
  return str.replace('+', "%2b");
}

function openInsertLabelDialog2(features, width, height, project, labels, dialogname, extras) {

    if (insertLabelWin[dialogname] && !insertLabelWin[dialogname].closed){
      
        // there's already a window
        insertLabelWin[dialogname].focus();
     
    } else {
	var url = INSERT_LABEL_URL;
	if (labels != null) {
	    url += "&preselected="+encode_url_parameter(labels);
	}
	if (project) {
	  url += "&proj="+encode_url_parameter(project);
	}
	if (dialogname != null) {
	  url += "&dialogname="+encode_url_parameter(dialogname);
	}
	if (extras) {
	  url += extras;
	}
	insertLabelWin[dialogname] = do_window_open(url, INSERT_LABEL_NAME, features + getSizeAndPosition(width, height));
    }
}

function closeInsertLabelDialog(labelArray, dialogname) {
  alert("closeInsertLabelDialog("+labelArray+","+dialogname+"): Override this function to do something!");
}

// --------------------------------------------------------------------------------
// generic dialog

var DIALOG_BASE_URL = FORM_ACTION_READ_ONLY + "?type=";
var dialogWin = new Array();

var DIALOG_FLAG_MULTIPLE_WINDOWS = (1 << 0);
var DIALOG_FLAG_REUSE_WINDOW     = (1 << 1);

var WINDOW_FEATURES_DIALOG = "scrollbars=no,titlebar=no,resizable=yes,alwaysRaised=no,menubar=no,location=no,status=no,toolbar=no,directories=no";
var WINDOW_FEATURES_SCROLL = "scrollbars=yes,titlebar=no,resizable=yes,alwaysRaised=no,menubar=no,location=no,status=no,toolbar=no,directories=no";
var WFD = WINDOW_FEATURES_DIALOG;
var WFS = WINDOW_FEATURES_SCROLL;

// Including these here will allow them to be included in the JS.
//i18n("acleditor_project_window_width_extra_iewin")
//i18n("acleditor_project_window_height_extra_iewin")
//i18n("groupeditor_window_width_extra_iewin")
//i18n("groupeditor_window_height_extra_iewin")
//i18n("acleditor_server_window_width_extra_iewin")
//i18n("acleditor_server_window_height_extra_iewin")

function openDialog(viewtype, propkey, dialogname, extras, flags) {

  var features = ua(propkey+"_features");

  // Introduced useragent- and locale-specific extra width and height,
  // because some windows look absolutely gross on Japanese Windows
  // with regular dimensions.  [shep 09.Sep.2005]
  var width;
  try {
    width = new String(parseInt(ua(propkey+"_width"),  10) + parseInt(i18n(propkey+"_width_extra_"+userAgentName,  "0"), 10));
  } catch (xcp) {
    width = ua(propkey+"_width");
  }
  var height;
  try {
    height = new String(parseInt(ua(propkey+"_height"), 10) + parseInt(i18n(propkey+"_height_extra_"+userAgentName, "0"), 10));
  } catch (xcp) {
    height = ua(propkey+"_height");
  }
  openDialogEx(viewtype, features, width, height, "", dialogname, extras, flags);

}

function openDialogEx(viewtype, features, width, height, winname, dialogname, extras, flags) {

  var winkey = viewtype + ":" + dialogname;

  var multiple = ((flags & DIALOG_FLAG_MULTIPLE_WINDOWS) != 0);
  var reuse    = ((flags & DIALOG_FLAG_REUSE_WINDOW)     != 0);

  var url = DIALOG_BASE_URL + viewtype;

  if (dialogname) {
    url = add_param(url, "dialogname", dialogname);
  }
  
  if (extras) {
    url += extras;
  }

  if (!multiple && dialogWin[winkey] && !dialogWin[winkey].closed) {

    if (reuse) {
      dialogWin[winkey].location = url;
    }
    // there's already a window
    dialogWin[winkey].focus();

  } else {

    dialogWin[winkey] = do_window_open(url, winname, features + getSizeAndPosition(width, height));
  }

}

//////////////////////////////////////////////////////////////////////
// params is an Array object whose contents depends on the type of
// view being called.
//
// this function should be overridden once, with a test for the
// viewtype for each dialog appearing on the page.  if multiple
// dialogs of the same viewtype appear on one page, use the dialogname
// parameter to differentiate.
//
function closeDialog(viewtype, dialogname, params) {
  alert("closeDialog("+viewtype+","+dialogname+","+params+") Override this function to do something!");
}

// --------------------------------------------------------------------------------
// edit relationships dialog

var RELATIONSHIPS_URL = FORM_ACTION_READ_ONLY + "?type=editrelationships";
var relationshipsWin = new Array();

				
// project: the project from which the verbs will be listed
// verbString: a string containing the selected verbs, comma separated
// targetsString: a string containing the targets, comma separated
// dialogname: a name that will be passed back to the closeRelationshipsDialog function
// extra: any extra parameters that are permitted, in &param=value form
//
function openRelationshipsDialog(project, verbsString, targetsString, dialogname, extras) {
  openRelationshipsDialog2(ua("modern_relationships_window_features"),
			   ua("modern_relationships_window_width"),
			   ua("modern_relationships_window_height"),
			   project,
			   verbsString,
			   targetsString,
			   dialogname,
			   extras);
  
}
function openRelationshipsDialog2(features, width, height, project, verbsString, targetsString, dialogname, extras) {

    if (relationshipsWin[dialogname] && !relationshipsWin[dialogname].closed){
      
        // there's already a window
        relationshipsWin[dialogname].focus();
     
    } else {
        var url = RELATIONSHIPS_URL;

	url = add_param(url, "proj", project);
	url = add_param(url, "verbs", verbsString);
	url = add_param(url, "targets", targetsString);
	url = add_param(url, "dialogname", dialogname);

	if (extras) {
	  url += extras;
	}
	
	relationshipsWin[dialogname] = do_window_open(url, "", features + getSizeAndPosition(width, height));
    }  
}


function add_param(url, name, value) {
  return (name && value) ? url + "&" + name + "=" + encode_url_parameter(value) : url;
}

function closeRelationshipsDialog(dialogname, verbsString, targetsString, count) {
  alert("closeRelationshipsDialog("+dialogname+","+verbsString+","+targetsString+"): Override this function to do something!");
}


var ATTACHMENTS_URL = FORM_ACTION_READ_ONLY + "?type=editattachments";
var attachmentsWin = new Array();

/**
 * @param attachmentsString the string containing the serialization of
 * the Java com.traction.sdk.file.FileData objects representing the
 * files that are already attached to the entry as well as files
 * already added to be attached to the entry.

 * @param startAttachmentNumber an int that represents the number from
 * which any numbering of new attachments should start.  This should
 * always be the same number until an entry update is submitted to the
 * journal so that if any files are removed from the list of
 * attachments before being attached, other new attachments will
 * always be numbered starting from the same number.

 * @param dialogname a string that will be passed back to the
 * closeAttachmentsDialog function to identify the current
 * editattachment session.

 * param extras a string that will be added to the URL to be used to
 * open the editattachments view.  It should be in the form of URL
 * parameters (&param1=value1&param2=value2...).
 */
function openAttachmentsDialog(attachmentsString, startAttachmentNumber, isEdit, renumber, dialogname, extras) {

  if (attachmentsWin[dialogname] && !attachmentsWin[dialogname].closed){

    // there's already a window
    attachmentsWin[dialogname].focus();

  } else {

    var url = ATTACHMENTS_URL;

    url = add_param(url, "isedit", (isEdit ? "true" : "false"));
    url = add_param(url, "renumber", (renumber ? "true" : "false"));
    url = add_param(url, "filecollection_files", attachmentsString);
    // add_param wants a String, so convert startAttachmentNumber to a String.  [shep 20.May.2005]
    url = add_param(url, "filecollection_next_file_number", new String(startAttachmentNumber));

    if (extras) {
      url += extras;
    }

    var features = ua("editattachments_window_features");
    var width    = ua("editattachments_window_width");
    var height   = ua("editattachments_window_height");

    grabCenterWindow();

    attachmentsWin[dialogname] = do_window_open(url, name, features + getSizeAndPosition(width, height));
  }

}

/**
 * dialogname: a string that identifies the current editattachment session.
 * attachmentsString: the string containing the updated serialization of the
 *   Java com.traction.sdk.file.FileData objects representing the files that are
 *   already attached to the entry as well as files already added to be attached
 *   to the entry.  It should incorporate any changes that have been made to the
 *   FileData properties, particularly the description.
 * count: the number of files whose FileData objects are represented in the
 *   newAttachmentsString parameter's value.
 * fm: the FORM whose attachment data is being updated.
 */
function closeAttachmentsDialog(dialogname, newAttachmentsString, count, fm) {
  alert("closeAttachmentsDialog("+"'"+dialogname+"','"+newAttachmentsString+"',"+count+", "+(fm != null ? fm.id : "(no form)")+"): Override this function to do something!");
}


// labelArray: an array containing the values of the labels selected
// they will be in rapid selector format.  ex. :news for existing
// labels, +:foobar for new labels.
//this func is defined in newentrysdk.js
//function closeInsertLabelDialog(labelArray) {
//  alert("closeInsertLabelDialog: Override this function to do something!");
//}

// --------------------------------------------------------------------------------
// Sections dialog - for editing sections on an edit form

var SECTIONS_URL = FORM_ACTION_READ_ONLY + "?type=editsections";
var sectionsWin = new Array();

function openSectionsDialog(dialogname, sectionsString, extras) { 

  if (sectionsWin[dialogname] && !sectionsWin[dialogname].closed) {

    // there's already a window
    sectionsWin[dialogname].focus();

  } else {
    
    var url = SECTIONS_URL;
    
    url = add_param(url, "sections", sectionsString);

    if (extras) {
      url += extras;
    }

    var features = ua("editsections_window_features");
    var width    = ua("editsections_window_width");
    var height   = ua("editsections_window_height");

    grabCenterWindow();
    
    sectionsWin[dialogname] = do_window_open(url, name, features + getSizeAndPosition(width, height));
  }

}

function closeSectionsDialog(dialogname, newSectionsString, count) {
  alert("closeSectionsDialog('"+dialogname+"','"+newSectionsString+"',"+count+"): Override this function to do something!");
}


// --------------------------------------------------------------------------------
// Edit Names Dialog


var NAMES_URL = FORM_ACTION_READ_ONLY + "?type=editnames";
var namesWin = new Array();


/**
 * @param linkByNames indicates whether linking by name is supported
 * for this article.
 * @param forwardLinkName the forward link name, if any, associated
 * with the article (for new articles only).
 * @param forwardLinkProject the name of the project in which the
 * forward link name specified, if any, will be created.
 * @param editTargetID the Traction ID of the article being edited, if
 * any.
 * @param projectName the name of the project that is currently
 * selected in the edit view.
 * @param title the current title of the article.
 * @param dialogname the name of the dialog window
 * @param extras extra URL parameters to be added to the URL for the
 * editnames view.
 */
function openNamesDialog(linkByNames, linkByNamesLocked, namesString, forwardLinkName, forwardLinkProject, editTargetID, projectName, title, dialogname, extras) {

  if (namesWin[dialogname] && !namesWin[dialogname].closed){

    // there's already a window
    namesWin[dialogname].focus();

  } else {

    var url = NAMES_URL;

    url = add_param(url, "linkbynames",           linkByNames);       // indicates whether name linking is active for this entry
    url = add_param(url, "linkbynameslocked",     linkByNamesLocked); // indicates whether name linking is required for this entry
    url = add_param(url, "namedata",              namesString);       // the existing name data to be decoded
    url = add_param(url, "edit_edit_original_id", editTargetID);      // for dialog HTML title ("Names for [Traction ID]" or something)
    if (projectName != null) {
      if (projectName.indexOf("::") == 0) {
	projectName = projectName.substring(2);
      }
    }
    url = add_param(url, "proj",         projectName);       // for default name scoping (for new entries)
    url = add_param(url, "title",        title);             // the article's current title, for determining which of the names, if any, is the title
    url = add_param(url, "forwardlinkname", forwardLinkName);    // a name that is required to be assigned to this article
    url = add_param(url, "forwardlinkproj", forwardLinkProject); // the project scoping for a name that is required to be assigned to this article

    if (extras) {
      url += extras;
    }

    var features = ua("editnames_window_features");
    var width    = ua("editnames_window_width");
    var height   = ua("editnames_window_height");

    grabCenterWindow();

    namesWin[dialogname] = do_window_open(url, name, features + getSizeAndPosition(width, height));

  }

}


/**
 * This function is invoked by the names dialog (editnames) when the
 * user clicks the OK button.
 */
function closeNamesDialog(dialogname, newNamesString, newTitle, count, fm) {
  alert("closeAttachmentsDialog("+"'"+dialogname+"','"+newNamesString+"',"+count+", '"+newTitle+"',"+(fm != null ? fm.id : "(no form)")+"): Override this function to do something!");
}

// --------------------------------------------------------------------------------
// Choose Name Dialog

var CHOOSE_NAME_URL = FORM_ACTION_READ_ONLY + "?type=choosename";
var chooseNameWin = new Array();

function openChooseNameDialog(dialogname, editTargetID, project, currentName, changedName, extras) {

  if (chooseNameWin[dialogname] && !chooseNameWin[dialogname].closed) {

    // there's already a window
    chooseNameWin[dialogname].focus();

  } else {

    var url = CHOOSE_NAME_URL;

    url = add_param(url, "edit_edit_original_id", editTargetID);
    url = add_param(url, "proj", project);
    url = add_param(url, "oldname", currentName);
    url = add_param(url, "newname", changedName);

    if (extras) {
      url += extras;
    }

    var features = ua("choosename_window_features");
    var width    = ua("choosename_window_width");
    var height   = ua("choosename_window_height");

    grabCenterWindow();

    chooseNameWin[dialogname] = do_window_open(url, name, features + getSizeAndPosition(width, height));
  }

}


function closeChooseNameDialog(projectID, projName, lastName, newName) {
  alert("closeChooseNameDialog(" + projectID, ", '" + projName + "', '" + lastName + "', '" + newName + "'): Override this function to do something!");
}


// --------------------------------------------------------------------------------
// User Details dialog - display details of a user

var USER_DETAILS_URL = FORM_ACTION_READ_ONLY + "?type=userdetails&username=";
var USER_DETAILS_NAME = "";
var USER_DETAILS_WIDTH  = 250;
var USER_DETAILS_HEIGHT = 400;
var USER_DETAILS_FEATURES = "scrollbars=yes, titlebar=no, resizable=yes, alwaysRaised=yes, menubar=no, location=no, status=no, toolbar=no, directories=no";

function userdetails(user) {
  openUserDetailsDialog(user);
}

function openUserDetailsDialog(user) {
  do_window_open(USER_DETAILS_URL + user, USER_DETAILS_NAME, USER_DETAILS_FEATURES + getSizeAndPosition(USER_DETAILS_WIDTH, USER_DETAILS_HEIGHT));
}


// --------------------------------------------------------------------------------
// Modify Collection Portlet (MCP) Dialog

var MCP_URL = FORM_ACTION_READ_ONLY + "?type=mcp";
var MCP_NAME = "ModifyCollectionPortlet";  // only allow one, because otherwise it gets complicated

var mcpWin = new Array();

function openModifyCollectionPortletDialog(index, column, username, collection, portletsize, title, oneUser) {

  var url = MCP_URL;

  if (index != null) {
    url += "&index="+index;
  }
  if (column != null) {
    url += "&column="+column;
  }
  if (username != null) {
    url += "&username="+username;
  }
  if (collection != null) {
    url += "&collection="+collection;
  }
  if (portletsize != null) {
    url += "&portletsize="+portletsize;
  }
  if (title && title != null) {
    url += "&title="+title;
  }
  if (oneUser) {
    url += "&oneuser=true";
  }
  
  var windowName = MCP_NAME;
  if (mcpWin[windowName] && !mcpWin[windowName].closed) {

    var mcpwindow = mcpWin[windowName];
    
    // there's already a window
    mcpwindow.focus();
    
    // navigate to the new one
    mcpwindow.location = url;
    
  } else {

    mcpWin[windowName] = do_window_open(url, 
					MCP_NAME, 
					ua("modify_collection_portlet_window_features") + 
					getSizeAndPosition(ua("modify_collection_portlet_window_width"), 
							   ua("modify_collection_portlet_window_height")));

  }
}

function closeModifyCollectionPortletDialog(index, column, username, collection, portletsize) {
  alert("closeModifyCollectionPortletDialog("+index+","+column+"): Override this function to do something!");
}

// --------------------------------------------------------------------------------
// Calendar Dialog - a dialog for selecting a date

var CALENDAR_URL = FORM_ACTION_READ_ONLY + "?type=calendar";
var CALENDAR_NAME = "";
var CALENDAR_WIDTH  = 215;
var CALENDAR_HEIGHT = 215;
var CALENDAR_FEATURES = "scrollbars=no, titlebar=no, resizable=yes, alwaysRaised=yes, menubar=no, location=no, status=no, toolbar=no, directories=no";

var calendarWin = new Array();

function openCalendarDialog(dialogname, datestr) {
  openCalendarDialog(dialogname, datestr, null);
}
function openCalendarDialog(dialogname, datestr, mindate) {

  //see if there's a window

  var calwin = calendarWin[dialogname];

  if (calendarWin[dialogname] && !calendarWin[dialogname].closed){
      
      var calwin = calendarWin[dialogname];
      
      // there's already a window
      calwin.focus();
     
  } else {

    var url = CALENDAR_URL;

    if (datestr != null) {
      url += "&date="+encode_url_parameter(datestr);
    }
    if (dialogname != null) {
      url += "&dialogname="+encode_url_parameter(dialogname);
    }
    if (mindate != null) {
      url += "&mindate="+encode_url_parameter(mindate)
    }
    
    calendarWin[dialogname] = do_window_open(url, CALENDAR_NAME, CALENDAR_FEATURES + getSizeAndPosition(CALENDAR_WIDTH, CALENDAR_HEIGHT));
  }
}

function closeCalendarDialog(dialogname, datestr) {
  alert("closeCalendarDialog("+datestr+"): Override this function to do something!");
}


// --------------------------------------------------------------------------------
// Skinedit Dialog

var SKINEDIT_URL = FORM_ACTION_READ_ONLY + "?type=skinedit2";
var SKINEDIT_NAME = "";
var SKINEDIT_WIDTH  = 605;
var SKINEDIT_HEIGHT = 730;
var SKINEDIT_FEATURES = "scrollbars=yes, titlebar=no, resizable=yes, alwaysRaised=yes, menubar=no, location=no, status=no, toolbar=no, directories=no";

function openCookieSkinEditorDialog(skinname, username) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&user='+username+'&cookie=true';
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

function openUserSkinEditorDialog(skinname, username) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&user='+username;
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

function openProjectSkinEditorDialog(skinname, projname) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&projectsettings=true';
  if (typeof(projname) != "undefined" && projname) {
    url += "&proj=" + projname;
  }
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

function openProjectDefaultSkinEditorDialog(skinname) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&projectdefaults=true';
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));  
}

function openServerSkinEditorDialog(skinname) {
  var url = SKINEDIT_URL+'&curskin='+skinname
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

function openUserDigestEditorDialog(skinname, username) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&user='+username+'&mode=digest';
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

function openServerDigestEditorDialog(skinname) {
  var url = SKINEDIT_URL+'&curskin='+skinname+'&mode=digest'
  do_window_open(url, SKINEDIT_NAME, SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
}

// --------------------------------------------------------------------------------
// general purpose functions

var EXTRA_SPACE_X = 10;   // need to compensate for window borders
var EXTRA_SPACE_Y = 30;  // need to compensate for window title and borders

function getSizeAndPosition(width, height) {
  return getPosition(width, height)+",width="+width+",height="+height;
}

function getPosition(width, height) {

  var positioning;

  // make sure these are numbers
  width *= 1;
  height *= 1;

  if (mouseScreenX == -1 || mouseScreenY == -1) {
    positioning = "";  // no positioning information (the window manager will put it somewhere reasonable)
  } else {
    var screenWidth  = window.screen.availWidth;
    var screenHeight = window.screen.availHeight;

    var idealX = mouseScreenX - (width  / 2);
    var idealY = mouseScreenY - (height / 2);

    if (idealX + width > screenWidth) {
      idealX = screenWidth - (width + EXTRA_SPACE_X);
    }
    if (idealX < 0) {
      idealX = 0;
    }
    
    if (idealY + height > screenHeight) {
    idealY = screenHeight - (height + EXTRA_SPACE_Y);
    }
    if (idealY < 0) {
      idealY = 0;
    }

    positioning = ",left="+idealX+",top="+idealY+",screenX="+idealX+",screenY="+idealY;
  }

  return positioning;
}

function dialog_ok() {}
function dialog_exit() { 
  if (typeof(leavePage) == "undefined" || leavePage()) {
    var ref = document.referrer;
    if (ref != null && ref != "") {
      window.location = ref;
    } else {
      window.location = "/traction";
    }
  }
}
function dialog_cancel() { window.close(); }
function dialog_close() { window.close(); }
function dialog_refresh() { window.location = window.location; }
function dialog_reset() {}
function dialog_apply() {}

var FILEINSPECT_URL = FORM_ACTION_READ_ONLY + "?type=fileinspect";

function inspectFile(path) {
  var url = FILEINSPECT_URL + "&path=" + path + "&closewindow=true";
  var features = ua("fileinspect_window_features") + getSizeAndPosition(ua("fileinspect_window_width"), ua("fileinspect_window_height"));
//   alert("window.open(\"" + url + "\", \"" + windowname + "\", \"" + features + "\")");
  window.open(url, "", features);
}


/**
 * Opens the kill list editor window.
 * dialogname: a string that identifies the current killlistedit session.
 * killListString: a string representing the initial kill list.
 * collectionName: the name of a collection for the given user that
 *   represents the content to be included in the kill list edit
 *   session.  If the argument for this parameter is undefined or
 *   empty, an argument should be supplied for the entryList
 *   parameter.
 * entryList: a list of Traction IDs referring to the entries to be
 *   included in the kill list edit session.  If the argument for
 *   this parameter is undefined/null, the script will attempt to
 *   retrieve values from either the "curentry" or "entryid" form fields,
 *   if either is present.
 * xrefDepth:  to support the ability to include all articles
 *   referenced by the top level articles (from the named collection or
 *   list of entries), this optional parameter refers to how many
 *   levels of cross-references should be displayed as top level
 *   articles in the killlistedit view.
 * xrefDirection:  to support the ability to include all articles
 *   referenced by the top level articles (from the named collection or
 *   list of entries), this optional parameter refers to the direction
 *   of the desired cross-reference exploration.  Valid values are
 *   incoming, outgoing, and both; the default value is both if an
 *   argument is specified for xrefDepth but not for this parameter.
 */
function openKillListEditor(dialogName, killListString, collectionName, entryList, xrefDepth, xrefDirection, excludeComments) {

  if (entryList == null) {
    if (document.fm != null) {
      if (document.fm.curentry && document.fm.curentry.value != "") {
	entryList = document.fm.curentry.value;
      } else {
	if (document.fm.entryid && document.fm.entryid.value != "") {
	  entryList = document.fm.entryid.value;
	}
      }
    }
  }

  var url = FORM_ACTION_READ_ONLY +
    "?type=killlistedit" +
    "&dialogname=" + dialogName +
    "&killlist=" + (killListString != null ? killListString : "") +
    ((xrefDepth != null) ? ("&xrefdepth=" + xrefDepth) : "") +
    ((xrefDirection != null) ? ("&xrefdirection=" + xrefDirection) : "") +
    ((collectionName != null && collectionName != "") ? ("&collection=" + collectionName) : "") +
    (entryList != null ? ("&rec=" + entryList) : "") +
    "&excludecomments=" + (excludeComments ? "true" : "false");

  var features = ua("killlist_window_features", "scrollbars\=no,titlebar\=no,resizable\=yes,alwaysRaised\=yes,menubar\=no,location\=no,status\=no,toolbar\=no,directories\=no") + getSizeAndPosition(ua("killlist_window_width", "600"), ua("killlist_window_height", "600"));

  do_window_open( url, dialogName, features );

}

/**
 * Closes the kill list editor window.  The default implementation
 * does nothing useful, and this function should be overridden by a
 * view-specific version.
 * dialogName: a string that identifies the current killlistedit session.
 * newKillListString: the final new value of the kill list string.
 */
function closeKillListEditor(dialogName, newKillListString) {
  alert("closeKillListEditor("+"'"+dialogname+"','"+newKillListString+"'): Override this function to do something!");
}



function uninstallPlugin(name, displayname) {
  
  if (confirm((new MessageFormat(i18n("ssetup_plugins_uninstall_plugin_confirmation_message",
				     "Are you sure you want to permanently uninstall the \"{0}\" plugin?"))).format(displayname))) {
    document.fm.plugin.value = name;
    document.fm.fb_submit.value = "Uninstall Plugin";
    document.fm.submit();
  }
}

function configurePluginCustom(url) {
  do_window_open(url, "", SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
  return false;
}

function configurePluginServer(name, viewtype) {
  if (!viewtype) viewtype = "pluginedit_server";
  var url = FORM_ACTION_READ_WRITE + '?type='+viewtype+'&plugin='+name;
  do_window_open(url, "", SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
  return false;
}

function configurePluginUser(name, userid, viewtype) {
  if (!viewtype) viewtype = "pluginedit_user";
  var url = FORM_ACTION_READ_WRITE + '?type='+viewtype+'&plugin='+name+'&userid='+userid;
  do_window_open(url, "", SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
  return false;
}

function configurePluginProject(name, projid, viewtype) {
  if (!viewtype) viewtype = "pluginedit_project";
  var url = FORM_ACTION_READ_WRITE + '?type='+viewtype+'&plugin='+name+'&proj='+projid;
  do_window_open(url, "", SKINEDIT_FEATURES + getSizeAndPosition(SKINEDIT_WIDTH, SKINEDIT_HEIGHT));
  return false;
}

function openSkinCustomizeDialog(skinName, options) {

  if (!skinName) {
    return;
  }

  if (skinName.indexOf("def-val-") != -1) {
    skinName = skinName.substring(8);
  }

  switch (options.mode) {
  case "cookie":
    openCookieSkinEditorDialog(skinName, encode_url_parameter(options.userName));
    break;
  case "user":
    if (options.skinType == "digest") {
      openUserDigestEditorDialog(skinName, encode_url_parameter(options.userName));
    } else {
      openUserSkinEditorDialog(skinName, encode_url_parameter(options.userName));
    }
    break;
  case "userdefault":
    if (options.skinType == "digest") {
      openServerDigestEditorDialog(skinName);
    } else {
      openServerSkinEditorDialog(skinName);
    }
    break;
  case "project":
    openProjectSkinEditorDialog(skinName, options.projectName);
    break;
  case "projectdefault":
    openProjectDefaultSkinEditorDialog(skinName);
    break;
  }

}

var LOGO_MANAGER_VIEW_TYPE = "logomanager";
var LOGO_MANAGER_PROPERTY_KEY = "logomanager_window";

function openLogoManager(dialogname, extras) {
  openDialog(LOGO_MANAGER_VIEW_TYPE, LOGO_MANAGER_PROPERTY_KEY, dialogname, extras);
}



var PROFILE_PICTURE_MANAGER_VIEW_TYPE = "profilepictures";
var DEFAULT_PROFILE_PICTURE_MANAGER_VIEW_TYPE = "defaultprofilepictures";
var PROFILE_PICTURE_MANAGER_PROPERTY_KEY = "profilepicturemanager_window";

function openProfilePictureManager(userID, dialogname, extras) {
  var viewType = (userID == -1) ? DEFAULT_PROFILE_PICTURE_MANAGER_VIEW_TYPE : PROFILE_PICTURE_MANAGER_VIEW_TYPE;
  extras = (userID == -1) ? extras : (((extras) ? extras : "") + "&userid=" + userID);
  openDialog(viewType, PROFILE_PICTURE_MANAGER_PROPERTY_KEY, dialogname, extras);
}

var TEST_MAIL_VIEW_TYPE = "testmail";
var TEST_MAIL_PROPERTY_KEY = "testmail_window";

function openTestMail(dialogname, proj, server, extras) {
  openDialog(TEST_MAIL_VIEW_TYPE,
	     TEST_MAIL_PROPERTY_KEY,
	     null,
	     "&server=" + (server ? "true" : "false") + (proj ? ("&proj=" + encode_url_parameter(proj)) : "") + (extras ? extras : ""));
}
