//   ------------------------------------------------------------
//   Version.js
//   ------------------------------------------------------------
//   VCS INFO:
//   
//   $Revision:   1.22  $
//   $Date:   13 Jan 2000 15:00:36  $
//   $Author:   shsieh  $
//   $Workfile:   Version.js  $
//   ------------------------------------------------------------
//   INCLUDE DEPEDENCY:
//   WSCAPI/InitPage.js
//   WSCAPI/Result.js
//   WSCAPI/Event.js
//   WSCAPI/Misc.js
//   Redist/PopupManager/PopupManager.js
//   WSCAPI/EnableRemoteObject.asp
//   ------------------------------------------------------------
//   All Rights Reserved.  Copyright (c) 1988-1999 FileNET Corp.
//   ------------------------------------------------------------

// IDMWSC_Version method implementations:

var IDMWSC_Version_archiveReclaim      = 0x0001;
var IDMWSC_Version_archiveDispose      = 0x0000;
var IDMWSC_Version_indexContent        = 0x0000;
var IDMWSC_Version_indexContentDeindex = 0x0001;
var IDMWSC_Version_makePermanent       = 0x0000;
var IDMWSC_Version_makeImpermanent     = 0x0001;

function VersionCancelCheckoutPopup(x, y, libraryName, objId, featureString)
{
	var width, height;
	var isLargeFonts = IDMWSC_getCookie('IDMWSC_largeFonts');
	if (eval(isLargeFonts))
	{
		// make popup bigger for large font case
		width = 390;
		height = 130;
	}
	else
	{
		width = 297;
		height = 75;
	}
    if (x < 0)
            x = 200;  // use default
    if (y < 0)
            y = 100;  // use default

    var URL = IDMWSC_baseURL + "/Redist/DocControls/CancelCheckout.asp?id=" + objId + "&Library=" + libraryName;
    var PopupFeatures = "dependent=no,toolbar=no,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,width=" + width + ",height=" + height + ",top=" + y + ",left=" + x;

    // Form the unique popup window name.
    var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(libraryName)  + "Document_ID_" + IDMWSC_replaceSeparatorsWithUnderscores(objId);

    IDMWSC_PopupManager_open("CancelXoutControl", popupName, URL, PopupFeatures, false);
}

function IDMWSC_Version_indexContentImpl(options) {
	var co = RSExecute(IDMWSC_baseURL + "/WSAPI/Version.asp","indexContent",this.document.library.getName().returnValue,this.document.getId().returnValue,options);
	objResult = IDMWSC_rsCallObjectToResult(co);
	objResult.returnValue = objResult.succeeded();
	return objResult;	
}

function IDMWSC_Version_checkout(options) {
	// check if compound doc
	var objResult = this.document.getState(IDMWSC_Document_hasChild);
	if (objResult.failed()) { // getstate failed
		objResult.showResultMessageDialog();
		return;
	}
	else {
		if (objResult.returnValue) { // it is a compound doc
			IDMWSC_showMessageBox(IDMWSC_iconCritical,"Error","Checkout of compound documents is not allowed.",400,100);
			return;
		}
	}
	
	// check if an external document
	objResult = this.document.getState(IDMWSC_Document_isExternalDocument);
	if (objResult.failed()) { // getstate failed
		objResult.showResultMessageDialog();
	}
	else {
		if (objResult.returnValue == true) { // external
			var co = RSExecute(IDMWSC_baseURL + "/WSAPI/Version.asp","checkoutExternal",this.document.library.getName().returnValue,this.document.getId().returnValue);
			objResult = IDMWSC_rsCallObjectToResult(co);
			// note: to stay consistent not returning anything here, just 
			// display an error if there
			if (objResult.failed()) { // getstate failed
				objResult.showResultMessageDialog();
			}
			else  { // send event 
				var event = new IDMWSC_Event("Object_StateChange",this.document);
				event.propagate();
			}
		}
		else {  // regular doc
			var featureString = "operation=checkout&saveMode=prompt";

			if(options == IDMWSC_Document_launchNativeApplication)
			{
				featureString += "&openMode=open";
			} 
			else 
			{
				featureString += "&openMode=none";
			}
			DocOperationsPopup(-1,-1,this.document.getLibrary().returnValue.getName().returnValue, this.document.getId().returnValue, featureString);
		}
	}
}

