var cityPickTool = new CheckboxPickTool('selectedCities') ;
var zipPickTool = new CheckboxPickTool('selectedZips') ;

//Remember if advancedSearch was opened
//In this case, form validations are different and
//defer to the server to validate area related fields
//see mlsSearchableFields.xml
var advancedSearchActivated= false ;

function setAdvancedSearchActivated(){
	advancedSearchActivated= true ;
}

function isAdvancedSearchActivated(){
	return advancedSearchActivated ;
}

function hideOrDisplayCityPicker(){
	cityPickTool.synchronizePickTool();
  if($('cityPicker').visible()){
    hideCityPicker();
  }else{
    showCityPicker();
  }
}

function hideCityPicker(){
  $('cityPicker').hide();
  $('cityList').show();
}

function showCityPicker(){
  $('cityPicker').show();
  $('cityList').hide();
}

function hideOrDisplayZipPicker(){
  zipPickTool.synchronizePickTool(); 
  if($('zipPicker').visible()){
    hideZipPicker();
  }else{
    showZipPicker();
  }
}

function hideZipPicker(){
  $('zipPicker').hide();
  $('cityList').show();     
}

function showZipPicker(){
  $('zipPicker').show();
  $('cityList').hide();     
}

/**
 * Function called to load  the advanced search page for 
 * a given board.  The request gets an html snippet that we
 * include in the search page.
 * 
 * @param {Object} url
 * @param {Object} params
 */
function updateAdvancedSearch( url, params ){
	setAdvancedSearchActivated();
  //Get new contents for the advanced search area.
  if( url ){
    //Update div id=ihf_search_advsearch_750
    new Ajax.Request(url, {
        method: 'post',
        parameters: params,
        onSuccess: function(response){
         //debugger;
         $('ihf_search_advsearch_750').update(response.responseText);
        },
        onFailure: function(response){
          //alert( response.responseText );
          $('ihf_search_advsearch_750').update(response.responseText);
        }
     }
    );
  }
}

/**
 * Function called to load multi select boxes in Advanced Search.
 * We load these only when required, because some of the select lists are
 * very big.  The results is an html snippet that we include in the
 * advanced search html
 * 
 * @param {Object} params
 * @param {Object} updateContent
 * @param {Object} hideContent
 */
function updateAdvancedSearchMultiSelect( params, updateContent, hideContent ){
  new Ajax.Request( "/idx/controllers/SearchMapController.cfc",{ 
                          method: 'post', 
                          parameters: params,
                          onSuccess: function(response){ 
                           $(hideContent).hide(); 
                           $(updateContent).update(response.responseText);
                           $(updateContent).show();
                           
                          }
                        }); 
}

function hideOrDisplayAdvancedSearch() {
  //Hide or display the advanced search area
  if( $('ihf_search_advsearch_750').visible() ) {
    $('ihf_search_advsearch_750').hide();
  } else {
    $('ihf_search_advsearch_750').show();
  }
}

/**
* Advanced Search should only be an option, for 
* residential properties
*/
function hideOrDisplayAdvancedSearchContainer(){
	if($('propertyTypeSelection') != null){
		var propertyType=$('propertyTypeSelection').getValue();
		var advancedSearchContainer=$('advancedSearchContainer');
		if( advancedSearchContainer != null && propertyType != null ){
			if( propertyType.indexOf('SFR') > -1 ||  propertyType.indexOf('CND') > -1){
				advancedSearchContainer.show();
				$('advancedSearchContainer').select('input', 'textarea', 'select').each(function(item){ item.enable()});				
			}
			else{	
				//now we need to disable any form fields found in the advancedSearchContainer
				$('advancedSearchContainer').select('input', 'textarea', 'select').each(function(item){ item.disable()});
				advancedSearchContainer.hide();
			}
		}		
	}
}

