
	//******************************************************//
	/**
	 * used by the Flash map to receive values from checkboxes
	 * @param {Object} town
	 */
	function passTown(town) {
		var checkd = town.checked;
		if (checkd == true) {
			var myState = 'on';
			$(town).parent().css('background-color', '#C0E2CC');
		} else {
			var myState = 'off';
			$(town).parent().css('background-color', '');
		}
		var swf = $('#swfmap')[0];

		swf.setTown(town.id, myState);

	}
	
	
	function setTownCheckbox(town) {
		$('#'+town).attr('checked', true);
		$('#'+town).parent().css('background-color', '#C0E2CC');
	}
	
	
	function unsetTownCheckbox(town) {
		$('#'+town).attr('checked', false);
		$('#'+town).parent().css('background-color', '');
	}
	
	//******************************************************//
	
	function setF(txtfield) {
		var val = (txtfield.value/10000);
		val = (x < 200) ? x/2 : (x < 300) ? 100+((x-200)*5) : 600+((x-300)*10);
		if (txtfield.id == 'price_min') {
			$('#slider').SliderSetValues([[val,0],[]]);
		} else {
			$('#slider').SliderSetValues([[0,0],[val,0]]);
		}
	}
	function unsetF(txtfield) {

	}
	
	
	function leapto(form) {
	var myindex=form.setSelect.selectedIndex;
	var myValue = form.setSelect.options[myindex].value;
		if (myValue != '') {
			location.href=myValue;
			//myindex = 0; // reset pulldown menu
		}
	}
	
	//******************************************************//
	/**
	 * saves the format of search results style: large img, small img, gallery
	 * @param {Object} style
	 */
	function switch_view_style(style) {
		document.cookie = "view_style=" +escape( style ); + ' expires=Thu, 2 Aug 2012 20:47:11 UTC; path=/'
		return true;
	}
	
	
	//******************************************************//
	var div_state = 'block';
	/**
	 * expands all town lists on the county tab
	 */
	function expand_all() {
	var txt_state = (div_state == 'block') ? 'hide towns' : 'show towns';
		for (i=1; i<=16; ++i) {
			$('#div_'+i).css('display', div_state);
			$('#expand_'+i).html(txt_state);
		}
		if (div_state == 'block') {
			div_state = 'none';
			$('#expand_all').html('hide all towns');
			
		} else {
			div_state = 'block';
			$('#expand_all').html('show all towns');
		}
	}

	//******************************************************//
	/**
	 * expands/collapses the town lists on "select towns by county" tab
	 * @param {Object} id
	 */
	function expand(id) {
		var dv = $('#div_'+id);
		var tv = $('#expand_'+id);

		if (dv.css('display') == 'block') {
			dv.css('display', 'none');
			tv.html('show towns');
		} else {
			dv.css('display', 'block');
			tv.html('hide towns');
		}
	}

	//******************************************************//
	
	var checkbox_array = Array();
	/**
	 * called by the "select all" link on the county divs
	 * checks or unchecks the box on the related other tab
	 * @param {Object} id
	 */
	function select(id) {

		var dv = $('#div_'+id);
		var tv = $('#select_'+id);
		var inp = dv.find('input:checkbox');
		
		if (checkbox_array[id] == 'checked') {

			inp.attr('checked', '');
			checkbox_array[id] = 'unchecked';
			tv.html('select all');
			search_list_pop((inp).fieldValue(false));
			$.each(inp, function(i) {
				var uid = inp[i].id.substr(1);
				$('#n'+uid).attr('checked', '');
			});
			
		} else {

			inp.attr('checked', 'true');
			checkbox_array[id] = 'checked';
			tv.html('deselect all');
			search_list_push((inp).fieldValue());
			$.each(inp, function(i) {
				var uid = inp[i].id.substr(1);
				$('#n'+uid).attr('checked', 'true');
			});
			
		}

	}
	
	//******************************************************//
	/**
	 * called byt each checkbox when clicked - sets it's sibling to the same state
	 * on the 'other' tab
	 * @param {Object} box
	 */
	function ck(box) {
		var prefix = (box.id.substr(0, 1)=='n') ? 'c' : 'n';
		var uid = box.id.substr(1);
		$('#'+prefix+uid).attr('checked', box.checked);
		if (box.checked == true) {
			search_list_push(box.value);
		} else {
			search_list_pop(box.value);
		}
	}

	//******************************************************//
	/**
	 * formats number string to have comma every three digits
	 * @param {Object} nStr
	 */
	function addCommas(nStr){
		nStr += '';
		x1 = nStr;
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1;
	}

	//******************************************************//
	/**
	 * adds a town to the search_list array to be searched in the listings
	 * this array is only for displaying the towns to the visitor - the
	 * actual search uses the checkboxes, so a POST will work
	 * @param {Object} val
	 */
	function search_list_push(val) {
		
		if (typeof(val) == 'object') {
			$.each(val, function(k) {
				var exists = $.grep(search_list, function(i) {return i == val[k]});
				if (exists.length == 0) search_list.push(val[k]);
			});
		} else {
			var exists = $.grep(search_list, function(i) {return i == val});
			if (exists.length == 0) search_list.push(val);
		}

		update_screen();
	}
	
	//******************************************************//
	/**
	 * receives the value selected from the autocomplete box
	 * @param {Object} li
	 */
	function receive_autocomplete(li) {

		if ( li == null ) {
			return alert("No match!");
		} else {
			var sValue = li.selectValue;
			add_from_autocomplete(sValue);
		}
	}
	
	//******************************************************//
	/**
	 * adds the autocomplete value to the search_list array function
	 * @param {Object} sValue
	 */
	function add_from_autocomplete(sValue) {

		var j = ar_t.indexOf(sValue, 0);
		$('#c'+j).attr('checked', 'true');
		$('#n'+j).attr('checked', 'true');
		
		search_list_push(sValue);
		
	}
	//******************************************************//
	/**
	 * deletes a town from the search_list array
	 * @param {Object} val
	 */
	function search_list_pop(val) {
		
		if (typeof(val) == 'object') {
			$.each(val, function(k) {
				search_list = $.grep(search_list, function(i) {return i != val[k]});
			});
		} else {
			search_list = $.grep(search_list, function(i) {return i != val});
		}
		update_screen();
	}
		
	//******************************************************//
	/**
	 * refreshes the search_list array
	 */
	function update_screen() {
		var str = '<span id="town_alert">please select one or more towns below</span>';
		if (search_list.length > 0) {
			str = search_list.join(', ');
		}
		$('#town_search_list').html(str);
	}
	
	//******************************************************//
	/**
	 * gives us a method to find the index of a value in an array
	 * used by many functions here to find if a town is in an array
	 * @param {Object} v
	 * @param {Object} b
	 * @param {Object} s
	 */
	Array.prototype.indexOf = function( v, b, s ) {
		for( var i = +b || 0, l = this.length; i < l; i++ ) {
			if( this[i]===v || s && this[i]==v ) { return i; }
		}
		return -1;
	};

	
	//******************************************************//
	function brokername(name) {
		
		$('#brokername').html(name);
	
	}
	
	/**
	 * used by autocomplete
	 * @param {Object} li
	 */
	function selectItem(li) {
		receive_autocomplete(li);
	}
	
	function formatItem(row) {
		return row[0] + " (id: " + row[1] + ")";
	}

	// *****************************************
	// called by the search local page on Document Load
	function search_ready() {

//		var slider = new $.ui.slider($('div#slider'),	{
//			handle:'.indicator',
//			minValue: 0,
//			maxValue: 400,
//			slide: function(e, ui) {
//				var x = ui.value;
//				val = (x < 200) ? x/2 : (x < 300) ? 100+((x-200)*5) : 600+((x-300)*10);
//				document.getElementById((ui.handle.id == 'drag_min') ? 'price_min' : 'price_max').value = '$' + addCommas(val*10000);
//			}
//		});
	}
	
	//*******************************************
	// called by the search maine page on Document Load
	function search_maine_ready() {

		$('#search_tabs').tabs(1);
		
		$("#town_lookup").autocompleteArray(ar_t,
			{
				delay:10,
				minChars:1,
				matchSubset:1,
				onItemSelect:selectItem,
				onFindValue:receive_autocomplete,
				autoFill:true,
				maxItemsToShow:15
			}
		);

			var slider = new $.ui.slider($('div#slider'),	{
			handle:'.indicator',
			minValue: 0,
			maxValue: 400,
			slide: function(e, ui) {
				var x = ui.value;
				val = (x < 200) ? x/2 : (x < 300) ? 100+((x-200)*5) : 600+((x-300)*10);
				document.getElementById((ui.handle.id == 'drag_min') ? 'price_min' : 'price_max').value = '$' + addCommas((val*10000));
			}
		});
	}
		
	function lookupLocal(){
		var oSuggest = $("#autoTown")[0].autocompleter;
		oSuggest.findValue();
		return false;
	}

	function check_key() {
		if (fcs == true) return false;
	}
	
	function setF() {fcs = true;}
	function unsetF() {fcs = false;}
	
	//*******************************************
	// called by view/listing.php


		function show_email_listing() {
			if ($('#em_div').css('display') == 'none') {
				$('#em_div').fadeIn();
			} else {
				$('#em_div').hide();
			}
			return false;
		}
		
		var email_being_sent = false;
		
		function email_listing() {
		
		if (email_being_sent == true) return false;
			
			email_being_sent = true;
			
			var mls_id = $('#mls_id').val();
			var sName = $('#sender_name').val();
			var sEmail = $('#sender_email').val();
			var rEmail =  $('#recipient_email').val();
			var eMessage = $('#email_message').val();
			
			$('#em_result').html('<img src="/images/loaders/email-progress.gif" />');
			
			$.ajax({
				type: 'POST',
				url: '/listing/email',
				data: 'sName=' + sName + '&mls_id=' +  mls_id + '&sEmail=' +  sEmail + '&rEmail=' + rEmail + '&eMessage=' +  eMessage, 
				success: function(data) {
					$('#em_result').html(data);
					email_being_sent = false;
				},
				error: function(obj, str, ex){
	     			alert('Error: ' + ex);
	     			email_being_sent = false;
	  			}
	  		});
			return false;
		
		}
		
	//*******************************************
	
	// used on request page to make sure no more than one broker is selected
		jQuery.radioCheckboxGroup = function(name, filter) {

			filter = filter || "*";
			var expression = "input[@type=checkbox]";
			if(name) {
				expression += "[@name=" + name + "]"
			}
			var x = jQuery(expression).filter(filter);
			x.click(
				function() {
					// uncheck every other box with the same name
					x.not(this).each(
						function() {
							this.checked = false;
						}
					).end();
				}
			);
		};
		
