var ds = 'ms_search_bunbury';
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 getMapRequest(env)
{

	var axl = 'INTERNAL_PLOT=true&SITE=' + site + '&minx=' + minx + '&miny=' + miny + '&maxx=' + maxx + '&maxy=' + maxy + '&width=' + mwidth + '&height=' + mheight + '&cache=' + cache + '&scale=' + scale + '&zoom=' + fixedzoom + '&CACHE_ORIGIN_X=' + cache_origin_x + '&CACHE_ORIGIN_Y=' + cache_origin_y + '&US_TILES=true';

	for (var i = 0; i < req_vars.length; i++) {
		axl += '&' + req_vars[i] + '=' + req_vals[i];
	}

	
	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");
        resolution = xmlDoc.getElementsByTagName("TILES").item(0).getAttribute("resolution");

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

        positionSlider();

        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"));

        clearImageMap();
	
        placeTiles(tiles);

        fixedzoom = "";

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

	processPropertyRecs(props);
	processOfficeRecs(offices);

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

   okToSend = true;

  }
 
}

function processOfficeRecs(offices)
{

        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 photo_string = "";

                if (offices[i].getAttribute('O_DFLT_PHOTO_URL').length > 0) {
                        photo_string = '<img src="' + root_url + cleanString(offices[i].getAttribute('O_DFLT_PHOTO_URL')) + '" width="150" height="117" border="0"></a>';
                }

                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 += photo_string;
                balloon += '<div class="address">' + cleanString(offices[i].getAttribute('O_NM_OFFICE')) + '</div>';
                balloon += cleanString(offices[i].getAttribute('O_ADDRESS_01')) + '<br />';
                balloon += '</div>';
                balloon += '</div>';
                balloon += '</div>';

                if (offices[i].getAttribute("O_WWWSITE").length > 0) {

                        myDiv.innerHTML = balloon;

                        mapContainer.appendChild(myDiv);

                        var area = document.createElement('DIV');
                        area.id = i;
                        area.innerHTML = '<img src="images/Restaino_Logo.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)
{

        var props_list = "";

        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;
                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 = "LISTING_" + 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 photo_string = "";
                if (props[i].getAttribute('LP_PHOTO_URL').length > 0) {
                        photo_string = '<img src="http://' + cleanString(props[i].getAttribute('LP_PHOTO_URL')) + props[i].getAttribute('L_MLS_NUMBER') + 'at.jpg" width="150" height="117" border="0">';
                }

                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 += photo_string;
		if (props[i].getAttribute('LA_FG_SHOWONWEB') != 'A') {
	                balloon += '<div class="address">' + cleanString(props[i].getAttribute('L_LISTINGADDR1')) + '</div>';
		}

                var price = props[i].getAttribute('LS_AMT_SALE_PRICE') != null && props[i].getAttribute('L_LISTINGSTATUS') == 'Sold' ? props[i].getAttribute('LS_AMT_SALE_PRICE') : props[i].getAttribute('L_LISTING_PRICE');
                balloon += '$' + cleanString(formatPrice(price)) + '<br />';
                balloon += props[i].getAttribute('LS_NO_BEDROOMS') + ' Beds ' + props[i].getAttribute('LS_NO_BATHROOMS') + ' Baths<br>';
                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/ResidentialListing.gif" 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);

        }

	if (props.length > 0) {
		buildListingRouteTable(props);
	}
}


