/* 
   automatically add class required to all fields listed in the
   hidden input required_fields

   additional classes of numeric, characters, email, zipcode
   and phone can be added manually

   <script type="text/javascript" src="/jslib/jquery.js"></script>
   <script type="text/javascript" src="/jslib/bol.validate.js"></script>

   <form method="post" onSubmit="return validateForm(this);" action="xyz">

   For a mailform_db3 form the fields listed in required_field will 
   automatically have the class required added.  For other forms
   simply add required as a class

   <input name="fname" class="aclass required">

   You can also add the special classes for additional format checking

   <input name="fname" class="aclass required email">

*/

var error_message = "";

$(document).ready( function() {

	var rflds = $("input:hidden[name=required_fields]").val();
	var thefields = rflds.split(",");
	for(var i = 0; i < thefields.length; i++) {
		// alert("field " + i + " is " + thefields[i]);
		$("input[name=" + thefields[i] + "]").addClass("required");
		$("select[name=" + thefields[i] + "]").addClass("required");
		$("checkbox[name=" + thefields[i] + "]").addClass("required");
		$("radio[name=" + thefields[i] + "]").addClass("required");
	}
	$("input, select, textarea", this).each( function() {
		if ($(this).attr("class")) {
			$(this).blur( function() { validateField(this, true); } );
		}

	});
});

function validateField(field, doalert) {
	var error = false;
	var myname = $(field).attr("name");

	if(doalert) {
		error_message = "";
	}
	// required fields
	if ($(field).attr("class").indexOf("required") != -1) {
		// alert("checking " + myname);
		if (!$(field).val().length) {
			error = true;
			error_message += myname + " is required\n";
		}
		if ($(field).attr("class").indexOf("checkbox") != -1) {
			// have to use straight JS to check checkbox setting
			if (field.checked == false) {
				error = true;
				error_message += myname + " is required\n";
			}
		}
	}
	// numeric fields
	if ($(field).attr("class").indexOf("numeric") != -1) {
		if (!/^[0-9]*$/.test($(field).val())) {
			error = true;
			error_message += myname + " is invalid\n";
		}
	}
	// characters (letters)
	if ($(field).attr("class").indexOf("character") != -1) {
		if (!/^[a-zA-ZöÖäÄåÅ]*$/.test($(field).val())) {
			error = true;
			error_message += myname + " is invalid\n";
		}
	}
	// emails
	if ($(field).attr("class").indexOf("email") != -1) {
		if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val())) {
			error = true;
			error_message += myname + " is invalid\n";
		}
	}
	if ($(field).attr("class").indexOf("zip") != -1) {
		if (! validZip($(field).val())) {
			error = true;
			error_message += myname + " is invalid\n";
		}
	}
	if(error && doalert) {
		$(field).focus();
		alert(error_message);
		// field.focus();
	}
		
	return ! error;
}

function validateForm(form) {

	var validationError = false;
	error_message = "";
	// check each input field, if it has a class test it
	$("input, select, checkbox, radio, textarea", form).each( function() {
		// alert("validate " + this);
		if ($(this).attr("class")) {
			if (!validateField(this, false)) {
				validationError = true;
			}
		}
	});
	if(validationError) {
		alert("Your form has the following errors:\n\n" + error_message);
		return false;
	}
	else {
		mySubmit(form);
		return true;
	}
}

function validZip(zip) {
	if (zip.match(/^[0-9]{5}$/)) {
		return true;
	}
	if (zip.match(/^[0-9]{5}.[0-9]{4}$/)) {
		return true;
	}
	zip=zip.toUpperCase();
	if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
		return true;
	}
	if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
		return true;
	}
	return false;
} 

