/* -*- mode: java -*- */
/*****************************************
 * Copyright (c) 2008, Emmett M. Pate, Jr.
 * emmett@epate.com
 *
 */

var map;
var center, zoom;
var gpano, panopoint;

function getHTTPObject()
{
 var xmlhttp;
 /*@cc_on
 @if (@_jscript_version >= 5)
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  } @else xmlhttp = false;
   @end @*/
   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
   try {
    xmlhttp = new XMLHttpRequest();
   } catch (e) {
   xmlhttp = false;
  }
 }
 return xmlhttp;
}
// We create the HTTP Object
var mapobj = getHTTPObject();
var sumobj = getHTTPObject();
var detobj = getHTTPObject();
var cmpobj = getHTTPObject();

function Search(offset, noresults)
{
    urchinTracker('/reinftp/search');

    // clear the stats, summary, and details divs
    statsDiv = document.getElementById('stats');
    summaryDiv = document.getElementById('summary');
    detailsDiv = document.getElementById('details');

    statsDiv.style.textAlign = "center";
    statsDiv.innerHTML  = "<br><div style=\"position:relative;top:16\">";
    statsDiv.innerHTML += "<img src=/reinftp/graphics/pb_gap.gif></div>";

    detailsDiv.innerHTML = "";

    summaryDiv.style.textAlign = "center";
    summaryDiv.innerHTML  = "<br><div style=\"position:absolute;top:16\">";
    summaryDiv.innerHTML += "<img src=/reinftp/graphics/pb_gap.gif></div>";

    if (typeof(map) == 'undefined')
	{
	    map = new GMap2(document.getElementById("map"));
	    map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
	    map.addControl(new GOverviewMapControl());
	    map.enableScrollWheelZoom();
	    GEvent.addListener(map, "infowindowopen", function() {
		    hasStreetview();
		});
	    /*
	    GEvent.addListener(map, "infowindowclose", function() {
		    if (document.getElementById("panoflash1")) {
			document.getElementsByTagName("body").item(0).removeChild(document.getElementById('panoflash1')); 
			}
		});
	    */

	    CreateResetControl();
	    map.setCenter(new GLatLng(36.003, -75.645), 8);
	}
    // clear existing overlays
    map.clearOverlays();

    // Null Icon
    var NullIcon = new GIcon();
    NullIcon.image = "/graphics/nullicon.png";
    NullIcon.shadow = "/graphics/nullicon.png";
    NullIcon.iconSize = new GSize(2, 2);
    NullIcon.shadowSize = new GSize(2, 2);
    NullIcon.iconAnchor = new GPoint(1, 1);
    NullIcon.infoWindowAnchor = new GPoint(1, 1);
    var marker = new PdMarker(map.getCenter(), NullIcon);
    marker.setTooltip("Searching...");
    marker.setOpacity(100);
    map.addOverlay(marker);
    marker.showTooltip();

    var str = "";
    for (i=0; i<document.ps.length; i++)
	{
	    str = str + document.ps.elements[i].name + "=";
	    if (document.ps.elements[i].type == "checkbox")
		{
		    str += (document.ps.elements[i].checked ? 1 : 0) + "&";
		}
	    else
		{
		    str += escape(document.ps.elements[i].value) + "&";
		}
	    
	}
    str += "pg=" + offset;
    ShowSummary(str, noresults);
    if (document.ps.mls.value) { ShowDetailsTab(); }
}

function handleNoFlash(errorCode)
{
    if (errorCode == 603)
	{
	    panoDiv = document.getElementById('pano');
	    panoDiv.style.textAlign = "center";
	    panoDiv.innerHTML  = "Error: Flash doesn't appear to be supported by your browser";
	}
}

