 var searchid = "";

Event.observe(window,"load", function() {

	$$("#email, #zipcode").invoke("observe","focus",function(ev) {
		el = ev.element();
		if (el.value == el.defaultValue)
			el.value = "";
	});

	$$("#email, #zipcode").invoke("observe","blur",function(ev) {
		el = ev.element();
		if (el.value == "")
			el.value = el.defaultValue;
	});
			
	new Ajax.Request("http://www.livebaltimore.com/NeighborhoodSearch.cfm?action=getoptions&option=NeighborhoodTraits", { evalJSON: 'force', onComplete: function(response) {
		data = response.responseJSON;
		$$(".neighborhood_trait").each(function(ell) {
			data.OPTIONS.each(function(el) {
				op = new Option(el.TXCODEDESCRIPTION,el.IDCODEVALUE);
				ell.options[ell.options.length] = op;
			});
		});
		
		$$(".neighborhood_trait").invoke("observe","change",function(ev) {
			el = ev.element();
			$$(".neighborhood_trait").each(function(ell) {
				if (ell != el) {
					x = 0;
					while (x < ell.options.length) {
						if (ell.options[x].value == el.value)
							ell.options[x] = null;
						x++;
					}
				}
			});
		});
		
	}});
	
	Event.observe("match_form","submit",function(ev) {
		ev.stop();
		errors = "";
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!$("email").value || $("email").value == $("email").defaultValue || filter.test($("email").value) == false)
			errors += "You must enter a valid email address.\n";
		if (!$("zipcode").value || $("zipcode").value == $("zipcode").defaultValue)
			errors += "You must enter your zip code.\n";
		if (!$("agree").checked)
			errors += "You must agree to the terms listed in the Disclaimer.\n";
		
		if (errors)
			alert(errors);
		else {
			new Ajax.Request("http://www.livebaltimore.com/NeighborhoodSearch.cfm?action=createSearchID&Email=" + escape($("email").value) + "&ZipCode=" + escape($("zipcode").value), { evalJSON: 'force', onComplete: function(response) {
				searchid = response.responseJSON.NEIGHBORHOODSEARCHID;
			}});			
			$("match_step_1").hide();
			$("match_step_2").show();
		}
	});
	
	Event.observe("match_form_two","submit",function(ev) {
		ev.stop();
		traits = "";
		$$(".neighborhood_trait").each(function(el) { traits += el.value + ","; });
		
		new Ajax.Request("http://www.livebaltimore.com/NeighborhoodSearch.cfm?action=SearchByTraits&option=JSON&idNS=" + searchid + "&NeighborhoodTraits=" + traits.substr(0,traits.length-1), { evalJSON: "force", onComplete: function(response) {
			results_five = [];
			results_four = [];
			results_three = [];
			results_two = [];
			results_one = [];
			response.responseJSON.NEIGHBORHOODS.each(function(el) {
				result = {};
				hits = 0;
				hits = el.TRAITS.length;
				result.id = el.NEIGHBORHOODID;
				result.title = el.TITLE;
				result.neighborhoodurl = el.NEIGHBORHOODURL;
				if (hits == 5)
					results_five[results_five.length] = result;
				if (hits == 4)
					results_four[results_four.length] = result;
				if (hits == 3)
					results_three[results_three.length] = result;
				if (hits == 2)
					results_two[results_two.length] = result;
				if (hits == 1)
					results_one[results_one.length] = result;
			});
			
			html = "";
			
			if (results_five.length) {
				html += '<br /><br /><span class="match">5 QUALITIES IN COMMON</span><br /><br /><div><ul>';
				cc = "";
				x = 0;
				results_five.each(function(el) {
					if (el.neighborhoodurl != "") {
						strReadMore = "<br /><a href=" + "'" + el.neighborhoodurl + "' target='_blank'>" + '<img src="http://www.livebaltimore.com/Common/Images/SevaCM/match-read-more.png" alt="" /></a>'
					}
					else {
						strReadMore = ""
					}
					if ((x+1) % 4 == 0)
						html += '<li class="last">' + el.title + strReadMore + '</li>';
					else
						html += "<li>" + el.title + strReadMore + '</li>';
					x++;
					if (x % 4 == 0 && x < results_five.length) {
						html += "</ul></div>";
						if (cc == "")
							cc = "alt";
						else
							cc = "";
						html += '<div class="' + cc + '"><ul>';
					}
				});
				html += '</ul></div><br class="clear" />';
			}
			
			if (results_four.length) {
				html += '<br /><br /><span class="match">4 QUALITIES IN COMMON</span><br /><br /><div><ul>';
				cc = "";
				x = 0;
				results_four.each(function(el) {
					if (el.neighborhoodurl != "") {
						strReadMore = "<br /><a href=" + "'" + el.neighborhoodurl + "' target='_blank'>" + '<img src="http://www.livebaltimore.com/Common/Images/SevaCM/match-read-more.png" alt="" /></a>'
					}
					else {
						strReadMore = ""
					}
					if ((x+1) % 4 == 0)
						html += '<li class="last">' + el.title + strReadMore + '</li>';
					else
						html += "<li>" + el.title + strReadMore + '</li>';
					x++;
					if (x % 4 == 0 && x < results_four.length) {
						html += "</ul></div>";
						if (cc == "")
							cc = "alt";
						else
							cc = "";
						html += '<div class="' + cc + '"><ul>';
					}
				});
				html += '</ul></div><br class="clear" />';
				$("match_results").innerHTML += html;
			}
			
			if (results_three.length) {
				html += '<br /><br /><span class="match">3 QUALITIES IN COMMON</span><br /><br /><div><ul>';
				cc = "";
				x = 0;
				results_three.each(function(el) {
					if (el.neighborhoodurl != "") {
						strReadMore = "<br /><a href=" + "'" + el.neighborhoodurl + "' target='_blank'>" + '<img src="http://www.livebaltimore.com/Common/Images/SevaCM/match-read-more.png" alt="" /></a>'
					}
					else {
						strReadMore = ""
					}
					if ((x+1) % 4 == 0)
						html += '<li class="last">' + el.title + strReadMore + '</li>';
					else
						html += "<li>" + el.title + strReadMore + '</li>';
					x++;
					if (x % 4 == 0 && x < results_three.length) {
						html += "</ul></div>";
						if (cc == "")
							cc = "alt";
						else
							cc = "";
						html += '<div class="' + cc + '"><ul>';
					}
				});
				html += '</ul></div><br class="clear" />';
			}
			
			if (results_two.length) {
				html += '<br /><br /><span class="match">2 QUALITIES IN COMMON</span><br /><br /><div><ul>';
				cc = "";
				x = 0;
				results_two.each(function(el) {
					if (el.neighborhoodurl != "") {
						strReadMore = "<br /><a href=" + "'" + el.neighborhoodurl + "' target='_blank'>" + '<img src="http://www.livebaltimore.com/Common/Images/SevaCM/match-read-more.png" alt="" /></a>'
					}
					else {
						strReadMore = ""
					}
					if ((x+1) % 4 == 0)
						html += '<li class="last">' + el.title + strReadMore + '</li>';
					else
						html += "<li>" + el.title + strReadMore + '</li>';
					x++;
					if (x % 4 == 0 && x < results_two.length) {
						html += "</ul></div>";
						if (cc == "")
							cc = "alt";
						else
							cc = "";
						html += '<div class="' + cc + '"><ul>';
					}
				});
				html += '</ul></div><br class="clear" />';
			}
			
			if (results_one.length) {
				html += '<br /><br /><span class="match">1 QUALITY IN COMMON</span><br /><br /><div><ul>';
				cc = "";
				x = 0;
				results_one.each(function(el) {
					if (el.neighborhoodurl != "") {
						strReadMore = "<br /><a href=" + "'" + el.neighborhoodurl + "' target='_blank'>" + '<img src="http://www.livebaltimore.com/Common/Images/SevaCM/match-read-more.png" alt="" /></a>'
					}
					else {
						strReadMore = ""
					}
					if ((x+1) % 4 == 0)
						html += '<li class="last">' + el.title + strReadMore + '</li>';
					else
						html += "<li>" + el.title + strReadMore + '</li>';
					x++;
					if (x % 4 == 0 && x < results_one.length) {
						html += "</ul></div>";
						if (cc == "")
							cc = "alt";
						else
							cc = "";
						html += '<div class="' + cc + '"><ul>';
					}
				});
				html += '</ul></div>';
			}
			
			$("match_results").innerHTML = html;
		}});
	});

});
