function select_location_close() {

  popup_hide('select_location');
}

function select_location_search() {
  
  var input_string = $('popup_select_location_input').value;
  
  new Ajax.Request('ajax.php', 
  {
    parameters: { 
      _action: 'autocomplete_location', 
      input_id: "input_string", 
      input_string: input_string, 
      response : "div"
    }, 
    onSuccess: function(response) {
      // Returns comment_id and html
      var json_response = response.responseText.evalJSON();
  
      for (i=0;i<json_response.locations.length;i++)
        alert(json_response.locations[i]);

    },
    onFailure: function(){ 
    
    }
  });
}

/////////////////////////////////////////////////////////////////////////////////////////
// Select Location Class
function location_selector(options) {
  this.options = options;
  
  this.name = this.options.name;
	this.ajax_url = 'ajax.php';
  this.ajax_name = 'autocomplete_location'
  
  //this.required = this.options.required ? this.options.required : 0;
  this.input_class = this.options.input_class ? this.options.input_class : 'f_input';
  
  // base url
  this.base_url = this.options.base_url;
  
  // Id for elements
  this.input_id = this.options.input_id ? this.options.input_id : this.name+"_input";
  this.span_id  = this.name+"_span";
  this.hints_id = this.name+"_hints";
  this.location_input_id = this.name+"_location";  
  this.indicator_id = this.name+"_indicator";
  
  // State of location
  this.location_id = this.options.location_id ? this.options.location_id : null;
  this.city = this.options.city ? this.options.city : null;
  this.state = this.options.state ? this.options.state : null;
  this.zipcode = this.options.zipcode != null ? this.options.zipcode : null;
  
  // Options
  this.form_width = this.options.width != null ? this.options.width: 150;
  
  // Callback for select
  this.select_callback = this.options.select_callback ? this.options.select_callback : "";
  
  this.autocomplete = null;
}

location_selector.prototype.generate_html = function() {
 var html = "";

  if(this.location_id != null) {
    html += '<span><input id="'+this.input_id+'" name="'+this.input_id+'" class="'+this.input_class+'" type="text" style="width:'+this.form_width+'px;" value="'+this.city+', '+this.state+' '+this.zipcode+'" /></span>';
    html += '<span style="margin-left:5px;vertical-align:middle;display:none" id="'+this.indicator_id+'"><img src="'+this.base_url+'layout/progress.gif"></span>';
    html += '<div id="'+this.hints_id+'" class="autocomplete"></div>';
    html += '<input id="'+this.location_input_id+'" name="'+this.location_input_id+'" type="hidden" value="'+this.location_id+'" />';
  }
  else {
    html += '<span><input id="'+this.input_id+'" name="'+this.input_id+'" class="'+this.input_class+'" type="text" style="width:'+this.form_width+'px" /></span>';
    html += '<span style="margin-left:5px;vertical-align:middle;display:none" id="'+this.indicator_id+'"><img src="'+this.base_url+'layout/progress.gif"></span>';
    html += '<div id="'+this.hints_id+'" class="autocomplete"></div>';
    html += '<input id="'+this.location_input_id+'" name="'+this.location_input_id+'" type="hidden" value="0" />';
  }
  
  $(this.name).innerHTML = html;
}

location_selector.prototype.init_ajax = function() {
  var autocomplete_param = '_action='+this.ajax_name+'&input_id='+this.input_id;
  this.autocomplete = new Ajax.Autocompleter(this.input_id, this.hints_id, this.ajax_url, {indicator:this.indicator_id, object:this, parameters: autocomplete_param});
}

location_selector.prototype.select = function(li) {
  // Parsed variables
	var location_id = li.id;
	
	var str = li.innerHTML;
	str = str.replace(/\<b\>/gi, "");
	str = str.replace(/\<\/b\>/gi, "");

	var temparray = str.split(" ");
	var zipcode = temparray[temparray.length-1];
	zipcode = zipcode.replace(/\s/g, "");

	$(this.input_id).value = str;
	//$(this.input_id).hide();
	//$(this.span_id).innerHTML = str + " <span class=\"content_small\">(<a href=\"javascript:void(0)\" onclick=\""+this.name+".change_location()\">Change</a>)</span>";
	//$(this.span_id).show();
	$(this.location_input_id).value = location_id;
  
  // Callback
  if(this.select_callback != "")
    eval(this.select_callback);
}

location_selector.prototype.change_location = function() {
	$(this.input_id).show();
	$(this.span_id).hide();
	$(this.location_input_id).value = '0';
}

