var ds = 'ms_search_restaino';
var max_results = "100";
var open_div = "";

function getMap(envelope) {

	if (okToSend) {
  	http = getHTTPObject();

  	if ((http != null) ) {
	okToSend = false;
    	var axl = getMapRequest(envelope);
   	 
    	http.open("POST", url, true);
    	http.onreadystatechange = printResponse;
    	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	showLayer('loading');
    	http.send(axl);
	}
	} else {
		alert ("Waiting for previous response");
	}
}

function createQueryString()
{
	var retVal = "minx=" + minx + "&miny=" + miny + "&maxx=" + maxx + "&maxy=" + maxy;

	var myvars = new Array('lp_low', 'lp_high', 'beds_low', 'baths_low', 'p_type', 'sq_ft_min', 'sq_ft_max', 'house_age_min', 'house_age_max');

	for (var i = 0; i < myvars.length; i++) {
		retVal = retVal + "&" + myvars[i] + "=" + getElmById(myvars[i]).value;
	}

        retVal += "&zoom=true&scale=" + scale;

	
	return escape(retVal);
}

function buildPoiWhere() {
        var w = "";
        var cats = "";

        var inputs = document.getElementsByTagName('input');

        for(var i=0; i<inputs.length; i++){
                var type = inputs[i].type;
                var id = inputs[i].id;
                var name = inputs[i].name;
                if (type == 'checkbox' && id.indexOf('POI_') == 0) {
                        if (inputs[i].checked) {
                                if (cats.length == 0) {
                                        cats += "'" + name + "'";
                                } else {
                                        cats += ",'" + name + "'";
                                }
                        }
                }
        }

        if (cats.length > 0) {
                w = " AND tp_category IN (" + cats + ")";
        }

        return w;
}


function getMapRequest(env)
{
	var w_poi = buildPoiWhere();

	var lp_low = document.getElementById('lp_low').value;
	var lp_high = document.getElementById('lp_high').value;
	var beds_low = document.getElementById('beds_low').value;
	var baths_low = document.getElementById('baths_low').value;
	var p_type = document.getElementById('p_type').value;
	var sq_ft_min = document.getElementById('sq_ft_min').value;
	var sq_ft_max = document.getElementById('sq_ft_max').value;
	var house_age_min = document.getElementById('house_age_min').value;
	var house_age_max = document.getElementById('house_age_max').value;

	var w = ' AND lp_Listing_Price >= ' + lp_low + ' AND lp_Listing_Price <= ' + lp_high + ' AND lp_No_Bedrooms >= ' + beds_low + '  AND lp_No_Bathrooms >= ' + baths_low;

	if (sq_ft_min.length > 0) {
		w += " AND lp_amt_Struct_SqFt_Act >= " + sq_ft_min;
	}
	if (sq_ft_max.length > 0) {
		w += " AND lp_amt_Struct_SqFt_Act <= " + sq_ft_max;
	}
	if (p_type.length > 0) {
		w += " AND lp_tp_Improvement = '" + escape(p_type) + "'";
	}
	if (house_age_min.length > 0) {
		w += " AND lp_House_Age >= " + house_age_min;
	}
	if (house_age_max.length > 0) {
		w += " AND lp_House_Age <= " + house_age_max;
	}

	w += " ORDER BY " + order_by + " " + up_down;

	var fl = "TO_DAYS(NOW()) - TO_DAYS(lp_Listing_Date) AS DOM,cd_MLS,lp_MLS_Number,FORMAT(lp_Listing_Price, 0) as lp_Listing_Price_Display,FLOOR(lp_Listing_Price) AS lp_Listing_Price,lp_ListingCity,lp_ListingAddr1,lp_No_Bedrooms,lp_No_Bathrooms,lp_res_Lat,lp_res_Long,lp_Photo_URL,lp_PhotoDesc_1,lp_ind_OpenHouse,lp_ds_OpenHouseTime,cd_Company,lp_fg_ShowOnWeb";
	
        var axl = 'SITE=' + site + '&INC_OFFICES=' + (getElmById('INC_OFFICES').checked ? "true" : "") + '&minx=' + minx + '&maxy=' + maxy + '&maxx=' + maxx + '&miny=' + miny + '&cache=' + cache + '&width=' + width + '&height=' + height + '&INC_LISTINGS=y&DS=' + ds + '&MAX_RESULTS=' + max_results + '&FL=' + fl + "&scale=" + scale + '&zoom=' + fixedzoom + "&CACHE_ORIGIN_X=" + cache_origin_x + "&CACHE_ORIGIN_Y=" + cache_origin_y + "&SRCH_WHERE=" + w + "&INC_POI=" + (w_poi.length > 0 ? "true" : "") + "&POI_WHERE=" + w_poi + '&US_TILES=true';


	return axl;
}