function ShowSummary(str, noresults)
{
    sumobj.open("GET", "cgi-bin/ShowSummary.cgi?"+str, true);
    sumobj.onreadystatechange = function() {
	if (sumobj.readyState == 4) {
	    var xml = GXml.parse(sumobj.responseText);

	    summaryDiv = document.getElementById('summary');
	    summaryDiv.innerHTML = xml.documentElement.getElementsByTagName("results")[0].firstChild.nodeValue;

	    statsDiv = document.getElementById('stats');
	    statsDiv.innerHTML = xml.documentElement.getElementsByTagName("statistics")[0].firstChild.nodeValue;

	    ShowMap(xml);
	    listings = xml.documentElement.getElementsByTagName("listings");
	    // parse agent=XXX out of str
	    var agent = '';
	    var a = str.match(/.*agent=([a-z]+)&/);
	    if (a) { agent = a[1]; }
	    ShowDetails(listings[0].getAttribute("first"), agent, false);
	    if (listings[0].getAttribute("first") && ! noresults) { ShowSummaryTab(); }

	    // Sold Data
	    // Placing this condition here causes it to display after everything else is done.
	    if (document.ps.sold.checked)
		{
		    urchinTracker('/reinftp/soldsearch');

		    var NullIcon = new GIcon();
		    NullIcon.image = "/graphics/nullicon.png";
		    NullIcon.shadow = "/graphics/nullicon.png";
		    NullIcon.iconSize = new GSize(2, 2);
		    NullIcon.shadowSize = new GSize(2, 2);
		    NullIcon.iconAnchor = new GPoint(1, 1);
		    NullIcon.infoWindowAnchor = new GPoint(1, 1);
		    var marker = new PdMarker(map.getCenter(), NullIcon);
		    marker.setTooltip("Searching Recently Solds...");
		    marker.setOpacity(100);
		    map.addOverlay(marker);
		    marker.showTooltip();

		    mapobj.open("GET", "cgi-bin/ShowSolds.cgi?"+str, true);
		    mapobj.onreadystatechange = function() {
			if (mapobj.readyState == 4) {
			    var sold = GXml.parse(mapobj.responseText);
			    ShowSoldsMap(sold, marker);
			}
		    }
		    mapobj.send(null);
		}
	}
    }
    sumobj.send(null);

}

function ShowDetails(mls, agent, showtab)
{
    detailsDiv = document.getElementById('details');
    detailsDiv.style.textAlign = "center";
    detailsDiv.innerHTML  = "<br><div style=\"position:relative;top:16\">";
    detailsDiv.innerHTML += "<img src=/reinftp/graphics/pb_gap.gif></div>";

    detobj.open("GET", "cgi-bin/ShowDetails.cgi?mls="+mls+"&agent="+agent, true);
    detobj.onreadystatechange = function() {
	if (detobj.readyState == 4) {
	    detailsDiv.style.textAlign = "left";
	    var xml = GXml.parse(detobj.responseText);
	    detailsDiv.innerHTML = xml.documentElement.getElementsByTagName("detailview")[0].firstChild.nodeValue;
	    BuildPhotoArray(xml);
	    ShowImage(0,
		      xml.documentElement.getElementsByTagName("detailview")[0].getAttribute("lat"),
		      xml.documentElement.getElementsByTagName("detailview")[0].getAttribute("lon"));
	}
    }
    detobj.send(null);
    if (window.addthis){
	window.addthis = null;
    }

    if (showtab) { ShowDetailsTab(); }
    ShowComparisons(mls, agent, false);
}

function ShowComparisons(mls, agent, showtab)
{
    compareDiv = document.getElementById('compare');
    compareDiv.style.textAlign = "center";
    compareDiv.innerHTML  = "<br><div style=\"position:relative;top:16\">";
    compareDiv.innerHTML += "<img src=/reinftp/graphics/pb_gap.gif></div>";

    cmpobj.open("GET", "cgi-bin/ShowComparisons.cgi?mls="+mls+"&agent="+agent, true);
    cmpobj.onreadystatechange = function() {
	if (cmpobj.readyState == 4) {
	    compareDiv.style.textAlign = "left";
	    compareDiv.innerHTML = cmpobj.responseText;
	}
    }
    cmpobj.send(null);
    if (showtab) { ShowCompareTab(); }
}

/* Streetview Functions */
function hasStreetview()
{
    mapDiv = document.getElementById('map');
    detailsDiv = document.getElementById('details');

    if (mapDiv.style.height != detailsDiv.style.height)
	{
	    if (gpano) {
		gpano.remove();
		if (document.getElementById("pano")) { document.getElementById("pano").parentNode.removeChild(document.getElementById("pano")); }
	    }
	    mapDiv.style.height = detailsDiv.style.height;
	    map.checkResize();
	}

    panoClient = new GStreetviewClient();
    panoClient.getNearestPanorama(panopoint, onResponse);
}

