/**
 * Opens the CustomDataType input form.
 *
 * doPostback
 *   Boolean that specifies how the calling window should be updated.
 *   If true, a post of the form will be used. If false, the calling window will be refreshed.
 * formID
 *   Integer index of the form in the document.forms collection that contains the input specified
 *   by the valueFieldName parameter.
 * qualifiedAttributeName
 *   The qualified name of the attribute that will reference the custom data entity.
 * valueFieldName
 *   Name of the input control that receives the OID of the custom data entity.
 *   Can be null if a non-null, non-blank value is specified for the itemOID parameter (edit case)
 *   or if the typeName parameter has been specified (creation case).
 * displayFieldName
 *   Name of the input control that receives the display value of the custom data entity. Can be null.
 * itemOID
 *   OID of the custom data entity to modify.
 *   If null, the OID will be taken from the value of the form field specified by valueFieldName.
 * typeName (optional)
 *   Name of the custom data type. Used if the itemOID parameter or the value from the field indicated
 *   by valueFieldName is null.  This is used to create a new entity of the specified type.
 * entityView (optional)
 *   OID of entity view to display on the custom data form.
 * transID (optional; internal to change tracking)
 * overrideKey (optional; internal to change tracking)
 * showOkAndAddAnother (optional)
 *   Boolean indicating whether to show the "OK and Add Another" button. The default is to not show it.
 * rootEntity (optional)
 *   Used by the view validation and binding override script hooks.
 * activityType (optional)
 *   If change tracking is to be turned on, this is the OID of the relevant ActivityType entity.
 *   Previously, this parameter was simply a boolean indicating whether change tracking should be
 *   enabled. However, the addition of "interim change tracking" in 5.6 made it necessary to know
 *   more about the activity to be logged.
 * setOID (optional)
 *   When editing an existing set member, this is the OID of the set itself. Passing this causes
 *   a hint to be given to the change tracking system if the transaction commits.
 * wizardPage (optional)
 *   If this CDT form is being used within the context of a SmartForm, this is the OID of the current
 *   WizardPage. This parameter was added in support of "interim change tracking" in 5.6. The OID
 *   is just added to the querystring for the benefit of Side-by-Side View Differences (custom).
 * aldOID (optional)
 *   OID of the AttributeLayoutDescription entity corresponding to the view control, if applicable.
 *   This argument is optional and is used for ajax postbacks. If the doPostback argument is true and
 *   aldOID is also passed, an ajax postback will be used to update the control. If aldOID is not
 *   specified, a standard postback will be performed.
 */
