//  ------------------------------------------------------------
//   StoredSearch.js
//   ------------------------------------------------------------
//   VCS INFO:
//   
//   $Revision:   1.13  $
//   $Date:   15 Mar 2001 16:25:44  $
//   $Author:   vpham  $
//   $Workfile:   storedsearch.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.
//   ------------------------------------------------------------

// Constants for getState:
var IDMWSC_StoredSearch_launchable = 2;
var IDMWSC_StoredSearch_canDelete = 3;
var IDMWSC_StoredSearch_isTemplate = 4;

// IDMWSC_StoredSearch method implementations:

function IDMWSC_StoredSearch_addBookmark(label) {
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Stored Search]";

		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=search&id=" + this.id;

	window.external.AddFavorite(szURL, szLabel);
}

function IDMWSC_StoredSearch_send(label) {	
	var szLabel;
	if (label)
		szLabel = label
	else {
		szLabel = "[IDM Stored Search]";

		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=search&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_StoredSearch_deleteObject(options) {
	var objResult;
	var label = this.label;
	if (!label)
		label = this.id;

	if (options == IDMWSC_withUI)
		if (!window.confirm("Delete search " + label + "?")) {
			objResult = new IDMWSC_Result(IDMWSC_Result_information, 0, "",IDMWSC_Result_S_FALSE, "");
			return objResult;
		}

	var co = RSExecute(IDMWSC_baseURL + "/WSAPI/StoredSearch.asp","deleteObject",this.library.getName().returnValue,this.id);
	objResult = IDMWSC_rsCallObjectToResult(co);
	if (objResult.succeeded())
		objResult.returnValue = true;
	return objResult;
}

function IDMWSC_StoredSearch_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/PropSheetSearch.asp?Library=' + this.library.getName().returnValue + '&id=' + this.id + '&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 popupName = IDMWSC_PopupManager_generatePopupNamePrefix(this.library.getName().returnValue) + "Property_" + this.objectType + "_" + this.id;
	IDMWSC_PopupManager_open("PropertyPage", popupName, URL, PopupFeatures, false);
}

function IDMWSC_StoredSearch_getLabel() {
	if (this.label == null)  { // not set, get from server
		var co = RSExecute(IDMWSC_baseURL + "/WSAPI/StoredSearch.asp","getLabel",this.library.getName().returnValue,this.id);
		var objResult = IDMWSC_rsCallObjectToResult(co);
		if (objResult.succeeded())
			this.label = objResult.returnValue;  // save 
	}
	else
		var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.label, "");
	return objResult;
}

function IDMWSC_StoredSearch_getId() {	
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.id, "");
	return objResult;
}

function IDMWSC_StoredSearch_getLibrary() {	
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.library, "");
	return objResult;
}

function IDMWSC_StoredSearch_getObjectType() {	
	var	objResult = new IDMWSC_Result(IDMWSC_Result_success, 0, "", this.objectType, "");
	return objResult;
}

function IDMWSC_StoredSearch_getSystemType() {	
	return this.library.getSystemType();
}

function IDMWSC_StoredSearch_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/StoredSearch.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_StoredSearch_launchable;i<=IDMWSC_StoredSearch_isTemplate;i++) {			
				tempMask = 1 << (i-1);
				if (eval(folderStates[i-IDMWSC_StoredSearch_launchable]))
					this.states = this.states | tempMask;
				else
					this.states = this.states & (~tempMask);
				this.statesStatus = this.statesStatus | tempMask;
			}
			objResult.returnValue = (this.states & mask)==mask;
		}
	}
	return objResult;
}

function IDMWSC_StoredSearch_setState(state,value) {
	// set a search 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 retrieved:
	this.statesStatus = this.statesStatus | mask;
}

function IDMWSC_StoredSearch_launch() {	
	IDMWSC_showSearchWindow(this.library, this.id);
}

function IDMWSC_StoredSearch_setLabel(label) {	
	this.label = label;
}

// Object Constructor:
function IDMWSC_StoredSearch(id,library)
{
	// Properties
	this.id = id;
	this.library = library;
	this.objectType = IDMWSC_objTypeStoredSearch;
	this.fileName = "";
	this.label = null;
	this.states = 0;  // holds search states
	this.statesStatus = 0;  // holds whether the state was retrieved
}

// Instance Methods:
IDMWSC_StoredSearch.prototype.addBookmark = IDMWSC_StoredSearch_addBookmark;
IDMWSC_StoredSearch.prototype.deleteObject = IDMWSC_StoredSearch_deleteObject;
IDMWSC_StoredSearch.prototype.showPropertiesDialog = IDMWSC_StoredSearch_showPropertiesDialog;
IDMWSC_StoredSearch.prototype.getLabel = IDMWSC_StoredSearch_getLabel;
IDMWSC_StoredSearch.prototype.getId = IDMWSC_StoredSearch_getId;
IDMWSC_StoredSearch.prototype.getLibrary = IDMWSC_StoredSearch_getLibrary;
IDMWSC_StoredSearch.prototype.getObjectType = IDMWSC_StoredSearch_getObjectType;
IDMWSC_StoredSearch.prototype.getSystemType = IDMWSC_StoredSearch_getSystemType;
IDMWSC_StoredSearch.prototype.getState = IDMWSC_StoredSearch_getState;
IDMWSC_StoredSearch.prototype.launch = IDMWSC_StoredSearch_launch;
IDMWSC_StoredSearch.prototype.setLabel = IDMWSC_StoredSearch_setLabel;
IDMWSC_StoredSearch.prototype.send = IDMWSC_StoredSearch_send;
IDMWSC_StoredSearch.prototype.setState = IDMWSC_StoredSearch_setState;