function onResponse(panoData)
{
    svDiv = document.getElementById('hasSV');
    if (panoData.code == 200)
	{
	    svDiv.style.textAlign = "center";
	    svDiv.innerHTML = "<a href=\"JavaScript:StreetView();\">Google&nbsp;Streetview</a>";
	    panopoint = panoData.location.latlng;
	}
    else
	{
	    svDiv.innerHTML = "";
	}
}

function StreetView()
{
    mapDiv = document.getElementById('map');
    detailsDiv = document.getElementById('details');

    /*
    if (document.getElementById("panoflash1") == null)
	{
	    var panoflash1 = document.createElement('div');
	    panoflash1.id = "panoflash1";
	    panoflash1.style.display = "none";
	    document.getElementsByTagName("body").item(0).appendChild(panoflash1);
	    document.getElementById("panoflash1").SetReturnValue = function(){};
	}
    */
    if (mapDiv.style.height == detailsDiv.style.height)
	{
	    mapDiv.style.height = (parseInt(mapDiv.style.height) / 2);
	    map.checkResize();
	}

    var panoContent = document.createElement('div');

    panoContent.style.position = "absolute";
    panoContent.style.width = mapDiv.style.width;
    panoContent.style.left = mapDiv.style.left;
    panoContent.style.height = parseInt(mapDiv.style.height) - 5;
    panoContent.style.top = parseInt(mapDiv.style.top) + parseInt(panoContent.style.height) + 10;
    panoContent.id = 'pano';
    document.getElementById("rightside").appendChild(panoContent);

    gpano = new GStreetviewPanorama(panoContent);
    gpano.setLocationAndPOV(panopoint, null);
    GEvent.addListener(gpano, "error", handleNoFlash);
}

function ShowMap(xml)
{
    function createMarker(point, icon, html, address, num)
	{
	    // var marker = new GMarker(point, icon);
	    var marker = new PdMarker(point, icon);
	    marker.setOpacity(100);
	    GEvent.addListener(marker, "mouseover", function() {
		    marker.setTooltip(address);
		});
	    GEvent.addListener(marker, "mouseout", function() {
		    marker.hideTooltip();
		});
	    GEvent.addListener(marker, "click", function() {
		    marker.openInfoWindowHtml(html);
		    panopoint = point;
		});
	    GEvent.addDomListener(document.getElementById("item-"+num), "mouseover", function() {
		    ShowMapTab();
		    marker.openInfoWindowHtml(html);
		    map.setCenter(point, 15);
		    panopoint = point;
		});
	    GEvent.addDomListener(document.getElementById("pic-"+num), "click", function() {
		    marker.openInfoWindowHtml(html);
		    map.setCenter(point, 15);
		    panopoint = point;
		});
	    return marker;
	}

    // use Googles "tiny" icon
    var BlueIcon = new GIcon();
    var RedIcon = new GIcon();
    BlueIcon.image = "/graphics/mm_20_blue.png";
    BlueIcon.shadow = "/graphics/mm_20_shadow.png";
    BlueIcon.iconSize = new GSize(12, 20);
    BlueIcon.shadowSize = new GSize(22, 20);
    BlueIcon.iconAnchor = new GPoint(6, 20);
    BlueIcon.infoWindowAnchor = new GPoint(5, 1);
    RedIcon.image = "/graphics/wew_20_red.png";
    RedIcon.shadow = "/graphics/wew_20_shadow.png";
    RedIcon.iconSize = new GSize(20, 20);
    RedIcon.shadowSize = new GSize(40, 20);
    RedIcon.iconAnchor = new GPoint(9, 20);
    RedIcon.infoWindowAnchor = new GPoint(5, 1);
    
    var listings = xml.documentElement.getElementsByTagName("listing");
    var bounds = new GLatLngBounds;
    for (var i=0; i<listings.length; i++)
    {
	var point = new GLatLng(parseFloat(listings[i].getElementsByTagName("marker")[0].getAttribute("lat")),
				parseFloat(listings[i].getElementsByTagName("marker")[0].getAttribute("lng")));

	map.addOverlay(createMarker(point,
				    listings[i].getAttribute("broker") == 470 ? RedIcon : BlueIcon,
				    listings[i].getElementsByTagName("overlay")[0].childNodes[0].nodeValue,
				    unescape(listings[i].getAttribute("address")),
				    i
			   ));
    }

    var marker = map.getFirstMarker();
    map.removeOverlay(marker); // this removes the "Searching..." overlay
    map.zoomToMarkers(5); // pdMarker extension
    center = map.getCenter(); // save these for "Reset" control
    zoom = map.getZoom();
}

