window.onload = startUp;

function startUp() {
	hoverSubmit();
	onFocusForm();
}

function hoverSubmit() {
	/*Gathers all the INPUT tags in the document.*/
	var formInputs = document.getElementsByTagName("input");
	/*Loops through all of the INPUT tags.*/
	for (n=0; n < formInputs.length; n++) {
		/*If the INPUT has a CLASS of "submit-button", the function executes.*/
		if (formInputs[n].className == "submit-button") {
			var submitButton = formInputs[n];
			/*Depending on the hover state, the CLASS property is set appropriately. SUBMIT-BUTTON and SUBMIT-BUTTON-HOVER are defined in STYLES.CSS .*/
			submitButton.onmouseover = function () {
				this.className = "submit-button-hover";
			};
			submitButton.onmouseout = function onSubmitOff() {
				this.className = "submit-button";
			};
		}
	}
}

function onFocusForm() {
	/*Gathers all the INPUT tags in the document.*/
	var formInputs = document.getElementsByTagName("input");
	/*Loops through all of the INPUT tags.*/
	for (n=0; n < formInputs.length; n++) {
		/*Since SUBMIT buttons will be treated differently, and us the SUBMIT-BUTTON class, they are excluded from the function with this if statement.*/
		if (!(formInputs[n].className == "submit-button")) {
			/*Depending on the state of the field, the function changes the SYTLE of the BACKGROUND.*/
			formInputs[n].onfocus = function () {
				this.style.background = "#ffffff";
			};
			formInputs[n].onblur = function () {
				this.style.background = "#fff8e5";
			};
		}
	}
}

/*This function checks to make sure the form field has been filled in and sends a warning message if a required field is empty.*/
/*This function requires the 'onSubmit="return formValidator()"' event to be attached to the form requiring validation.*/
function askFormValidator() {
	/*Requires the form's name and the name of the field.*/
	/*Each field that needs to be checked must be defined as a variable here.*/
	var nameField = document.ask.name;
	var emailField = document.ask.email;
	
	/*Checks to see if the field is empty; if so, the function runs a warning message.*/
	/*Repeat this code for each field that needs to be checked.*/
	if (nameField.value == "" || nameField.value == "*Name") {
		/*The actual message is built by the "alertMsg" function.*/
		alertMsg("Please include your name.",nameField);
		/*This returns the function as false, which stops the form from processing.*/
		return false;
	}
	if (emailField.value == "" || emailField.value == "*E-Mail") {
		/*The actual message is built by the "alertMsg" function.*/
		alertMsg("Please include your e-mail address.",emailField);
		/*This returns the function as false, which stops the form from processing.*/
		return false;
	}
	if (! checkEmail(emailField.value)) {
		alertMsg("This is not a vaild e-mail address.",emailField);
		return false;
	}
}

function contactFormValidator() {
	var nameField = document.contact_form.name;
	var phoneField = document.contact_form.phone;
	var emailField = document.contact_form.email;
	
	if (nameField.value == "") {
		alertMsg("Please include your name.",nameField);
		return false;
	}
	if (emailField.value == "") {
		alertMsg("Please include your e-mail address.",emailField);
		return false;
	}
	if (! checkEmail(emailField.value)) {
		alertMsg("This is not a vaild e-mail address.",emailField);
		return false;
	}
	if (phoneField.value == "") {
		alertMsg("Please include your phone number.",phoneField);
		return false;
	}
	if (! checkPhone(phoneField.value)) {
		alertMsg("This is not a valid phone number.",phoneField);
		return false;
	}
}

/*Creates the validator warning message and then places the focus on the empty field.*/
/*Requires two arguments, the message to display and the field that triggered the warning.*/
function alertMsg (msg,field) {
		/*Displays the message.*/
		alert(msg);
		/*Places the cursor on the offending field.*/
		field.focus();
}

function checkEmail (emailVal) {
	if (
		emailVal.search("@") >= 0 && 
		emailVal.search(".") >= 0 && 
		emailVal.search(" ") < 0 && 
		emailVal.length >= 6 && 
		emailVal.indexOf("@") > 0 &&
		emailVal.indexOf("@") < (emailVal.length - 4) &&
		emailVal.indexOf(".") > 0 &&
		emailVal.indexOf(".") < (emailVal.length - 2)
	) {
		return true;
	} else {
		return false;
	}
}

function checkPhone (phoneVal) {
	if (phoneVal.length >= 7) {
		return true;
	} else {
		return false;
	}
}