function CustomerStoryFinder(ID, RootID, MetaDataGroupType, ContentTypes, SiteRoot, PostBack)
{
	this.ID = ID;
	this.RootID = RootID;
	this.MetaDataGroupType = MetaDataGroupType;
	this.ContentTypes = ContentTypes;
	this.PostBack = PostBack;
	this.SiteRoot = SiteRoot;
	this.Lists = new Array();
	this.Selected = null;
	this.Content = new Array();
	return this;
}

CustomerStoryFinder.prototype.AddList = function(CategoryID, ListID, ParentCategoryID, InitialText)
{
	this.Lists[this.Lists.length] = {ListID:ListID, CategoryID:CategoryID, ParentCategoryID:ParentCategoryID, InitialText:InitialText};
}

CustomerStoryFinder.prototype.SetList = function(list)
{
	if(list.selectedIndex > 0)
	{
		document.getElementById('errorMessage').style.display = 'none';
	}
	for(i = 0; i < this.Lists.length; i++)
	{
		if(list.id == this.Lists[i].ListID)
		{
			for(j = 0; j < this.Lists.length; j++)
			{
				if(this.Lists[i].CategoryID == this.Lists[j].ParentCategoryID)
				{
					var childList = document.getElementById(this.Lists[j].ListID);
					childList.disabled = true;
					this.previousCode = childList[childList.selectedIndex].value;
					this.Selected = this.Lists[j];
					var remote = new RemoteMethod('CustomerStoryFinder.epi');
					
					if(list.selectedIndex > 1)
					{
						remote.MethodName = 'GetCodesByParentCategory';
						remote.AddArguments(this.MetaDataGroupType + '|' + this.ContentTypes[0] + '|' + this.Lists[i].CategoryID + '|' + this.Lists[j].CategoryID + '|' + list[list.selectedIndex].value);
					}else{
						remote.MethodName = 'GetCodesByCategory';
						remote.AddArguments(this.MetaDataGroupType + '|' + this.ContentTypes[0] + '|' + this.Lists[j].CategoryID);
					}
						remote.LocalAction = PopulateList;
						remote.Invoke();
				}
			}
		}
	}
}

CustomerStoryFinder.prototype.PopulateList = function(args)
{
	var info = eval(args);
	if(info != null && this.Selected != null)
	{
		var list = document.getElementById(this.Selected.ListID);
		list.length = 2;
		for(i = 1; i < info.length; i++)
		{
			list[list.length] = new Option(info[i].LongTranslation, info[i].Code);
			if(this.previousCode == info[i].Code)
				list.selectedIndex = list.length - 1;
		}
		list.disabled = false;
	}
}

CustomerStoryFinder.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 > 0)
		{
			/*query = query.substring(0, query.length - 1);

			var remote = new RemoteMethod('CustomerStoryFinder.epi');
			remote.MethodName = 'GetContent';
			remote.AddArguments(SerializeSearchQuery(this.MetaDataGroupType, this.ContentTypes, query), this.SiteRoot, location.pathname, false, this.PostBack);
			remote.LocalAction = SetContent;
			remote.Invoke();*/
			return true;
		}else
				document.getElementById('errorMessage').style.display = 'inline';
		return false;
}

CustomerStoryFinder.prototype.SetContent = function(args)
{
		this.Content = eval(args);
		if(this.Content != null && this.Content.length > 0)
		{
			document.getElementById('finderResults').InnerHTML = '';
			for(i = 0; i < this.Content.length; i++)
			{
				document.getElementById('finderResults').InnerHTML += this.Content[i].Content;
			}
			//document.getElementById(this.RootID + "_mpnlResultList").style.display = 'inline';
		}else{
			this.Content = new Array();
		}
}

function SetContent(args)
{
	finder.SetContent(args)
}

function PopulateList(args)
{
	finder.PopulateList(args);
}

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;
}

var previousOnload = window.onload;

window.onload = function()
{
	if(previousOnload != null)
		eval(previousOnload);
	for(i = 0; i < finder.Lists.length; i++)
	{
		var list = document.getElementById(finder.Lists[i].ListID);
		if(list != null && list.selectedIndex > 1)
		{
			finder.SetList(list);
		}
	}
}