//<![CDATA[
// global variables
var SIST_MAX_LISTINGS_PER_MAP = 100;
var SIST_PRICE_MIN = '0';
var SIST_PRICE_MAX = '900000000';
var SIST_BEDS_MIN = '0';
var SIST_BEDS_MAX = '100';
var SIST_BATHS_MIN = '0';
var SIST_BATHS_MAX = '100';

var sidebar_html;
var gmarkers = [];
var htmls = [];
var htabs = [];
var i = 0;
var map;
var bounds;
var baseIcon;

var force_recenter_and_zoom = false;

var firstLoad = true;
var initialLoad = true;
var isOpenHouse = false;

Event.observe( window, 'load', function() {	
	addClientOnLoad();	
	addOnLoad();
} );

function addOnLoad(){
	$("mapOverlay").show();
	setUpOverlay();
	checkUser();
	
	//alert('whichRegion: ' + whichRegion);
	// Load the quick search form below the map for the selected reagion
	loadQuickSearchForm(whichRegion);
	
	// Load the map
	initiateMap();
}

function setUpOverlay() {

	if (readCookie('siMapFirstLoad')) {
		if (readCookie('siMapFirstLoad') == "false") {
			firstLoad = false;
		} else {
			firstLoad = true;
		}
	} else {
		firstLoad = true;
	}
	
	//firstLoad = true;
	//alert('first Load: ' + firstLoad);
}

function hideOverlay() {
	$('mapOverlay').hide();
}

//
// Loads the quick search form below the map for the specified region
//
function loadQuickSearchForm(whichRegion){
		// sets region to default region if length is not equal to 1
	if (whichRegion.length >= 3) { whichRegion = default_mls_region; }
		
	var search_form = $('qs_search_form');	// the div that holds the quick search form
	var whichPage = '/property-search/res/includes/search_forms/qs_form_' + whichRegion + '.asp';		// sets whichPage string with selected region
	var whichJS = '/property-search/res/includes/search_forms/qs_form_' + whichRegion + '.js';		// sets whichJS string with selected region
		
	// clears innerHTML
	//local_javascript.innerHTML = '';
	var params = Form.serialize($('frmSearch'));
	
	// creates post variable		
	var bindArgs = {
		asynchronous: false,
		method: 'post',
		parameters: params,
		onFailure: function(){
			search_form.innerHTML='<div style="font-size: 12px; font-weight: bold;">An error occurred and we could not load the search form.  Please refresh this page to try again.</div>';
		},
		onSuccess: function(t){
			//load the contents of the selected search form into the search container
			search_form.innerHTML = t.responseText;
			
			// adds the regional javascript file if it is not already loaded
			newJS = document.createElement('script');
				newJS.type='text/javascript';
				newJS.src=whichJS;
				document.getElementsByTagName('head')[0].appendChild(newJS); 
		}
	};	
	
	// loads search for for specified region from server
	new Ajax.Request(whichPage ,bindArgs);
		
	$(search_form).show();
}


// start code for Map Results area

