function Validate (form) {
	var req_fields = $(form).find(".required");
	var valid = true;

	$(form).find("input,select,textarea").removeClass('left_blank');
	
	for (i=0; i<req_fields.length; i++) {
		var field_name = $(req_fields[i]).attr('name');
		
		if ($(req_fields[i]).val() == '') {
			$(req_fields[i]).addClass('left_blank');
			valid = false;
		}
	}
	
	if (valid == false) {
		DisplayError("You left a required field blank");
	}
	
	return valid;
}

function DisplayError (text) {
	if (text) $("#error").html(text);
	$("#error").animate({ opacity: 1.0 }, 0).slideDown(200).animate({ opacity: 1.0 });
}

function DisplayMessage (text) {
	if (text) $("#error").html(text);
	$("#message").animate({ opacity: 1.0 }, 0).slideDown(200).animate({ opacity: 1.0 }).animate({ opacity: 0 }, 1000).slideUp();
}

function ViewDialog (title,id) {
	CreateDialog(title,600);
	$("#dialog").load("in/dialog.php?id="+id, function() {
		ShowDialog();
	}).dialog("open");
}

function CreateDialog (title, width) {
	ResetDialog();
	$("#dialog").dialog( { modal: true, title: title, autoOpen: false, width: width, height: $(window).height()-200 } );
}

function ResetDialog () {
	if ($("#dialog").length == 0) $("body").append('<div id="dialog"><div class="spinner">Loading...</div></div>');
	$("#dialog").html('');
	$("#dialog").dialog('destroy');
}

function ShowDialog() {
	var content_height = $("#dialog div").height();
	var max_height = $(window).height() - 200;
	var height = (content_height > max_height) ? max_height : content_height+60;
	if (height < max_height) { 
		$("#dialog").dialog("option","height",height);
	}
}