function validateForeclosuresSearchForm(searchForm){
  if(((searchForm.qCity.value == '') || (searchForm.qState.selectedIndex == 0)) && (searchForm.qZip.value == '')){
    alert('Please enter a valid city and state or zip code value');
    return false;
  }

  return validatePropertySearchForm(searchForm);
}

function validateCommuniplexSearchForm(searchForm){
  if ((searchForm.qZip.value != '') && (!isValidZip(searchForm.qZip.value)) && (!isValidPostalCode(searchForm.qZip.value)))  {
    alert('Please enter a valid zip code (xxxxx for US, XXX XXX for CA).')
    searchForm.qZip.focus();
    return false;
  }//End if
  
  if(((searchForm.qZip.value == '') || (!isValidZip(searchForm.qZip.value))) && (searchForm.qZipExtension.value != '')){
    alert('Please enter a valid zip code when using the zip code extension');
    searchForm.qZip.focus();
    return false;
  }
  
  if ((searchForm.qRadius.options[searchForm.qRadius.selectedIndex].value != '')
    	&& (searchForm.qCity.value == '')
    	&& (searchForm.qZip.value == '')) {
    alert('You must specify City or Zip when doing a radial search!');
    searchForm.qCity.focus();
    return false;
	}//End if

	if (searchForm.qSortBy.options[searchForm.qSortBy.selectedIndex].value == 'Distance'
    	&& searchForm.qRadius.options[searchForm.qRadius.selectedIndex].value == '') {
    alert('You can only sort by distance when doing a radial search.');
    searchForm.qSortBy.focus();
    return false;
	}//End if
	
	return true;
}

function validatePropertySearchForm(searchForm) {

  /*
  if ((searchForm.qCity.value == '') && (searchForm.qZip.value == '')) {
    alert('Please enter a city or a zip code.')
    searchForm.qCity.focus();
    return false;
  }//End if
  */
  
  if ((searchForm.qZip.value != '') && (!isValidZip(searchForm.qZip.value)) && (!isValidPostalCode(searchForm.qZip.value)))  {
    alert('Please enter a valid zip code (xxxxx for US, XXX XXX for CA).')
    searchForm.qZip.focus();
    return false;
  }//End if
  
  if(((searchForm.qZip.value == '') || (!isValidZip(searchForm.qZip.value))) && (searchForm.qZipExtension.value != '')){
    alert('Please enter a valid zip code when using the zip code extension');
    searchForm.qZip.focus();
    return false;
  }
  
  if ((searchForm.qRadius.options[searchForm.qRadius.selectedIndex].value != '')
    	&& (searchForm.qCity.value == '')
    	&& (searchForm.qZip.value == '')) {
    alert('You must specify City or Zip when doing a radial search!');
    searchForm.qCity.focus();
    return false;
	}//End if

  if ((searchForm.qMinPrice.value != "Any") && (searchForm.qMinPrice.value != '') && (!isValidMoney(searchForm.qMinPrice.value))) {
    alert('Invalid money figure: ' + searchForm.qMinPrice.value);
    searchForm.qMinPrice.focus();
    return false;
  }//End if
  
  if ((searchForm.qMaxPrice.value != "Any") && (searchForm.qMaxPrice.value != '') && (!isValidMoney(searchForm.qMaxPrice.value))) {
  	alert('Invalid money figure: ' + searchForm.qMaxPrice.value);
    searchForm.qMaxPrice.focus();
    return false;
  }//End if

  if (searchForm.qMinLotSize.value != '') {
    
  	if (!isValidNumber(searchForm.qMinLotSize.value)) {
    	alert('Invalid numeric figure: ' + searchForm.qMinLotSize.value + '\nWhole numbers and decimals only, please. Commas are ok as thousands separator.');
    	searchForm.qMinLotSize.focus();
    	return false;
  	}//End if
  	
    if (searchForm.qLotSizeUnits.selectedIndex == 0) {
    	alert('You must specify the units for Minimum Lot Size');
  	  searchForm.qLotSizeUnits.focus();
      return false;
    }//End if
    
  }//End if

	if (searchForm.qSortBy.options[searchForm.qSortBy.selectedIndex].value == 'Distance'
    	&& searchForm.qRadius.options[searchForm.qRadius.selectedIndex].value == '') {
    alert('You can only sort by distance when doing a radial search.');
    searchForm.qSortBy.focus();
    return false;
	}//End if
	
	return true;
	
}//End validateSearchForm()

function isValidNumber(number) {
    if (number.search(/^\s*(?:\d{1,3}(?:,?\s*\d{3})*(?:\.\d*)?|(?:\.\d+))\s*$/) != -1) {
        return true;
    } else {
        return false;
    }
}

// returns true if money is a valid money figure with the proper punctuation
// currency can optionally begin with a $, may contain commas separating triplets, may contain a decimal point and up to 2 decimal places
// may contain whitespace in certain places (between $ and money, before/after commas, etc.)
function isValidMoney(money) {
    if (money.search(/^\$?\s*(?:(?:\d[\d\s]+|\d{1,3}(\s*,\s*\d{3})*)(?:\.\s*\d{0,2})?|\.\s*\d{1,2})$/) != -1) {
        return true;
    } else {
        return false;
    }
}

// returns true if valid US zip code eg. 00000-0000 (last 4 digits are optional)
function isValidZip(zip) {
 if (zip.search(/^[0-9]{5}( *\-? *[0-9]{4})?$/) != -1) return true;
 else return false;
}

// returns true if valid Canadian postal code
function isValidPostalCode(postalCode) {
 if (postalCode.search(/^[A-Z][0-9][A-Z] *[0-9][A-Z][0-9]$/i) != -1) return true;
 else return false;
}

function prefillKeywords(object) {

  var keywords = document.getElementById('qKeywords');

  if (object.value != '') {
    keywords.value = object.value;
    keywords.readOnly = true;
    keywords.style.backgroundColor = '#EBEBE4';
  }//End if
  
  else {
    keywords.value = '';
    keywords.readOnly = false;
    keywords.style.backgroundColor = '#FFFFFF';    
  }//End else
  
}//End prefillKeywords()

function greyOut(whichElement) {
  
    var f = document.getElementById('propertySearchForm');
  
  if ((whichElement.value != '') && ((whichElement == f.city) || (whichElement == f.state) || (whichElement == f.zip) || (whichElement == f.zipExtension))) {
            
    f.ddRegion.disabled = true;
    f.ddCommunity.disabled = true;
          
  }//End if
          
  else if ((whichElement.selectedIndex != 0) && ((whichElement == f.ddRegion) || (whichElement == f.ddCommunity))) {
          
    f.city.disabled = true;
    f.state.disabled = true;
    f.zip.disabled = true;
    f.zipExtension.disabled = true;
  }//End else
          
  else if ((whichElement == f.ddRegion) && (f.ddRegion.options[f.ddRegion.selectedIndex == 0])) {
    f.ddCommunity.disabled = true;
  }//End else if
          
  return true;

}//End greyOut()
        
function resetLocations() {
        
  var f = document.getElementById('propertySearchForm');
          
  f.city.disabled = 
  f.state.disabled =
  f.zip.disabled =
  f.zipExtension.disabled =
  f.ddRegion.disabled =
  f.ddCommunity.disabled =
    0;
          
  f.city.value = 
  f.zip.value = 
    '';
            
  f.state.selectedIndex = 
  f.ddRegion.selectedIndex = 
  f.ddCommunity.selectedIndex =
    0;
          
  return true;
        
}//End reset()

function validateBrokerSearchForm() {
  return;
}

