//  ------------------------------------------------------------
//   Library.js
//   ------------------------------------------------------------
//   VCS INFO:
//   
//   $Revision:   1.29  $
//   $Date:   06 Dec 2001 11:39:48  $
//   $Author:   vpham  $
//   $Workfile:   library.js  $
//   ------------------------------------------------------------
//   INCLUDE DEPEDENCY:
//   WSCAPI/InitPage.js
//   Redist/PopupManager/PopupManager.js
//   WSCAPI/Result.js
//   WSCAPI/Folder.js
//   WSCAPI/Document.js
//   WSCAPI/StoredSearch.js
//   WSCAPI/EnableRemoteObject.asp
//   ------------------------------------------------------------
//   All Rights Reserved.  Copyright (c) 1988-1999 FileNET Corp.
//   ------------------------------------------------------------

//
// IDMWSC_Library state constants (constant values matches the enum values defined
// in the Library automation object.
//
var IDMWSC_Library_loggedOn					= 1;
var IDMWSC_Library_canAddRootLevelFolders	= 2;
var IDMWSC_Library_canAddDocuments			= 5;

//
// IDMWSC_Library utility functions
// (NOTE:  These functions are not methods on the IDMWSC_Library JavaScript object)
//

// IDMWSC_Library_select
function IDMWSC_Library_select(libraryName)
{
	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "select", libraryName);
	return IDMWSC_rsCallObjectToResult(co);
}


//
// IDMWSC_Library method implementations
//

// IDMWSC_Library_getName
function IDMWSC_Library_getName()
{
	var	oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.name, "");
	return oResult;
}

// IDMWSC_Library_getLabel
function IDMWSC_Library_getLabel()
{
	var	oResult;
	if (this.label == null)
	{
		// Retrieve label from server
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "getLabel", this.name, this.systemType);
		oResult = IDMWSC_rsCallObjectToResult(co);
		if (oResult.succeeded())
			this.label = oResult.returnValue;
		else
			return oResult;
	}
	oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.label, "");
	return oResult;
}

// IDMWSC_Library_setLabel
function IDMWSC_Library_setLabel(label)
{
	this.label = label;
}

// IDMWSC_Library_getObjectType
function IDMWSC_Library_getObjectType()
{
	var	oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", IDMWSC_objTypeLibrary, "");
	return oResult;
}

// IDMWSC_Library_getSystemType
function IDMWSC_Library_getSystemType()
{
	var	oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.systemType, "");
	return oResult;
}

// IDMWSC_Library_addBookmark
function IDMWSC_Library_addBookmark(label)
{
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Library]";

		var result = this.getLabel();
		if (result.failed())
			result.showResultMessageDialog();
		else
			szLabel += " " + result.returnValue;
		}

	var szURL = IDMWSC_baseURL + "/";

	szURL += "home.asp?library=" + this.name;

	window.external.AddFavorite(szURL, szLabel);
}

// IDMWSC_Library_send
function IDMWSC_Library_send(label)
{
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Library]";

		var result = this.getLabel();
		if (result.failed())
			result.showResultMessageDialog();
		else
			szLabel += " " + result.returnValue;
		}

	var szURL = IDMWSC_baseURL + "/";

	szURL += "home.asp?library=" + this.name;

	if (IDMWSC_IEBrowser) {
		if (navigator.appVersion.search(/MSIE 5.0/) > -1 ||
		    navigator.appVersion.search(/MSIE 4.01/) > -1) 
			{
			szLabel = szLabel.replace(/\%/g, "%25");
			szLabel = szLabel.replace(/\?/g, "%3F");
			szLabel = szLabel.replace(/\&/g, "%26");

			szURL = szURL.replace(/\%/g, "%25");
			szURL = szURL.replace(/\?/g, "%3F");
			szURL = szURL.replace(/\&/g, "%26");
			}
		}

	document.location.href = "mailto:?subject=" + escape(szLabel) + "&body=%20%20" + escape(szURL);
}

// IDMWSC_Library_showAddFolderWizard
function IDMWSC_Library_showAddFolderWizard() {
	var x = (screen.width - 400) / 2;
	var y = (screen.height - 180) / 2;
	var	name = "";			
	var URL = IDMWSC_baseURL + "/Redist/AddFolder/addfolder.asp?Library=" + this.name + "&Name=" + name;
	var PopupFeatures = "dependent=no,toolbar=no,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,width=" + 400 + ",height=" + 180 + ",top=" + y + ",left=" + x;
	// Form the unique popup window name.
	var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.name) + "AddFolder";
	IDMWSC_PopupManager_open("AddFolder", popupName, URL, PopupFeatures, false);
}