function ShowSoldsMap(xml, mkr)
{
    function createMarker(point, icon, html, address, num)
	{
	    // var marker = new GMarker(point, icon);
	    var marker = new PdMarker(point, icon);
	    marker.setOpacity(100);
	    GEvent.addListener(marker, "mouseover", function() {
	    	    marker.setTooltip(address);
	    	});
	    	    GEvent.addListener(marker, "mouseout", function() {
	    		    marker.hideTooltip();
	    		});
	    	    GEvent.addListener(marker, "click", function() {
	    		    marker.openInfoWindowHtml(html);
	    		});
	    	    return marker;
	}

    // use Googles "tiny" icon
    var GreenIcon = new GIcon();
    GreenIcon.image = "/graphics/mm_07_green.png";
    GreenIcon.shadow = "/graphics/nullicon.png";
    GreenIcon.iconSize = new GSize(9, 9);
    GreenIcon.shadowSize = new GSize(9, 9);
    GreenIcon.iconAnchor = new GPoint(4, 4);
    GreenIcon.infoWindowAnchor = new GPoint(4, 4);
    
    var listings = xml.documentElement.getElementsByTagName("listing");
    var bounds = new GLatLngBounds;
    for (var i=0; i<listings.length; i++)
    {
	var point = new GLatLng(parseFloat(listings[i].getElementsByTagName("marker")[0].getAttribute("lat")),
				parseFloat(listings[i].getElementsByTagName("marker")[0].getAttribute("lng")));

	map.addOverlay(createMarker(point,
				    GreenIcon,
				    listings[i].getElementsByTagName("overlay")[0].childNodes[0].nodeValue,
				    unescape(listings[i].getAttribute("address")),
				    i
			   ));
    }
    map.removeOverlay(mkr); // this removes the "Searching..." overlay
}

function ShowMapOnDetail(lat, lon) {
    var Icon = new GIcon();
    Icon.image = "/graphics/mm_20_red.png";
    Icon.shadow = "/graphics/mm_20_shadow.png";
    Icon.iconSize = new GSize(12, 20);
    Icon.shadowSize = new GSize(22, 20);
    Icon.iconAnchor = new GPoint(6, 20);
    Icon.infoWindowAnchor = new GPoint(5, 1);

    var point = new GLatLng(lat, lon);
    var marker = new GMarker(point, Icon);
    // alert(photoDiv.style.backgroundColor);
    // dmap = new GMap2(document.getElementById("photo"), {backgroundColor:""});
    dmap = new GMap2(document.getElementById("photo"));
    dmap.addControl(new GSmallMapControl());
    dmap.addControl(new GMapTypeControl());
    dmap.setCenter(point, 15);
    dmap.addOverlay(marker);
}

function setMapView(lat, lng)
{
    map.setCenter(new GLatLng(lat,lng), 15);
}

function TextualResetControl() {
}

function CreateResetControl()
{
    TextualResetControl.prototype = new GControl();
    
    TextualResetControl.prototype.initialize = function(map)
	{
	    var container = document.createElement("div");
	    var resetDiv = document.createElement("div");
	    
	    this.setButtonStyle_(resetDiv);
	    container.appendChild(resetDiv);
	    resetDiv.appendChild(document.createTextNode("Reset"));
	    GEvent.addDomListener(resetDiv, "click", function() {
		    if (gpano) {
			gpano.remove();
			if (document.getElementById("pano")) { document.getElementById("pano").parentNode.removeChild(document.getElementById("pano")); }
		    }
		    mapDiv.style.height = detailsDiv.style.height;
		    map.checkResize();
		    map.setCenter(center, zoom);
		});
	    
	    map.getContainer().appendChild(container);
	    return container;
	}
    
    TextualResetControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 27));
    }

    TextualResetControl.prototype.setButtonStyle_ = function(button) {
	button.style.textDecoration = "none";
	button.style.color = "black";
	button.style.backgroundColor = "white";
	button.style.font = "12px Arial";
	button.style.border = "1px solid black";
	button.style.padding = "2px";
	button.style.marginBottom = "2px";
	button.style.textAlign = "center";
	button.style.width = "3em";
	button.style.cursor = "pointer";
    }
    map.addControl(new TextualResetControl());
}