function zoomout(){
map.zoomOut();map.zoomOut();map.zoomOut();
}
function zoomin(){
map.zoomIn();map.zoomIn();map.zoomIn();
}
//
// Creates Tabbed marker for the map
//
function createTabbedMarker(latlng,label,mhtmls,labels,listingid,fav,sitelisting,price,sqft,beds,baths,address) {
	
	var marker;
	var listingAddress = address;
	var icon = new GIcon(baseIcon);
	var ttip = label + '';
	var favTip, sitelistingTip, mainTip;
	
	if (fav == '1') {
		icon.image = favIconImage;
	} else if (sitelisting == '1') {
		icon.image = sitelistingIconImage;	
		icon.iconSize = new GSize(siteListingIconImageWidth, siteListingIconImageHeight);
	} else {
		icon.image = mainIconImage;
	}	
	
	if (ttip == 'null') { ttip = '' } else { ttip = capitalize(ttip) }
															  
	if (ttip != '') { 
		favTip = favTitle + ttip;
		favTip = capitalize(favTip);
		
		sitelistingTip = sitelistingTitle + ttip;
		sitelistingTip = capitalize(sitelistingTip);	
		
		mainTip = ttip;
		mainTip = capitalize(mainTip);
		
		if (fav == '1') {
			gmarkers[i] = new GMarker(latlng, { icon: icon, title: favTip});
		} else if (sitelisting == '1') {
			gmarkers[i] = new GMarker(latlng, { icon: icon, title: sitelistingTip});	
		} else {
			gmarkers[i] = new GMarker(latlng, { icon: icon, title: mainTip});
		}
		
		marker = gmarkers[i];
	
		GEvent.addListener(gmarkers[i], "click", function() {
		  // adjust the width so that the info window is large enough for this many tabs
		  if (mhtmls.length > 2) {
			mhtmls[0] = '<div style="width:'+mhtmls.length*88+'px">' + mhtmls[0] + '</div>';
		  }
		  //alert(i+': '+mhtmls[0]);
		  var tabs = [];
		  for (var j=0; j<mhtmls.length; j++) {
			tabs.push(new GInfoWindowTab(labels[j],mhtmls[j]));
		  }
		  //alert(tabs);
		  marker.openInfoWindowTabsHtml(tabs);
		});
		
		// setup hot tabs data
		if (mhtmls.length > 2) {
		   mhtmls[0] = '<div style="width:'+mhtmls.length*88+'px">' + mhtmls[0] + '</div>';
		}
		htabs[i] = new Array(2);
		for (var j=0; j<mhtmls.length; j++) {
			htabs[i][j]= new GInfoWindowTab(labels[j],mhtmls[j]);
		}			
	} else {
		if (fav == '1') {
			gmarkers[i] = new GMarker(latlng, { icon: icon });
		} else if (sitelisting == '1') {
			gmarkers[i] = new GMarker(latlng, { icon: icon });	
		} else {
			gmarkers[i] = new GMarker(latlng, { icon: icon });
		}	
		
		marker = gmarkers[i];
	
		GEvent.addListener(gmarkers[i], "click", function() {
		  //alert(i+': '+mhtmls[0]);
			var tabs = [];
			tabs.push(new GInfoWindowTab(labels[0],mhtmls[0]));

		  //alert(tabs);
		  marker.openInfoWindowTabsHtml(tabs);
		});
		
		htabs[i] = new Array(1);
		htabs[i][0]= new GInfoWindowTab(labels[0],mhtmls[0]);
	}
	
	//alert(htabs[i]);
	 
	//var rowcolor;
	//if (i % 2 == 1) { rowcolor = "1"; } else {rowcolor = "2";}

	//sidebar_html += '<tr bgcolor="' + rowcolor + '"><td align=left valign=center>' +(i+1)+ ' <a href="javascript:onMap(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout('+i+')">On Map</a> ' + label + '</td><td>' + price + '</td><td>' + sqft + '</td><td>' + beds + '</td><td>' + baths + '</td><td>' + address + '</td><td><a href="javascript: showDetailPage('+listingid+');" align=center>Details</a></td></tr>';
	//sidebar_html += '<tr><td class="mapsdata_'+rowcolor+'" valign="top"><a href="javascript: onMap('+i+');">On Map</a></td><td class="mapsdata_'+rowcolor+'" valign="top">' + price + '</td>					<td class="mapsdata_'+rowcolor+'" valign="top">' + sqft + '</td>	<td class="mapsdata_'+rowcolor+'" valign="top">' + beds + '</td><td class="mapsdata_'+rowcolor+'" valign="top">' + baths + '</td><td class="mapsdata_'+rowcolor+'" valign="top">' + address + '</td>	<td class="mapsdata_'+rowcolor+'" valign="top" align="center"><a href="javascript: showDetailPage('+listingid+');">View Details</a></td></tr>';

	i++;
	
	return marker;
}
//
// This function picks up the click and opens the corresponding info window
//
function onMap(i) {
	gmarkers[i].openInfoWindowTabsHtml(htabs[i]);
}
//
// These functions dynamically switch the marker when a listing is added to or removed from the Favorites
//    
function showFavIcon(i) {
	if (i == '') {i = 0}
	gmarkers[i].setImage(favIconImage);
}

function removeFavIcon(i) {
	//alert('i: ' + i);
	if (i == '') {i = 0}

	gmarkers[i].setImage(mainIconImage);	
}

function pausecomp(millis) {
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
} 

function clearMap() {
	//sidebar_html = '';
	//$("sidebar").innerHTML = sidebar_html;
	map.clearOverlays(); 
}

