function CapabilityFinder(ID, ContentID, MetaDataGroupType, ContentTypes, SiteRoot)
{
	this.ID = ID;
	this.ContentID = ContentID;
	this.MetaDataGroupType = MetaDataGroupType;
	this.ContentTypes = ContentTypes;
	this.SiteRoot = SiteRoot;
	this.Lists = new Array();
	this.Selected = null;
	this.Content = new Array();
	return this;
}

CapabilityFinder.prototype.AddList = function(CategoryID, ListID, ParentCategoryID, InitialText)
{
	this.Lists[this.Lists.length] = {ListID:ListID, CategoryID:CategoryID, ParentCategoryID:ParentCategoryID, InitialText:InitialText};
}

CapabilityFinder.prototype.GetList = function(list)
{
	document.getElementById(this.ContentID).innerHTML = '';
	document.getElementById('titles').style.visibility = 'hidden';
	this.Content.length = 0;
	for(i = 0; i < this.Lists.length; i++)
	{
		if(this.Lists[i].ListID == list.id)
		{
			var found = false;
			for(j = 0; j < this.Lists.length; j++)
			{
				if(this.Lists[j].ParentCategoryID == this.Lists[i].CategoryID)
				{
					found = true;
					document.getElementById(this.Lists[j].ListID).disabled = true;
					var CodesRemote = new RemoteMethod('CodesRemote.epi');
					CodesRemote.MethodName = 'GetCodeTranslationsByParentCategory';
					this.Selected = {ListID:this.Lists[j].ListID,ParentCategoryID:this.Lists[j].ParentCategoryID, ParentCode:list[list.selectedIndex].value, CategoryID:this.Lists[j].CategoryID, InitialText:this.Lists[j].InitialText};
					CodesRemote.AddArguments(this.Lists[j].ParentCategoryID, list[list.selectedIndex].value, this.Lists[j].CategoryID);
					CodesRemote.LocalAction = DisplayList;

					CodesRemote.Invoke();
					j = this.Lists.length;					
				}
			}
			document.getElementById('errorMessage').style.display = 'none';
			i = this.Lists.length;
		}
	}
}

CapabilityFinder.prototype.GetContent = function()
{
		var query = new String();
		var count = 0;
				
		for(j = 0; j < this.Lists.length; j++)
		{
			var list = document.getElementById(this.Lists[j].ListID);
			if(list.options.length > 0 && list[list.selectedIndex].value != '')
			{
				query += this.Lists[j].CategoryID + ":" + list[list.selectedIndex].value + ";";	
				count++;
			}
		}
		if(count == (this.Lists.length))
		{
			query = query.substring(0, query.length - 1);

			var remote = new RemoteMethod('CapabilityFinder.epi');
			remote.MethodName = 'GetContent';
			remote.AddArguments(SerializeSearchQuery(this.MetaDataGroupType, this.ContentTypes, query), this.SiteRoot, location.pathname, false);
			remote.LocalAction = SetContent;
			remote.Invoke();
		}else
				document.getElementById('errorMessage').style.display = 'inline';
		return false;
}

function SerializeSearchQuery(MetaDataGroupType, ContentTypes, MetaData)
{
	var query = MetaDataGroupType + '|' + MetaData + '|';
	for(k = 0; k < ContentTypes.length; k++)
	{
		query += ContentTypes[k];
		if(k < ContentTypes.length - 1)
			query += '!';
	}
	return query;
}

CapabilityFinder.prototype.DisplayList = function(args)
{
	var cat = new CategoryInfo(args);
	if(this.Selected != null)
	{
		if(this.Selected.ParentCategoryID == cat.ParentCategoryID && this.Selected.ParentCode == cat.ParentCode && this.Selected.CategoryID == cat.CategoryID)
		{
			var list = document.getElementById(this.Selected.ListID);
			list.disabled = false;
			list.options.length = 0;
			if(this.Selected.InitialText != null)
			{
				list.options[list.options.length] = new Option(this.Selected.InitialText, '');
			}
			for(i = 0; i < cat.Codes.length; i++)
			{
				list.options[list.options.length] = new Option(cat.Codes[i].LongTranslation, cat.Codes[i].Code);
			}
			list.selectedIndex = 0;
			this.GetList(list);
		}
	}
}

CapabilityFinder.prototype.DisplayContent = function()
{
	if(document.getElementById('finderInitialContent') != null)
		document.getElementById('finderInitialContent').style.display = 'none';
	if(document.getElementById('finderResultContent') != null)
		document.getElementById('finderResultContent').style.display = 'inline';
	document.getElementById(this.ContentID).innerHTML = '';
	document.getElementById('titles').style.visibility = 'visible';
	for(i = 0; i < this.Content.length; i++)
	{
		document.getElementById(this.ContentID).innerHTML += this.Content[i].Content;
	}
}

CapabilityFinder.prototype.SetContent = function(args)
{
	this.Content = eval(args);
	if(this.Content != null && this.Content.length > 0)
	{
		this.DisplayContent();
	}else{
		this.Content = new Array();
	}
}

function DisplayList(args)
{
	finder.DisplayList(args);
}

function SetContent(args)
{
	finder.SetContent(args);
}

function CategoryInfo(args)
{
	var info = eval(args);
	this.ParentCategoryID = info[0];
	this.ParentCode = info[1];
	this.CategoryID = info[2];
	this.Codes = info[3];
	return this;
}