function printResponse(){

if (http.readyState == 4) {
    if (http.status == 200) {

      isWorking = false;
      
      	var result = http.responseText;

	var xmlDoc;
	
      	if(document.implementation && document.implementation.createDocument) { 
      		// MOZILLA 
       	 	xmlDoc = document.implementation.createDocument("", "", null);
      	 	xmlDoc.async="false";
         	xmlDoc.loadXML(result);
      	} else if (window.ActiveXObject){
      	 	//IE
      	  	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  	xmlDoc.async="false";
	  	xmlDoc.loadXML(result);
       	}
	
      var tiles = xmlDoc.getElementsByTagName("TILES")[0].getElementsByTagName("TILE");
        scale = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("scale");

        resolution = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("resolution");

        var env = xmlDoc.getElementsByTagName("ENVELOPE").item(0);

        minx = parseFloat(env.getAttribute("minx"));
        miny = parseFloat(env.getAttribute("miny"));
        maxx = parseFloat(env.getAttribute("maxx"));
        maxy = parseFloat(env.getAttribute("maxy"));

	write_debug(minx + '/' + miny + '/' + maxx + '/' + maxy + '/' + scale);

        clearImageMap();

	positionSlider();

        placeTiles(tiles);

        fixedzoom = "";

        document.getElementById('propcount').innerHTML = '<span class="red_txt">Property Count: ' + xmlDoc.getElementsByTagName("PROPERTIES").item(0).getAttribute("count") + "</span>";

        var props = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("PROPERTY");
        var offices = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("OFFICE");

	var pois = xmlDoc.getElementsByTagName("RESPONSE")[0].getElementsByTagName("POI");

        processPropertyRecs(props, xmlDoc.getElementsByTagName("PROPERTIES").item(0).getAttribute("count"));

        processOfficeRecs(offices);

	if (props.length > 0) {
                document.getElementById('poi_div').style.visibility = 'visible';
                processPoiRecs(pois);
        } else {
                document.getElementById('poi_div').style.visibility = 'hidden';
                document.getElementById('POI_SCHOOL').checked = false;
        }

	hideLayer('loading');
    }else alert("Error retreiving data");
 
   okToSend = true;
  } 
}

