function js_trim(inputString) {
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") {
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") {
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) {
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length);
    }
    return retValue;
}
					$(document).ready(
					function() {
						$("#change-location").autocomplete(
							"http://www.ncsy.org/autocomplete_location.php",
							{
								delay:10,
								minChars:3,
								matchSubset:1,
								//matchContains:1,
								cacheLength:10,
								maxItemsToShow:10,
								//onItemSelect:selectItem,
								//onFindValue:findValue,
								//formatItem:formatItem,
								autoFill:false
							}
						);
						$("#switchLocation").click(function() {
							
							var new_location = $jq("#change-location").val();
							// validation on the string entered by the user
							if (new_location.length == 0) {
								alert("To switch location, type name of city and click Submit");
								$("#change-location").get(0).focus();
								return;
							}
							// parse new location to yield new_city, new_region, new_country_short
							// if there is no comma in the location, treat the entire location as the city
							var new_city="", new_region="", new_country_short="";
							if (new_location.indexOf(",") == -1) {
								new_city = new_location;
							} else {
								new_city = new_location.split(",")[0];
								var rest = js_trim(new_location.split(",")[1]);
								if (rest.indexOf(" ") == -1) {
									// there was no space, the rest is either region or country
									// should both possibilities be tried in the SQL in the php program?
									new_country_short = rest;
								} else {
									new_region = rest.split(" ")[0];
									new_country_short = rest.split(" ")[1];
								} 
							}
							// make a synchronous call to get all matching city/region/country records
							// if exactly one match, then proceed with location switch, 
							// otherwise, display list of matches in thickbox dialog box

							var matching_location_url = "http://www.ncsy.org/maxmind_lookup.php";
/*
							var matching_location_url_root = "http://www.ncsy.org/test/maxmind_lookup.php";
							var loc_params = new Array();
							if (new_city != "") {
								loc_params['city'] = new_city;
							}		
							if (new_region != "") {
								loc_params['region'] =  new_region;
							}
							if (new_country_short != "") {
								loc_params['country'] = new_country_short;
							}
*/
							var queryParts = new Array();
							if (new_city != "") {
								queryParts[0] = "city=" + new_city;
							}
							if (new_region != "") {
								queryParts[1] = "region=" + new_region;
							}
							if (new_country_short != "") {
								queryParts[2] = "country=" + new_country_short;
							}
							queryString = queryParts.join("&").toLowerCase();
							queryString = queryString.replace(/&&/g, "&");
							matching_location_url = matching_location_url + '?' + queryString;
							//alert(matching_location_url);
							// check the cities database to make sure we have exactly one city to proceed with
							// if there are too many cities that match, tell user to refine the search
							// if more than one but several match, give the user a list of cities to choose from

							$.getJSON(matching_location_url, function(data){
														urchinTracker("/map/search/" + queryString);
                                                        globalvariable = "hello";
														map.setCenter(new GLatLng(data.lat, data.lng));
															
});

/*
							var myData = $jq.ajax({type: "GET",
										url: matching_location_url,
										dataType: "script",
										//data: params.join('&'),
										async: false}).responseText;
							processMatchingLocationsUponSubmit(myData);
*/						
						return;
						});  // end of switchLocation	
					});

