// JavaScript Document: form checking
window.onload = function() { // on submit
checkForms();
var inputBox = document.getElementsByTagName('input');
for (var i = 0; i < inputBox.length; i++) {
	inputBox[i].onkeydown = checkForms;
	inputBox[i].onkeyup = checkForms;} }

function checkForms() {// function 1
var forms = document.getElementsByTagName('form');
var formsLength = forms.length; 
	for (var i = 0; i < formsLength; i++) { // for 1
	
	var inputs = forms[i].getElementsByTagName('input'); 
	var inputsLength = inputs.length; 
		for (var x = 0; x < inputsLength; x++) { // for 2
		var types = inputs[x].getAttribute('class');
		if(types) {
		var type = types.split(' ');
			for(var z = 0; z < type.length; z++) { // for 3
			var strng = inputs[x].value;
				// if this is an email
				if(type[z] == 'email') { // if 1
				function checkEmail(strng) {
				var error="";
					if (strng == "") { // if 2
					inputs[x].setAttribute('class', type[z] + ' error');
					error = "You didn't enter an email address.\n";
					return false;
					} // if 2 end
				
				var emailFilter=/^.+@.+\..{2,3}$/;
					if (!(emailFilter.test(strng))) { // if/else 1
					inputs[x].setAttribute('class', type[z] + ' error');
					error = "Please enter a valid email address.\n";
					return false;
					} 
					else {
						var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
						if (strng.match(illegalChars)) { // if/else 2
						inputs[x].setAttribute('class', type[z] + ' error');
						error = "The email address contains illegal characters.\n";
						return false;
						}
						else {
						inputs[x].setAttribute('class', type[z] + ' good'); }
						} // if/else 2 end
					return;
					}	// if/else 1 end
				checkEmail(strng)
				} // if 1 end
				
				// if this is a phone number
				if(type[z] == 'phone') { // if 3
				
				function checkPhone(strng) {
				var error = "";
				if (strng == "") {
					inputs[x].setAttribute('class', type[z] + ' error');
				   error = "You didn't enter a phone number.\n";
				   return false;
				}
				
				var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
					if (isNaN(parseInt(stripped))) {
						inputs[x].setAttribute('class', type[z] + ' error');
					   error = "The phone number contains illegal characters.";
					   return false;
					}
					if (!(stripped.length == 10 || stripped.length == 11)) {
					error = "The phone number is the wrong length. Make sure you included an area code.\n";
					inputs[x].setAttribute('class', type[z] + ' error');
					return false;
					}
					inputs[x].setAttribute('class', type[z] + ' good');
						return;
				}
				checkPhone(strng)				
				} // if 3 end
				
				// if field is required
				if(type[z] == 'required') { // if 4
				
				function checkField(strng) {
				var error = "";
				if (strng == "") {
					inputs[x].setAttribute('class', type[z] + ' error');
				   error = "You didn't enter a value.\n";
				   return false;
				}
				else {
					inputs[x].setAttribute('class', type[z] + ' good');
						return;
				} }
				checkField(strng)				
				} // if 4 end
				
				// if this is a zip
				if(type[z] == 'zip') { // if 5
				
				function checkZip(strng) {
				var error = "";
				if (strng == "") {
					inputs[x].setAttribute('class', type[z] + ' error');
				   error = "You didn't enter a zip code.\n";
				   return false;
				}
				
				var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
					if (isNaN(parseInt(stripped))) {
						inputs[x].setAttribute('class', type[z] + ' error');
					   error = "The zip code contains illegal characters.";
					   return false;
					}
					if (!(stripped.length == 5 || stripped.length == 9)) {
					error = "The zip is the wrong length. Make sure you included an area code.\n";
					inputs[x].setAttribute('class', type[z] + ' error');
					return false;
					}
					inputs[x].setAttribute('class', type[z] + ' good');
						return;
				}
				checkZip(strng)				
				} // if 5 end
				
			}
			} // end for 3
		} // end for 2
	} // end for 1
} // end function 1
