$(function() {

	$("#submitZipBtn").click(function(event) {
		event.preventDefault();
		$("#storeList").html("<span>Loading...</span>");
		
		$.post( // /test-stores.php
			"/scripts/php/form_handler.php", { action:"get_stores", zip:$("#zipCodeTxt").val(), keyid:"phuCuvexazezuyUfRaX5w8am" }, 
			function(storesJSON) {
			  $("#storeList").html(storesJSON);
				/*$("#storeList").html("");
				for (var i=0; i < storesJSON.length; i++) {
					$("#storeList").append(
						'<div class="store">' +
							'<span class="name">' + storesJSON[i].location + '</span>' +
							'<span class="address">' + storesJSON[i].address + '</span>' +
							'<span class="city">' + storesJSON[i].city + ', ' + storesJSON[i].state + ' ' + storesJSON[i].zip + '</span>' +
							'<span class="phone">' + storesJSON[i].phone + '</span>' +
							'<span class="distance">Distance: ' + Math.round(storesJSON[i].dist) + ' miles</span>' +
						'</div>'
					);
				}*/
			},
			"html"
		);
	});
	
	// generic ajax error handler
	$("#storeList").ajaxError(function(event, request, settings){
		alert("There was an unexpected error. We apologize for this inconvenience!");
	});	
	
	$("#zipCodeTxt").keydown(function(event) {
		if (event.keyCode == 13) {
			event.preventDefault();
			$("#submitZipBtn").click();
		}
	});
});