function showCustomDataForm(doPostback, formID, qualifiedAttributeName, valueFieldName, displayFieldName,
                            itemOID, typeName, entityView, transID, overrideKey, showOkAndAddAnother,
                            rootEntity, activityType, setOID, wizardPage, aldOID)
{
	var errPrefix = "EXCEPTION WebrCommon/Scripts/EntityChooser/CDTInputFormLauncher.js showCustomDataForm():\r\n\r\n";
	var arrWindowUrl = [_cdtInputFormURL, "?postback=", (doPostback ? "1" : "0")];
	if (isNaN(formID)) {
		alert(errPrefix + "Argument formID is invalid");
		return;
	}
	arrWindowUrl.push("&form=", formID);
	if (qualifiedAttributeName != null && qualifiedAttributeName != "") {
		arrWindowUrl.push("&qualifiedAttributeName=", escape(qualifiedAttributeName));
	}
	if (valueFieldName != null && valueFieldName != "") {
		arrWindowUrl.push("&valueField=", escape(valueFieldName));
	}
	if (itemOID == null || itemOID == "" || itemOID == ":00000000000000000000000000000000") {
		// Look for the itemOID in the value field.
		if (valueFieldName != null && valueFieldName != "") {
			var valueField = document.forms[formID].elements[valueFieldName];
			if (valueField == null) {
				alert(errPrefix + "Cannot find form field " + valueFieldName);
				return;
			}
			itemOID = valueField.value;
		}
	}
	if (itemOID == null || itemOID == "" || itemOID == ":00000000000000000000000000000000") {
		// Still no itemOID; this is applicable to the creation case,
		// which makes the typeName argument required.
		if (typeName == null || typeName == "") {
			alert(errPrefix + "Argument typeName is invalid");
			return;
		}
	} else {
		arrWindowUrl.push("&itemOID=", escape(itemOID));
	}
	if (typeName != null && typeName != "") {
		arrWindowUrl.push("&typeName=", escape(typeName));
	}
	if (entityView != null && entityView != "" && entityView.toLowerCase() != "default") {
		arrWindowUrl.push("&entityview=", escape(entityView));
	}
	if (displayFieldName != null && displayFieldName != "") {
		arrWindowUrl.push("&displayField=", escape(displayFieldName));
	}
	if (transID != null && transID != "") {
		arrWindowUrl.push("&_webrChangeTrackingParentTransID=", escape(transID));
	}
	if (overrideKey != null && overrideKey != "") {
		arrWindowUrl.push("&overrideKey=", escape(overrideKey));
	}
	if (showOkAndAddAnother) {
		arrWindowUrl.push("&showOkAndAddAnother=true");
	}
	if (rootEntity != null && rootEntity != "") {
		arrWindowUrl.push("&rootEntity=", escape(rootEntity));
	}
	if (activityType != null && activityType != "") {
		arrWindowUrl.push("&activityType=", escape(activityType));
	}
	if (setOID != null && setOID != "") {
		arrWindowUrl.push("&setOID=", escape(setOID));
	}
	if (wizardPage != null && wizardPage != "") {
		arrWindowUrl.push("&WizardPageOID=", escape(wizardPage));
	}
	if (aldOID != null && aldOID != "") {
		arrWindowUrl.push("&aldOID=", escape(aldOID));
	}
	var winWidth = 800;
	var winHeight = 600;
	var arrCtrWinPos = _getCenteredWindowPosition(winWidth, winHeight);
	var winFeatures =
		["toolbar=no,scrollbars=yes,resizable=yes,width=", winWidth, ",height=", winHeight,
		",left=", arrCtrWinPos[0], ",top=", arrCtrWinPos[1]].join("");
	var windowUrl = arrWindowUrl.join("");
	window.open(windowUrl, "_blank", winFeatures);
}

/**
 * Opens the CustomDataType form in read-only mode. This is only cosmetic; actual viewing and
 * editing rights are checked by the target page itself before rendering.
 *
 * itemOID
 *   OID of the custom data entity to view.
 * entityView (optional)
 *   OID of entity view to display on the custom data form.
 * rootEntity (optional)
 *   Used by the binding override script hook.
 */
function showReadOnlyCustomDataForm(itemOID, entityView, rootEntity)
{
	var errPrefix = "EXCEPTION WebrCommon/Scripts/EntityChooser/CDTInputFormLauncher.js showReadOnlyCustomDataForm():\r\n\r\n";
	var arrWindowUrl = [_cdtInputFormURL, "?readonly=1"];
	if (itemOID == null || itemOID == "") {
		alert(errPrefix + "Argument itemOID is invalid");
		return;
	}
	arrWindowUrl.push("&itemOID=", escape(itemOID));
	if (entityView != null && entityView != "" && entityView.toLowerCase() != "default") {
		arrWindowUrl.push("&entityview=", escape(entityView));
	}
	if (rootEntity != null && rootEntity != "") {
		arrWindowUrl.push("&rootEntity=", escape(rootEntity));
	}
	var winWidth = 800;
	var winHeight = 600;
	var arrCtrWinPos = _getCenteredWindowPosition(winWidth, winHeight);
	var winFeatures =
		["toolbar=no,scrollbars=yes,resizable=yes,width=", winWidth, ",height=", winHeight,
		",left=", arrCtrWinPos[0], ",top=", arrCtrWinPos[1]].join("");
	var windowUrl = arrWindowUrl.join("");
	window.open(windowUrl, "_blank", winFeatures);
}

// For a window of the specified dimensions, this returns an array containing the coordinates
// for the top left corner of the window when centered on the screen.
function _getCenteredWindowPosition(iWinWidth, iWinHeight)
{
	var iWinLeft = 0;
	var iWinTop = 0;
	if (typeof window.screen != "undefined") {
		iWinLeft = (window.screen.availWidth - iWinWidth) / 2;
		if (iWinLeft < 0) iWinLeft = 0;
		iWinTop = (window.screen.availHeight - iWinHeight) / 2;
		if (iWinTop < 0) iWinTop = 0;
   	}
   	return [iWinLeft, iWinTop];
}
