//  ------------------------------------------------------------
//   Folder.js
//   ------------------------------------------------------------
//   VCS INFO:
//   
//   $Revision:   1.23  $
//   $Date:   15 Mar 2001 16:25:04  $
//   $Author:   vpham  $
//   $Workfile:   folder.js  $
//   ------------------------------------------------------------
//   INCLUDE DEPEDENCY:
//   WSCAPI/InitPage.js
//   Redist/PopupManager/PopupManager.js
//   WSCAPI/Result.js
//   WSCAPI/Misc.js 
//   WSCAPI/EnableRemoteObject.asp
//   ------------------------------------------------------------
//   All Rights Reserved.  Copyright (c) 1988-1999 FileNET Corp.
//   ------------------------------------------------------------

var IDMWSC_Folder_canDelete = 1;
var IDMWSC_Folder_canModify = 2;
var IDMWSC_Folder_canFileIn = 3;
var IDMWSC_Folder_isReplica = 5;
var IDMWSC_Folder_canCreateSubFolder = 6;
var IDMWSC_Folder_canUnfile = 7;
var IDMWSC_Folder_canMove = 8;
var IDMWSC_Folder_isVirtual = 9;

var IDMWSC_Folder_minState = IDMWSC_Folder_canDelete;
var IDMWSC_Folder_maxState = IDMWSC_Folder_isVirtual;

// IDMWSC_Folder method implementations:

function IDMWSC_Folder_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 sID = this.id;
	sID = sID.replace(/\//g,"%2F");  // clean up ID (in case its a path)
	var URL = IDMWSC_baseURL + "/Redist/DocWizard/AddDoc.asp?Library=" + this.library.name + "&Folder=" + sID;
	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.library.getName().returnValue) + "AddDocument";
	IDMWSC_PopupManager_open("Add Document", popupName, URL, PopupFeatures, false);	
}

function IDMWSC_Folder_showAddFolderWizard() {
	var x = (screen.width - 400) / 2;
	var y = (screen.height - 180) / 2;
	var	name = "";	
	var sID = this.id;
	sID = sID.replace(/\//g,"%2F");  // clean up ID (in case its a path)		
	var URL = IDMWSC_baseURL + "/Redist/AddFolder/addfolder.asp?Library=" + this.library.getName().returnValue + "&Parent=" + sID + "&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.library.getName().returnValue + "AddFolder_" + this.id);
	IDMWSC_PopupManager_open("AddFolder", popupName, URL, PopupFeatures, false);
}

function IDMWSC_Folder_getPathName() {
	var objResult;
	if (this.pathName != null) {  // already got it
		objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", "", "");
		objResult.returnValue = this.pathName;
	}
	else {
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","getPathName",this.library.getName().returnValue,this.id);
		objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded())		
			this.pathName = objResult.returnValue;  // cache name
	}
	return objResult;
}

function IDMWSC_Folder_addBookmark(label) {
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Folder]";

		var result = this.getLabel();
		if (result.failed())
			result.showResultMessageDialog();
		else
			szLabel += " " + result.returnValue;
		}

	var szURL = IDMWSC_baseURL + "/";

	szURL += "home.asp?library=" + this.library.getName().returnValue + "&mode=browse&id=" + this.id;

	window.external.AddFavorite(szURL, szLabel);
}

function IDMWSC_Folder_send(label)
{
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Folder]";

		var result = this.getLabel();
		if (result.failed())
			result.showResultMessageDialog();
		else
			szLabel += " " + result.returnValue;
		}

	var szURL = IDMWSC_baseURL + "/";

	szURL += "home.asp?library=" + this.library.getName().returnValue + "&mode=browse&id=" + this.id;

	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);
}

function IDMWSC_Folder_copy(dest,options) {
	var objResult;
	if (options == IDMWSC_withUI) {
		IDMWSC_showCopyWindow(this.library,this,this,dest);
	}
	else {
		if (dest.getObjectType().returnValue == IDMWSC_objTypeLibrary)
			var destID = "";  // 'cuz lib doesn't have getid
		else
			var destID = dest.getId().returnValue;
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","copy",this.library.getName().returnValue,this.id,destID,dest.getObjectType().returnValue);
		objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded())
			objResult.returnValue = true;
		return objResult;
	}
	
}

function IDMWSC_Folder_deleteObject(options) {
	var objResult;
	var label = this.label;
	if (!label)
		label = this.id;
	if (options == IDMWSC_withUI)
		if (!window.confirm("Delete folder " + label + "?")) {
			objResult = new IDMWSC_Result(IDMWSC_Result_information, 0, "",IDMWSC_Result_S_FALSE, "");
			return objResult;
		}
	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","deleteObject",this.library.getName().returnValue,this.id);
	objResult = IDMWSC_rsCallObjectToResult(co);
	if (objResult.succeeded())
		objResult.returnValue = true;
	return objResult;
}

function IDMWSC_Folder_file(objToFile) {	
	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","file",this.library.getName().returnValue,this.id,objToFile.getId().returnValue,objToFile.getObjectType().returnValue);
	var objResult = IDMWSC_rsCallObjectToResult(co);
	if (objResult.succeeded())
		objResult.returnValue = true;
	return objResult;
}

function IDMWSC_Folder_move(dest,options) {
	var objResult;
	if (options == IDMWSC_withUI) {
		IDMWSC_showMoveWindow(this.library,this,this,dest);
	}
	else {
		if (dest.getObjectType().returnValue == IDMWSC_objTypeLibrary)
			var destID = "";  // 'cuz lib doesn't have getid
		else
			var destID = dest.getId().returnValue;	
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","move",this.library.getName().returnValue,this.id,destID,dest.getObjectType().returnValue);
		objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded()) {
			objResult.returnValue = true;
		}
		return objResult;
	}
}

