var validdc = true;
var validchurchname = false;

function updateSelection(stage) {
	var countryVal = ($("#countrySelect")).val();
	//var nonsanetowncity = $("towncitySelect").html();
	validchurchname = false;
	switch (stage) {
		case 1:
		{
			if (countryVal == 0) {
				alert("Please contact us as to which country you wish to have included and we will try to include it very soon!");
			} else {
				//fetch all the regions in this country
				getList(stage, countryVal);
			}
		}
		break;
		case 2:
		{
			var regionVal = ($("#regionSelect")).val();
			if (regionVal == 0) {
				showAddChurch(countryVal, "", "");
			} else {
				//fetch all the regions in this country
				getList(stage, regionVal);
			}
		}
		break;
		case 3:
		{
			var towncityVal = ($("#towncitySelect")).val();
			if (towncityVal == 0) {
				var regionVal = ($("#regionSelect")).val();
				showAddChurch(countryVal, regionVal, "");
			} else {
				//fetch all the churches in this towncity
				getList(stage, towncityVal);
			}
		}
		break;
		case 4:
		{
			var churchVal = ($("#churchSelect")).val();
			if (churchVal == 0) {
				var regionVal = ($("#regionSelect")).val();
				var towncityVal = ($("#towncitySelect")).val();
				showAddChurch(countryVal, regionVal, towncityVal);
			} else {
				//fix enable save button
				validchurchname = true;
			}
		}
		break;
	};
	enableSaveBtn();	
}

function enableSaveBtn() {
	if ($("#save").length == 0) {//fix
		enableSignupBtn();	//fix
	} else {
		if (validdc && validchurchname) {
			$("#save").removeAttr("disabled");
		} else {
			$("#save").attr("disabled",true);
		}
	}
}

function getList(stage, val) {	
	$("loading").html("<img src=\"/images/loading.gif\" height=\"16\" width=\"16\" alt=\"\"/>");
	$.ajax({
	type: 'GET',
	url: '/ajax/getchurch.php',
	data:{s:stage, v:val, r:(new Date().getTime())},
			  dataType:'xml',
			  success: function(xml) {
				$(xml).find("re").each(function()
				{
					var error = $(this).find("err").text();
					if (error) {
						alert("getList error: "+error);
					} else {
						var st = parseInt($(this).find("st").text());
						if (st == 1) {
	var regionopts = "";
	$(this).find("rg").each(function() {
		var tnam = $(this).find("na").text();
		regionopts = regionopts + "<option value=\""+tnam+"\">"+tnam+" ("+$(this).find("rn").text()+")</option>";
	});	
	$("#churchRegion").html("<select id=\"regionSelect\" name=\"region\" onchange=\"updateSelection(2)\"><option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>"+regionopts+"</select>");//fix
	//reset church select if had a default church already, otherwise leave
	$("#churchSelect").html("<option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>");
	$("#towncitySelect").html("<option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>");
						} else if (st == 2) {			
		var townopts = "";
		$(this).find("cy").each(function() {
			var tnam = $(this).find("na").text();
			townopts = townopts + "<option value=\""+tnam+"\">"+tnam+" ("+$(this).find("ct").text()+")</option>";
		});
		$("#churchTownCity").html("<select id=\"towncitySelect\" name=\"towncity\" onchange=\"updateSelection(3)\"><option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>"+townopts+"</select>");//fix
		//reset church select if had a default church already, otherwise leave
		$("#churchSelect").html("<option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>");
			//$("#towncitySelect").html("<option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>");
						} else if (st == 3) {
		var churchopts = "";
		$(this).find("ch").each(function() {
			churchopts = churchopts + "<option value=\""+$(this).find("id").text()+"\">"+$(this).find("na").text()+"</option>";
		});
		$("#church").html("<select id=\"churchSelect\" name=\"churchselect\" onchange=\"updateSelection(4)\"><option value=\"-1\" selected=\"selected\">-- Select --</option><option value=\"0\">(Not found in list)</option>"+churchopts+"</select>");	
									}
								}
		  $("#loading").html("&nbsp;");
							});
						  }
						});		
}
	