// IDMWSC_Library_getObject
function IDMWSC_Library_getObject(objectType, id)
{
	var oObject;
	if (objectType == IDMWSC_objTypeFolder)
		oObject = new IDMWSC_Folder(id, this);
	else if (objectType == IDMWSC_objTypeDocument)
		oObject = new IDMWSC_Document(id, this);
	else if (objectType == IDMWSC_objTypeStoredSearch)
		oObject = new IDMWSC_StoredSearch(id, this);

	var oResult;
	if (oObject)
		oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", oObject);
	else
		oResult = new IDMWSC_Result(IDMWSC_Result_error, -1, "JavaScript object creation failed");
	return oResult;
}

// IDMWSC_Library_getState
function IDMWSC_Library_getState(state)
{
	var oResult;
	if (state == IDMWSC_Library_loggedOn)
	{
		// Always get this state from the server in case the ASP session has timed out.
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "isLoggedOn", this.name, this.systemType);
		oResult = IDMWSC_rsCallObjectToResult(co);
		if (oResult.succeeded())
			oResult.returnValue = eval(oResult.returnValue);
		else
		if (oResult.resultMessage.search("LDAP credentials") != -1)
		{
			// use alert for now, since ShowResultMessageDialog doesn't show a modal dialog
			alert (oResult.resultMessage);
			//oResult.showResultMessageDialog();
			// change oResult back to normal state: lib is NOT logged on.
			oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "false", IDMWSC_LDAPLogonFail);
		}
		return oResult;
	}

	// Get cached state from specified state
	var mask = 1 << (state-1);
	if ((this.statesStatus & mask) == mask)  // already cached
		oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", (this.states & mask)==mask);
	else
	{
		// Make RS call to get the states
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "getStates", this.name);
		oResult = IDMWSC_rsCallObjectToResult(co);
		if (oResult.succeeded())
		{
			var states = oResult.returnValue.split(",");
			var tempMask;
			var i;
			for (i=0; i<states.length; i++)
			{
				tempMask = 1 << i;
				if (states[i] != "")  // there is a valid state value
				{
					if (eval(states[i]))
						this.states = this.states | tempMask;
					else
						this.states = this.states & (~tempMask);

					// Set state status bit
					this.statesStatus = this.statesStatus | tempMask;
				}
			}
			oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", (this.states & mask)==mask);
		}
	}

	return oResult;
}

// IDMWSC_Library_setState
function IDMWSC_Library_setState(state, value)
{
	// set a library state
	var mask = 1 << (state-1);
	if (value)  // set to true
		this.states = this.states | mask;
	else  // set to false
		this.states = this.states & (~mask);

	// mark this state as cached
	this.statesStatus = this.statesStatus | mask;
}

// IDMWSC_Library_logoff
function IDMWSC_Library_logoff()
{
	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "logoff", this.name);
	return IDMWSC_rsCallObjectToResult(co);
}

// IDMWSC_Library_logon
function IDMWSC_Library_logon(username, password, group, options)
{
	if ((options == IDMWSC_withUI) || (options == IDMWSC_withUINoLDAP))
	{
		// center the popup on the screen
		var width = 385;
		var height = 240;
		var x = (screen.width - width) / 2;
		var y = (screen.height - height) / 2;

		var label = this.label;
		if (!label)
		{
			// attempt to get label from the server
			oResult = this.getLabel();
			if (oResult.succeeded())
				label = oResult.returnValue;
			else
				label = "";  // default to empty string
		}
        
		var logonURL = IDMWSC_baseURL + "/Redist/Logon/Logon.asp?Library=" + escape(this.name) + "&LibraryLabel=" + escape(label) + "&SystemType=" + this.systemType
		if (options == IDMWSC_withUINoLDAP) 
		{
			// pass this parameter to disable ldap logon style and just use regular logon dialog.
			logonURL = logonURL + "&bNoLDAP=1";
			//alert (logonURL);
		}
		// logonPopupFeatures string must not have any blank spaces
		var logonPopupFeatures = "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.name) + "Logon";
		
		var logonWindow = IDMWSC_PopupManager_open("Logon", popupName, logonURL, logonPopupFeatures, false);
		if ((username.length > 0) || (password.length > 0) || (group.length > 0))
			IDMWSC_Library_logonSetNameAgain(popupName, escape(username), escape(password), escape(group), 0);
		return;
	}

	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "logon", this.name, this.systemType, username, password, group, options);
	var oResult = IDMWSC_rsCallObjectToResult(co);
	if (oResult.succeeded())
		if (this.systemType == IDMWSC_sysTypeIS) {
			var returnValues = oResult.returnValue.split(";");
			
			this.logonID = returnValues[0];
			this.passwordFirstLogon = !(returnValues[1] == "0");
			this.passwordInGracePeriod = ! (returnValues[2] == "0");
			this.passwordExpirationDate = returnValues[3];
			this.passwordExpirationTime = returnValues[4];
			}
		else
			this.logonID = oResult.returnValue;
		
	return oResult;
}