function initiateMap() {
	var zoom_level;
	
	// create the map
	sidebar_html;
	gmarkers = [];
	hmarkers = [];
	whichMarker = [];
	htmls = [];
	//htabs = [];
	i = 0;	

	map = new GMap2($("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.removeMapType(G_SATELLITE_MAP);
	map.addMapType(G_PHYSICAL_MAP);
	//map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
	
	//var svOverlay = new GStreetviewOverlay();
	//map.addOverlay(svOverlay);	
	
	//alert('Beginning initiate map function');	
	//alert('0. forcerecenterandzoom: ' + $('forcerecenterandzoom').value);
	
	//alert(document.frmSearch.forcerecenterandzoom.value);
	if ($F('forcerecenterandzoom') == 'true') {
		//alert('fcz');
		force_recenter_and_zoom = true;
		$('forcerecenterandzoom').value = '';
	} else {
		//alert('fczf');
		$('forcerecenterandzoom').value = '';
	}
	
	if ($('openhouses').value == 'true') {
		initialLoad == true;
		isOpenHouse = true;
	} else {
		if (readCookie('siMapFirstLoad') == 'true') {
			initialLoad == true
		} else {
			initialLoad == false
		}
	}
	
	//if ((initialLoad == true) && ($('mapsearchregion').value == '')) {
	//	//alert('3. starting from default zoom level');
	//	map.setCenter(new GLatLng(default_lat_pos, default_lon_pos), default_zoom_level);
	//	initial
	//} else 
	
	if (force_recenter_and_zoom == true) {
		//alert('1. starting from broadest zoom level');	
		map.setCenter(new GLatLng(default_lat_pos, default_lon_pos), default_zoom_level_broad);
	} else {				
		if (($('zoomlevel').value != '') && ($('nlat').value != '') && ($('elon').value != '')) {
			//alert('2. starting from form value zoom level: ' + $('zoomlevel').value);
			
			//alert($('nlat').value + ', ' + $('slat').value);
			//alert($('elon').value + ', ' + $('wlon').value);
			
			var lat_pos = ((parseFloat($('nlat').value) + parseFloat($('slat').value)) / 2)
			var lon_pos = ((parseFloat($('elon').value) + parseFloat($('wlon').value)) / 2)
			
			//alert('2.5 center lat: ' + lat_pos);
			//alert('2.5 center lon: ' + lon_pos);
			
			zoom_level = parseFloat($('zoomlevel').value);
			map.setCenter(new GLatLng(lat_pos, lon_pos), zoom_level);
		} else {
			//alert('3. starting from default zoom level');
			
			if ((saved_search_lat_pos != null) && (saved_search_lon_pos != null) && (saved_search_zoom_level != null)) {
				map.setCenter(new GLatLng(saved_search_lat_pos, saved_search_lon_pos), saved_search_zoom_level);
			} else {
				zoom_level = default_zoom_level;
				map.setCenter(new GLatLng(default_lat_pos, default_lon_pos), default_zoom_level);				
			}
		}
	}

	map.enableContinuousZoom(); 
	
	bounds = new GLatLngBounds();
	
	baseIcon = new GIcon();
    baseIcon.shadow = "";
    baseIcon.iconSize = new GSize(18, 19);
    baseIcon.shadowSize = new GSize(18, 19);
    baseIcon.iconAnchor = new GPoint(9, 10);
    baseIcon.infoWindowAnchor = new GPoint(9, 7);
	
	updateMap();
}

function updateMap(){
		var mapBoundsNE;
		var mapBoundsSW;
		
		
		// We need to clear the onZoomEnd & onDragEnd event listeners while the map is updating
		//	because leaving them in place throughout the process was causing it to zoom an extra time.
		//  These get restored at the end of this function.
		GEvent.clearInstanceListeners(map)	

		$('mapOverlay').hide();
		if ($('mapOverlay').hasClassName('zoom')) {$('mapOverlay').removeClassName('zoom');}
		if ($('mapOverlay').hasClassName('zoomOpenHouse')) {$('mapOverlay').removeClassName('zoomOpenHouse');}
		if ($('mapOverlayContent').hasClassName('zoomMessage')) {$('mapOverlayContent').removeClassName('zoomMessage');}
		if (!($('mapOverlay').hasClassName('loading'))) {$('mapOverlay').addClassName('loading')}
		if (!($('mapOverlay').hasClassName('loadingMessage'))) {$('mapOverlayContent').addClassName('loadingMessage')}			
		$('mapOverlay').show();
		$('mapOverlayContent').innerHTML='<div style="color: #000000;"><img src="/property-search/res/images/loading_big.gif" align="absmiddle">&nbsp;&nbsp;Searching for number of markers...</div>';
		
		mapBoundsNE = map.getBounds().getNorthEast();
		mapBoundsSW = map.getBounds().getSouthWest();
		//var pointCount = markerCountXML(mapBoundsNE,mapBoundsSW); 
		
		$('nlat').value = mapBoundsNE.lat()
		$('slat').value = mapBoundsSW.lat()
		$('wlon').value = mapBoundsSW.lng()
		$('elon').value = mapBoundsNE.lng()
		$('zoomlevel').value = map.getZoom();	
		
		//alert($('nlat').value + ', ' + $('slat').value);
		
		var formData = Form.serialize($("frmSearch"));
		
		var pointCount = 0;
		var maxLat = 0;
		var minLat = 0;
		var maxLon = 0;
		var minLon = 0;
				
		var bindArgs = {
			asynchronous: false,
			method: 'post',
			parameters: formData,
			onFailure: function(){
				alert('The map markers failed to load.');
			},
			onSuccess: function(t){
				//alert('success');

				var xmlDoc = t.responseXML.documentElement;
				var markers
				
				// Return the number of markers and the lat / lon bounds for the markers
				markers = xmlDoc.getElementsByTagName("count");
				//alert('1. Markers in Bounds (actual): '+parseFloat(markers[0].getAttribute("value")));
				pointCount = parseFloat(markers[0].getAttribute("value"));
				
				markers = xmlDoc.getElementsByTagName("maxlat");
				//alert('2. Max Lat in Bounds (actual): '+parseFloat(markers[0].getAttribute("value")));
				maxLat = parseFloat(markers[0].getAttribute("value"));	
				
				markers = xmlDoc.getElementsByTagName("minlat");
				//alert('3. Min Lat in Bounds (actual): '+parseFloat(markers[0].getAttribute("value")));
				minLat = parseFloat(markers[0].getAttribute("value"));	
				
				markers = xmlDoc.getElementsByTagName("maxlon");
				//alert('4. Max Lon in Bounds (actual): '+parseFloat(markers[0].getAttribute("value")));
				maxLon = parseFloat(markers[0].getAttribute("value"));	

				markers = xmlDoc.getElementsByTagName("minlon");
				//alert('5. Min Lon in Bounds (actual): '+parseFloat(markers[0].getAttribute("value")));
				minLon = parseFloat(markers[0].getAttribute("value"));	
				
				//alert('pointCount: ' + pointCount);
				
				// If more than max number, display message and zoom to lat / long bounds of results
				if (pointCount > SIST_MAX_LISTINGS_PER_MAP) {
					$('markerMessage').innerHTML = pointCount + ' listings visible.<br />Zoom in or narrow your search.';
					
					if (isOpenHouse == true) {
						//alert('first Load');
						
						if ($('mapOverlay').hasClassName('loading')) {$('mapOverlay').removeClassName('loading');}
						if ($('mapOverlayContent').hasClassName('loadingMessage')) {$('mapOverlayContent').removeClassName('loadingMessage');}
						if ($('mapOverlayContent').hasClassName('zoom')) {$('mapOverlayContent').removeClassName('zoom');}
						if (!($('mapOverlay').hasClassName('zoomOpenHouse'))) {$('mapOverlay').addClassName('zoomOpenHouse')}
						if (!($('mapOverlay').hasClassName('zoomMessage'))) {$('mapOverlayContent').addClassName('zoomMessage')}
						
						$('mapOverlayContent').innerHTML = '<div style="text-align: right; margin-bottom: 10px; color: #000;"><a href="javascript: hideOverlay();"><img src="/property-search/res/images/close_icon.png" border="0" alt="Close" /></a></div>'
						$('mapOverlayContent').innerHTML += 'There are ' + pointCount + ' listings in the visible area. A max of 100 can be displayed at one time.<br /><br />'
						$('mapOverlayContent').innerHTML += '<strong>Zoom in using the bar at left or just double-click any point on the map.</strong></div>'
						
						$('mapOverlay').show();
						
						//Now that the zoom message has appeared once, set cookie so that it won't appear again on reload.
						createCookie('siMapFirstLoad','false',1);
						firstLoad = false;
						
						initialLoad = false;
						isOpenHouse = false;
						
					} else if (initialLoad == true) {
						
						//alert('first Load');
						
						if ($('mapOverlay').hasClassName('loading')) {$('mapOverlay').removeClassName('loading');}
						if ($('mapOverlayContent').hasClassName('loadingMessage')) {$('mapOverlayContent').removeClassName('loadingMessage');}
						if ($('mapOverlayContent').hasClassName('zoomOpenHouse')) {$('mapOverlayContent').removeClassName('zoomOpenHouse');}
						if (!($('mapOverlay').hasClassName('zoom'))) {$('mapOverlay').addClassName('zoom')}
						if (!($('mapOverlay').hasClassName('zoomMessage'))) {$('mapOverlayContent').addClassName('zoomMessage')}
						
						$('mapOverlayContent').innerHTML = '<div style="text-align: right; margin-bottom: 10px;"><a href="javascript: hideOverlay();"><img src="/property-search/res/images/close_icon.png" border="0" alt="Close" /></a></div>'
						$('mapOverlayContent').innerHTML += 'There are ' + pointCount + ' listings in the visible area. A max of 100 can be displayed at one time.<br /><br />'
						$('mapOverlayContent').innerHTML += '<strong>Zoom in using the bar at left or just double-click any point on the map.</strong></div>'
						
						$('mapOverlay').show();
						
						//Now that the zoom message has appeared once, set cookie so that it won't appear again on reload.
						createCookie('siMapFirstLoad','false',1);
						firstLoad = false;
						
						initialLoad = false;
					} else {
						
						//alert('not initial load');
						
						if ($('mapOverlay').hasClassName('zoom')) {$('mapOverlay').removeClassName('zoom');}
						if ($('mapOverlay').hasClassName('zoomOpenHouse')) {$('mapOverlay').removeClassName('zoomOpenHouse');}
						if ($('mapOverlayContent').hasClassName('zoomMessage')) {$('mapOverlayContent').removeClassName('zoomMessage');}
						if (!($('mapOverlay').hasClassName('loading'))) {$('mapOverlay').addClassName('loading')}
						if (!($('mapOverlay').hasClassName('loadingMessage'))) {$('mapOverlayContent').addClassName('loadingMessage')}						
						
						$('mapOverlayContent').innerHTML = '<div style="font-weight: bold; text-align: center; padding: 0px 8px 2px 8px;"><div style="float: right; width: 16px; height: 16px;"><a href="javascript: hideOverlay();"><img src="/property-search/res/images/close_icon.png" border="0" alt="Close" /></a></div>'
						$('mapOverlayContent').innerHTML += pointCount + ' listings in visible area.<br />Double-click on the map to zoom in.</div>';

					}	
					
					//alert('4. force_recenter_and_zoom: ' + force_recenter_and_zoom);

					if (force_recenter_and_zoom == true) {
						
						//alert('5. forcing recenter and zoom');
	
						// ===== determine the centre from the bounds ======
						var clat = (maxLat + minLat) / 2;
						var clng = (maxLon + minLon) / 2;
						
						
						var point_bound_up = new GLatLng(maxLat + .005, maxLon + .005);
						var point_bound_down = new GLatLng(minLat - .005, minLon - .005);
						
						bounds = new GLatLngBounds();
						
						bounds.extend(point_bound_up);
						bounds.extend(point_bound_down);					
						
						map.setCenter(new GLatLng(clat,clng),map.getBoundsZoomLevel(bounds));
						
						mapBoundsNE = map.getBounds().getNorthEast();
						mapBoundsSW = map.getBounds().getSouthWest();
						
						$('nlat').value = mapBoundsNE.lat()
						$('slat').value = mapBoundsSW.lat()
						$('wlon').value = mapBoundsSW.lng()
						$('elon').value = mapBoundsNE.lng()
						$('zoomlevel').value = map.getZoom();							
						
						force_recenter_and_zoom = false;

					}
					
					clearMap();
					
				} else {
					
					if ($('mapOverlay').hasClassName('zoom')) {$('mapOverlay').removeClassName('zoom');}
					if ($('mapOverlayContent').hasClassName('zoomMessage')) {$('mapOverlayContent').removeClassName('zoomMessage');}
					if (!($('mapOverlay').hasClassName('loading'))) {$('mapOverlay').addClassName('loading')}
					if (!($('mapOverlay').hasClassName('loadingMessage'))) {$('mapOverlayContent').addClassName('loadingMessage')}	
					
					if ((pointCount==0) || (pointCount=='')) {
						
						$('markerMessage').innerHTML = 'No listings match at this location and zoom level.';
						$('mapOverlayContent').innerHTML = '<div style="color: #000000; font-weight: bold; text-align: center; padding: 2px 8px 2px 8px;">No listings in visible area.<br />Zoom out or change location.</div>';
						clearMap();
					} else {
						
						$('markerMessage').innerHTML='Viewing ' + pointCount + ' listings.<br />Click markers to<br />view listing info.';
						$('mapOverlayContent').innerHTML = '<div style="font-weight: bold; color: #000000; font-size: 12px !important;"><img src="/property-search/res/images/loading_big.gif" align="absmiddle">&nbsp;&nbsp;Loading ' + pointCount + ' listings at this zoom level.</div>';
						
						//document.location.href = '/property-search/sist_ajax/mls_mapmarkers.asp?' + formData;
						var mapURL = '/property-search/sist_ajax/mls_mapmarkers.asp';
						var mapData = formData;
						
						//alert('6. force_recenter_and_zoom: ' + force_recenter_and_zoom);
						
						if (force_recenter_and_zoom == true) {
							
							//alert('7. forcing recenter and zoom');
							
							var clat = (maxLat + minLat) / 2;
							var clng = (maxLon + minLon) / 2;
							
							var point_bound_up = new GLatLng(maxLat + .005, maxLon + .005);
							var point_bound_down = new GLatLng(minLat - .005, minLon - .005);
							
							var bounds = new GLatLngBounds();
							
							bounds.extend(point_bound_up);
							bounds.extend(point_bound_down);					
							
							map.setCenter(new GLatLng(clat,clng),map.getBoundsZoomLevel(bounds));					
							
							//var bounds2 = map.getBounds();
							//var southWest = bounds2.getSouthWest();
							//var northEast = bounds2.getNorthEast();
							
							mapBoundsNE = map.getBounds().getNorthEast();
							mapBoundsSW = map.getBounds().getSouthWest();
							
							$('nlat').value = mapBoundsNE.lat()
							$('slat').value = mapBoundsSW.lat()
							$('wlon').value = mapBoundsSW.lng()
							$('elon').value = mapBoundsNE.lng()
							$('zoomlevel').value = map.getZoom();									
	
							//$('nlat').value = northEast.lat()
							//$('slat').value = southWest.lat()
							//$('wlon').value =southWest.lng()
							//$('elon').value = northEast.lng()
							//$('zoomlevel').value = map.getZoom();									
							
							//alert($('nlat').value + ', ' + $('slat').value);
														
							force_recenter_and_zoom = false;
						}
						
						readMap(mapURL,mapData);						
							
					}
				}
			}
		};	
		
		// get the number of results
		new Ajax.Request('/property-search/sist_ajax/mls_mapmarkerscount.asp' ,bindArgs);


		// Restore the event listeners that we removed at the beginning of this function
		GEvent.addListener(map, "dragend", function(){
			updateMap();
		});
		GEvent.addListener(map, "zoomend", function(){
			updateMap();
		});	
		
}

// A function to read the data
function readMap(url, postData) {
	
	var request = GXmlHttp.create();
	request.open("POST", url, true);
	request.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
	
			// obtain the array of markers and loop through it
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	
			// hide the info window, otherwise it still stays open where the removed marker used to be
			map.getInfoWindow().hide();
			
			// clear all existing markers
			map.clearOverlays();
	
			// empty the array
			gmarkers = [];
			gmarkers.length = 0;
			i = 0;
	
			// reset bounds
			bounds = new GLatLngBounds();
			for (var xx = 0; xx < markers.length; xx++) {
				htmls[xx] = new Array(2);
				htabs[xx] = new Array(2);
				// obtain the attribues of each marker
				var lat = parseFloat(markers[xx].getAttribute("lat"));
				var lng = parseFloat(markers[xx].getAttribute("lng"));
				var point = new GLatLng(lat,lng);
				var point_bound_up = new GLatLng(lat+.005, lng+.005);
				var point_bound_down = new GLatLng(lat-.005, lng-.005)
				htmls[xx][0] = markers[xx].getAttribute("html1");
				htmls[xx][1] = markers[xx].getAttribute("html2");
				var label = markers[xx].getAttribute("tooltip");
				var listingid = markers[xx].getAttribute("listingid");
				var fav = markers[xx].getAttribute("fav");
				var sitelisting = markers[xx].getAttribute("sitelisting");
				
				//create marker info
				var marker = createTabbedMarker(point, label, htmls[xx],["Details","Description"],listingid,fav,sitelisting,markers[xx].getAttribute("price"),markers[xx].getAttribute("sqft"),markers[xx].getAttribute("beds"),markers[xx].getAttribute("baths"),markers[xx].getAttribute("address"));
				
				// add marker
				map.addOverlay(marker);
				
				// extend bounds to include new point
				bounds.extend(point_bound_up);
				bounds.extend(point_bound_down);
			}
			
			$('mapOverlay').hide();
		}
	}
	request.send(postData);
}

