function geoDefault() {
    var jwIntf = "http://boingo.jiwire.com/index.htm";
    window.location=jwIntf;
}


function geoResultsAll(obj) {
   geoResults(obj,"");
}

function geoResultsMobileOnly(obj) {
   geoResults(obj,"mobile_only=true");
}

function geoResults(obj,mobileOnly) {
    var jwIntf = "/search-wifi-hotspots.htm?"+mobileOnly+"&result_display=list&technology_id=0&venue_group_id=0&city_id=0&disableBrowseBy=false&type=&state_id=0&city=";
    var msg = "Geocode Results:\n";
    msg += "Status: " + obj.Status.code + " : " + obj.Status.message + "\n";
    msg += "Source: " + obj.Source + "\n";
    msg += "Search Term: " + obj.searchterm + "\n";
    if (obj.Status.code == 200)
    {
        msg += "Accuracy: " + obj.Placemark[0].accuracy + "\n";
        msg += "Latitude: " + obj.Placemark[0].Latitude + " Longitude: " + obj.Placemark[0].Longitude + "\n";
        msg += "Address: " + obj.Placemark[0].Address + "\n";
        msg += "Street: " + obj.Placemark[0].AddressDetails.streetaddress + "\n";
        msg += "City: " + obj.Placemark[0].AddressDetails.city + "\n";
        msg += "State: " + obj.Placemark[0].AddressDetails.state + "\n";
        msg += "Zip: " + obj.Placemark[0].AddressDetails.postalcode + "\n";
        msg += "Country: " + obj.Placemark[0].AddressDetails.country + "\n";
        if (obj.WiFiKeywords.strictmode==false)
        {
            if (obj.WiFiKeywords.userlocationtypecode >= 0)
                jwIntf += "&location_type_id="+obj.WiFiKeywords.userlocationtypecode;
            if (obj.WiFiKeywords.venuename.length > 0)
                jwIntf += "&hotspot_name="+obj.WiFiKeywords.venuename;
            if (obj.WiFiKeywords.hotspottype.length > 0)
                jwIntf += "&pay_free="+obj.WiFiKeywords.hotspottype;
            if (obj.WiFiKeywords.providerid.length > 0)
                jwIntf += "&provider_id="+obj.WiFiKeywords.providerid;
            if (obj.WiFiKeywords.possiblezip.length > 0)
                jwIntf += "&zip="+obj.WiFiKeywords.zip;
            if (obj.WiFiKeywords.radius.length > 0)
                jwIntf += "&radius="+obj.WiFiKeywords.radius;
            if (obj.Placemark[0].accuracy!="city"&&obj.Placemark[0].accuracy!="zip"&&obj.Placemark[0].accuracy!="street"&&obj.Placemark[0].accuracy!="intersection"&&obj.Placemark[0].accuracy!="address"&&obj.Placemark[0].accuracy!="state"&&obj.Placemark[0].accuracy!="premise")
			{
                jwIntf += "&latitude=";
                jwIntf += "&longitude=";
                jwIntf += "&radius=100.0";
                jwIntf += "&address=";
                if (obj.Placemark[0].accuracy=="country")
                   jwIntf += "&country="+obj.Placemark[0].AddressDetails.country;
			}
            else
            {
                var addr = new String(obj.Placemark[0].Address);
                addr = addr.replace(/ /g,"+");
                addr = addr.replace(/,/g,"%2C");
                jwIntf += "&address="+addr;
                jwIntf += "&latitude="+obj.Placemark[0].Latitude;
                jwIntf += "&longitude="+obj.Placemark[0].Longitude;
                if (obj.Placemark[0].accuracy=="state")
                    jwIntf += "&radius=100.0";
                else
                    jwIntf += "&radius=20.0";
            }
            msg += "WiFi Keywords Found:\n";
            msg += "  - Hotspot Type: " + obj.WiFiKeywords.hotspottype + "\n";
            msg += "  - User Location Type Code: " + obj.WiFiKeywords.userlocationtypecode + "\n";
            msg += "  - User Location Type Message: " + obj.WiFiKeywords.userlocationtypemessage + "\n";
            msg += "  - Venue Name: " + obj.WiFiKeywords.venuename + "\n";
            msg += "  - Provider Id: " + obj.WiFiKeywords.providerid + "\n";
            msg += "  - Radius: " + obj.WiFiKeywords.radius + "\n";
            msg += "  - Radius Units: " + obj.WiFiKeywords.radiusunits + "\n";
            msg += "  - Possible Zip: " + obj.WiFiKeywords.possiblezip + "\n";
        }
        else
            msg += "WiFi Keywords were not processed: mode strict\n";
	    //alert(msg);
	    window.location=jwIntf;
	}
	else if (obj.Status.code == 6000)
	{
        if (obj.WiFiKeywords.strictmode==false)
        {
            if (obj.WiFiKeywords.userlocationtypecode >= 0)
                jwIntf += "&location_type_id="+obj.WiFiKeywords.userlocationtypecode;
            if (obj.WiFiKeywords.venuename.length > 0)
                jwIntf += "&hotspot_name="+obj.WiFiKeywords.venuename;
            if (obj.WiFiKeywords.hotspottype.length > 0)
                jwIntf += "&pay_free="+obj.WiFiKeywords.hotspottype;
            if (obj.WiFiKeywords.providerid.length > 0)
                jwIntf += "&provider_id="+obj.WiFiKeywords.providerid;
            if (obj.WiFiKeywords.possiblezip.length > 0)
                jwIntf += "&zip="+obj.WiFiKeywords.zip;
            if (obj.WiFiKeywords.radius.length > 0)

            jwIntf += "&radius="+obj.WiFiKeywords.radius.length;
            jwIntf += "&latitude=";
            jwIntf += "&longitude=";
            jwIntf += "&radius=100.0";
            jwIntf += "&address=";
            jwIntf += "&country_id=";
 	        window.location=jwIntf;
       }
	}
	else if (obj.Status.code == 5000 || obj.Status.code == 5001)
	{
	    jwIntf += "&type=parametric";
	    window.location=jwIntf;
	}
	else
	{
	    //alert(msg);
	    GetGeocode(obj.searchterm);
	}
}

	function JSONscriptRequest(fullUrl) {
		// REST request path
		this.fullUrl = fullUrl;
		// Keep IE from caching requests
		this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
		// Get the DOM location to put the script tag
		this.headLoc = document.getElementsByTagName("head").item(0);
		// Generate a unique script tag id
		this.scriptId = 'jwJSONScript';
	}


	// buildScriptTag method
	//
	JSONscriptRequest.prototype.buildScriptTag = function () {
		var oldscript = document.getElementById('jwJSONScript');
		if (oldscript!=undefined && oldscript!=null)
			this.headLoc.removeChild(oldscript);

		// Create the script tag
		this.scriptObj = document.createElement("script");
		// Add script object attributes
		//this.scriptObj.onreadystatechange= function () {
		//  if (this.readyState == 'complete' || this.readyState == 'loaded' )
	   //}
		this.scriptObj.setAttribute("type", "text/javascript");
		this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
		this.scriptObj.setAttribute("id", this.scriptId);
	}

	// removeScriptTag method
	//
	JSONscriptRequest.prototype.removeScriptTag = function () {
		// Destroy the script tag
		this.headLoc.removeChild(this.scriptObj);
	}

	// addScriptTag method
	//
	JSONscriptRequest.prototype.addScriptTag = function () {
		// Create the script tag
		this.headLoc.appendChild(this.scriptObj);
	}

	function sendGeoRequest(location_str,isMobileOnly)
	{
	  var dt = new Date();
      var url = 'http://boingo.jiwire.com/geoJSON.jsp?callback=geoResultsAll';
	  if(isMobileOnly==true){
    	  var url = 'http://boingo.jiwire.com/geoJSON.jsp?callback=geoResultsMobileOnly';
      }
	  var addr = new String(location_str);
	  if (addr == null || addr == undefined || addr.length <= 0)
	  {
		alert("You must enter an address string to be processed.");
		return;
	  }
	  addr = addr.replace(/&/g," and ");
	  addr = addr.replace(/e.g. Hotspots/g,"Hotspots");
	  var sel = document.getElementById('engine');
	  //sel = document.getElementById('strict');
	  //var strict = sel.options[sel.selectedIndex].value;
	  url += "&strict="+false;
	  url += "&addr="+addr;
	  var obj=new JSONscriptRequest(url);
	  obj.buildScriptTag(); // Build the script tag
	  obj.addScriptTag(); // Execute (add) the script tag
	}

	function stopAutoSubmit()
	{ return false; }

	function getElement(evt){
		// equalize W3C/IE event models to get event object
		evt = (evt) ? evt : ((window.event) ? window.event : null);
		if (evt)
		{
			// equalize W3C/IE models to get event target reference
			return( (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null));
		}
		return null;
	}

	function clearField(evt)
	{
		var e = getElement(evt);
		if (e.value=="e.g. Hotspots Los Angeles within 5 mi of Wilshire & N. Fairfax Ave")
			e.value="";
		e.select();
	}

	function restoreField(evt)
	{
		var e = getElement(evt);
		if (e.value=="")
			e.value = "e.g. Hotspots Los Angeles within 5 mi of Wilshire & N. Fairfax Ave";
	}



    function enterEvent(event)
    {
	  	if (event && event.keyCode == 13){
            if( document.getElementById("mof").value=="true")
                sendGeoRequest(document.OneBoxSearch.address.value,true);
            else
                sendGeoRequest(document.OneBoxSearch.address.value,false);
	    	return false;
	  	}else{
	    	return true;
		}
    }

    function advSearchModal()
    {
		try
		{
			div = document.getElementById("AdvancedSearch");
			if (div != undefined && div != null)
				openModal("AdvancedSearch");
		}catch(e)
		{
			window.location = "search-hotspot-locations.htm?openShadowOver=AdvancedSearch#";
		}
    }