function showAddChurch(countryVal, regionVal, towncityVal) {
	validdc = false;
	validchurchname = false;
	var sel = "";
	if ($("#save").length == 0) {
	} else {
		sel = sel + "<form method=\"post\" action=\"#\">";
	}
	sel = sel + "<table><tr><td>Country:</td><td>";
	if (countryVal == 0) {	
		//sel = sel + "<div id=\"churchCountry\"><select onchange=\"updateSelection(1)\" name=\"country_code\"><option value=\"ID\">ID</option><option value=\"KE\">KE</option><option value=\"UK\">UK</option><option value=\"US\">US</option><option value=\"ZA\">ZA</option></select></div>";//fix have default	
		countryVal = "US";
	} 
	//{
		sel = sel + "<input size=\"2\" maxlength=\"2\" name=\"country_code\" value=\""+countryVal+"\"/>";//fix or keep in a select drop down?
	//}
	sel = sel + "</td></tr>";
	sel = sel + "<tr><td>Region:</td><td>";	
	sel = sel + "<input size=\"\" maxlength=\"64\" name=\"region\" value=\""+regionVal+"\"/>";//fix what if region has apostrophe?
	sel = sel + "</td></tr>";	
	sel = sel + "<tr><td>Town/city:</td><td>";	
	sel = sel + "<input size=\"\" maxlength=\"64\" name=\"towncity\" value=\""+towncityVal+"\"/>";//fix what if town has apostrophe?
	sel = sel + "</td></tr>";	
	sel = sel + "<tr><td>Church name:</td><td><div id=\"church\"><input id=\"churchname\" size=\"20\" maxlength=\"128\" name=\"church_name\" onblur=\"churchnameCheck()\"/></div></td></tr>";	
	sel = sel + "<tr><td colspan=\"2\"><input id=\"detailscheck\" type=\"checkbox\" onclick=\"churchDetailsCheck()\"/> I confirm that these details are correct and that<br />this church agrees with the <a href=\"http://en.wikipedia.org/wiki/Nicene_Creed\" target=\"_blank\">Nicene Creed</a>.</td></tr></table>";//fix add doctrinal basis
	if ($("#save").length == 0) {
	} else {
		sel = sel + "<input id=\"save\" type=\"submit\" disabled=\"disabled\" value=\"Save\"/></form>";//fix make save button appear after selection?
	}
	//sel = sel + "<div id=\"saveChurch\">[save]</div></form>";//fix make save button appear after selection?
	$("#churchSel").html(sel);
}

function churchnameCheck() {
	if ($('#churchname').val()) {			
		validchurchname = true;
	} else {
		validchurchname = false;//fix feedback why not?		
	}
	enableSaveBtn();
}
	
function churchDetailsCheck() {
	if ($('#detailscheck').attr("checked")) {
		validdc = true;
	} else {
		validdc = false;
	}
	enableSaveBtn();
}
	
function showChurchSel() {
	var sel = "<form action=\"#\" method=\"post\"><table><tr><td>Country:</td><td><div id=\"churchCountry\"><select id=\"countrySelect\" onchange=\"updateSelection(1)\" name=\"country_code\"><option value=\"0\">(Not found in list)</option>"+countryopts+"</select></div></td></tr>";
	sel = sel + "<tr><td>Region:</td><td><div id=\"churchRegion\"><select id=\"regionSelect\" onchange=\"updateSelection(2)\" name=\"region\"><option value=\"0\">(Not found in list)</option>"+regionopts+"</select></div></td></tr>";
	sel = sel + "<tr><td>Town/city:</td><td><div id=\"churchTownCity\"><select id=\"towncitySelect\" onchange=\"updateSelection(3)\" name=\"towncity\"><option value=\"0\">(Not found in list)</option>"+townopts+"</select></div></td></tr>";
	sel = sel + "<tr><td>Church name:</td><td><div id=\"church\"><select id=\"churchSelect\" name=\"churchselect\" onchange=\"updateSelection(4)\" name=\"churchselect\"><option value=\"0\">(Not found in list)</option>"+churchopts+"</select></div></td></tr>";
	sel = sel + "<input id=\"save\" type=\"submit\" disabled=\"disabled\" value=\"Save\"/></form>";
	$("#churchSel").html(sel);
}
