// JavaScript Form Functions

// ################################
// DEPRECATED, use /include/form.js
// ################################

// by james <at> bandit.co.nz
function doValidate(what) {
	if(!what) what = document.forms[0];
		$(what).find(".required").each(function() {
		// make sure this element isn't disabled...
		if($(this).is(":enabled")) {
			if($(this).attr("type")=="checkbox"&&!$(this).is(":checked")) $(this).parent().addClass("e");
			//else if($(this).attr("name").indexOf("email")!=-1&&$(this).val().match(/[\w\-_\.]+@[\w\-]+\.[\w\-\.]{2,}/i)==null) $(this).addClass("e");
			else if(jQuery.trim($(this).val())=="") $(this).parent().addClass("e");
			else $(this).parent().removeClass("e");
		}
		else $(this).parent().removeClass("e");
	});
	if($(what).find(".e").length>0) {
		alert("Please complete all required fields!\n(These are now highlighted in red)");
		$(what).find(".e").focus();
		return false;
	}
	return true;
}

// hook forms
// by james <at> bandit.co.nz
$(document).ready(function() {
	// hook all non ajax forms
	$("form").not("form[action$=ajax]").each(function() {
		$(this).submit(function() {
			return doValidate(this);
		});
	});
	// hook the ajax forms and prep for handling
	$("form[action$=ajax]").each(function() {
		$(this).submit(function() {
			if(doValidate(this)) {
				// run any custom validations
				if(typeof customValidate == "function") { if(customValidate(this)==false) { return false; } }

				// make ajax request
				$.ajax({
					type: $(this).attr("method"),
					url: $(this).attr("action")+":js",
					data: $(this).serialize(),
					dataType: "json",
					success: eval($(this).attr("name"))
				});
			}
			return false;
		});
	});
	// hook all checkboxes
	$("p.checkbox span").click(function() {
		$(this).parent().children("input[type='checkbox']").click();
	}).css("cursor","pointer");
});
