function searchByLocationForm(containerSelector, data) {
  if(typeof(data)!='object') return false;
  
  var container = $(containerSelector);
  if(!container.length) return false;
  var countrySelect = container.find('select[name="search_country"]:first');
  var citySelect = container.find('select[name="search_city"]:first');
  this.searchButton = container.find('input[type="button"][name="search"], span.search-submit');
  var callback = this;
  
  
  this.getSearchType = function () {
    var radios = container.find('input[type="radio"][name="search_type"]:checked');
	if(radios.length==1) return radios.val();
	else return container.find('input[name="search_type"]:first').val();
  }
  
  this.getCountry = function () {
    return countrySelect.val();
  }
  
  this.getCountryName = function () {
    return $('option:selected', countrySelect).eq(0).text();
  }
  
  this.getCity = function () {
    return citySelect.val();
  }
  
  this.getCityName = function () {
    return $('option:selected', citySelect).eq(0).text();
  }
  
    
  this.updateCountryList = function () {
    var searchType = this.getSearchType();
    var countryList = data[searchType];
      
    countrySelect.empty();
    countrySelect.append($('<option>-- select country --</option>').attr('value',-1));
    for(var i=0; i<countryList.length; i++) {    
      countrySelect.append($('<option></option>').attr('value',i).text(countryList[i][0]).data('href', countryList[i][1]));
    }
    countrySelect.trigger('change');
  }
  
  this.updateCityList = function () {
    var searchType = this.getSearchType();
    var country = this.getCountry();    
    
    citySelect.empty();
    citySelect.append($('<option>-- select city --</option>').attr('value',-1));
    
    if(country<0) {
      for(var j=0; j<data[searchType].length; j++) {
        addCities(searchType, j);
      }
    } else {
      addCities(searchType, country);
    }
    
    callback.sortCities();
    
    if(citySelect.find('option[value!="-1"]').length==1 && callback.searchButton.length>0) {
      citySelect.val(citySelect.find('option[value!="-1"]').val());
    }
  }
  
  this.sortCities = function () {
    citySelect.find('option[value!="-1"]').tsort('', {place: 'end' });
  }
  
  this.doSearch = function () {
    var searchHref, gaAction, gaLabel;
    var gaCategory = 'Search Box';
    
    if(this.getCity()>=0) {
      searchHref=citySelect.find('option:selected').data('href');
      gaAction = 'Jump to City';
      gaLabel = this.getCityName();
    } else if(this.getCountry()>=0) {
      searchHref=countrySelect.find('option:selected').data('href');
      gaAction = 'Jump to Country';
      gaLabel = this.getCountryName();
    } else {
      return false;
    }
    
    if(typeof(searchHref)=='string') {
        // Trigger GA event
        try {
            //alert('TrackEvent - Category: ' + gaCategory + ', Action: ' + gaAction + ', Label: ' + gaLabel);
            _gaq.push(['_trackEvent', gaCategory, gaAction, gaLabel]);
        } catch(e) {}
        // Jump to URL
        location.href=searchHref;
    }

  }
  
  function addCities(searchType, country) {
    var cityList = data[searchType][country][2];
    
    for(var i=0; i<cityList.length; i++) {    
      citySelect.append($('<option></option>').attr('value',i).html(cityList[i][0]).data('href', cityList[i][1]));
    }
  }
  
 
  container.find('input[type="radio"][name="search_type"]').bind('click', function () {
    callback.updateCountryList();
  });
  
  countrySelect.bind('change', function () {
    callback.updateCityList();
  });
   
  this.searchButton.bind('click', function () {
    callback.doSearch();
  })
  
   citySelect.bind('change', function () {
     if(callback.searchButton.length==0) callback.doSearch();
   });
  
  this.updateCountryList();
 
 
}