function processPoiRecs(props)
{

        for (var i = 0; i < props.length; i++) {
                var x = parseInt(props[i].getAttribute('x'));
                var y = parseInt(props[i].getAttribute('y'));

                var x1 = x;
                var y1 = y;

                var icon_type = "";
                if (x > (width / 2)) {
                        if (y > (height / 2)) {
                                icon_type = "BR";
                                x = x - icon_w;
                                y = y - icon_h;
                        } else {
                                icon_type = "TR";
                                x = x - icon_w + 6;
                                y = y + 4;
                        }
                } else {
                        if (y > (height / 2)) {
                                icon_type = "BL";
                                y = y - icon_h;
                                x = x + 5;
                        } else {
                                icon_type = "TL";
                                x = x + 5;
                                y = y + 3;
                        }
                }


                var cd_key = cleanString(props[i].getAttribute('cd_key'));

                var myDiv = document.createElement('DIV');
                myDiv.id = "POI_" + cd_key;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';
                myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
		myDiv.style.width = icon_w + 'px';
		myDiv.style.height = icon_h + 'px';
                myDiv.style.zIndex=2;

                var tmp_balloon = '<div class="balloon_' + icon_type + '">';
                tmp_balloon += '<div class="inner_balloon_poi">';

                tmp_balloon += '<div class="close_icon"><a href="javascript:hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div><br>';
                if (cleanString(props[i].getAttribute('ds_image')).indexOf('<img') > -1) {
                        tmp_balloon += cleanString(props[i].getAttribute('ds_image'));
                } else if (cleanString(props[i].getAttribute('ds_image')).length > 0) {
                        tmp_balloon += '<img src="http://mapping.redata.com/plots/poi/' + site + '/poi/'  + cleanString(props[i].getAttribute('ds_image')) + '" with="150" height="117"><br>';
                }

                if (cleanString(props[i].getAttribute('ds_display')).length > 0) {
                        tmp_balloon += '<table><tr><td align="left">' + cleanString(props[i].getAttribute('ds_display')) + '</td></tr></table>';
                } else {
                        tmp_balloon += cleanString(props[i].getAttribute('ds_text1')) + '<br>';
                        tmp_balloon += cleanString(props[i].getAttribute('ds_text2')) + '<br>';
                        tmp_balloon += cleanString(props[i].getAttribute('ds_text3')) + '<br>';
                }

                tmp_balloon += '</div>';

                tmp_balloon += '</div>';

                myDiv.innerHTML = tmp_balloon;

                mapContainer.appendChild(myDiv);

                var area = document.createElement('DIV');
                area.id = cd_key;

 		area.innerHTML = '<img src="http://mapping.redata.com/plots/poi/' + site + '/icon/' + cleanString(props[i].getAttribute('ds_icon')) + '" border="0">';

                area.style.left=x1 + 'px';
                area.style.top=y1 + 'px';
                area.style.position='absolute';
                area.style.zIndex=1;
		area.onmouseover = function() {
                        hideAll();
                        document.getElementById('POI_' + this.id).style.visibility = 'visible';
                        open_div = document.getElementById('POI_' + this.id);
                        document.onmousemove = checkPosition;
                }

                mapContainer.appendChild(area);

        }
}

function processOfficeRecs(offices)
{

        var queryString = createQueryString();

	for (var i = 0; i < offices.length; i++) 
	{
		var x = parseInt(offices[i].getAttribute('x'));
		var y = parseInt(offices[i].getAttribute('y'));
  	
		var x1 = x;
                var y1 = y;

		var icon_type = "";

                if (x > (width / 2)) {
                	if (y > (height / 2)) {
				icon_type = "BR";
				x = x - icon_w;
				y = y - icon_h; 
			} else {
				icon_type = "TR";
				x = x - icon_w + 6;
				y = y + 4;
			}				
		} else {
                	if (y > (height / 2)) {
				icon_type = "BL";
				y = y - icon_h;
				x = x + 5;
			} else {
				icon_type = "TL";
				x = x + 5;
				y = y + 3;
			}
                }

                var myDiv = document.createElement('DIV');
                myDiv.id = "OFFICE_" + i;
                myDiv.style.position='absolute';
                myDiv.style.visibility='hidden';
                myDiv.style.left =x + 'px';
                myDiv.style.top = y + 'px';
                myDiv.style.width = icon_w + 'px';
                myDiv.style.height= icon_h + 'px';
                myDiv.style.zIndex=2;

		var balloon = '<div class="balloon_' + icon_type + '">';
		balloon += '<div class="inner_balloon">';
		balloon += '<div class="close_icon"><a href="javascript:hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div>';
		balloon += '<div class="address">' + cleanString(offices[i].getAttribute('O_NM_OFFICE')) + '</div>';
		balloon += cleanString(offices[i].getAttribute('O_ADDRESS_01')) + '<br />';
		balloon += '<a href="' + root_url + '/vp/AgentServlet?cd_Company=RESTAINO&cd_Office=' + cleanString(offices[i].getAttribute('O_CD_OFFICE')) + '&fg_Active=Y&SITE=RESTAINO&Search=True&ScreenID=AgentsSummary_Public&ScreenID_Cur=AGENT_LINK" target="_parent">More Details</a>';
		balloon += '</div>';
		balloon += '</div>';
		balloon += '</div>';

			myDiv.innerHTML = balloon;

                	mapContainer.appendChild(myDiv);
                        
	                var area = document.createElement('DIV');
        	        area.id = i;
                	area.innerHTML = '<img src="images/office_icon.gif" width="16" height="16" border="0">';
	                area.style.left=x1 + 'px';
        	        area.style.top=y1 + 'px';
                	area.style.position='absolute';
	                area.style.zIndex=1;
			area.onmouseover = function() {
                        	hideAll();
                        	document.getElementById('OFFICE_' + this.id).style.visibility = 'visible';
                        	open_div = document.getElementById('OFFICE_' + this.id);
                        	document.onmousemove = checkPosition;
                	}
                
                mapContainer.appendChild(area); 
		
                
        }       
}
	
