var KD = {
	formSubmit: function() {
		new Ajax.Request("/clickability.py/keyword_density", {
			method:"post",
			postBody:"url="+escape($F("url")),
			onSuccess: KD.handleResponse
		})
	},
	handleResponse: function(t) {
		data = eval('('+t.responseText+')')
		Element.show("results")
		Element.infanticide("results_cont")
		cont = $("results_cont")
		table = Builder.node("table", {"class":"kd_information"}, [
			Builder.node("caption", "Text information"),
			Builder.node("tbody", [
				Builder.node("tr", [
					Builder.node("th", "Title:"),
					Builder.node("td", data.title)
				]),
				Builder.node("tr", [
					Builder.node("th", "Words found in headings:"),
					Builder.node("td", data.tagdata.headings)
				]),
				Builder.node("tr", [
					Builder.node("th", "Words found in bold:"),
					Builder.node("td", data.tagdata.bold)
				]),
				Builder.node("tr", [
					Builder.node("th", "Words found in italics:"),
					Builder.node("td", data.tagdata.italics)
				]),
				Builder.node("tr", [
					Builder.node("th", "Internal links:"),
					Builder.node("td", data.intlinks)
				]),
				Builder.node("tr", [
					Builder.node("th", "External links:"),
					Builder.node("td", data.extlinks)
				]),
				Builder.node("tr", [
					Builder.node("th", "External/Internal link ratio:"),
					Builder.node("td", Math.round(data.extlinks/data.intlinks*100*100)/100+"%")
				]),
				Builder.node("tr", [
					Builder.node("th", "Linked external domains:"),
					Builder.node("td", [
						Builder.node("ul", function() {
							l = []
							data.extdomains.each(function(domain) {
								if (domain)
									l.push(Builder.node("li", [
										Builder.node("a", {"href":"http://"+domain+"/"}, domain)
									]))
							})
							return l
						}())
					])
				])
			])
		])
		cont.appendChild(table)
		for (n = 2; n <= 5; n++) {
			worddata = data['worddata'+n]
			if (!worddata.length)
				continue
			table = Builder.node("table", [
				Builder.node("caption", n+"-word phrases"),
				Builder.node("thead", [
					Builder.node("tr", [
						Builder.node("th", "Phrase"),
						Builder.node("th", "Occurances"),
						Builder.node("th", "Percentage")
					])
				]),
				Builder.node("tbody")
			])
			cont.appendChild(table)
			tbody = table.lastChild

			worddata.each(function(info) {
				tbody.appendChild(Builder.node("tr", [
					Builder.node("td", info[0]),
					Builder.node("td", info[1]),
					Builder.node("td", info[2]+"%")
				]))
			})
		}
	}
}
