document.write('<div id="partyexpress_div"></div>');
$(function() {
	$("#partyexpress_div").load("/party-contact.php?ajax=1&formtag=1", function() {
		// AJAX forms.
		ajaxify();
		displayErrors();
	});
});






/**
 * Turns forms with special "ajax" class into AJAX forms.
 * Note: Requires compatible PHP code on server-side in order to
 * receive and properly display HTML based on passed "ajax" variable.
 */
var ajaxconfig = {};
function ajaxify() {
	$("form.ajax").submit(function(){
		// Get form properties.
		action = $(this).attr("action");
		if (action.indexOf("#") != -1) {
			// Parse out trailing hash.
			action = action.substring(0, action.indexOf("#"))
		}
		method = $(this).attr("method").toUpperCase();
		if (method != "GET" && method != "POST") method = "POST";
		formid = $(this).attr("id");
		if (formid == "") {
			// Dynamically assign ID.
			formid = "form" + parseInt(Math.random() * 9999);
			$(this).attr("id", formid);
		}
		
		// Add "ajax" field to form and serialize data.
		$(this).append("<input type=\"hidden\" name=\"ajax\" value=\"1\" />");
		data = $(this).serialize();
		
		// Disable form fields to show it is processing.
		$("#" + formid + " input").attr("disabled", "disabled");
		$("#" + formid + " textarea").attr("disabled", "disabled");
		
		// Configure AJAX call.
		ajaxconfig = {
			type: method,
			url: action,
			data: data,
			success: function(data, textStatus, XMLHttpRequest) {
				// Update form contents and animate error messages, if applicable.
				$("#" + this.formid).html(data);
				if (typeof(partyexpress_callback) != "undefined") partyexpress_callback();
				displayErrors();
			},
			formid: formid
		};
		
		// Give error message time to scroll up before submitting AJAX call.
		if ($(".errmsg").length > 0) {
			$(".errmsg").slideUp(200, function() {
				ajaxify_submit();
			});
		} else {
			ajaxify_submit();
		}
		
		// Prevent form from submitting.
		return false;
	});
	if (typeof(partyexpress_callback) != "undefined") partyexpress_callback();
}

// AJAX submit callback.
function ajaxify_submit() {
	$.ajax(ajaxconfig);
}


/**
 * Animates error messages.
 */
function displayErrors() {
	$(".errmsg").css({
		"display": "none"
	});
	$(".errmsg").slideDown(500);
}