var photos;
function ShowImage(n, lat, lon) {

    photoDiv = document.getElementById('photo');
    if (! photoDiv) { return; }
    if ((typeof(photos) == 'undefined') && (lat && lon)) { ShowMapOnDetail(lat, lon); return; }
    if (typeof(photos) == 'undefined') { return; }
    if ((photos.length == 0) && (lat && lon)) { ShowMapOnDetail(lat, lon); return; }

    photosDiv = document.getElementById('photos');
    if (photosDiv) { bgColor = document.getElementById('photos').style.backgroundColor; }
    else { bgColor = "white"; }
    photoDiv.style.backgroundColor = bgColor;
    photoDiv.style.textAlign = "center";
    photoDiv.innerHTML = photos[n];
}

function BuildPhotoArray(xml) {
    photos = [];
    var p = xml.documentElement.getElementsByTagName("photo");
    for (var i=0; i<p.length; i++)
    {
	photos.push(p[i].firstChild.nodeValue);
    }
}

var mySherlock;
function SaveSearch()
{
    var str = "";
    for (i=0; i<document.ps.length; i++)
	{
	    str = str + document.ps.elements[i].name + "=";
	    if (document.ps.elements[i].type == "checkbox")
		{
		    str += (document.ps.elements[i].checked ? 1 : 0) + "&";
		}
	    else
		{
		    str += escape(document.ps.elements[i].value) + "&";
		}
	    
	}
    str += "ls=REIN";
    if (mySherlock && !mySherlock.closed) {
	mySherlock.document.write();
	mySherlock.document.close();
    }
    else {
	mySherlock = window.open('cgi-bin/SaveSearch.cgi?'+str,'mySherlock','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=no,width=370,height=220');
    }
}

var mySherlock
function SaveProperty(MLS,LS) {
	if (mySherlock && !mySherlock.closed) {
		mySherlock.document.write();
		mySherlock.document.close();
	}
	else {
		mySherlock = window.open('/cgi-bin/saveproperty.cgi?mls='+MLS+'&ls='+LS,'mySherlock','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=no,width=370,height=220');
	}
}

var Feedback;
function RequestFeedback()
{
    Feedback = window.open('feedback.html','Feedback','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,copyhistory=no,scrollbars=no,width=350,height=220');
}

function initPage()
{
    setupForm(); /* setup the REIN form elements */
    Resize();    /* Resize the divisions */
    Search(0, 1) /* initial search results */
}

function Resize()
{
    headerDiv = document.getElementById('header');
    mapDiv = document.getElementById('map');
    formDiv = document.getElementById('form');
    summaryDiv = document.getElementById('summary');
    detailsDiv = document.getElementById('details');
    compareDiv = document.getElementById('compare');
    keyDiv = document.getElementById('keytab');
    reinDiv = document.getElementById('rein');
    footerDiv = document.getElementById('footer');

    // set the widths...
    if (document.body.clientWidth < 800)
	{
	    headerDiv.style.width = 785;
	    mapDiv.style.width = detailsDiv.style.width = compareDiv.style.width = 400;
	    keyDiv.style.width = reinDiv.style.width = footerDiv.style.width = 785;
	}
    else
	{
	    headerDiv.style.width = document.body.clientWidth - 10;
	    mapDiv.style.width = detailsDiv.style.width =
		compareDiv.style.width = parseInt(document.body.clientWidth) - 380 - 15;
	    keyDiv.style.width = reinDiv.style.width = footerDiv.style.width = document.body.clientWidth - 10;
	}

    // set the heights...
    tabHeight = parseInt(document.getElementById('tabblock').style.height);
    tabTop = parseInt(document.getElementById('tabblock').style.top);
    // large screen, use it all
    if (document.body.clientHeight > 720)
	{
	    formDiv.style.height = summaryDiv.style.height =
		mapDiv.style.height = detailsDiv.style.height =
		compareDiv.style.height = parseInt(document.body.clientHeight) - tabTop - tabHeight - 5;
	}
    // smaller screen, resize form and map to fill screen
    else if (document.body.clientHeight > 660)
	{
	    formDiv.style.height = summaryDiv.style.height =
		mapDiv.style.height = detailsDiv.style.height =
		compareDiv.style.height = parseInt(document.body.clientHeight) - tabHeight - 10;
	}	    
    else
	{
	    formDiv.style.height = summaryDiv.style.height = 
		mapDiv.style.height = detailsDiv.style.height = 
		compareDiv.style.height = 660 - tabHeight;
	}

    if (map) { map.checkResize(); }
    if (gpano) {
	gpano.remove();
	if (document.getElementById("pano")) { document.getElementById("pano").parentNode.removeChild(document.getElementById("pano")); }
    }

    // move the key, rein, and footer up to just beneath the form and map
    keyDiv = document.getElementById('key');
    keyDiv.style.top = tabTop + tabHeight + parseInt(formDiv.style.height) + 5;
    reinDiv.style.top = tabTop + tabHeight + parseInt(formDiv.style.height) + parseInt(keyDiv.style.height) + 5;
    footerDiv.style.top = tabTop + tabHeight + parseInt(formDiv.style.height) + parseInt(keyDiv.style.height) + parseInt(reinDiv.style.height)+ 5;
}