// WORKFLOW
// IDMWSC_Library_logonWithToken
function IDMWSC_Library_logonWithToken(token)
{
//	alert ("IDMWSC_Library_logonWithToken! token="+token);
	return this.logon(token, "", "", IDMWSC_withToken);
}
//end of WORKFLOW

// LDAP support
// IDMWSC_Library_LDAP_logon
function IDMWSC_Library_LDAP_logon (LDAPServerName, LDAPDn, username, password, options)
{
    //alert ("inside IDMWSC_LDAP_logon");
	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Library.asp", "LDAPlogon", LDAPServerName, LDAPDn, username, password,  this.name, this.systemType, options);
	var oResult = IDMWSC_rsCallObjectToResult(co);
    //alert ("after IDMWSC_LDAP_logon");
	if (oResult.succeeded())
	{
	    //alert ("IDMWSC_LDAP_logon ok.");
		if (this.systemType == IDMWSC_sysTypeIS) {
			var returnValues = oResult.returnValue.split(";");
			
			this.logonID = returnValues[0];
			this.passwordFirstLogon = !(returnValues[1] == "0");
			this.passwordInGracePeriod = ! (returnValues[2] == "0");
			this.passwordExpirationDate = returnValues[3];
			this.passwordExpirationTime = returnValues[4];
			}
		else
			this.logonID = oResult.returnValue;
	}
		
	return oResult;
}

function IDMWSC_Library_logonSetNameAgain(popupName, username, password, group, iters)
{
	// If we don't find popups or logonWin, or time out, just give up.
	var popups = IDMWSC_PopupManager_findPopupsArray();
	if (popups)
	{
		var logonWin = IDMWSC_PopupManager_findPopup(popups, popupName);
		if (logonWin)
		{
			if ((logonWin.document == null) ||
				(logonWin.document.LogonForm == null) ||
				(logonWin.document.LogonForm.Username == null) ||
				(logonWin.document.LogonForm.Password == null) ||
				(logonWin.document.LogonForm.Group == null))
			{
				// Try again later, if we haven't maxed out. 5000 is probably about 10 seconds.
				if (iters < 5000)
				{
					iters = iters + 1;
					var cmd = "IDMWSC_Library_logonSetNameAgain('" + 
								popupName + "', '" +
								username + "', '" + 
								password + "', '" +
								group + "', " +
								iters + ")";
					setTimeout(cmd, 1);
				}
			}
			else
			{
				logonWin.document.LogonForm.Username.value = unescape(username);
				logonWin.document.LogonForm.Password.value = unescape(password);
				logonWin.document.LogonForm.Group.value = unescape(group);
			}
		}
	}
}


// IDMWSC_Library_showAddDocumentWizard
function IDMWSC_Library_showAddDocumentWizard()
{
	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/AddDoc.asp?Library=" + this.name;
	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.name) + "AddDocument";
	IDMWSC_PopupManager_open("Add Document", popupName, URL, PopupFeatures, false);	
}

// IDMWSC_Library_showCheckoutListWindow
function IDMWSC_Library_showCheckoutListWindow()
{
	var x = (screen.width - 600) / 2;
	var y = (screen.height - 450) / 2;
	var URL = IDMWSC_baseURL + "/Redist/CheckoutList/CheckoutList.asp?Library=" + this.name;
	var PopupFeatures = "dependent=no,toolbar=no,directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,width=600,height=450,top=" + y + ",left=" + x;
	// Form the unique popup window name.
	var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.name) + "CheckOutList";
	IDMWSC_PopupManager_open("CheckOutList", popupName, URL, PopupFeatures, false);
}