function processPropertyRecs(props, numProps)
{

	var props_list = "";

	var queryString = createQueryString();

	for (var i = 0; i < props.length; i++) {
		var x = parseInt(props[i].getAttribute('x'));
		var y = parseInt(props[i].getAttribute('y'));
	
		if (props_list.length == 0) {
			props_list = props[i].getAttribute("cd_MLS");
		} else {
			props_list = props_list +  "," + props[i].getAttribute("cd_MLS");
		}
	
		var x1 = x - 8;
		var y1 = y - 8;

                var icon_type = "";

                if (x > (width / 2)) {
                        if (y > (height / 2)) {
                                icon_type = "BR";
                                x = x - icon_w;
                                y = y - icon_h;
                        } else {
                                icon_type = "TR";
                                x = x - icon_w + 6;
                                y = y + 4;
                        }
                } else {
                        if (y > (height / 2)) {
                                icon_type = "BL";
                                y = y - icon_h;
                                x = x + 5;
                        } else {
                                icon_type = "TL";
                                x = x + 5;
                                y = y + 3;
                        }
                }

		var myDiv = document.createElement('DIV');
		myDiv.id = "LISTING_" + i;
		myDiv.style.position='absolute';
		myDiv.style.visibility='hidden';
		if (s_layer == props[i].getAttribute('cd_MLS')) {
			myDiv.style.visibility = 'visible';
			s_layer = '';
		}

		myDiv.style.left =x + 'px';
		myDiv.style.top = y + 'px';
		myDiv.style.width = icon_w + 'px';
		myDiv.style.height= icon_h + 'px';
		myDiv.style.zIndex=2;

		var photo_string = "";

		if (props[i].getAttribute('lp_Photo_URL').length > 0) {
			if (pres_agent.length > 0) {
	    			photo_string = '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=AGENT_LISTING_DETAIL_P&pres_agent=' + pres_agent + '&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') +'" target="_parent"><img src="http://' + cleanString(props[i].getAttribute('lp_Photo_URL')) + props[i].getAttribute('lp_MLS_Number') + 'at.jpg" width="150" height="117" border="0"></a>';
			} else {
    				photo_string = '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') +'" target="_parent"><img src="http://' + cleanString(props[i].getAttribute('lp_Photo_URL')) + props[i].getAttribute('lp_MLS_Number') + 'at.jpg" width="150" height="117" border="0"></a>';
			}
		}	

                var balloon = '<div class="balloon_' + icon_type + '">';
                balloon += '<div class="inner_balloon">';
		if (props[i].getAttribute('lp_ind_OpenHouse') == 'Y') {
			if (pres_agent.length > 0) {
				balloon += '<div style="font-size:9px;font-face:Arial"><a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=AGENT_LISTING_DETAIL_P&pres_agent=' + pres_agent + '&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS')  + '" target="_parent">Open:' + cleanString(props[i].getAttribute('lp_ds_OpenHouseDay')) + ' ' + cleanString(props[i].getAttribute('lp_ds_OpenHouseTime')) + '</a></div>';
			} else {
				balloon += '<div style="font-size:9px;font-face:Arial"><a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS')  + '" target="_parent">Open:' + cleanString(props[i].getAttribute('lp_ds_OpenHouseDay')) + ' ' + cleanString(props[i].getAttribute('lp_ds_OpenHouseTime')) + '</a></div>';
			}
		}	

        	balloon += '<div class="close_icon"><a href="javascript:hideAll();"><img border="0" src="images/close_icon.gif" style="width:17px;"></a></div>';
 
		balloon += photo_string;
		if (props[i].getAttribute('lp_fg_ShowOnWeb') != 'A') {
	                balloon += '<div class="address">' + cleanString(props[i].getAttribute('lp_ListingAddr1')) + '</div>';
		}

                balloon += '<font color="black">$' + cleanString(props[i].getAttribute('lp_Listing_Price_Display')) + '<br />';
		balloon += props[i].getAttribute('lp_No_Bedrooms') + ' Beds ' + props[i].getAttribute('lp_No_Bathrooms') + ' Baths</font><br />';
		if (pres_agent.length > 0) {
			balloon += '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=AGENT_LISTING_DETAIL_P&pres_agent=' + pres_agent + '&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') + '" target="_parent">More Details</a>';
		} else {
			balloon += '<a href="' + root_url + '/vp/ListingServlet?cd_MLS=' + props[i].getAttribute('cd_MLS') + '&SITE=' + site + '&ScreenID=LISTING_DETAIL_P&mapvars=' + queryString + '%26s_layer%3D' + props[i].getAttribute('cd_MLS') + '" target="_parent">More Details</a>';

		}

                balloon += '</div>';
                balloon += '</div>';
                balloon += '</div>';

                myDiv.innerHTML = balloon;

		mapContainer.appendChild(myDiv);
	
		var area = document.createElement('DIV');
		area.id = i;

		if (cleanString(props[i].getAttribute('lp_ind_OpenHouse')) == 'Y')  {
			area.innerHTML = '<img src="images/map_icon_oh.png" border="0">';
		} else if (cleanString(props[i].getAttribute('cd_Company')) == 'RESTAINO') {
			area.innerHTML = '<img src="images/map_icon_restaino.png" border="0">';
		} else if (parseInt(cleanString(props[i].getAttribute('DOM'))) < 14) {
			area.innerHTML = '<img src="images/map_icon_new.png" border="0">';
		} else {
			area.innerHTML = '<img src="images/map_icon_active.png" border="0">';
		}
		area.style.left=x1 + 'px';
		area.style.top=y1 + 'px';
		area.style.position='absolute';
		area.style.zIndex=1;
		area.onmouseover = function() {
                        hideAll();
                        document.getElementById('LISTING_' + this.id).style.visibility = 'visible';
                        open_div = document.getElementById('LISTING_' + this.id);
                        document.onmousemove = checkPosition;
                }

		mapContainer.appendChild(area);
	
	}

	var pca = document.getElementById('propcount_alert');

	if (numProps > parseInt(max_results)) {
		pca.innerHTML = 'Too many to display:</span> Refine your search to fewer than ' + max_results + ' by zooming in closer, or by adjusting the menus at left.';
	} else if (numProps == 0) {
		pca.innerHTML = 'No properties found: Refine your search to more than 0 by zooming out further, or by adjusting the menus above.';
	} else {
		pca.innerHTML = 'Move your mouse over any house icon for more information. Move your mouse away from the property to close the propery information.';
	}
			
}


