function onSearchSubmit(formElem, errorElemId){
	var city = $j.trim($j(formElem.ci).val());
	var state = $j.trim($j(formElem.st).val());
	var cno = $j(formElem.cno).val();
	var zip = $j.trim($j(formElem.zip).val());
	var mlsid = $j.trim($j(formElem.mlsid).val());
	var reoid = $j.trim($j(formElem.src_ref).val());
	
	if(state == "" && city == "" && zip == "" && reoid == "" && mlsid == ""){
		$j('#' + errorElemId).show();
		return false;
	} 
	
	if(zip != ""){
		if(city == "" && state == "" && cno == ""){
			return true;
		} else {
			$j(formElem.zip).attr("name", "z");
			return true;
		}
	}
}
	
function siderailSearch_showSearchTab(){
	selectTab('search');
	$j('#siderail_search_advanced').hide();
	$j('#siderail_search_advanced input').attr('checked', false);
}

function siderailSearch_showFinancingTab(){
	selectTab('financing');
	$j('#siderail_search_advanced').show();
}

function selectTab(tabNamePrefix){
	$j('#search_form_links li').removeClass('current');
	$j('#'+tabNamePrefix+'_form_link').addClass('current');
}

function getCounties(selectedCounty, resetZip, resetCity){
	var state = $j('#hp_state_selector').val();
	var countySelector = document.getElementById('hp_county_selector');

	resetCounties(countySelector);
	if(resetZip){
		resetZipcode();
	}
	if(resetCity){
		hpResetCity();
	}
	
	if(state == '-1' || state == ''){
		var tmpOpt = new Option('Select a County', '');
		countySelector.options[0] = tmpOpt;
		return;
	} else {
		var tmpOpt = new Option('Getting Counties...', '');
		countySelector.options[0] = tmpOpt;
		$j(countySelector).attr('disabled', true);
		$j('#cno_loader').show();
	}
	
	var url = appPath + 'http://static-mirror.foreclosure.com/ajax/getCounties.xml';
	var parms = 'st=' + state;
	$j.get(url, parms, function(countyXml){
		countyList = countyXml.getElementsByTagName('county');
		resetCounties(document.getElementById('hp_county_selector'));
		countySelector.removeAttribute('disabled');
		$j('#cno_loader').hide();
		countySelector.options[0] = new Option('Select a County', '000');
		for(var i=0; i < countyList.length; i++){
			fips = countyList[i].getAttribute('fips');
			county = countyList[i].firstChild.nodeValue;
			var tmpOpt = new Option(county, fips);
			if(fips == selectedCounty){
				tmpOpt.selected = true;
			}
			countySelector.options[i+1] = tmpOpt;
		}
	}, 'xml');
}

function resetCounties(elem){
	while((op = (elem.options.length - 1)) > 0){
		elem.remove(op);
	} 
}

function resetZipcode(){
	$j('#hp_zip').val("");
}

function hpResetCity(){
	$j('#city_auto_input').val("");
}
	
function selectState(state){
	var stateSelector = document.getElementById('hp_state_selector');
	for(var i = 0; i < stateSelector.options.length; i++){
		if(state == stateSelector.options[i].value){
			stateSelector.options[i].selected = true;
			return;
		}	
	}
}

function initSearchCity(selection) {
	var cityState = $j(selection).html();
	var match = /^([^,]+), ([a-zA-Z]{2})/.exec(cityState);
	if(match.length == 3){
		var city = match[1];
		var state = match[2].toUpperCase();
		$j('#city_auto_input').val(city);
		selectState(state);
		getCounties('', true, false);
	}
}