// JavaScript Document
function WR_trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function WR_trimnumber(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$|\D/g,"");
}

function WR_get_radio_value()
{

for (var i=0; i < document.SearchMRED.radioDistressType.length; i++)
   {
   if (document.SearchMRED.radioDistressType[i].checked)
      {
      return document.SearchMRED.radioDistressType[i].value;
      }
   }
}

function WR_ProcessSearchMLS()
{

	var myConnector;
		
	// get all of the fields from the form and strip out leading and trailing white space
//	var	myDistressObj = document.getElementById("radioDistressType");
 	var	myDistressType = WR_get_radio_value();

	var myCity = WR_trim(document.getElementById("txtCity").value);
	var myZip = WR_trim(document.getElementById("txtZipCode").value);
	var myCounty = WR_trim(document.getElementById("selCounty").value);
	var myMinPrice = WR_trimnumber(document.getElementById("txtMinPrice").value);
	var myMaxPrice = WR_trimnumber(document.getElementById("txtMaxPrice").value);
	var myMinBeds = WR_trimnumber(document.getElementById("txtBedsMin").value);
	var myMinBaths = WR_trimnumber(document.getElementById("txtBathsMin").value);
	var myTargetURL = WR_trim(document.getElementById("txtBaseURL").value);
	
	// verify that the user has selected either a county, city, or zip
    if (((myCounty == null) || (myCounty == "") || (myCounty == "Any")) &&
		((myCity == null) || (myCity == "")) &&
		((myZip == null) || (myZip == "")) ) {
	   // a specific county, city, or zip is not entered 
	   alert("Please specify a location using one of the following fields: County, City, or Zip Code. Adjust your search criteria and try again.");
	   return false;
    }
	
	// convert escape characters
	// build the query strings
	
	
	// distress type
	if ((myDistressType == null) || (myDistressType == "")) {
		myDistressType = "2";
	}
	
	if (myDistressType == "0") {
		// we want all properties, no distress type parameter used
		myConnector = "?";
	}
	else {
		myTargetURL = myTargetURL + "?idx-q-DistressTypes=" + myDistressType;
		myConnector =  "&";
	}
	
	// city

	if ((myCity == null) || (myCity == "")) {
	   // the city is not entered, do nothing
	}
    else {
		// the city is entered
		myTargetURL = myTargetURL + myConnector + "idx-q-Cities<0>=" + escape(myCity);
		myConnector =  "&";
	}
	
	// zipcode
    if ((myZip == null) || (myZip == "")) {
	   // the zip code is not entered, do nothing
    }
    else {
		// the zip code is entered
		myTargetURL = myTargetURL + myConnector + "idx-q-ZipCodes<0>=" + escape(myZip);
		myConnector =  "&";
	}
	
	// county
    if ((myCounty == null) || (myCounty == "") || (myCounty == "Any")) {
	   // a specific county  is not entered 
    }
	else {
		// the county is entered
		myTargetURL = myTargetURL + myConnector + "idx-q-Counties<0>=" + escape(myCounty);
		myConnector =  "&";
	}

	// minprice
    if ((myMinPrice == null) || (myMinPrice == "")) {
	   // the minimum price is not entered, do nothing
    }
    else {
		// the minimum is entered
		// we have already trimmed this as a number above
		myTargetURL = myTargetURL + myConnector + "idx-q-PriceMin=" + myMinPrice;
		myConnector =  "&";
	}
	
	// maxprice
    if ((myMaxPrice == null) || (myMaxPrice == "")) {
	   // the minimum price is not entered, do nothing
    }
    else {
		// the minimum is entered
		// we have already trimmed this as a number above
		myTargetURL = myTargetURL + myConnector + "idx-q-PriceMax=" + myMaxPrice;
		myConnector =  "&";
	}
	
	// min beds
    if ((myMinBeds == null) || (myMinBeds == "")) {
	   // the minimum price is not entered, do nothing
    }
    else {
		// the minimum is entered
		// we have already trimmed this as a number above
		myTargetURL = myTargetURL + myConnector + "idx-q-BedsMin=" + myMinBeds;
		myConnector =  "&";
	}
	
	// min baths
    if ((myMinBaths == null) || (myMinBaths == "")) {
	   // the minimum price is not entered, do nothing
    }
    else {
		// the minimum is entered
		// we have already trimmed this as a number above
		myTargetURL = myTargetURL + myConnector + "idx-q-BathsMin=" + myMinBaths;
		myConnector =  "&";
	}

	if (myConnector == "?") {
		alert("No search criteria entered. At a minimum, you must specify a County, City, or Zip Code. Please try your search again.");
	}
	else {
		window.location = myTargetURL;
	}
	return false;

}

