function Search() {
	var MySelf = this;

	var SearchServiceURLs = new Array();
	var SearchServiceIDs = new Array();

	var SearchList;
	
	var masterControl = document.createElement('div');	
	var mapBook;
	
	this.addSearchService = function(name,url,id) {
		SearchServiceURLs[name] = url;
		SearchServiceIDs[name] = id;
	}

	this.setMapBook = function (mBook) {
		mapBook = mBook;
	}

	this.addSearchServicesFromMapbook = function(paramMapBook) {
		mapBook = paramMapBook;
		var services = mapBook.getElementsByTagName('service');
		for(var i = 0; i < services.length; i++) {
			if(services[i].getAttribute('searchbox')) {
				var serviceURL = services[i].getElementsByTagName('url')[0];
				var url = serviceURL.firstChild.nodeValue;

				var firstInput = services[i].getElementsByTagName('input')[0];
				
				MySelf.addSearchService(services[i].getAttribute('title'),url,firstInput.getAttribute('name'));
			}
		}
		MySelf.refreshSearchList();
	}
	
	this.refreshSearchList = function() {
		var currentOpts = SearchList.getElementsByTagName('option');
		for(var i = currentOpts.length - 1; i >= 0; i--) {
			SearchList.removeChild(currentOpts[i]);
		}
		for(var name in SearchServiceURLs) {
			var opt = document.createElement('option');
			opt.appendChild(document.createTextNode(name));
			opt.setAttribute('value', name);
			SearchList.appendChild(opt);
		}
	}

	this.getActiveSearchService = function () {
		var currentOpts = SearchList.getElementsByTagName('option');
		for(var i = currentOpts.length - 1; i >= 0; i--) {
			if(currentOpts[i].selected) {
				return currentOpts[i].getAttribute('value');
			}
		}
		return null;
	}


	this.getSearchBox = function() {
		masterControl.appendChild(document.createTextNode('Search:'));
		var searchBox = document.createElement('input');
		searchBox.setAttribute('subtype','user');

		masterControl.appendChild(searchBox);
		var searchSubmit = document.createElement('input');
		searchSubmit.value = 'Search';
		searchSubmit.type = 'submit';

		searchSubmit.onclick = function() {
			var currentService = MySelf.getActiveSearchService();
			if(currentService) {
				searchBox.name = SearchServiceIDs[currentService];
				var myService = new Service(getServiceByTitle(mapBook,currentService), AllResults);
				myService.setForm(masterControl);
				alert(myService.createURL());
				myService.getResults();
			}
			
			alert(MySelf.getActiveSearchService());		
		}
		
		masterControl.appendChild(searchSubmit);
		masterControl.appendChild(document.createElement('br'));

		SearchList = document.createElement('select');
		masterControl.appendChild(SearchList);
		this.refreshSearchList();

		return masterControl;
	}

}