function mapsvalidateLPrice()
{
		
	var lPrice = $('qslprice');
	var uPrice = $('qsuprice');
	
	var minPrice = parseInt(lPrice.options[lPrice.selectedIndex].value);
	var maxPrice = parseInt(uPrice.options[uPrice.selectedIndex].value);
	
	if(isNaN(maxPrice))
	{
		maxPrice = 0;
	}
	var newSelected = false;

	var newMax = lPrice.options[lPrice.selectedIndex + 1].value;
	if(maxPrice > newMax)
	{
		newMax = maxPrice;
	}
	
	for (var i=1;i<lPrice.options.length; i++)
	{
	
		if(parseInt(lPrice.options[i].value) < minPrice)
		{
			//alert("Set it grey");			
			//lPrice.options[i].style.color='#999999';
		}
		else if(parseInt(lPrice.options[i].value) <= newMax)
		{
			lPrice.options[i].style.color='#000000';
		}
		else
		{
			break;
		}
	}

	for (var i=uPrice.options.length - 1;i>=0; i--) {
		if(parseInt(uPrice.options[i].value) < minPrice)
		{
			//uPrice.options[i].disabled = true;					
			//alert(i);
			//alert(uPrice.options[i].style.color);
			//uPrice.options[i].style.color = '#CCCCCC';
			//alert(uPrice.options[i].style.color);
			if(!newSelected && maxPrice < minPrice)
			{
				if( (i + 2) < uPrice.options.length)
				{					
					if(uPrice.selectedIndex != 0)
					{
						uPrice.options[i + 2].selected = true;
					}
					newSelected = true;
				}
			}
			//uPrice.remove(i);
		}
		else
		{
			uPrice.options[i].style.color='#000000';
			//uPrice.options[i].disabled = false;
		}
	}
}

