



// FORM VALIDATION

function validateForm(formObj) { // Hello, I'll be your form validating script today.

var formElements = formObj.elements;



// Now I'll set a bunch of boolean variables that will help us validate your form.

var canSubmit = true; // This will ultimately decide if the form gets submitted or not, and is dependant on the other vars to do so.

var emailValid = true; // This checks if the user's email address is valid (string@string.string).

var fieldsComplete = true; // This checks that all the required text fields are not blank.

var anyChecked = true; // This makes sure all required checkbox/radio box arrays are valid.



// These arrays are used to validate the check/radio boxes.

var rcBoxes = new Array();

rcBoxes[0] = new Array();

rcBoxes[1] = new Array();

var rcArrays = new Array();

rcArrays[0] = new Array;

rcArrays[1] = new Array;



// Getting down to business now, we'll loop through all the elements of your form

for (i=0;i<formElements.length;i++) {

// We'll start with the text fields - making sure all the required ones are not blank.

	if (formElements[i].type == 'text' ||  formElements[i].type == 'textarea') {

		if (formElements[i].id.substring((formElements[i].id.length -4),(formElements[i].id.length)) == '_req' && (formElements[i].value == '' || formElements[i].value == 'undefined')) {

		formElements[i].className += ' highlight'; // Highlights a required field left blank.

		fieldsComplete = false;

			}

// Now we'll check if the user has provided you with a valid email address...

		if (formElements[i].id == 'email_req') {

			if (formElements[i].value.search('.+@.+\\.[a-z]+') < 0) {

				formElements[i].className += ' highlight'; // Highlights the email field if blank or invalid.

				emailValid = false;

				}

			else { // Removes the highlight if email is valid.

				formElements[i].className = formElements[i].className.replace(' highlight','');

				}

			}

		else if (formElements[i].id.substring((formElements[i].id.length -4),(formElements[i].id.length)) == '_req' && formElements[i].value != '') {

			// Removes the highlight if fields are properly filled.

			formElements[i].className = formElements[i].className.replace(' highlight','');

			}

		}



// Finally, we go through the checkboxes and radio boxes, making sure each array of required check/radio boxes has at least one element checked.

	else if (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') {

		if (formElements[i].id.substring((formElements[i].id.length -4),(formElements[i].id.length)) == '_req') {

			// Adds all checkboxes to an array which we'll be looping through later.

			rcBoxes[0].push(formElements[i].id);

			rcBoxes[1].push(formElements[i].checked);

			}

		}

	}



// After loading all the check/radio boxes into a separate array, we go through them again.

if (rcBoxes[0].length > 0) {

	for (i=0;i<rcBoxes[0].length;i++) {

		// The first thing we'll do is separate them into little arrays where they have a common name attribute.

		targetCheck = formObj[rcBoxes[0][i]];

		i += targetCheck.length;

		// Then we'll loop through that array.

		for (j=0;j<targetCheck.length;j++) {

			if (targetCheck[j].checked) {

				// If we find a checked box within a required array, add the element's name to rcArrays[0] and add true to rcArrays[1].

				rcArrays[1].push(true);

				rcArrays[0].push(targetCheck[j].id);

				// That's all we need, so we can break out of the loop.

				break;

				}

			else if (j == (targetCheck.length -1)) {

				// If nothing is checked, we only add the name of the element to rcArrays[0].

				rcArrays[0].push(targetCheck[j].id);

				}

			}

		}

	// So, with all the info we gathered up there, we'll check the length of rcArrays[0] against rcArrays[1].

	// If all the required check/radio arrays have at least one box checked, they're valid and the lengths should match.

	if (rcArrays[0].length != rcArrays[1].length) {

		// If the lengths of rcArrays[0] and rcArrays[1] don't match, it means something that should've been checked was left unchecked.

		anyChecked = false;

		}

	}



// Remember our boolean vars? This is where we check them and give the user an appropriate response.

if (!fieldsComplete || !anyChecked) {

	// This message will display if a required textfield was left blank or a checkbox was left un-checked.

	alert('Udfyld venligst alle felter markeret med * inden afsendelse!');

	canSubmit = false; // Don't let the form submit.

	}

else if (!emailValid) {

	// This message will display if the user didn't enter a valid email address.

	alert('Indtast venligst en gyldig E-Mail adresse i det markerede felt!');

	canSubmit = false; // Don't let the form submit.

	}

if (fieldsComplete && emailValid && anyChecked) {

	// If everything has been properly filled and a valid email address has been entered, the form can submit!

	canSubmit = true;

	}

return canSubmit;

}


