var MAX_DUMP_DEPTH = 10;
/**
 * For debugging, dump object (property-value pairs)
 */
function dumpObj(obj, name, indent, depth) {
    if (depth > MAX_DUMP_DEPTH) {
        return indent + name + ": <Maximum Depth Reached>\n";
    }

    if (typeof obj == "object") {
        var child = null;
        var output = indent + name + "\n";
        indent += "\t";

        for (var item in obj)
        {
            try {
                child = obj[item];
            } catch (e) {
                child = "<Unable to Evaluate>";
            }

            if (typeof child == "object") {
                output += dumpObj(child, item, indent, depth + 1);
            } else if (typeof chile != 'function') {
                output += indent + item + ": " + child + "\n";
            }
        }
        return output;
    } else {
        return obj;
    }
}

/**
 * Show error message in error div.
 */
function showLocationError(msg) {
    var div = $('location_list_error');
    div.update(msg);
    div.show();
}

/**
 * Called when user selects a location among the many locations
 * matched by Google.
 */
function selectLocation(a) {
    setLocation(a.place);
    $('location_list_actual').update('');
}

function fixGPlaceAccuracy(gplace) {
    var address = gplace.AddressDetails;
    if (address.Accuracy < 4 && address.Accuracy > 1) {
        if (address.Country.AdministrativeArea.SubAdministrativeArea) {
            if (address.Country.AdministrativeArea.SubAdministrativeArea.Locality) {
                address.Accuracy = 4;
            }
        }
    }
}

/**
 * Extract city name. Google's return value is not very regular,
 * many exceptional cases that must be handled.
 */
function extractCityName(gplace) {
    var city = null;
    var admin = gplace.AddressDetails.Country.AdministrativeArea;

    if (admin) {
        if (admin.Locality) {
            city = admin.Locality.LocalityName; 
        } else if (admin.SubAdministrativeArea) {
            var locality = admin.SubAdministrativeArea.Locality;
            if (locality.LocalityName) {
                city = locality.LocalityName;
            } else if (admin.SubAdministrativeArea.SubAdministrativeAreaName) {
                city = admin.SubAdministrativeArea.SubAdministrativeAreaName;
            } else if (locality.DependentLocality) {
                city = locality.DependentLocality.DependentLocalityName;
            }
        } 
    } else {
        city = address.Country.Locality.LocalityName;
    }
    return city;
}