function mapsvalidateUPrice()
{	

	var lPrice = $('qslprice');
	var uPrice = $('qsuprice');
	
	var minPrice = parseInt(lPrice.options[lPrice.selectedIndex].value);
	var maxPrice = parseInt(uPrice.options[uPrice.selectedIndex].value);
	
	if(isNaN(minPrice))
	{
		minPrice = 0;
	}
	uPrice.options[uPrice.selectedIndex].style.color = '#000000';
	//var newSelected = false;
	for (var i=lPrice.options.length - 1;i>=0; i--) {
		if(parseInt(lPrice.options[i].value) >= maxPrice)
		{
			//lPrice.options[i].style.color='#CCCCCC';
			//lPrice.options[i].disabled = true;
			//lPrice.options[i].style.display = "none"
			//lPrice.remove(i);
			if( minPrice > maxPrice)
			{
				lPrice.options[i - 1].selected = true;	
				lPrice.options[i - 1].style.color = '#000000';
			}
		}	
		else if(parseInt(lPrice.options[i].value) >= minPrice)
		{
			lPrice.options[i].style.color='#000000';
			//lPrice.options[i].disabled = false;
		}
	}
}

function capitalize(thisString) {
var thisArray = thisString.split(' ');
var newString = '';
for (var n=0; n<thisArray.length; n++) {
  var firstChar = thisArray[n].charAt(0).toUpperCase();
  var theRest = thisArray[n].substring(1, thisArray[n].length);
  newString += firstChar+theRest+' ';
  }
return newString.substring(0, newString.length - 1);
}
//]]>

