/** Instruction messages  **/
var firstMsg = 'First, select a county:';
var secondMsg = 'Select a city; skip this to search the entire county:';
var thirdMsg = 'Neighborhood and School District are optional:';
var fourthMsg = 'Select property type:';
var fifthMsg = 'Select property style:';


/** Function to fetch listing details  **/
function getDetails(theForm)
	{
	document.details.propid.value = theForm.PropertyID.value;
	document.details.ListingNum.value = theForm.ListingNum.value;
	document.details.ListOfficeCode.value = theForm.ListOfficeCode.value;
	document.details.AgentCode.value = theForm.AgentCode.value;
	document.details.ListPrice.value = theForm.ListPrice.value;
	document.details.Bedrooms.value = theForm.Bedrooms.value;
	document.details.Baths.value = theForm.Baths.value;
	document.details.Basement.value = theForm.Basement.value;
	document.details.Garage.value = theForm.Garage.value;
	document.details.CentralAir.value = theForm.CentralAir.value;
	document.details.Acreage.value = theForm.Acreage.value;
	document.details.Style.value = theForm.Style.value;
	document.details.submit();
	toggleViewer('block');
	}

/** Function to show/hide Detail viewer div  **/
/** set mode = '' to hide the layer          **/
/** set mode = 'block' to display the layer  **/
function toggleViewer(mode)
	{
	modeDisp = (mode == 'block')? 'none' : 'block';
	if (document.getElementById)
		{
		// this is the way the standards work
		var style2 = document.getElementById('viewer').style;
		style2.display = mode;
		}
	else if (document.all)
		{
		// this is the way old msie versions work
		var style2 = document.all['viewer'].style;
		style2.display = mode;
		}
	else if (document.layers)
		{
		// this is the way nn4 works
		var style2 = document.layers['viewer'].style;
		style2.display = mode;
		}
	}

/** Function to show/hide divs  **/
function toggleLayer(whichLayer)
	{
	if (document.getElementById)
		{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
		}
	else if (document.all)
		{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
		}
	else if (document.layers)
		{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
		}
	}

/** Function to set messages **/
function setMessage(theMsg)
	{
	var form = document.searchParams;
	switch(theMsg)
		{
		case 1:
			form.message1.value = firstMsg;
			form.message2.value = '';
			break;
		case 2:
			form.message1.value = secondMsg;
			form.message2.value = fourthMsg;
			break;
		case 3:
			if (form.city.value == '')
				{
				form.message1.value = secondMsg;
				}
			else
				{
				form.message1.value = thirdMsg;
				}
			if (form.category.value == '')
				{
				form.message2.value = fourthMsg;
				}
			else
				{
				form.message2.value = fifthMsg;
				}
			break;
		case 4:
			if (form.city.value == '')
				{
				form.message1.value = secondMsg;
				}
			else
				{
				form.message1.value = thirdMsg;
				}
			form.message2.value = fifthMsg;
			break;
		}
	}
	
/** Function to fix IE's broken DOM **/
function reloader()
	{
	var theForm = document.searchParams;
	var theValue = theForm.county.value;
	if(theValue != '')
		{
		var theLayer = 'county' + theValue;
		showLayer(theLayer);
		showLayer('categories');
		showLayer('row3');
		}
	if(theForm.citySel.value != '')
		{
		var theNeighborhood = theForm.neighborhoodDiv.value;
		showLayer(theNeighborhood);
		var theDistrict = theForm.districtDiv.value;
		showLayer(theDistrict);
		setMessage(3);
		}
	if (theForm.category.value != '')
		{
		theStyle = theForm.styleDiv.value;
		toggleLayer(theStyle);
		setMessage(4);
		}
	}


/** Function to show divs for reloader()  **/
function showLayer(whichLayer)
	{
	if (document.getElementById)
		{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "block";
		}
	else if (document.all)
		{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "block";
		}
	else if (document.layers)
		{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "block";
		}
	}
	
	