function IDMWSC_Version_cancelCheckout(options) {
	var objResult;

	// check if an external document
	objResult = this.document.getState(IDMWSC_Document_isExternalDocument);
	if (objResult.failed()) { // getstate failed
		objResult.showResultMessageDialog();
		return;
	}
	else {
		var isExternalDoc = objResult.returnValue;

		if (!isExternalDoc && options == IDMWSC_withUI)
		{
			VersionCancelCheckoutPopup(-1, -1, this.document.library.getName().returnValue, this.document.getId().returnValue, "");
			return;
		}
		
		// check if it is a compound document
		objResult = this.document.getState(IDMWSC_Document_hasChild);
		if (objResult.failed()) { // getstate failed
			objResult.showResultMessageDialog();
			return;
		}
		else {
			if (objResult.returnValue) { // it is a compound doc
				IDMWSC_showMessageBox(IDMWSC_iconCritical,"Error","Cancel checkout of compound documents is not allowed.",400,100);
				return;
			}
		}

		var co = RSExecute(IDMWSC_baseURL + "/WSAPI/Version.asp","cancelCheckout",this.document.library.getName().returnValue,this.document.getId().returnValue);
		objResult = IDMWSC_rsCallObjectToResult(co);
		// a kluge here to display a message RE replication:
		if (objResult.returnValue.substring(0,20) ==  "Replication message:") {
			var sMsg = objResult.returnValue.substring(20);
			IDMWSC_showMessageBox(IDMWSC_iconInformation,"Replication Message",sMsg,400,100);
		}

		if (isExternalDoc) {
			var event = new IDMWSC_Event("Object_StateChange",this.document);
			event.propagate();
		}
		
		objResult.returnValue = objResult.succeeded();
		return objResult;	
	}
}

function IDMWSC_Version_checkin() {
	// check if it is a compound document
	var objResult = this.document.getState(IDMWSC_Document_hasChild);
	if (objResult.failed()) { // getstate failed
		objResult.showResultMessageDialog();
		return;
	}
	else {
		if (objResult.returnValue) { // it is a compound doc
			IDMWSC_showMessageBox(IDMWSC_iconCritical,"Error","Checkin of compound documents is not allowed.",400,100);
			return;
		}
	}

	var width, height, x, y;
	var isLargeFonts = IDMWSC_getCookie('IDMWSC_largeFonts');
	if (eval(isLargeFonts))
	{
		// make popup bigger for large font case
		width = 620;
		height = 520;
		y = (screen.height - height) / 2 - 35;  // subtract 35 to accomodate for 800x600 mode
	}
	else
	{
		width = 470;
		height = 420;
		y = (screen.height - height) / 2;
	}
	x = (screen.width - width) / 2;

	var URL = IDMWSC_baseURL + "/Redist/DocWizard/CheckinDoc.asp?Library=" + this.document.library.getName().returnValue + "&id=" + this.document.getId().returnValue;
	var PopupFeatures = "dependent=no,toolbar=no,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,width=" + width + ",height=" + height + ",top=" + y + ",left=" + x;
	// Form the unique popup window name.
	var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.document.getLibrary().returnValue.getName().returnValue) + "CheckinDocument";
	IDMWSC_PopupManager_open("Checkin Document " + this.document.getId().returnValue, popupName, URL, PopupFeatures, false);
}

function IDMWSC_Version_archive(options) {
	var co = RSExecute(IDMWSC_baseURL + "/WSAPI/Version.asp","archive",this.document.library.getName().returnValue,this.document.getId().returnValue,options);
	objResult = IDMWSC_rsCallObjectToResult(co);
	objResult.returnValue = objResult.succeeded();
	return objResult;	
}

function IDMWSC_Version_makePermanentImpl(options) {
	var co = RSExecute(IDMWSC_baseURL + "/WSAPI/Version.asp","makePermanent",this.document.library.getName().returnValue,this.document.getId().returnValue,options);
	objResult = IDMWSC_rsCallObjectToResult(co);
	objResult.returnValue = objResult.succeeded();
	return objResult;	
}

// Properties

function IDMWSC_Version_property_document() 
{
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.document, "");
	return objResult;
}

// Object Constructor:
function IDMWSC_Version(document)
{
	// Properties
	this.document = document
}

// Property Accessor Methods
IDMWSC_Version.prototype.getDocument = IDMWSC_Version_property_document;

// Instance Methods:
IDMWSC_Version.prototype.indexContent = IDMWSC_Version_indexContentImpl;
IDMWSC_Version.prototype.checkout = IDMWSC_Version_checkout;
IDMWSC_Version.prototype.cancelCheckout = IDMWSC_Version_cancelCheckout;
IDMWSC_Version.prototype.checkin = IDMWSC_Version_checkin;
IDMWSC_Version.prototype.archive = IDMWSC_Version_archive;
IDMWSC_Version.prototype.makePermanent = IDMWSC_Version_makePermanentImpl;