// IDMWSC_Library_showPropertiesDialog
function IDMWSC_Library_showPropertiesDialog(x,y,startTab)
{
	var width = 450;
	var height = 530;
	var decoration = 30;
	
	if ((!x) || (x < 0)) x = (screen.width - width) / 2; // def to center
	if ((!y) || (y < 0)) y = (screen.height - height) / 2 - decoration;
	if (!startTab) startTab = "";
	var URL = IDMWSC_baseURL + '/Redist/PropertySheets/PropSheetLibrary.asp?Library=' + this.name + '&starttab=' + startTab + '&username=' + this.username;
	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;
	var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.name) + "Property";
	IDMWSC_PopupManager_open("PropertyPage", popupName, URL, PopupFeatures, false);
}

// IDMWSC_Library_getExtendedProperty
function IDMWSC_Library_getExtendedProperty(property)
{
	var oResult;
	if (this.systemType == IDMWSC_sysTypeIS)
		switch (property)
		{
			case "F_PASSWORD_FIRSTLOGON":
				oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.passwordFirstLogon);
				break;
			case "F_PASSWORD_INGRACEPERIOD":
				oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.passwordInGracePeriod);
				break;
			case "F_PASSWORD_EXPIRATIONDATE":
				oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.passwordExpirationDate);
				break;
			case "F_PASSWORD_EXPIRATIONTIME":
				oResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.passwordExpirationTime);
				break;
			default:
				oResult = new IDMWSC_Result(IDMWSC_Result_error, -1, "getExtendedProperty for " + property + " not supported");
				break;
		}
	else
		oResult = new IDMWSC_Result(IDMWSC_Result_error, -1, "getExtendedProperty not supported");
	
	return oResult;
}


// IDMWSC_Library constructor
function IDMWSC_Library(name, systemType)
{
	// Properties
	this.name = name;
	this.systemType = systemType;

	this.label = null;
	this.logonID = "";
	this.passwordFirstLogon = false;
	this.passwordInGracePeriod = false;
	this.passwordExpirationDate = "";
	this.passwordExpirationTime = "";
	this.username = "";

	// Private bit stream to cache the state values
	this.states = 0;
	// Private bit stream to cache whether a state value was cached
	this.statesStatus = 0;
}

// IDMWSC_Library methods
IDMWSC_Library.prototype.getName = IDMWSC_Library_getName;
IDMWSC_Library.prototype.getLabel = IDMWSC_Library_getLabel;
IDMWSC_Library.prototype.setLabel = IDMWSC_Library_setLabel;
IDMWSC_Library.prototype.getObjectType = IDMWSC_Library_getObjectType;
IDMWSC_Library.prototype.getSystemType = IDMWSC_Library_getSystemType;

IDMWSC_Library.prototype.addBookmark = IDMWSC_Library_addBookmark;
IDMWSC_Library.prototype.showAddFolderWizard = IDMWSC_Library_showAddFolderWizard;
IDMWSC_Library.prototype.getObject = IDMWSC_Library_getObject;
IDMWSC_Library.prototype.getState = IDMWSC_Library_getState;
IDMWSC_Library.prototype.setState = IDMWSC_Library_setState;
IDMWSC_Library.prototype.logoff = IDMWSC_Library_logoff;
IDMWSC_Library.prototype.logon = IDMWSC_Library_logon;
IDMWSC_Library.prototype.send = IDMWSC_Library_send;
IDMWSC_Library.prototype.showAddDocumentWizard = IDMWSC_Library_showAddDocumentWizard;
IDMWSC_Library.prototype.showCheckoutListWindow = IDMWSC_Library_showCheckoutListWindow;
IDMWSC_Library.prototype.showPropertiesDialog = IDMWSC_Library_showPropertiesDialog;
IDMWSC_Library.prototype.getExtendedProperty = IDMWSC_Library_getExtendedProperty;

// LDAP support
IDMWSC_Library.prototype.LDAPlogon = IDMWSC_Library_LDAP_logon;


// WORKFLOW
IDMWSC_Library.prototype.logonWithToken = IDMWSC_Library_logonWithToken;
// end of WORKFLOW
