// JavaScript Document
window.onload = initForms;

function initForms() {
	for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
		// validForm -> valid form can either be true or false.
		// if everything was entered properly then its true and so
		// - the browser will submit the form
	}
}

function validForm() {
	var allGood = true; // start with the assumption that everything is fine
	var allTags = document.getElementsByTagName("*");
	// * -> we are going to look at every element on the page

	for (var i=0; i<allTags.length; i++) {
	// we are going to look at every tag
		if (!validTag(allTags[i])) {
		// validTag will return true or false
		// we are going to look at the ith element of allTags and pass to the function validTag
		// if the tag is not valid it will return false and set allGood to false
		// - and we will know we have an error
			allGood = false;
		}
	}
	return allGood;

	//validTag function is inside the validForm function
	// - so that they can pass info. back and forth
	function validTag(thisTag) {
		var outClass = "";
		var allClasses = thisTag.className.split(" ");
		// split -> takes a string and turns it into an array
	
		for (var j=0; j<allClasses.length; j++) {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
		// this is where you can dynamically change the 
		// - class in the form
	
		if (outClass.indexOf("invalid") > -1) {
			// -1 -> we found it somewhere inside outClass(array)
			thisTag.focus();
			//focus will put a cursor in that field
			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
				//if its an input field we are also going to 
				// - select the value
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
				// if this class is nothing or is already set to invalid
				// - then we do a break(break means go back down to the line after 
				// the default and head right back to where you came from)
					break;
				case "reqd":
				// if this class is reqd and they entered nothing 
				// - then they have an error
					if (allGood && thisTag.value == "") {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;

				default:
					classBack += thisClass;
			}
			return classBack;
		}
		function validEmail(email) {
			var invalidChars = " /:,;";
			//set characters that are not allowed in an email address
		
			if (email == "") {
				return false;
			}
			for (var k=0; k<invalidChars.length; k++) {
			// looping through the email address using the k variable
			// - for the length of invalid chars
			// we are taking the particular bad char that we are going
			// - to look at by using charAt
			// charAt -> built in javascript function that takes a num
			// - between 0 and 4(from invalidChars), takes the character
			// out of that string(invalidChars) 
				var badChar = invalidChars.charAt(k);
				if (email.indexOf(badChar) > -1) {
					// we are looking to see if this bad character is 
					// - in the email address
					return false;
				}
			}
			var atPos = email.indexOf("@",1);
			// atPos -> position of the "@" symbol in the email
			// if there is no "1" it will start looking at char
			// - num 0
			// we want to know if there is an @ symbol after the 1st 
			// - character (the zero position character)
			if (atPos == -1) {
				return false;
			}
			if (email.indexOf("@",atPos+1) != -1) {
			// check if there is more than 1 "@" symbol
				return false;
			}
			var periodPos = email.indexOf(".",atPos);
			// is there a period somewhere from the "@" symbol
			if (periodPos == -1) {	
				return false;
			}
			if (periodPos+3 > email.length)	{
			// we check if there are t3 char before the period
				return false;
			}
			return true;
		}


	}
}

