// JavaScript Document

	function toggle_more(){
		var mb_el = document.getElementById("more_button");
		var cb_el = document.getElementById("checkbox_atts");
		if (mb_el.value == "More..."){
			cb_el.style.display = "block";
			cb_el.style.width = "360px";
			mb_el.value = "Less...";
		} else if (mb_el.value == "Less..."){
			cb_el.style.display = "none";
			cb_el.style.width = "0px";
			mb_el.value = "More...";
		}
	}

	function toggle_hp_more(){
		var mb_el = document.getElementById("more_button");
		var cb_el = document.getElementById("checkbox_atts");
		if (mb_el.value == "More..."){
			cb_el.style.display = "block";
			cb_el.style.width = "240px";
			mb_el.value = "Less...";
		} else if (mb_el.value == "Less..."){
			cb_el.style.display = "none";
			cb_el.style.width = "0px";
			mb_el.value = "More...";
		}
	}

	function setup_querystring(){
		var fm = document.search_form;
		update_watch_list_element();
		update_exclude_list_element();
		var qs = "";
		qs += (fm.County.options[fm.County.selectedIndex].value.length) ? "&County="+fm.County.options[fm.County.selectedIndex].value : "";
		qs += (fm.City.options[fm.City.selectedIndex].value.length) ? "&City="+fm.City.options[fm.City.selectedIndex].value : "";
		qs += (fm.Neighborhood.options[fm.Neighborhood.selectedIndex].value.length) ? "&Neighborhood="+fm.Neighborhood.options[fm.Neighborhood.selectedIndex].value : "";
		qs += (fm.Property_Type.options[fm.Property_Type.selectedIndex].value !== "Any") ? "&Property_Type="+fm.Property_Type.options[fm.Property_Type.selectedIndex].value : "";
		qs += (fm.Sold.checked) ? "&Sold=1" : "";
		qs += (fm.AO.checked) ? "&AO=1" : "";
		qs += ((fm.Watched) && (fm.Watched.checked)) ? "&Watched=1" : "";
		qs += ((fm.Excluded) && (fm.Excluded.checked)) ? "&Excluded=1" : "";
		qs += (fm.Low_Price.value) ? "&Low_Price="+fm.Low_Price.value : "";
		qs += (fm.High_Price.value) ? "&High_Price="+fm.High_Price.value : "";
		qs += (fm.Num_Bedrooms.value) ? "&Num_Bedrooms="+fm.Num_Bedrooms.value : "";
		qs += (fm.Num_Bathrooms.value) ? "&Num_Bathrooms="+fm.Num_Bathrooms.value : "";
		qs += (fm.SqFt.value) ? "&SqFt="+fm.SqFt.value : "";
		qs += (fm.acreage.checked) ? "&acreage="+fm.acreage.value : "";
		qs += (fm.price_reduced.checked) ? "&price_reduced="+fm.price_reduced.value : "";
		qs += (fm.assumable_mortgage.checked) ? "&assumable_mortgage="+fm.assumable_mortgage.value : "";
		qs += (fm.new_construction.checked) ? "&new_construction="+fm.new_construction.value : "";
		qs += (fm.waterfront_property.checked) ? "&waterfront_property="+fm.waterfront_property.value : "";
		qs += (fm.vacation_property.checked) ? "&vacation_property="+fm.vacation_property.value : "";
		qs += (fm.income_property.checked) ? "&income_property="+fm.income_property.value : "";
		qs += (fm.owner_financing.checked) ? "&owner_financing="+fm.owner_financing.value : "";
		qs += ((fm.Watched) && (fm.Watched.checked) && (fm.ID_watch_list.value.length>3)) ? "&ID_watch_list="+fm.ID_watch_list.value : "";
		qs += ((fm.Excluded) && (fm.Excluded.checked) && (fm.ID_exclude_list.value.length>3)) ? "&ID_exclude_list="+fm.ID_exclude_list.value : "";
		return qs;
	}

	function setup_feed_querystring(){
		var fm = document.search_form;
		update_watch_list_element();
		update_exclude_list_element();
		var qs = "";
		qs += (fm.County.options[fm.County.selectedIndex].value.length) ? "&County="+fm.County.options[fm.County.selectedIndex].value : "";
		qs += (fm.City.options[fm.City.selectedIndex].value.length) ? "&City="+fm.City.options[fm.City.selectedIndex].value : "";
		qs += (fm.Neighborhood.options[fm.Neighborhood.selectedIndex].value.length) ? "&Neighborhood="+fm.Neighborhood.options[fm.Neighborhood.selectedIndex].value : "";
		qs += (fm.Property_Type.options[fm.Property_Type.selectedIndex].value !== "Any") ? "&Property_Type="+fm.Property_Type.options[fm.Property_Type.selectedIndex].value : "";
		qs += (fm.Sold.checked) ? "&Sold=1" : "";
		qs += (fm.AO.checked) ? "&AO=1" : "";
		qs += ((fm.Watched) && (fm.Watched.checked)) ? "&Watched=1" : "";
		qs += ((fm.Excluded) && (fm.Excluded.checked)) ? "&Excluded=1" : "";
		qs += (parseInt(fm.Low_Price.value)) ? "&Low_Price="+fm.Low_Price.value : "";
		qs += (parseInt(fm.High_Price.value)!=1000000000) ? "&High_Price="+fm.High_Price.value : "";
		qs += (parseInt(fm.Num_Bedrooms.value)) ? "&Num_Bedrooms="+fm.Num_Bedrooms.value : "";
		qs += (parseInt(fm.Num_Bathrooms.value)) ? "&Num_Bathrooms="+fm.Num_Bathrooms.value : "";
		qs += (parseInt(fm.SqFt.value)) ? "&SqFt="+fm.SqFt.value : "";
		qs += (fm.acreage.checked) ? "&acreage="+fm.acreage.value : "";
		qs += (fm.price_reduced.checked) ? "&price_reduced="+fm.price_reduced.value : "";
		qs += (fm.assumable_mortgage.checked) ? "&assumable_mortgage="+fm.assumable_mortgage.value : "";
		qs += (fm.new_construction.checked) ? "&new_construction="+fm.new_construction.value : "";
		qs += (fm.waterfront_property.checked) ? "&waterfront_property="+fm.waterfront_property.value : "";
		qs += (fm.vacation_property.checked) ? "&vacation_property="+fm.vacation_property.value : "";
		qs += (fm.income_property.checked) ? "&income_property="+fm.income_property.value : "";
		qs += (fm.owner_financing.checked) ? "&owner_financing="+fm.owner_financing.value : "";
		qs += (fm.show_pics.checked) ? "&show_pics=1" : "";
		qs += ((fm.Watched) && (fm.Watched.checked) && (fm.ID_watch_list.value.length>3)) ? "&ID_watch_list="+fm.ID_watch_list.value : "";
		qs += ((fm.Excluded) && (fm.Excluded.checked) && (fm.ID_exclude_list.value.length>3)) ? "&ID_exclude_list="+fm.ID_exclude_list.value : "";
		return qs.substr(1);
	}

	function count_properties(){
		var xmlhttp = create_xmlhttp();
		document.getElementById("working").innerHTML = "<img src=\""+host+"images/waiting-16.gif\" width=\"16\" height=\"16\" />";
		serverPage = ""+host+"processors/search_count.asp?ms="+new Date().getTime()+setup_querystring();
		if (debugging) {
			document.getElementById("debugging").innerHTML = serverPage+"<br />"+document.getElementById("debugging").innerHTML;
		}
		xmlhttp.open("GET", serverPage, true); // asynch param set to false for synch requests, true for asynch

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var response = xmlhttp.responseText;
				num_props = parseInt(response);
			//	document.getElementById("num_props").innerHTML = prefix+num_props;
				if (document.getElementById("num_props")) {
					document.getElementById("num_props").innerHTML = "Props: "+num_props;
				} else if (document.getElementById("num_props_hp")) {
					document.getElementById("num_props_hp").innerHTML = num_props;
				}
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/pix.gif\" width=\"16\" height=\"16\" />";
				if (window.update_feed_url){
					update_feed_url();
				}
			} else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/error-16x16.gif\" width=\"16\" height=\"16\" />";
			}
		}
		
		xmlhttp.send(null);
	}
	
	function update_neighborhood_menu_by_county(){
		var fm = document.search_form;
		var xmlhttp = create_xmlhttp();
		document.getElementById("working").innerHTML = "<img src=\""+host+"images/waiting-16.gif\" width=\"16\" height=\"16\" />";
		serverPage = ""+host+"processors/get-neighborhoods-menu.asp?ms="+new Date().getTime()+"&county="+fm.County.options[fm.County.selectedIndex].value;
		if (debugging) {
			document.getElementById("debugging").innerHTML = serverPage+"<br />"+document.getElementById("debugging").innerHTML;
		}
		xmlhttp.open("GET", serverPage, true); // asynch param set to false for synch requests, true for asynch

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var response = xmlhttp.responseText;
				document.getElementById("neighborhood_menu").innerHTML = response;
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/pix.gif\" width=\"16\" height=\"16\" />";
				count_properties();
			} else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/error-16x16.gif\" width=\"16\" height=\"16\" />";
			}
		}
		
		xmlhttp.send(null);
	}
	
	function do_county_menu_change(){
		var fm = document.search_form;
		var xmlhttp = create_xmlhttp();
		document.getElementById("working").innerHTML = "<img src=\""+host+"images/waiting-16.gif\" width=\"16\" height=\"16\" />";
		serverPage = ""+host+"processors/get-cities-menu.asp?ms="+new Date().getTime()+"&county="+fm.County.options[fm.County.selectedIndex].value;
		if (debugging) {
			document.getElementById("debugging").innerHTML = serverPage+"<br />"+document.getElementById("debugging").innerHTML;
		}
		xmlhttp.open("GET", serverPage, true); // asynch param set to false for synch requests, true for asynch

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var response = xmlhttp.responseText;
				document.getElementById("city_menu").innerHTML = response;
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/pix.gif\" width=\"16\" height=\"16\" />";
				update_neighborhood_menu_by_county();
			//	count_properties(); called by update_neighborhood_menu_by_county()
			} else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/error-16x16.gif\" width=\"16\" height=\"16\" />";
			}
		}
		
		xmlhttp.send(null);
	}

	
	function do_city_menu_change(){
		var fm = document.search_form;
		var xmlhttp = create_xmlhttp();
		document.getElementById("working").innerHTML = "<img src=\""+host+"images/waiting-16.gif\" width=\"16\" height=\"16\" />";
		serverPage = ""+host+"processors/get-neighborhoods-menu.asp?ms="+new Date().getTime()+"&city="+fm.City.options[fm.City.selectedIndex].value;
		if (debugging) {
			document.getElementById("debugging").innerHTML = serverPage+"<br />"+document.getElementById("debugging").innerHTML;
		}
		xmlhttp.open("GET", serverPage, true); // asynch param set to false for synch requests, true for asynch

		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var response = xmlhttp.responseText;
				document.getElementById("neighborhood_menu").innerHTML = response;
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/pix.gif\" width=\"16\" height=\"16\" />";
				count_properties();
			} else if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
				document.getElementById("working").innerHTML = "<img src=\""+host+"images/error-16x16.gif\" width=\"16\" height=\"16\" />";
			}
		}
		
		xmlhttp.send(null);
	}

	function do_neighborhood_menu_change(){
		count_properties();
	}
	
	function reset_form(){
		var fm = document.search_form;
		fm.County.selectedIndex = 0;
		do_county_menu_change();
/*		fm.City.selectedIndex = 0;
		fm.Neighborhood.selectedIndex = 0;*/
		fm.Property_Type.selectedIndex = 0;
		fm.Low_Price.selectedIndex = 0;
		fm.High_Price.selectedIndex = 0;
		fm.Num_Bedrooms.selectedIndex = 0;
		fm.Num_Bathrooms.selectedIndex = 0;
		fm.SqFt.selectedIndex = 0;
		fm.Sold.checked = false;
		fm.AO.checked = false;
		if (fm.Watched) {
			fm.Watched.checked = false;
		}
		if (fm.Excluded) {
			fm.Excluded.checked = false;
		}
		fm.acreage.checked = false;
		fm.price_reduced.checked = false;
		fm.assumable_mortgage.checked = false;
		fm.new_construction.checked = false;
		fm.waterfront_property.checked = false;
		fm.vacation_property.checked = false;
		fm.income_property.checked = false;
		fm.owner_financing.checked = false;
		DeleteCookie("County");
		DeleteCookie("City");
		DeleteCookie("Neighborhood");
		DeleteCookie("Property_Type");
		DeleteCookie("Low_Price");
		DeleteCookie("High_Price");
		DeleteCookie("Num_Bedrooms");
		DeleteCookie("Num_Bathrooms");
		DeleteCookie("SqFt");
		DeleteCookie("Sold");
		DeleteCookie("AO");
		DeleteCookie("Watched");
		DeleteCookie("Excluded");
		DeleteCookie("acreage");
		DeleteCookie("price_reduced");
		DeleteCookie("assumable_mortgage");
		DeleteCookie("new_construction");
		DeleteCookie("waterfront_property");
		DeleteCookie("vacation_property");
		DeleteCookie("income_property");
		DeleteCookie("owner_financing");
		DeleteCookie("pp");
		count_properties();
	}

	function get_ID_list(cookie_name) {
		var st_ID_cookie = "";
		st_ID_cookie = GetCookie(cookie_name);	// get cookie

		if (st_ID_cookie != "") {					// cookie exists
			if (st_ID_cookie.indexOf(",-,") >= 0) {	// cookie is empty
				st_ID_cookie = ""
				//alert("Cookie is empty.");
			} else {									// cookie is not empty
				// Trim off leading comma
				if ((st_ID_cookie.length > 1) && (st_ID_cookie.charAt(0) == ","))
					st_ID_cookie = st_ID_cookie.substr(1, st_ID_cookie.length - 1);
				
				// Trim off trailing comma
				if ((st_ID_cookie.length > 1) && (st_ID_cookie.charAt(st_ID_cookie.length - 1) == ","))
					st_ID_cookie = st_ID_cookie.substr(0, st_ID_cookie.length - 1);
			}
		}
		return st_ID_cookie;
	}
	
	function update_watch_list_element(){
		if (document.search_form.Watched) {
			document.search_form.ID_watch_list.value = (document.search_form.Watched.value) ? get_ID_list("WatchList") : "";
		}
	}
	
	function update_exclude_list_element(){
		if (document.search_form.Excluded) {
			document.search_form.ID_exclude_list.value = (document.search_form.Excluded.value) ? get_ID_list("ExcludeList") : "";
		}
	}
	
	function do_submit(){
		update_watch_list_element();
		update_exclude_list_element();
		document.search_form.submit();
	}

	function do_submit_hp(){
		document.search_form.action=host+"madison_real_estate.asp";
		document.search_form.submit();
	}

	function do_page_jump(mn){
		document.location.href = base_url+"&page="+mn.options[mn.selectedIndex].value;
	}