var SAMPLE_POST_KVP = HOST_URL + '/geocoding/v1/reverse?key=YOUR_KEY_HERE&lat=40.0755&lng=-76.329999&callback=renderReverse';
var SAMPLE_POST_JSON = HOST_URL + '/geocoding/v1/reverse?key=YOUR_KEY_HERE&json={location:{latLng:{lat:40.0755,lng:-76.329999}}}&callback=renderReverse';
var SAMPLE_POST_XML = HOST_URL + '/geocoding/v1/reverse?key=YOUR_KEY_HERE&xml=<reverse><location><latLng><lat>40.0755</lat><lng>-76.329999</lng></latLng></location></reverse>&callback=renderReverse';
var safe = '';

function showReverseURL() {
	var reverseFormat = document.getElementById('reverseFormat').value;
	switch (reverseFormat) {
		case "kvp":
			safe = SAMPLE_POST_KVP;
			break;
		case "json":
			safe = SAMPLE_POST_JSON;
			break;
		case "xml":
			safe = SAMPLE_POST_XML;
			break;
	}
    document.getElementById('reverseSampleUrl').innerHTML = safe.replace(/</g, '&lt;').replace(/>/g, '&gt;');;
};

function doReverse() {	
	document.getElementById('reverseNarrative').innerHTML = '';
	var script = document.createElement('script');
    script.type = 'text/javascript';
    showReverseURL();
    var newURL = safe.replace('YOUR_KEY_HERE', APP_KEY);
    script.src = newURL;
    document.body.appendChild(script);
};

function renderReverse(response) {
    var html = '';
    var i = 0;
    var j = 0;
	
	html = "<p>Location: ";
	var location = response.results[0].locations[0];
	html += location.street + ", " + location.adminArea5 + ", " + location.adminArea4 + ", " + location.adminArea3 + ", " + location.adminArea1;
  	html += "</p>";
    
    document.getElementById('reverseNarrative').innerHTML = html;
}