function IDMWSC_Folder_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 sID = this.id;
	sID = sID.replace(/\//g,"%2F");  // clean up ID (in case its a path)
	var URL = IDMWSC_baseURL + '/Redist/PropertySheets/PropSheetFolder.asp?Library=' + this.library.getName().returnValue + '&id=' + sID + '&starttab=' + startTab;
	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 newID = this.getId().returnValue;
	newID = IDMWSC_replaceSeparatorsWithUnderscores(newID);
	var popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.library.getName().returnValue) + "Property_" + this.objectType + newID;
	IDMWSC_PopupManager_open("PropertyPage", popupName, URL, PopupFeatures, false);	
}

function IDMWSC_Folder_unfile(objToUnfile, options) {
	var objResult;
	
    var label = objToUnfile.label;
	if (!label)
		label = objToUnfile.id;
	
    var folderlabel = this.label;
	if (!folderlabel)
		folderlabel = this.id;

	if (options == IDMWSC_withUI)
		if (!window.confirm("Remove " + label + " from folder " + folderlabel + "?")) {
			objResult = new IDMWSC_Result(IDMWSC_Result_information, 0, "",IDMWSC_Result_S_FALSE, "");
			return objResult;
		}

	var co = RSExecute(IDMWSC_baseURL + "/Wsapi/folder.asp","unFile",this.library.getName().returnValue,this.id,objToUnfile.getId().returnValue,objToUnfile.getObjectType().returnValue);
	objResult = IDMWSC_rsCallObjectToResult(co);
	if (objResult.succeeded())
		objResult.returnValue = true;
	return objResult;
}

function IDMWSC_Folder_getState(state) {
	// get available states from constants declared at the top
	var objResult;	
	var mask = 1 << (state-1);
	if ((this.statesStatus & mask) == mask)  // already got it
		objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "",(this.states & mask)==mask,"");
	else { // make a RS call to get the folder states		
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Folder.asp","getStates",this.library.getName().returnValue,this.id);
		objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded()) {
			var i;
			var folderStates = objResult.returnValue.split(",");
			var tempMask;
			for (i=IDMWSC_Folder_minState;i<=IDMWSC_Folder_maxState;i++) {
				tempMask = 1 << (i-1);
				if (eval(folderStates[i-1]))
					this.states = this.states | tempMask;
				else
					this.states = this.states & (~tempMask);
				this.statesStatus = this.statesStatus | tempMask;  // mark as set
			}
			objResult.returnValue = (this.states & mask)==mask;
		}
	}
	return objResult;
}

function IDMWSC_Folder_getId() {
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.id, "");
	return objResult;
}

function IDMWSC_Folder_getLabel() {
	var	objResult;
	if (this.label == null)  { // not set, get from server
		var co = RSExecute(IDMWSC_baseURL + "/Wsapi/Folder.asp","getLabel",this.library.getName().returnValue,this.id);
		var objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded())
			this.label = objResult.returnValue;  // save 
	}
	else	
		objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.label, "");
	return objResult;
}

function IDMWSC_Folder_getLibrary() {
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.library, "");
	return objResult;
}

function IDMWSC_Folder_getObjectType() {
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.objectType, "");
	return objResult;
}

function IDMWSC_Folder_getSystemType() {
	return this.library.getSystemType();
}

function IDMWSC_Folder_setLabel(label) {
	this.label = label;
}

function IDMWSC_Folder_setState(state,value) {
	// set a folder 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 set:
	this.statesStatus = this.statesStatus | mask;
}


// Object Constructor:
function IDMWSC_Folder(id,library)
{
	// Properties
	this.id = id;
	this.pathName = null;
	this.library = library;
	this.objectType = IDMWSC_objTypeFolder;
	this.label = null;
	this.states = 0;  // holds folder getStates
	this.statesStatus = 0;  // holds whether the state was retrieved
	if ((typeof(id) == "string") && (id.indexOf("/") >= 0))  // id is the path
		this.pathName = id;
}

// Instance Methods:
IDMWSC_Folder.prototype.showAddFolderWizard = IDMWSC_Folder_showAddFolderWizard;
IDMWSC_Folder.prototype.showAddDocumentWizard = IDMWSC_Folder_showAddDocumentWizard;
IDMWSC_Folder.prototype.getPathName =  IDMWSC_Folder_getPathName;
IDMWSC_Folder.prototype.addBookmark = IDMWSC_Folder_addBookmark;
IDMWSC_Folder.prototype.copy = IDMWSC_Folder_copy;
IDMWSC_Folder.prototype.deleteObject = IDMWSC_Folder_deleteObject;
IDMWSC_Folder.prototype.file = IDMWSC_Folder_file;
IDMWSC_Folder.prototype.move = IDMWSC_Folder_move;
IDMWSC_Folder.prototype.showPropertiesDialog = IDMWSC_Folder_showPropertiesDialog;
IDMWSC_Folder.prototype.unfile = IDMWSC_Folder_unfile;
IDMWSC_Folder.prototype.getState = IDMWSC_Folder_getState;
IDMWSC_Folder.prototype.getId = IDMWSC_Folder_getId;
IDMWSC_Folder.prototype.getLabel = IDMWSC_Folder_getLabel;
IDMWSC_Folder.prototype.getLibrary = IDMWSC_Folder_getLibrary;
IDMWSC_Folder.prototype.getObjectType = IDMWSC_Folder_getObjectType;
IDMWSC_Folder.prototype.getSystemType = IDMWSC_Folder_getSystemType;
IDMWSC_Folder.prototype.setLabel = IDMWSC_Folder_setLabel;
IDMWSC_Folder.prototype.setState = IDMWSC_Folder_setState;
IDMWSC_Folder.prototype.send = IDMWSC_Folder_send;
