function showDialog(id) {
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
	
    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
	
    //transition effect		
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow", 0.8);
	
	$("#" + id).css("display", "block");

	$("#" + id).css("left", (parseInt((maskWidth - parseInt($("#" + id).width())) / 2) - 25) + "px");
	
	if (id == "pleasewait") window.location.hash = "#waitbox";
	
}

function hideDialog() {
    $(".dialog").hide();
	$("#mask").hide();
}

function confirmthat(msg, on_yes, on_no, focusbutton) {
    $("#confirm_question").html(msg);
	$("#confirm_yes").click(on_yes);
	$("#confirm_no").click(on_no);
	showDialog("confirmation");
	if (focusbutton != "") $("#" + focusbutton).focus();
	window.location.hash = "#confirmbox";
}

function showMessage(msg, title, onOk) {
	$("#alerttext").html(msg);
	$("#alerttitle").html(title);
	$("#ok_btn").click(onOk);
	showDialog("alertmsg");
	window.location.hash = "#alert_box_top";
	$("#ok_btn").focus();
}

function checkfamilyform() {
	if ($("input[name='phone']").val() == "") {
		showMessage("Home Phone Number must be entered.", 
					"Error", 
					function() { 
					    $("#alertmsg").hide(); 
					    $("#mask").hide();
					    $("input[name='phone']").focus();
					});
		return false;
	}
	
	if ($("input[name='address']").val() == "") {
		showMessage("Home Address must be entered.", 
					"Error", 
					function() { 
					$("#alertmsg").hide(); 
					$("#mask").hide();
					$("input[name='address']").focus();
					});
		return false;
	}
	
	if ($("input[name='parent_nameM']").val() == "" &&
		$("input[name='parent_lastM']").val() == "" &&
		$("input[name='parent_nameF']").val() == "" &&
		$("input[name='parent_lastF']").val() == "") {
		showMessage("The first and last name for at least one of the parents must be entered.", 
					"Error", 
					function() { 
					$("#alertmsg").hide(); 
					$("#mask").hide();
					$("input[name='parent_nameF']").focus();
					});
		return false;
	}
	
	if (($("input[name='parent_nameM']").val() != "" &&
		 $("input[name='parent_lastM']").val() == "") ||
		($("input[name='parent_nameM']").val() == "" &&
		 $("input[name='parent_lastM']").val() != "")) {
		showMessage("Both first and last name must be entered.", 
					"Error", 
					function() { 
					$("#alertmsg").hide(); 
					$("#mask").hide();
					$("input[name='parent_nameM']").focus();
					});
		return false;
	}
	
	if (($("input[name='parent_nameF']").val() != "" &&
		 $("input[name='parent_lastF']").val() == "") ||
		($("input[name='parent_nameF']").val() == "" &&
		 $("input[name='parent_lastF']").val() != "")) {
		showMessage("Both first and last name must be entered.", 
					"Error", 
					function() { 
					$("#alertmsg").hide(); 
					$("#mask").hide();
					$("input[name='parent_nameF']").focus();
					});
		return false;
	}

	if ($("input[name='parent_emailF']").val() == "" &&
		$("input[name='parent_emailM']").val() == "") {
		showMessage("At least one email address must be entered.", 
					"Error", 
					function() { 
					$("#alertmsg").hide(); 
					$("#mask").hide();
					if ($("input[name='parent_nameF']").val() != "") {
					    $("input[name='parent_emailF']").focus();
					} else {
					    $("input[name='parent_emailM']").focus();
					}
					});
		return false;
	}
	
	if ($("input[name='username']").length > 0) {
		if ($("input[name='username']").val() == "") {
		    showMessage("A User Name must be entered.", 
						"Error", 
						function() { 
						$("#alertmsg").hide(); 
						$("#mask").hide();
						$("input[name='username']").focus();
						});
			return false;	
		}
		
		// make sure user name doesn't already exist
	    var userfound = false;
		
		// Make AJAX sync request
		$.ajax({ method: "GET",
			   url: "checkuserdup.php",
			   cache: false,
			   async: false,
			   data: {user: $("input[name='username']").val()},
			   dataType: "text",
			   error: function(a, b, c) {
			   },
			   complete: function() { },
			   success: function(result) {
			   if (result == $("input[name='username']").val()) userfound = true;
			   }
			   });
		
		if (userfound) {
			showMessage("The user name you entered is already is use. Please enter a different user name.", 
						"Error", 
						function() { 
						$("#alertmsg").hide(); 
						$("#mask").hide();
						$("input[name='username']").val("");
						$("input[name='username']").focus();
						});
			return false;
		}
	}
	
	if ($("input[name='password']").length > 0) {
	    if (($("#chgpwdi1").length > 0 && $("#chgpwdi1").is(":visible")) ||
			$("#chgpwdi1").length == 0) {
		
			if ($("input[name='password']").val() == "") {
				showMessage("A Password must be entered.", 
							"Error", 
							function() { 
							$("#alertmsg").hide(); 
							$("#mask").hide();
							$("input[name='password']").focus();
							});
				return false;	
			}
			
			if ($("input[name='password']").val() != $("input[name='passwordconfirm']").val()) {
				showMessage("Both passwords entered must match.", 
							"Error", 
							function() { 
							$("#alertmsg").hide(); 
							$("#mask").hide();
							$("input[name='passwordconfirm']").val("");
							$("input[name='password']").val("");
							$("input[name='password']").focus()
							});
				return false;	
			}
		}
	}
 
	
	return true;
}

function searchUser(hint) {
	$.ajax({ method: "GET",
		   url: "getfamhint.php",
		   cache: false,
		   async: false,
		   data: {hint: hint},
		   dataType: "html",
		   error: function(a, b, c) {
		   },
		   complete: function() { },
		   success: function(result) {
		   $("#searchresult").html(result);
		   
		   if (result != "") {
		       if ($("#contentbox").length > 0) {
		           $("#contentbox").html("<h3>Directory Search Results</h3><div>" + result + "</div>");
		       } else {
		           $("#searchresult").show();
		       }
		   }
		   }
		   });
}

function showFamily(n) {
    $("#showfam").val(n);
	$("#searching").submit();
}

function prepareXML(sel) {
	var xml = "<table><head>";
	
	$(sel).find("th").each(function() {
						   xml += "<lbl>" + encodeURIComponent($(this).text()) + "</lbl>";
						   });
	xml += "</head><data>";
	
	$(sel).find("tr").each(function() {
						xml += "<row>";
						   $(this).find("td").each(function() {
												   xml += "<cell>" + encodeURIComponent($(this).text()) + "</cell>";
												   });
						   xml += "</row>";
						   });
	xml += "</data></table>";
	
	return xml;
}