function WR_ChangeParentURL(url) {
	document.location=url;
}		

function WR_saveFieldValues() {
	// save values into sessvars before changing URL on property search
//	alert('in save field values');
	var myFrame = document.getElementById('advanced_iframe').contentWindow;
	 
	sessvars.curSearchType1 = myFrame.document.getElementById('addressSearchType1').checked;
	sessvars.curSearchType2 = myFrame.document.getElementById('addressSearchType2').checked;
	sessvars.curSearchType3 = myFrame.document.getElementById('addressSearchType3').checked;
	sessvars.curSearchType4 = myFrame.document.getElementById('addressSearchType4').checked;
	
	sessvars.curCity = myFrame.document.getElementById('txtCity').value;
	sessvars.curCounty = myFrame.document.getElementById('selCounty').value;
	sessvars.curZip = myFrame.document.getElementById('txtZipCode').value;
	sessvars.curWorkAddress = myFrame.document.getElementById('workAddress').value;
	sessvars.curTravelMiles = myFrame.document.getElementById('travelMiles').value;
	sessvars.curPropType = myFrame.document.getElementById('selPropertyType').value;
	sessvars.curSubdivision = myFrame.document.getElementById('txtSubdivision').value;
	sessvars.curSchool = myFrame.document.getElementById('txtSchoolName').value;
	sessvars.curMinPrice = myFrame.document.getElementById('txtMinPrice').value;
	sessvars.curMaxPrice = myFrame.document.getElementById('txtMaxPrice').value;
	sessvars.curMinBeds = myFrame.document.getElementById('txtBedsMin').value;
	sessvars.curMaxBeds = myFrame.document.getElementById('txtBedsMax').value;
	sessvars.curMinBaths = myFrame.document.getElementById('txtBathsMin').value;
	sessvars.curMaxBaths = myFrame.document.getElementById('txtBathsMax').value;
	sessvars.curMinAcres = myFrame.document.getElementById('txtLotAcresMin').value;
	sessvars.curMaxAcres = myFrame.document.getElementById('txtLotAcresMax').value;
	sessvars.curMinYear = myFrame.document.getElementById('txtYearBuiltMin').value;
	sessvars.curMaxYear = myFrame.document.getElementById('txtYearBuiltMax').value;
	sessvars.curHasBasement = myFrame.document.getElementById('chkHasBasement').checked;

	sessvars.$.flush();

}

function WR_resetFieldValues() {
	
	// re-initialize sessvars to default state on a form reset
	// the reset event will initialize the form fields
	sessvars.curSearchType1 = true;
	sessvars.curSearchType2 = false;
	sessvars.curSearchType3 = false;
	sessvars.curSearchType4 = false;
	sessvars.curCity = '';
	sessvars.curCounty = 'Any';
	sessvars.curZip = '';
	sessvars.curWorkAddress = '';
	sessvars.curTravelMiles = '';
	sessvars.curPropType = 'Any';
	sessvars.curSubdivision = '';
	sessvars.curSchool = '';
	sessvars.curMinPrice = '';
	sessvars.curMaxPrice = '';
	sessvars.curMinBeds = '';
	sessvars.curMaxBeds = '';
	sessvars.curMinBaths = '';
	sessvars.curMaxBaths = '';
	sessvars.curMinAcres = '';
	sessvars.curMaxAcres = '';
	sessvars.curMinYear = '';
	sessvars.curMaxYear = '';
	sessvars.curHasBasement = false;
}