/** Function to hide unnecessary divs **/
function clearDivs(theMode)
	{
	var theForm = document.searchParams;
	switch(theMode)
		{
		case 1:
			if(theForm.cityDiv.value != '')
				{
				if(theForm.citySel.value != '')
					{
					var theSelector = theForm.elements[theForm.citySel.value];
					theSelector.selectedIndex = 0;
					}
				toggleLayer(theForm.cityDiv.value);
				theForm.cityDiv.value = '';
				theForm.citySel.value = '';
				theForm.city.value = '';
				toggleLayer('categories');
				toggleLayer('row3');
				}
		case 2:
			if(theForm.neighborhoodDiv.value != '')
				{
				if(theForm.neighborhoodSel.value != '')
					{
					var theSelector = theForm.elements[theForm.neighborhoodSel.value];
					theSelector.selectedIndex = 0;
					}
				toggleLayer(theForm.neighborhoodDiv.value);
				theForm.neighborhoodDiv.value = '';
				theForm.neighborhoodSel.value = '';
				theForm.neighborhood.value = '';
				}
			if(theForm.districtDiv.value != '')
				{
				if(theForm.districtSel.value != '')
					{
					var theSelector = theForm.elements[theForm.districtSel.value];
					theSelector.selectedIndex = 0;
					}
				toggleLayer(theForm.districtDiv.value);
				theForm.districtDiv.value = '';
				theForm.districtSel.value = '';
				theForm.district.value = '';
				}
		case 5:
			if(theForm.category.value != '')
				{
				if(theForm.styleSel.value != '')
					{
					var theSelector = theForm.elements[theForm.styleSel.value];
					theSelector.selectedIndex = 0;
					}
				toggleLayer(theForm.styleDiv.value);
				theForm.styleDiv.value = '';
				theForm.styleSel.value = '';
				theForm.style.value = '';
				}
		}
	}


function setVars(theValue, whichVar, whichSelector)
	{
	var theForm = document.searchParams;
	switch (whichVar)
		{
		case 1:
			if (theForm.county.value != '') clearDivs(1);
			theForm.county.value = theValue;
			if (theValue != '')
				{
				var theLayer = 'county' + theValue;
				toggleLayer(theLayer);
				toggleLayer('categories');
				toggleLayer('row3');
				theForm.cityDiv.value = theLayer;
				setMessage(2);
				}
			else setMessage(1);
			break;
		case 2:
			if (theForm.city.value != '') clearDivs(2);
			theForm.city.value = theValue;
			theForm.citySel.value = whichSelector;
			if (theValue != '')
				{
				var theDiv = theValue.replace(/ /,'');
				var theNeighborhood = 'N_' + theDiv;
				toggleLayer(theNeighborhood);
				theForm.neighborhoodDiv.value = theNeighborhood;
				var theDistrict = 'D_' + theDiv;
				toggleLayer(theDistrict);
				theForm.districtDiv.value = theDistrict;
				setMessage(3);
				}
			else setMessage(2);
			break;
		case 3:
			theForm.neighborhood.value = theValue;
			theForm.neighborhoodSel.value = whichSelector;
			break;
		case 4:
			theForm.district.value = theValue;
			theForm.districtSel.value = whichSelector;
			break;
		case 5:
			if (theForm.category.value != '') clearDivs(5);
			theForm.category.value = theValue;
			if (theValue != '')
				{
				var theStyle = 'S_' + theValue;
				toggleLayer(theStyle);
				theForm.styleDiv.value = theStyle;
				setMessage(4);
				}
			else setMessage(3);
			break;
		case 6:
			theForm.style.value = theValue;
			theForm.styleSel.value = whichSelector;
			break;
		}
	}
	
/*
 * Function to make entire row clickable
 */
function markRowsInit() {
    // for every table row ...
	var rows = document.getElementsByTagName('tr');
	for ( var i = 0; i < rows.length; i++ )
		{
	  // ... with the class 'odd' or 'even' ...
		if ( 'odd' != rows[i].className && 'even' != rows[i].className )
			{
		  continue;
			}
	  // ... add event listeners ...
    // ... to highlight the row on mouseover ...
	  if ( navigator.appName == 'Microsoft Internet Explorer' )
	  	{
	   	// but only for IE, other browsers are handled by :hover in css
			rows[i].onmouseover = function()
				{
		    this.className += '_hover';
				}
			rows[i].onmouseout = function()
				{
		    this.className = this.className.replace( '_hover', '' );
				}
	   	}
		rows[i].onmousedown = function()
			{
			// move to the top of the page...
			window.location.href= window.location.pathname + "#detailviewer";
			// change the color of the row...
		  this.className = 'gray';
		  // get a pointer to the row's data...
		  var theForm;
			theForm = this.getElementsByTagName( 'form' )[0];
			// load the property details...
			getDetails(theForm);
      }
	  }
	   
	}
