if (!Prototype) {
	alert("The Infobase script requires the Prototype library, which is not available.");
}

if (!SearchCombo) {
	alert("The Infobase script requires the Search Combo library, which is not available.");
}

var Infobase = {};
Infobase.Search = {};
Infobase.UI = {};

Infobase.Search.toggleSearchType = function(control, searchType, isSearchTypeHeading)	{
	var rdbvaluation = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbvaluation');
	var rdbSales = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbSales');
	var rdbonelineProperty = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbonelineProperty');
	var rdbthreelineProperty = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbthreelineProperty');
	var rdbonelineSales = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbonelineSales');
	var rdbthreelineSales = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_rdbthreelineSales');
	var chkExcludeSales = document.getElementById(control.id.substr(0, control.id.lastIndexOf('_')) + '_chkExcludeSales');
	
	if (isSearchTypeHeading) {
		rdbonelineProperty.checked = false;
		rdbthreelineProperty.checked = false;	
		rdbonelineSales.checked = false;
		rdbthreelineSales.checked = false;
		chkExcludeSales.checked = false;
	
		if (searchType == 'SALES') {
			rdbonelineSales.checked = true;
		} else {
			rdbonelineProperty.checked = true;
		}
	} else {
		if (searchType == 'SALES') {
			rdbSales.checked = true;
			rdbonelineProperty.checked = false;
			rdbthreelineProperty.checked = false;		
			if (!rdbonelineSales.checked && !rdbthreelineSales.checked) {
				rdbonelineSales.checked = true;
			}							
		} else {
			rdbvaluation.checked = true;
			chkExcludeSales.checked = false;
		}
	}		
}
	
Infobase.Search.ControlIndex = {ADDRESS: 0,
	                            STREET:  1,
	                            LEGAL:   2,
	                            VALREF:  3,
	                            OWNER:   4};
	
Infobase.Search.changeRegion = function(clearStreet) {
	var ddlRegion = $(ddlRegionId);
	if (ddlRegion) {
		var regionCode = Number(ddlRegion.value);
		if (regionCode == -2) {
			regionCode = 0;
		}
		//Requires JS code generated by SearchComboBox.vb (OnPreRender())
		if (clearStreet) {
			Infobase.UI.setInputValue(control_ids[Infobase.Search.ControlIndex.STREET].SEARCHKEYVALUE, '');
			Infobase.UI.setInputValue(control_ids[Infobase.Search.ControlIndex.STREET].SEARCHKEYTEXT, '');
		}
		control_params[Infobase.Search.ControlIndex.STREET].SEARCHURL = 'Content/PopulateStreetList.aspx?regionId=' + regionCode.toString() + '&ta=0';
	}
}

Infobase.Search.ActivateSearchType = function(searchBodyId){
	switch (searchBodyId) {
		case 'addressSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.ADDRESS);
			$(control_ids[Infobase.Search.ControlIndex.ADDRESS].SEARCHKEYTEXT).focus();
			break;
		case 'streetSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.STREET);
			Infobase.Search.changeRegion(false);
			$(ddlRegionId).focus();
			break;
		case 'legalSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.LEGAL);
			$(control_ids[Infobase.Search.ControlIndex.LEGAL].SEARCHKEYTEXT).focus();
			break;
		case 'valRefSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.VALREF);
			$(control_ids[Infobase.Search.ControlIndex.VALREF].SEARCHKEYTEXT).focus();
			break;
	}
}
	
Infobase.Search.selectSearchType = function(searchBodyId) {
	Infobase.UI.showTab(searchBodyId, 'addressSearch');
	Infobase.UI.showTab(searchBodyId, 'streetSearch');
	Infobase.UI.showTab(searchBodyId, 'legalSearch');
	Infobase.UI.showTab(searchBodyId, 'valRefSearch');
	//Infobase.UI.showTab(searchBodyId, 'ownerSearch');
	//Requires SearchComboBox.js
	switch (searchBodyId) {
		case 'addressSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.ADDRESS);
			$(control_ids[Infobase.Search.ControlIndex.ADDRESS].SEARCHKEYTEXT).focus();
			break;
		case 'streetSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.STREET);
			Infobase.Search.changeRegion(false);
			$(ddlRegionId).focus();
			break;
		case 'legalSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.LEGAL);
			$(control_ids[Infobase.Search.ControlIndex.LEGAL].SEARCHKEYTEXT).focus();
			break;
		case 'valRefSearch':
			initSearchComboBox(Infobase.Search.ControlIndex.VALREF);
			$(control_ids[Infobase.Search.ControlIndex.VALREF].SEARCHKEYTEXT).focus();
			break;
		//case 'ownerSearch':
		//	initSearchComboBox(Infobase.Search.ControlIndex.OWNER);
		//	$(control_ids[Infobase.Search.ControlIndex.OWNER].SEARCHKEYTEXT).focus();
		//	break;
	}
}
	
Infobase.UI.showTab = function(elementId, tabName) {
	Infobase.UI.showBlock(tabName, elementId == tabName);
	Infobase.UI.setTabActive(tabName, elementId == tabName);
}
	
Infobase.UI.setTabActive = function(tabName, active) {
	if (active) {
		$(tabName + 'Tab').addClassName('active');
	} else {
		$(tabName + 'Tab').removeClassName('active');
	}
}
	
Infobase.UI.showBlock = function(elementName, show) {
	var el = $(elementName);
	if (el) {
		el.style.display = show ? "block" : "none";
	}
}
	
Infobase.UI.setInputValue = function(elementName, value) {
	var el = $(elementName);
	if (el && el.value) {
		el.value = value;
	}
}