/* clear topbar hidden form values */
function clearTopbarValues(){
  if(typeof(searchForm) != 'object') return;
  searchForm.lat.value            = "";
  searchForm["long"].value        = "";
  searchForm.miles.value          = "";
  searchForm.market_id.value      = "";
  searchForm.street.value         = ""; 
  searchForm.city_state_zip.value = "";
}

storeWhere = function(value){
  var value = value || null;
  if(value != null){
    searchForm.where.value = value;
  }
}

/* store where for comparison */
storeComparisonWhere = function() {
  searchForm.store_where_for_comparison.value = searchForm.where.value;
} 

/* clean-up submission */
cleanTopbarSubmit = function(){
  if(typeof(searchForm) != 'object') return;
  // Only want to do this if the new where term does != the original where term
  if(searchForm.where.value != searchForm['store_where_for_comparison']){
    storeComparisonWhere();
    if (!searchForm.market_id.value) clearTopbarValues();
    else{
      // Obtained market id from saved locs, don't send where
      if (searchForm.city_state_zip.value){ // Obtained market id from saved locs, don't send where
        searchForm.lat.value      = "";
        searchForm["long"].value = "";
        searchForm.where.value    = "";
      }
    }
  }
}

focusTopbar = function() {
  if(typeof(searchForm) != 'object') return;
  if (!GECKO) searchForm.query.focus();
}


clearRecentLocations = function(parentId, childId){
  if(typeof(CS_DHTML) == "object" ){
    CS_DHTML.removeChild(parentId, childId);
  }

  if(navigator.cookieEnabled){
    document.cookie = 'recent_locs=; domain=.'+baseProdHost;
  }
}

/* change text in where input box, set hidden form fields (YP topbar) */
function changeWhere(whereString){
  if(typeof(searchForm) != 'object') return;
  var whereString = whereString || "";
  if (whereString != "") { 
    var whereArray = new Array();
    whereArray = whereString.split("|");

    searchForm.where.value          = whereArray[0];
    searchForm.lat.value            = whereArray[1];
    searchForm["long"].value        = whereArray[2];
    searchForm.miles.value          = whereArray[3];
    searchForm.market_id.value      = whereArray[4];
    searchForm.street.value         = whereArray[5];
    searchForm.city_state_zip.value = whereArray[6];
  }
}

/* create options in locations pulldown (YP topbar) */
function insertLocationsOptions(id){
  if (valuesObj[id]) {
    var buffer = "";
    var wrapper = null;
    if(document.getElementById) wrapper = document.getElementById(id + 'Wrapper') || null;
    if(wrapper == null) return;
    for (var location in valuesObj[id]){
      var location = valuesObj[id][location];
      var whereTerm = "";
      var optionStreet       = "";
      var optionCityStateZip = "";

      if (id == "recLocs"){
        whereTerm = location["where"];
      } else {
        if (location["street"]) whereTerm = location["street"] + ", ";
        if (location["city_state"]) {
           whereTerm += location["city_state"];
        } else {
          if (location["city"]) whereTerm += location["city"] + ", ";
          if (location["state"]) whereTerm += location["state"];
        }
        if ((location["city_state"] || location["state"]) && (location["c_zip"] || location["zip"])) whereTerm += " ";
        if (location["zip"] || location["c_zip"]) whereTerm += (location["zip"] || location["c_zip"]);
      }

      if (id == "savedLocs") {
        optionStreet       = location["street"];
        optionCityStateZip = location["city_state_zip"];
      }

      whereTerm += "|" + String(location["lat"]) + "|" + String(location["long"]) + "|" + location["miles"] + "|" + location["market_id"] + "|" + optionStreet + "|" + optionCityStateZip;

      var li = document.createElement('li');
      var link = document.createElement('a');
      link.setAttribute('href',"javascript:hotelsChangeMe('h_address');changeWhere('" +whereTerm+ "');storeWhere();storeComparisonWhere();CS_DHTML.switchMenuVisibility('locsWrapper','locsIframe','hidden');searchForm.ref_uri.value=ref_uri;searchForm.request_market_only.value=1;searchForm.submit();void(0)");
      var name = (location['nameD'] || whereTerm.substring(0,whereTerm.indexOf('|')));
      link.setAttribute('name',name);
      if (location["default"] == 'y') { 
        name += ' (Default)';
        if (IE) {
          link.style.fontWeight = "bold";
          link.style.setAttribute('color', '#660000');
        } else {
          link.setAttribute('style', 'font-weight:bold; color:#660000;');
        }
      }
      linkText = document.createTextNode(name);
      wrapper.appendChild(li);
      li.appendChild(link);
      link.appendChild(linkText);
    }
  }
}

