	TargetNode - first node found or null
//		TargetArray - array of all nodes if bFindAll == true
//		ModelAmbiguous - true if any found node exists under an ambiguous Model
//		EnvironmentAmbiguous - true if any found node exists under an ambiguous Environment
//
function GetSearchOutlineNodesObject( nOriginNode, oSearchCriteria, bFindAll ) {
	return new SearchOutlineNodes( nOriginNode, oSearchCriteria, bFindAll );
}
function SearchOutlineNodes( nOriginNode, oSearchCriteria, bFindAll ) {
	this.OriginNode = nOriginNode;
	this.SearchCriteria = oSearchCriteria;
	this.FindAll = ( bFindAll == true );
	
	this.Search = SearchOutlineNodes_Search;
	this.Descent = SearchOutlineNodes_Descent;					// private
	
	this.TargetNode = null;
	this.TargetArray = new Array();
	this.ModelAmbiguous = false;
	this.EnvironmentAmbiguous = false;
	
	this.ModelID = g_Wizard.App.Scripting.id_Model;				// private
	this.EnvironmentID = g_Wizard.App.Scripting.id_Environment;	// private
}
function SearchOutlineNodes_Search() {
	try {
		if( this.OriginNode == null || this.SearchCriteria == null ) throw new Error();
//
//		Ascending search starting with the OriginNode
//
		var nLastSearched = null;
		for( var nNode = this.OriginNode; nNode != null; nNode = nNode.Parent ) {
			this.Descent( nNode, nLastSearched, this.ModelAmbiguous, this.EnvironmentAmbiguous );
			if( !this.FindAll && this.TargetArray.length > 0 ) break;
			nLastSearched = nNode;
		}
//
//		Return true if targets were found, otherwise return false
//
		if( this.TargetArray.length > 0 ) {
			this.TargetNode = this.TargetArray[0];
			return true;
		}
		return false;
	}
	catch(e) {
		this.TargetNode = null;
		this.TargetArray = new Array();
		this.ModelAmbiguous = false;
		this.EnvironmentAmbiguous = false;
		return false;
	}
}
function SearchOutlineNodes_Descent( nNode, nLastSearched, bModelAmbiguous, bEnvironmentAmbiguous ) {
//
//	Check to see if the TreeView node matches. If so, add it to the array
//
	if( this.SearchCriteria.Test( nNode.Tag )) {
		this.TargetArray[ this.TargetArray.length ] = nNode;
		this.ModelAmbiguous = this.ModelAmbiguous || bModelAmbiguous;
		this.EnvironmentAmbiguous = this.EnvironmentAmbiguous || bEnvironmentAmbiguous;
		if( !this.FindAll ) return true;	// only looking for the first
	}
//
//	Iterate the TreeView node's children
//
	for( var nChild = nNode.Child; nChild != null; nChild = nChild.Next ) {
		if( nChild == nLastSearched ) continue;
		var bChildModelAmbiguous = bModelAmbiguous;
		var bChildEnvironmentAmbiguous = bEnvironmentAmbiguous;
		if( nChild.Tag.Class == this.ModelID || nChild.Tag.Class == this.EnvironmentID ) {
//
//			Avoid recusing down a different branch from the one already searched
//
			if( nLastSearched != null )
				if( nChild.Tag.Class == nLastSearched.Tag.Class ) continue;
//
//			If searching in multiple Models or Environments set the ambiguity flag
//
			for( var nSibling = nChild.FirstSibling; nSibling != null; nSibling = nSibling.Next ) {
				if( nSibling != nChild && nSibling.Tag.Class == nChild.Tag.Class ) {
					if( nChild.Tag.Class == this.ModelID ) bChildModelAmbiguous = true;
					if( nChild.Tag.Class == this.EnvironmentID ) bChildEnvironmentAmbiguous = true;
					break;
				}
			}
		}
//
//		Search the child recursively
//
		this.Descent( nChild, null, bChildModelAmbiguous, bChildEnvironmentAmbiguous );
		if( !this.FindAll && this.TargetArray.length > 0 ) return true;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//	SEARCH CRITERIA OBJECT
//	Constructor:
//		sGroupName - optional name of object group to load
//	Properties:
//		Class - array of class ID's defined in DSConstants.js
//		Type - matching array of loadType or ResultType ID's defined in DSConstants.js
//		State - required State of the object (e.g. id_Updated) or null
//		NameRegularExpression - regular expression object for testing the object name or null
//	Methods:
//		ClassAndTypeCount - length of Class and Type arrays
//		AddClassAndType - manually adds a class id and type id (or null)
//		LoadFromObjectTypes - appends arrays with contents of a group from ObjectTypes.xml
//		SetState - sets the State property based on the enum string from the schema
//		SetNameRegularExpression - creates a RegExp object based on a regular expression string
//		Test - returns true if the given outline object matches the criteria
//
function GetSearchCriteriaObject( sGroupName, sState, sNameRegExp ) {
	return new SearchCriteria( sGroupName, sState, sNameRegExp );
}
function SearchCriteria( sGroupName, sState, sNameRegExp ) {
	this.ClassAndTypeCount = SearchCriteria_GetLength;
	this.AddClassAndType = SearchCriteria_AddClassAndType;
	this.LoadFromObjectTypes = SearchCriteria_LoadFromObjectTypes;
	this.LoadFromObjectTypes_ErrorDialogCallback = SearchCriteria_LoadFromObjectTypes_ErrorDialogCallback;	// private
	this.SetState = SearchCriteria_SetState;
	this.SetNameRegularExpression = SearchCriteria_SetNameRegularExpression;
	this.Test = SearchCriteria_Test;
	
	this.Class = new Array();
	this.Type = new Array();
	
	if( sGroupName != null && sGroupName != "" ) this.LoadFromObjectTypes( sGroupName );
	this.SetState( sState );
	this.SetNameRegularExpression( sNameRegExp );
}
function SearchCriteria_GetLength() {
	return this.Class.length;
}
function SearchCriteria_AddClassAndType( idClass, idType ) {
	var i = this.ClassAndTypeCount();
	this.Class[i] = idClass;
	this.Type[i] = idType;
	return true;
}
function SearchCriteria_LoadFromObjectTypes( sGroupName ) {
	if( sGroupName.indexOf( "id_" ) == 0 ) {
		this.AddClassAndType( eval( "g_Wizard.App.Scripting." + sGroupName ), null );
	}
	else {
		var nlObjectTypes = g_Wizard.DOM.documentElement.selectNodes( "object-groups/object-group[@name='" + sGroupName.toLowerCase() + "']/object-type" );
		for( var i = 0; i < nlObjectTypes.length; i++ ) {
			var eObjectType = nlObjectTypes(i);
			try {
				var idClass = eval( "g_Wizard.App.Scripting." + eObjectType.getAttribute( "class" ) );
				if( idClass == null ) throw new Error( g_Wizard.GlobalStrings.Lookup( "id_ClassIdNotFound" ) );
				var sType = eObjectType.getAttribute( "type" );
				if( sType != null ) {
					var idType = null;
					idType = eval( "g_Wizard.App.Scripting." + sType );
					if( idType == null ) throw new Error( g_Wizard.GlobalStrings.Lookup( "id_TypeIdNotFound" ) );
				}
				this.AddClassAndType( idClass, idType );
			}
			catch( eError ) {
				var oDialog = new DialogObject();
				oDialog.ShowModal( this.LoadFromObjectTypes_ErrorDialogCallback, [ eError.description, sGroupName, eObjectType.xml ] );
			}
		}
	}
}
function SearchCriteria_LoadFromObjectTypes_ErrorDialogCallback( aArguments, oDialogObject, oDialogDocument ) {
	with( oDialogObject ) {
		InsertHeading( g_Wizard.GlobalStrings.Lookup( "id_ErrorResolvingObjectType" ) );
		InsertRow();
		InsertCell( g_Wizard.GlobalStrings.Lookup( "id_Description" ), true );
		InsertCell( aArguments[0] );
		InsertRow();
		InsertCell( g_Wizard.GlobalStrings.Lookup( "id_Group" ), true );
		InsertCell( aArguments[1] );
		InsertRow();
		InsertCell( g_Wizard.GlobalStrings.Lookup( "id_Source" ), true );
		InsertCell( aArguments[2] );
	}
}
function SearchCriteria_Test( oOutlineObject ) {
	if( oOutlineObject != null ) {
		if( this.ClassAndTypeCount() > 0 ) {
//			test each given <object-type>
			for( var i = 0; i < this.ClassAndTypeCount(); i++ ) {
				try { 
					if( oOutlineObject.Class == this.Class[i] ) {
						if( this.Type[i] != null ) {
							if( oOutlineObject.Class == g_Wizard.App.Scripting.id_Load ) {
								if( oOutlineObject.loadType != this.Type[i] ) continue;
							}
							else if( oOutlineObject.Class == g_Wizard.App.Scripting.id_Result )
								if( oOutlineObject.ResultType != this.Type[i] ) continue;
						}
						if( this.State != null )
							if( oOutlineObject.State != this.State ) continue;
						if( this.NameRegularExpression != null )
							if( !this.NameRegularExpression.test( oOutlineObject.Name )) continue;
						return true;		// hit
					}
				}
				catch( e ) { continue }
			}
		}
		else {
//			no list of <object-type> given, test only State and NameRegularExpression
			try { 
				if( this.State != null )
					if( oOutlineObject.State != this.State ) return false;
				if( this.NameRegularExpression != null )
					if( !this.NameRegularExpression.test( oOutlineObject.Name )) return false;
				return true;
			}
			catch( e ) { }
		}
	}
	return false;
}
function SearchCriteria_SetState( sState ) {
	with( g_Wizard.App.Scripting ) {
		switch ( sState ) {
			case "stateless":
				this.State = id_Stateless;
				break;
			case "fully-defined":
				this.State = id_FullyDefined;
				break;
			case "under-defined":
				this.State = id_UnderDefined;
				break;
			case "suppressed":
				this.State = id_Suppressed;
				break;
			case "not-updated":
				this.State = id_NotUpdated;
				break;
			case "updated":
				this.State = id_Updated;
				break;
			case "obsolete":
				this.State = id_Obsolete;
				break;
			case "error":
				this.State = id_Error;
				break;
			case "bad-license":
				this.State = id_BadLicense;
				break;
			case "waitforvalidation":
			    this.state = id_WaitForValidation;
			default:
				this.State = null;
		}
	}
}
function SearchCriteria_SetNameRegularExpression( sNameRegExp ) {
	if( sNameRegExp != null && sNameRegExp != "" ) this.NameRegularExpression = new RegExp( sNameRegExp );
	else this.NameRegularExpression = null;
}