function hideOrDisplaySearchFields(){
  var hideBedrooms = false;
  var hideBathrooms = false;
  var propertyType=$('propertyTypeSelection').getValue();
 // var sqFtIndicator = $('ihf_search_sqft');
  var okToDisplaySquareFeet = false;
  if ( $('showSquareFeet') && $('showSquareFeet').innerHTML == "1" )
  {
    okToDisplaySquareFeet = true;
  }
  if( propertyType && propertyType != null ){
 
    //
    // Property Type selection was made, hide
    // or show the search fields accordingly.
    switch ( propertyType )
    {
      // Property Types that Remove ALL Search Fields
      case 'RI':  if (okToDisplaySquareFeet) $('squareFeet').disable();
                      $('lotAcres').disable();
                      $('bedrooms').disable();
                      $('bathrooms').disable();
                      break;

      case 'COM':     $('lotAcres').disable();
                     $('bedrooms').disable();
                      $('bathrooms').disable();
                       if (okToDisplaySquareFeet)  $('squareFeet').enable();
                      break;

      case 'LL':      $('bedrooms').disable();
                      $('bathrooms').disable();
                      if (okToDisplaySquareFeet)  $('squareFeet').disable();
                      $('lotAcres').enable();
                      break;

      // Property Types that Need ALL Search Fields
      default:        $('bedrooms').enable();
                      $('bathrooms').enable();
                      if (okToDisplaySquareFeet)  $('squareFeet').enable();
                      $('lotAcres').disable();
                      break;
    }
	//debugger;
	hideOrDisplayAdvancedSearchContainer();
  }
}

/**
 * Validate the main search from, that includes price, city, ....
 * 
 * @param {Object} theForm
 * @param {Object} errorMessageField
 */
function validateSearch(theForm, errorMessageField ){
	
	var errorMessage = '' ;
	var isCitySelected = false;
	var isZipSelected = false;
	var cityField = theForm['cityID'];
	var zipList = theForm['zipList'] ;
	
	/**
	 * If advanced search has been opened, then another area field
	 * may have been selected, so we let the server validate
	 * area related fields.  Else, we ensure that either 
	 * city or zip is selected.
	 */
	if( !isAdvancedSearchActivated() ){
	  if( cityField && cityField != null && cityField.length > 0 ){   
	    for( var i=0; i<cityField.length; i++){
	      if( cityField[i].checked == true ){
	        isCitySelected = true;  
	        break;
	      }
	    }
	  }
	  
	  if( zipList && zipList != null && zipList.length > 0 ){   
	    for( var i=0; i<zipList.length; i++){
	      if( zipList[i].checked == true ){
	        isZipSelected = true;  
	        break;
	      }
	    }
	  } 	
    if( !isCitySelected && ! isZipSelected  ) {
      errorMessage += "Please select a city or zip code.";  
    }			
	}

		

	
  var minValueField = $('minListPrice');
  var maxValueField = $('maxListPrice');
	
	
  var minValue = minValueField.getValue();
	minValue = minValue.replace(/[^\d\.\-\ ]/g, "");
	minValue = parseFloat( minValue );
	
  var maxValue = maxValueField.getValue();
	maxValue = maxValue.replace(/[^\d\.\-\ ]/g, "");
  maxValue = parseFloat( maxValue );
	
 
  if( minValue > maxValue  ){
		if( errorMessage.length > 0 ){
			errorMessage +="<br/>";
		}
    errorMessage += "Maximum List Price must be greater than Minimum List Price.";  
		minValueField.focus();
  } 
	
	
	if( errorMessage != ''){
		$(errorMessageField).update( errorMessage );
		$('ihf_search_submitbutton','ihf_search_submitbutton_rotating').invoke('toggle');
		return false;
	}
	
	return true;
}

/**
 * Valdate the listing id search.
 * 
 * @param {Object} theForm
 * @param {Object} errorMessageField
 */
function validateListingIdSearch( theForm, errorMessageField ){
  var errorMessage = '' ;
  var listingNumber = $('listingNumber');
  if( listingNumber == null || listingNumber.getValue() == '') {
		errorMessage = 'Listing number is required.' ;
		listingNumber.focus();
	}   

  if( errorMessage != ''){
    $(errorMessageField).update( errorMessage );
    return false;
  }
  
  return true;
}

/**
 * Validate the Address Search form, which requires a street name
 * and city.  The city is a select list that defaults to all cities,
 * so no validation is required for city.
 * 
 * @param {Object} theForm
 * @param {Object} errorMessageField
 */
function validateAddressSearch( theForm, errorMessageField ){
  var errorMessage = '' ;
  var streetName = $('streetName');
  if( streetName == null || streetName.getValue() == '') {
    errorMessage = 'Street Name is required.' ;
		streetName.focus();
  }   

  if( errorMessage != ''){
    $(errorMessageField).update( errorMessage );
    return false;
  }
  
  return true;
}


