var zipcodeProximity = {
	resultsEl: null,
	statusEl: null,

	init: function() {
		this.resultsEl = $("results");
		this.statusEl = $("status");
	},

	trySubmit: function() {
		var checkFields = MMDeveloperPortal.checkForEmptyFields(["zipcode"]);

		if (checkFields[0] === false) {
			alert(checkFields[1]);
		}
		else {
			var formHash = new Hash();
			formHash.set("zipcode", $("zipcode").value);
			formHash.set("proximity", $("proximity").value);
			formHash.set("isAjax", "true");
			var tableBody = $("zipcodeSearchResultsTableBody");
			$w("sourceZipcode sourceCity sourceState sourceLatitude sourceLongitude").each(function(item) {
				$(item).update("&nbsp;");
			});

			$$("#zipcodeSearchResultsTableBody tr.zipcodeSearch_Result").each(function(item) {
				item.remove();
			});

			new Ajax.Request("http://www.mechanicmatt.com/component,programming/task,zipcode/", {
							parameters: formHash,
							evalJSON: true,
							onCreate: function(transport) {
								this.statusEl.innerHTML = "Processing...";
							}.bind(this),
							onSuccess: function(transport) {
								this.statusEl.innerHTML = "Done!";

								$("sourceZipcode").update(transport.responseJSON.source.zipCode);
								$("sourceCity").update(transport.responseJSON.source.city);
								$("sourceState").update(transport.responseJSON.source.state + "(" + transport.responseJSON.source.stateCode + ")");
								$("sourceLatitude").update(transport.responseJSON.source.latitude);
								$("sourceLongitude").update(transport.responseJSON.source.longitude);

								transport.responseJSON.results.each(function(item) {
										var row = new Element("tr", {"className": "zipcodeSearch_Result"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.zipCode);
										row.insert(cell, {position: "after"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.city);
										row.insert(cell, {position: "after"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.state + "(" + item.stateCode + ")");
										row.insert(cell, {position: "after"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.latitude);
										row.insert(cell, {position: "after"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.longitude);
										row.insert(cell, {position: "after"});

										var cell = new Element("td", {"className": "borderRight"}).update(item.distance);
										row.insert(cell, {position: "after"});

										tableBody.insert(row, {position: "after"});

								});
							}.bind(this)
					});
		}
	}
};