function ResizeDetails()
{
    photoDiv = document.getElementById('photos');
    featuresDiv = document.getElementById('features');
    afeaturesDiv = document.getElementById('afeatures');

    photoDiv.style.width = document.body.clientWidth - 300 - 20;
    featuresDiv.style.width = document.body.clientWidth - 10;
    afeaturesDiv.style.width = document.body.clientWidth - 10;
}

function ShowSearchTab() {
    formDiv = document.getElementById('form');
    summaryDiv = document.getElementById('summary');
    lefttabsDiv = document.getElementById('lefttabs');

    lefttabsDiv.innerHTML  = "<div class=\"tab-front\"><div class=\"tab-container\"><b class=\"lable-front\">SEARCH</b></div></div>";
    lefttabsDiv.innerHTML += "<div class=\"tab-back\" style=\"left:90px\" onclick=JavaScript:ShowSummaryTab()><div class=\"tab-container\"><b class=\"lable-back\">RESULTS</b></div></div>";

    formDiv.style.visibility = 'visible';
    summaryDiv.style.visibility = 'hidden';
}

function ShowSummaryTab() {
    formDiv = document.getElementById('form');
    summaryDiv = document.getElementById('summary');
    lefttabsDiv = document.getElementById('lefttabs');
    
    lefttabsDiv.innerHTML  = "<div class=\"tab-back\" onclick=\"JavaScript:ShowSearchTab();\"><div class=\"tab-container\"><b class=\"lable-back\">SEARCH</b></div></div>";
    lefttabsDiv.innerHTML += "<div class=\"tab-front\" style=\"left:90px\"><div class=\"tab-container\"><b class=\"lable-front\">RESULTS</b></div></div>";

    formDiv.style.visibility = 'hidden';
    summaryDiv.style.visibility = 'visible';
}

function ShowMapTab() {

    mapDiv = document.getElementById('map');
    detailsDiv = document.getElementById('details');
    compareDiv = document.getElementById('compare');
    righttabsDiv = document.getElementById('righttabs');

    righttabsDiv.innerHTML  = "<div class=\"tab-front\"><div class=\"tab-container\"><b class=\"lable-front\">MAP</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-back\" style=\"left:90px\" onclick=JavaScript:ShowDetailsTab()><div class=\"tab-container\"><b class=\"lable-back\">DETAILS</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-back\" style=\"left:180px\" onclick=JavaScript:ShowCompareTab()><div class=\"tab-container\"><b class=\"lable-back\">COMPARE</b></div></div>";

    mapDiv.style.visibility = 'visible';
    detailsDiv.style.visibility = 'hidden';
    compareDiv.style.visibility = 'hidden';
}

function ShowDetailsTab() {
    mapDiv = document.getElementById('map');
    detailsDiv = document.getElementById('details');
    compareDiv = document.getElementById('compare');
    righttabsDiv = document.getElementById('righttabs');

    righttabsDiv.innerHTML  = "<div class=\"tab-back\" onclick=JavaScript:ShowMapTab()><div class=\"tab-container\"><b class=\"lable-back\">MAP</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-front\" style=\"left:90px\"><div class=\"tab-container\"><b class=\"lable-front\">DETAILS</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-back\" style=\"left:180px\" onclick=JavaScript:ShowCompareTab()><div class=\"tab-container\"><b class=\"lable-back\">COMPARE</b></div></div>";

    mapDiv.style.visibility = 'hidden';
    detailsDiv.style.visibility = 'visible';
    compareDiv.style.visibility = 'hidden';

    if (mapDiv.style.height != detailsDiv.style.height)
	{
	    if (gpano) {
		gpano.remove();
		if (document.getElementById("pano")) { document.getElementById("pano").parentNode.removeChild(document.getElementById("pano")); }
	    }
	    mapDiv.style.height = detailsDiv.style.height;
	    map.checkResize();
	}
}

