var modelItem 	= new Array();
var objectItem 	= new Array();

var modelIndexFile 	= "../js/model_index.js";
var objectIndexFile = "../js/object_index.js";

var modelArrayLoaded 	= false;
var objectArrayLoaded = false;

var strSearchString	= "";
var boolFragment		= true;
var boolCaseSens		= false
var arrayModelTypes	= new Array();
var arrayClasses		= new Array();

var ergModels				= new Array();
var ergObjects			= new Array();

function modelItemElem(modelName, modelId, modelType)
{
	this.modelName 	= modelName;
	this.modelId 		= modelId;
	this.modelType 	= modelType;
}

function objectItemElem(objectName, objectId, objectClass, modelName, modelId, modelType)
{
	this.objectName		= objectName;
	this.objectId			= objectId;
	this.objectClass	= objectClass;
	this.modelName 		= modelName;
	this.modelId			= modelId;	
	this.modelType		= modelType;
}

function modelSearchInitialized()
{
	modelArrayLoaded = true;
}

function objectSearchInitialized()
{
	objectArrayLoaded = true;
}

function isInList(searchArray, strElem)
{
	for (var i = 0; i < searchArray.length; i++)
	{
		if (searchArray[i] == strElem)
		{
			return true
		}
	}
	return false
}

function getFile(searchString)
{
	for (var i = 0; i < iconItem.length; i++)
	{
		if (iconItem[i].elemName == searchString)
		{
			return iconItem[i].iconFile
		}
	}
	return ""
}

function getSearchSettings()
{
	if (searchWindow)
	{
		searchDoc 			= searchWindow.document;
		boolCaseSens 		= searchDoc.getElementById('casesens').checked;
		boolFragment 		= searchDoc.getElementById('fragment').checked;
		strSearchString = searchDoc.getElementById('searchtext').value;
		arrayModelTypes	= new Array();
 		arrayClasses		= new Array();	
		
		if (searchDoc.getElementById("allModels").checked)
		{
			arrayModelTypes = searchWindow.searchParams.getAllModelTypes()
		}
		else
		{
			var listElems = searchDoc.getElementById("modeltypeSelect").getElementsByTagName("input");
			for (var i = 0; i < listElems.length; i++)
			{
				var currElem = listElems[i];
				if (currElem.name != "allModels" && currElem.checked)
				{
					arrayModelTypes.push(currElem.name);	
				}
			}
		}
	
		var listElems = searchDoc.getElementById("classSelect").getElementsByTagName("input");
	
		if (searchDoc.getElementById("allClasses").checked)
		{
			for (var i = 0; i < listElems.length; i++)
			{
				var currElem = listElems[i];
				if (currElem.name != "allClasses")
				{
					arrayClasses.push(currElem.name);
				}
			}
		}
		else
		{
			for (var i = 0; i < listElems.length; i++)
			{
				var currElem = listElems[i];
				if (currElem.name != "allClasses" && currElem.checked)
				{
					arrayClasses.push(currElem.name);
				}
			}
		}
	}
	startSearch();
}

function startSearch()
{
	if (!modelArrayLoaded)
	{
		heads 							= document.getElementsByTagName("head");
		scriptElem					= document.createElement("script");
		scriptElem.charset	= "ISO-8859-1";
		scriptElem.type 		= "text/javascript";
		scriptElem.src 			= modelIndexFile;
		heads[0].appendChild(scriptElem);		
	}
	
	if (!objectArrayLoaded && (arrayClasses.length > 0))
	{
		heads 							= document.getElementsByTagName("head");
		scriptElem					= document.createElement("script");
		scriptElem.charset	= "ISO-8859-1";
		scriptElem.type 		= "text/javascript";
		scriptElem.src 			= objectIndexFile;
		heads[0].appendChild(scriptElem);			
	}
	doSearch();
}