function WR_getFieldValues() {
//	sessvars.$.debug();
//	alert('in get field values, sessvars.curSearchType1 = ' + sessvars.curSearchType1);

	var myFrame = document.getElementById('advanced_iframe').contentWindow;

	// get the field values from sessvars, if no value in sessvars then the form is not changed
	if (typeof(sessvars.curSearchType1) != 'undefined') {
//		alert('before set value funcs');
		WR_setFieldValue(myFrame.document.getElementById('addressSearchType1'), 'R', sessvars.curSearchType1);
		WR_setFieldValue(myFrame.document.getElementById('addressSearchType2'), 'R', sessvars.curSearchType2);
		WR_setFieldValue(myFrame.document.getElementById('addressSearchType3'), 'R', sessvars.curSearchType3);
		WR_setFieldValue(myFrame.document.getElementById('addressSearchType4'), 'R', sessvars.curSearchType4);
		
		WR_setFieldValue(myFrame.document.getElementById('txtCity'), 'T', sessvars.curCity);
		WR_setFieldValue(myFrame.document.getElementById('selCounty'), 'S', sessvars.curCounty);
		WR_setFieldValue(myFrame.document.getElementById('txtZipCode'), 'T', sessvars.curZip);
		WR_setFieldValue(myFrame.document.getElementById('workAddress'), 'T', sessvars.curWorkAddress);
		WR_setFieldValue(myFrame.document.getElementById('travelMiles'), 'T', sessvars.curTravelMiles);
		WR_setFieldValue(myFrame.document.getElementById('selPropertyType'), 'S', sessvars.curPropType);
		WR_setFieldValue(myFrame.document.getElementById('txtSubdivision'), 'T', sessvars.curSubdivision);
		WR_setFieldValue(myFrame.document.getElementById('txtSchoolName'), 'T', sessvars.curSchool);
		WR_setFieldValue(myFrame.document.getElementById('txtMinPrice'), 'T', sessvars.curMinPrice);
		WR_setFieldValue(myFrame.document.getElementById('txtMaxPrice'), 'T', sessvars.curMaxPrice);
		WR_setFieldValue(myFrame.document.getElementById('txtBedsMin'), 'T', sessvars.curMinBeds);
		WR_setFieldValue(myFrame.document.getElementById('txtBedsMax'), 'T', sessvars.curMaxBeds);
		WR_setFieldValue(myFrame.document.getElementById('txtBathsMin'), 'T', sessvars.curMinBaths);
		WR_setFieldValue(myFrame.document.getElementById('txtBathsMax'), 'T', sessvars.curMaxBaths);
		WR_setFieldValue(myFrame.document.getElementById('txtLotAcresMin'), 'T', sessvars.curMinAcres);
		WR_setFieldValue(myFrame.document.getElementById('txtLotAcresMax'), 'T', sessvars.curMaxAcres);
		WR_setFieldValue(myFrame.document.getElementById('txtYearBuiltMin'), 'T', sessvars.curMinYear);
		WR_setFieldValue(myFrame.document.getElementById('txtYearBuiltMax'), 'T', sessvars.curMaxYear);
		WR_setFieldValue(myFrame.document.getElementById('chkHasBasement'), 'C', sessvars.curHasBasement);
//		alert('after set value funcs');
	
//		alert('search radios = ' + sessvars.curSearchType1 + ', ' + sessvars.curSearchType2 + ', ' + sessvars.curSearchType3 + ', ' + sessvars.curSearchType4); 
		if (sessvars.curSearchType2 == true) {
			return(2);
		}
		else if (sessvars.curSearchType3 == true) {
			return(3);
		}
		else if (sessvars.curSearchType4 == true) {
			return(4);
		}
		else {
			return(1);
		}
	
	}
	else {
		return(1);
	}
	
}

function WR_setFieldValue(fieldObject, fieldType, newFieldValue) {
	switch (fieldType) {
		case 'T': // text
			fieldObject.value = newFieldValue;
			break;
		case 'S': // select
			fieldObject.value = newFieldValue;
			break;
		case 'R': // radio group
			fieldObject.checked = newFieldValue;
			break;
		case 'C': // checkbox
			fieldObject.checked = newFieldValue;
			break;
		default:
			// do nothing
			break;
	}
}