function ShowCompareTab() {
    mapDiv = document.getElementById('map');
    detailsDiv = document.getElementById('details');
    compareDiv = document.getElementById('compare');
    righttabsDiv = document.getElementById('righttabs');

    righttabsDiv.innerHTML  = "<div class=\"tab-back\" onclick=JavaScript:ShowMapTab()><div class=\"tab-container\"><b class=\"lable-back\">MAP</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-back\" style=\"left:90px\" onclick=JavaScript:ShowDetailsTab()><div class=\"tab-container\"><b class=\"lable-back\">DETAILS</b></div></div>";
    righttabsDiv.innerHTML += "<div class=\"tab-front\" style=\"left:180px\"><div class=\"tab-container\"><b class=\"lable-front\">COMPARE</b></div></div>";

    mapDiv.style.visibility = 'hidden';
    detailsDiv.style.visibility = 'hidden';
    compareDiv.style.visibility = 'visible';

    if (mapDiv.style.height != detailsDiv.style.height)
	{
	    if (gpano) {
		gpano.remove();
		if (document.getElementById("pano")) { document.getElementById("pano").parentNode.removeChild(document.getElementById("pano")); }
	    }
	    mapDiv.style.height = detailsDiv.style.height;
	    map.checkResize();
	}
}

function newClass() {
    var str = getCookie("reinftp-ps");
    dbclass = document.ps.dbclass.value;
    a = eval(dbclass + '_areas');
    areas = a.length;
    document.ps.area.length = 0;
    for (i=0; i<areas; i++) {
	document.ps.area.options[document.ps.area.length] = new Option(a[i], a[i]);
    }
    getValue(str, "area", document.ps.area, "select");
    a = eval(dbclass + '_types');
    types = a.length;
    document.ps.type.length = 0;
    for (i=0; i<types; i++) {
	document.ps.type.options[document.ps.type.length] = new Option(a[i], a[i]);
    }
    if (dbclass == 'residential') {
	document.ps.nbh.disabled = false;
	document.ps.br.disabled = false;
	document.ps.ba.disabled = false;
    } else {
	document.ps.nbh.disabled = true;
	document.ps.br.disabled = true;
	document.ps.ba.disabled = true;
    }
}

function PrintSummary()
{
    urchinTracker('/reinftp/print');

    var str = "";
    for (i=0; i<document.ps.length; i++)
	{
	    str = str + document.ps.elements[i].name + "=";
	    if (document.ps.elements[i].type == "checkbox")
		{
		    str += (document.ps.elements[i].checked ? 1 : 0) + "&";
		}
	    else
		{
		    str += escape(document.ps.elements[i].value) + "&";
		}
	    
	}
    str += "print=true";
    pwindow = window.open('/reinftp/jscript/working.html','print');
    //pwindow.document.write("<html><head><title>Summary Results</title></head>");
    //pwindow.document.write("<body><img src=/reinftp/graphics/pb_gap.gif></body></html>");
    //pwindow.document.close();

    sumobj.open("GET", "cgi-bin/ShowSummary.cgi?"+str, true);
    sumobj.onreadystatechange = function() {
	if (sumobj.readyState == 4) {
	    var xml = GXml.parse(sumobj.responseText);
	    pwindow = window.open('','print');
	    pwindow.document.write("<html><head><title>Summary Results</title>");
	    pwindow.document.write("<link rel=\"stylesheet\" href=\"/css/search-mls.css\" type=\"text/css\">");
	    pwindow.document.write("<link rel=\"stylesheet\" href=\"/css/search-mls-print.css\" type=\"text/css\">");
	    pwindow.document.write("</head>");
	    pwindow.document.write("<body>");

	    pwindow.document.write(xml.documentElement.getElementsByTagName("results")[0].firstChild.nodeValue);

	    pwindow.document.write("</body></html>\n");
	    pwindow.document.close();
	}
    }
    sumobj.send(null);
}