function doSearch()
{
	if (!modelArrayLoaded && (!objectArrayLoaded && (arrayClasses.length > 0)))
	{
		window.setTimeout("doSearch();", 100);
	}
	else
	{
		var intCounter 	= 0;
		ergModels		= new Array();
		ergObjects	= new Array();		
	
		if (!boolCaseSens)
		{
			currSearchString = strSearchString.toLowerCase();
		}
		else
		{
			currSearchString = strSearchString;
		}
		
		for (var currentModelItem = 1; currentModelItem < modelItem.length; currentModelItem++)
		{
			if (!boolCaseSens)
			{
				var modelName = modelItem[currentModelItem].modelName.toLowerCase();
			}
			else
			{
				var modelName = modelItem[currentModelItem].modelName;
			}
			
			if (((!boolFragment  && (modelName == currSearchString)) || (boolFragment && (modelName.indexOf(currSearchString) != -1))) && isInList(arrayModelTypes, modelItem[currentModelItem].modelType))
			{
				ergModels[intCounter] = modelItem[currentModelItem];
				intCounter = intCounter + 1;
			}
		}
		
		var intCounter 	= 0;
	
		for (var currentObjectItem = 1; currentObjectItem < objectItem.length; currentObjectItem++)
		{
			if (!boolCaseSens)
			{
				var objectName = objectItem[currentObjectItem].objectName.toLowerCase();
			}
			else
			{
				var objectName = objectItem[currentObjectItem].objectName;
			}
			
			if (((!boolFragment  && (objectName == currSearchString)) || (boolFragment && (objectName.indexOf(currSearchString) != -1))) && isInList(arrayModelTypes, objectItem[currentObjectItem].modelType) && isInList(arrayClasses, objectItem[currentObjectItem].objectClass))
			{
				ergObjects[intCounter] = objectItem[currentObjectItem];
				intCounter = intCounter + 1;
			}
		}

		if (parent.model.location.href.indexOf("result.html") == -1)
		{
			parent.model.location.href = "../misc/result.html";	
		}
		writeResult();
	}
}

function writeResult()
{
	if (!top.frames["model"].document.getElementById("mainContentIndex"))
	{
		window.setTimeout("writeResult();", 100);
	}
	else
	{
		var erg="";
		parent.model.document.title = top.strTitleModels;
		if (ergModels.length == 0 && ergObjects.length == 0)
		{
			erg = ("<div class='text'>" + top.strMessageNoResultsFound + "</div>");
		}
		else
		{
			if (ergModels.length > 0)
			{
				erg = erg + ("<div class='block'><div class='title'>" + top.strTitleModels + "</div><ul>");
				for (var currentModelItem = 0; currentModelItem < ergModels.length; currentModelItem++)
				{
					erg = erg + ("<li><img class='icon' src='../misc/images/icons/" + getFile(ergModels[currentModelItem].modelType) + "' /><a href='../ID" + ergModels[currentModelItem].modelId + "/ID" + ergModels[currentModelItem].modelId + ".html'>" + ergModels[currentModelItem].modelName + "</a></li>");
				}
				erg = erg + ("</ul></div>");
			}

			if (ergObjects.length > 0)
			{
				erg = erg + ("<div class='block'><div class='title'>" + top.strTitleObjects + "</div><ul>");
				for (var currentObjectItem = 0; currentObjectItem < ergObjects.length; currentObjectItem++)
				{
					erg = erg + ("<li><img class='icon' src='../misc/images/icons/" + getFile(ergObjects[currentObjectItem].objectClass) + "' /><a href='javascript:openWindow(&quot;ID" + ergObjects[currentObjectItem].modelId + "&quot;, &quot;ID" + ergObjects[currentObjectItem].objectId + "&quot;);'>" + ergObjects[currentObjectItem].objectName + "</a>&nbsp;->&nbsp;<img class='icon' src='../misc/images/icons/" + getFile(ergObjects[currentObjectItem].modelType) + "' /><a href='../ID" + ergObjects[currentObjectItem].modelId + "/ID" + ergObjects[currentObjectItem].modelId + ".html'>" + ergObjects[currentObjectItem].modelName + "</a></li>");
				}				
				erg = erg + ("</ul></div>");
			}
		}
		top.frames["model"].document.getElementById("mainContentIndex").innerHTML = erg;
		searchWindow.document.getElementById('searchWait').style.display	= 'none';
		parent.header.document.getElementById('modelname').innerHTML = "<nobr>" + top.strHeaderSearchResults + "</nobr>";
	}
}

searchFunctionsLoaded();