
function validate(theForm,errordiv) {
	
	$errdiv = errordiv;
	$errmsg = "";
	$wait = false;

	$(theForm).find("input,select,textarea,object").each(function() {

		if ( this.tagName.toLowerCase()=="object" ) {
			
			$(this).find("span,input,textarea,select").removeClass("error");
			var req = $(this).attr("required");
			if (req) {
				var inc = 0;
				$(this).find("[type='radio'],[type='checkbox']").each(function(){ if ($(this).attr("checked")) { inc ++; } });
				$(this).find("[type='text'],[type='password'],textarea").each(function(){ if ($(this).val()!="") { inc ++; } });
				$(this).find("select").each(function(){ if($(this).find("option:first").attr("selected")==false) { inc ++; } });
				if (inc == 0) {
					$errmsg += "<li>" + req + "</li>";
					$(this).find("span,input,textarea,select").addClass("error");
				}
			}
		}
	
		var req = $(this).attr("required");
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		$(this).removeClass("error");

		if (req) {
			
			var type = $(this).attr("type");
			var value = $(this).attr("value");
			var check = $(this).attr("checked");
			var index = $(this).get(0).selectedIndex;

			if (((type=="text"||type=="password"||type=="hidden"||type=="textarea")&&value=="")||((type="select-one")&&index==0)||((type=="radio"||type=="checkbox")&&check==false)) {
				if ($errmsg.search(req) == -1) { $errmsg += "<li>" + req + "</li>"; }
				$(this).addClass("error");
			}
		}

		if ($(this).attr("name") == "strEmail" && reg.test($(this).val()) == false && $(this).val() != "") {
			err="The email address contains illegal characters";
			if ($errmsg.search(err) == -1) {
				$errmsg += "<li>" + err + "</li>";
				$(this).addClass("error");
			}
		}

		if ($(this).attr("name") == "strEmailConfirm" && $(this).val() != $(theForm).find("[name='strEmail']").val() && $(this).val() != "") {
			err="The email confirmation is incorrect";
			if ($errmsg.search(err) == -1) {
				$errmsg += "<li>" + err + "</li>";
				$(this).addClass("error");
			}
		}

		if ($(this).attr("name") == "strPassword" && $(this).val().length < 6 && $(this).val() != "") {
			err="Your password must be over 6 characters in length";
			if ($errmsg.search(err) == -1) {
				$errmsg += "<li>" + err + "</li>";
				$(this).addClass("error");
			}
		}

		if ($(this).attr("name") == "strPasswordConfirm" && $(this).val() != $(theForm).find("[name='strPassword']").val() && $(this).val() != "") {
			err="Your password confirmation is incorrect";
			if ($errmsg.search(err) == -1) {
				$errmsg += "<li>" + err + "</li>";
				$(this).addClass("error");
			}
		}

	});

	if ($errmsg) {
		$("#"+$errdiv).html("<h3>You Have Missed Some Required Fields/Steps</h3><ul>" + $errmsg + "</ul>").fadeIn(500);
		window.scrollTo(0,0);
		return false;
	} else {
		return true;
	}
}

