﻿function compareNumbers(a, b) {
    return a["cvalue"] - b["cvalue"];
}
function showGeoLocationError(err) {
    //1 = denied, 2 = unavailable 3 = timeout, 4 = other
    if (err.code == 1) {
        $('#addresserror').text("To continue looking for paycenters near you, please allow your browser to share your location.");
    }
    else if (err.code == 3) {
        $('#addresserror').text("Timeout while retreiving your location.");
    }
    else {
        $('#addresserror').text("Unable to retreive your location.");
    }
}
function isInBounds(lat, lng, bb) {
    if (bb.EastExtreme <= lng && bb.WestExtreme >= lng && bb.NorthExtreme >= lat && bb.SouthExtreme <= lat) {
        return true;
    }
    else {
        return false;
    }
}
function performGeoSpatialQuery(cent, rad) {
    //check if rad was defined if not use the default 10 value
    rad === undefined ? boundingbox = new BoundingBox(cent, 10) : boundingbox = new BoundingBox(cent, rad);
    var returnarray = new Array();
    //we can assume nodes is not null
    for (var i = 0; i < nodes.length; i++) {
        if (isInBounds(nodes[i]["latitude"], nodes[i]["longitude"], boundingbox)) {
            //we add an attribute and load it to our results array.
            CalculateResult(nodes[i], cent);
            returnarray.push(nodes[i]);
        }
    }
    pushEntities(returnarray);
    placeEntity(cent);
    var sortedarray = returnarray.sort(compareNumbers);
    return sortedarray;
}
function performGeoCoding(pos, rad) {
    var pointlat;
    var pointlong;
    var fullurl;
    var boundingbox;
    //We're performing a parameterized search
    //First check and see if its a zip code,
    if (isNaN(pos)) {
        exturl = "/" + pos + "?key=" + cred;
    }
    else {
        exturl = "?postalCode=" + pos + "&key=" + cred;
    }
    var exturl;
    //this doesn't work.
    var jsonresult;
    fullurl = exturl;
    //This should make the request to Bing that will return a location.
    var response = $.ajax({
        url: bprequesturl,
        type: "POST",
        async: false,
        datatype: 'json',
        success: function (data) { jsonresult = data },
        error: reportError,
        data: {qstring: fullurl}
    });
    pointlat = jsonresult.resourceSets[0].resources[0].point.coordinates[0];
    pointlong = jsonresult.resourceSets[0].resources[0].point.coordinates[1];
    var centerpoint = new Microsoft.Maps.Location(pointlat, pointlong);
    return performGeoSpatialQuery(centerpoint, rad);
}
