/////////////////////////////////////////////////////////////
// Author: hailesa										   //
// Created: 11/18/2008									   //
// Description: js functions for the 'Search MLS' page	   //
/////////////////////////////////////////////////////////////
var MoreSearchCriteriaLoaded = false;
var LoadingIndicator = '<div class="Loading">Loading...</div>';

function sa_hideShow(el,tg,cls1,cls2) {
	var e = $('#'+el);
	var t = $('#'+tg);
	if (t.get(0).className == cls1) {
		e.show();
		t.get(0).className = cls2;
	} else {
		e.hide();
		t.get(0).className = cls1
	}
}

function smls_getMoreListingsNearby(el,SupplierId,City,State,Zip,Radius) {
	$('#'+el)
		.empty()
		.append(LoadingIndicator)
		.load('/SearchMLS.cfm',
			  { Action: 'GetMoreListingsNearby',
				SupplierId: SupplierId,
				City: City,
				State: State,
				Zip: Zip,
				Radius: Radius }
	);
}

function smls_getSupplierOrder(el,SupplierId,SupplierOrder) {
	$('#'+el)
		.empty()
		.append(LoadingIndicator)
		.load('/SearchMLS.cfm',
			  { Action: 'GetSupplierOrder',
				SupplierId: SupplierId,
				SupplierOrder: SupplierOrder }
	);
}

function smls_getMoreSearchCriteria(el,tg,SupplierId,SupplierOrder) {
	// sa_hideShow(el,tg,'ArrowOvalUp','ArrowOvalDown');
	if (!MoreSearchCriteriaLoaded) {
		$('#'+el)
			.empty()
			.append(LoadingIndicator)
			.load('/SearchMLS.cfm',
				  { Action: 'GetMoreSearchCriteria',
					SupplierId: SupplierId,
					SupplierOrder: SupplierOrder }
		);
		MoreSearchCriteriaLoaded = true;
	}								
}

function smls_onSubmit() {
	document.SaveSettings.Radius.value = document.SearchCriteria.Radius.value;
	document.SaveSettings.SupplierId.value = document.SearchCriteria.SupplierId.value;
	document.SaveSettings.SupplierOrder.value = document.SearchCriteria.SupplierOrderSelect.options[document.SearchCriteria.SupplierOrderSelect.selectedIndex].value;
	document.SaveSettings.AreaSearch.value = '';
	document.SaveSettings.AddCities.value = '';
	document.SaveSettings.ZIP.value = '';
	document.SaveSettings.AddZIPs.value = '';
	if (document.SaveSettings.SupplierOrder.value == 'ZIP') {
		document.SaveSettings.ZIP.value = document.SearchCriteria.ZIP.value;
		document.SaveSettings.AddZIPs.value = smls_makeCheckBoxValueList(document.SearchCriteria.AddZIPs);
	} else {
		document.SaveSettings.AreaSearch.value = document.SearchCriteria.AreaSearch.options[document.SearchCriteria.AreaSearch.selectedIndex].value;
		document.SaveSettings.AddCities.value = smls_makeCheckBoxValueList(document.SearchCriteria.AddCities);
	}
	return true;
}

function smls_makeCheckBoxValueList(CheckBox) {
	var _list = "";
	if (CheckBox.length) {
		for (var i=0; i < CheckBox.length; i++) {
			if (CheckBox[i].disabled == false && CheckBox[i].checked) _list = _list + CheckBox[i].value + ",";
	 	}
		if (_list.length > 1) { _list = _list.substr(0,_list.length-1); }
	}
	return _list;
}

function smls_onLoad() {
	var Radius = document.SaveSettings.Radius.value;
	var SupplierId = document.SaveSettings.SupplierId.value;
	var SupplierOrder = document.SaveSettings.SupplierOrder.value;
	var CityState = document.SaveSettings.AreaSearch.value;
	var AddCities = document.SaveSettings.AddCities.value;
	var AddZIPs = document.SaveSettings.AddZIPs.value;
	var City = "";
	var State = "";
	var Zip = document.SaveSettings.ZIP.value;
	
	City = CityState.substr(0,CityState.indexOf(':'));
	State = CityState.substr(CityState.indexOf(':')+1,2);
	if (SupplierOrder == 'ZIP') {
		$('#MoreSearchCriteria')
			.empty()
			.append(LoadingIndicator)
			.load('/SearchMLS.cfm',
				  { Action: 'GetMoreSearchCriteria',
					SupplierId: SupplierId,
					SupplierOrder: SupplierOrder,
					Radius: Radius,
					Zip: Zip,
					AddZIPs: AddZIPs}
		);
	} else {
		$('#MoreSearchCriteria')
			.empty()
			.append(LoadingIndicator)
			.load('/SearchMLS.cfm',
				  { Action: 'GetMoreSearchCriteria',
					SupplierId: SupplierId,
					SupplierOrder: SupplierOrder,
					Radius: Radius,
					City: City,
					State: State,
					AddCities: AddCities}
		);
	}
}

$(document).ready(
	function(){
		$('.HideShow').each(function(){$(this).hide();});
		$('#NavPropertyTypes').click();
		smls_onLoad();
	}	
);