$(document).ready(function() {
	$('a, input[type="submit"], input[type="button"], input[type="radio"], input[type="checkbox"]')
		.click(function() {
			$(this).blur();
	});

	$('a.external')
		.each(function() {
			var txt = $(this).attr("title");
			txt += (txt ? " " : "") + "[opens in a new window]";
			$(this).attr("title", txt);
		})
		.click(function() {
			var newWindow = window.open($(this).attr('href'), '_blank');
			newWindow.focus();
			return false;
		});

	$(":reset, .resetform").click(
		function() {
			$(this).parents().filter("form:first").resetForm();
			return false;
		}
	);

	$(".clearform").click(
		function() {
			$(this).parents().filter("form:first").clearForm();
			return false;
		}
	);

	$("form").find(":text, :password, :file, select, textarea").focus(
		function() {
			$(this).addClass("infocus");
			$(".inputResponse.notice").each(function() {
				if (($(this).find('.detail').text() != "") && ($(this).parent().find(".infocus:not('.error')").size() > 0)) {
					$(this).fadeIn(200);
				}
			});
			if ($(this).is(".error")) {
				//$(this).parent().find("label.error").fadeIn(200);
				$(this).parent().find("label.error").show();
				$(this).parent().find(".inputResponse.error").fadeIn(200);
			}
		}
	);

	$("form").find(":text, :password, :file, select, textarea").blur(
		function() {
			$(this).removeClass("infocus");
			$(".inputResponse.notice:visible").each(function() {
				if (!$(this).parent().find(".infocus").size()) {
					$(this).fadeOut(200);
				}
			});
			//$(this).parent().find("label.error").fadeOut(200);
			$(this).parent().find(".inputResponse").not(".show").fadeOut(200);
			$(this).parent().find("label.error").hide();
		}
	);

	$('form')
		.attr("autocomplete", "off")
		.filter(".tbvalidated")
		.submit(function() {
					if (!$(this).valid()) {
						return false;
					}
				})
		.each(function() {
			$(this).validate({
				errorPlacement: function(error, element) {
					errCtnr = element.parent(".formUnit").find("span.inputResponse.error");
					//error.appendTo( errCtnr.find('.detail') );
					errCtnr.find('.detail').html(error);
					//errCtnr.show();
				},
				showErrors: function(errorMap, errorList) {
					this.defaultShowErrors();
					$("label.error").each(function() {
						if ($(this).parent(":has('.infocus')").size() > 0) {
							$(this).parents(".formUnit").find(".inputResponse.notice").fadeOut(200);
							$(this).show();
						} else {
							$(this).hide();
						}
					});
					var frm = $(this).parents("form.tbvalidated");
					frm.find("span.checkbox").each(function() {
						if ($(this).find(".error:visible").size()) {
							$(this).addClass("error");
						} else {
							$(this).removeClass("error");
						}
					});
				},
				messages: {
					dateOfBirthStr: {date: "Please enter a valid date"},
					emailAddress: {
						required: "An email address is required for your registration",
						email: "Your email address must be in the format: name@domain.com"
					}
				},
				unhighlight: function(element, errorClass) {
					$(element).removeClass(errorClass);
					$(element).parent().find(".inputResponse.error").fadeOut();
					$(element).parent().find(".inputResponse .detail label.error").remove();
					//$(element.form).find("label[for=" + element.id + "]")
					//.removeClass(errorClass);
				},
				submitHandler: function(form) {
					if ($('.campaigns').size() > 0) {
						if ($('.campaigns input[type="checkbox"]:checked').size() == 0) {
							$(".campaigns .inputResponse.error").fadeIn(200);
							return false;
						} else {
							form.submit();
						}
					} else {
						form.submit();
					}
				}
			});
		});

	$('.campaigns input[type="checkbox"]').click(function() {
		if ($(this).is(':checked')) {
			$(this).parents('.formUnit').find(".inputResponse.error").fadeOut(200);
		}
	});
});

/* add a custom jQuery selector function to handle case-insensitive text search */
jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};

