addLoadEvent(prepareForm);

function isEmail(str) {
	var regexp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return regexp.test(str);
}

function isURL(str) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(str);
}

function validateName(field) {
	//remove any whitespace
	var name = trim(field.value);
	
	//if field is empty
	if (name.length == 0) {
		return "error-1";
	}
	//if field is too short
	if (name.length < 3) {
		return "error-2";
	}
	//if field is too long
	if (name.length >= 50) {
		return "error-3";
	}

	return true;
}

function validateEmail(field) {
	//remove any whitespace
	var email = trim(field.value);

	//if field is not empty
	if (email.length > 0) {
		//validate email
		return isEmail(email);
	}

	return true;
}

function validateURL(field) {
	//remove any whitespace
	var url = trim(field.value);

	//if field is not empty
	if (url.length > 0) {
		//validate url
		return isURL(url);
	}

	return true;
}

function validateMsg(field) {
	//remove any whitespace
	var msg = trim(field.value);

	//if field is empty
	if (msg.length == 0) {
		return "error-6";
	}
	//if field is too short
	if (msg.length < 5) {
		return "error-7";
	}
	//if field is too long
	if (msg.length >= 65535) {
		return "error-8";
	}

	return true;
}

function error(element) {
	element.className = element.className + " error";
}

function noError(element) {
	element.className = "field";
}

//inserts `error` into `array`
function addError(array,error) {
	//if `error` is already in `array` return false
	if (searchThroughArray(array,error)) return false;
	
	//create `name_set` array
	//contains a set of errors that only one can be shown at a time
	var name_set = Array (
					"error-1",
					"error-2",
					"error-3"
				);

	//create `msg_set` array
	//contains a set of errors that only one can be shown at a time
	var msg_set  = Array (
					"error-6",
					"error-7",
					"error-8"
				);

	/*if `error` we're inserting into `array` is 
		in one of the `name_set` array */
	if (searchThroughArray(name_set,error)) {
		//loop through `array` array
		for (var i=0; i <= array.length; i++) {
			//loop through `name_set` array
			for (var j=0; j <=name_set.length; j++) {
				//if both values match
				if (array[i] == name_set[j]) {
					//remove the error string `name_set[j]` from `array`
					array = removeFromArray(array,name_set[j]);
				}
			}
		}		
	}

	/*if `error` we're inserting into `array` is 
		in one of the  `msg_set` array */
	if (searchThroughArray(msg_set,error)) {
		//loop through `array` array
		for (var i=0; i <= array.length; i++) {
			//loop through `msg_set` array
			for (var j=0; j <= msg_set.length; j++) {
				//if both values match
				if (array[i] == msg_set[j]) {
					//remove the error string `msg_set[j]` from `array`
					array = removeFromArray(array,msg_set[j]);
				}
			}
		}
	}

	//inserts `error` into `array`
	array = addToArray(array,error);

	return array;
}

function removeError(array,field) {
	//create `name_set` array
	//contains a set of errors that are associated with `name` field
	var name_set = Array (
					"error-1",
					"error-2",
					"error-3"
				);

	//create `msg_set` array
	//contains a set of errors that are associated with `message` field
	var msg_set  = Array (
					"error-6",
					"error-7",
					"error-8"
				);

	//set error that is associated with website
	var website = "error-4";
	//set error that is associated with email
	var email   = "error-5";

	switch (field) {
		case "name":
			array = removeArrayFromArray(array,name_set);
			break;
		case "website":
			array = removeFromArray(array,website);
			break;
		case "email":
			array = removeFromArray(array,email);
			break;
		case "message":
			array = removeArrayFromArray(array,msg_set);
			break;
	}

	return array;
}

//set `errors` to display
function showErrors(errors) {
	//first hide all errors from view
	hideAllErrors();

	//then if there are no errors to display return false
	if (errors.length < 1) return false;

	//loop through `errors` array
	for (var i=0; i <= errors.length; i++) {
		//set `errors[i]` to view
		show(errors[i]);
	}
}

//sets all errors in hidden mode
function hideAllErrors() {
	//checkpoint
	if (!document.getElementById) return false;

	//create array with possible errors
	var errors = Array (
					"error-1",
					"error-2",
					"error-3",
					"error-4",
					"error-5",
					"error-6",
					"error-7",
					"error-8"
				);
	
	//loop through `errors` array
	for (var i=0; i <= errors.length; i++) {
		//if a tag with `errors[i]` id is found
		if (document.getElementById(errors[i])) {
			//hide it from view
			hide(errors[i]);
		}
	}
	
}

function validateForm(whichform) {
	//initialise
	var result = true;
	var errors = Array();

	//loop through form elements
	for (var i=0; i < whichform.elements.length; i++) {
		//use shortened var name
		var element    = whichform.elements[i];
		var element_id = element.getAttribute("id");

		//switch construct
		switch(element_id)
		{
			//element is name
			case "name":
				//validate field
				//'validate' will either contain true or some error number
				var validate = validateName(element);

				//if it doesn't return true
				if (validate != true) {
					//change field class to change background
					error(element);
					//add the error into `errors` array
					errors = addError(errors,validate);
					//set result to false
					result = false;
				}
				//else if it validated
				else {
					//set field class to original
					noError(element);
					errors = removeError(errors,element_id);
				}
				break;

			//if element is website
			case "website":
				//validate field
				if (validateURL(element) == false) {
					//change field class to change background
					error(element);
					//add the error into `errors` array
					errors = addError(errors,"error-4");
					//set result to false
					result = false;
				}
				//else if it validated
				else {
					//set field class to original
					noError(element);
					errors = removeError(errors,element_id);
				}
				break;

			//if element is email
			case "email":
				//validate field
				if (validateEmail(element) == false) {
					//change field class to change background
					error(element);
					//add the error into `errors` array
					errors = addError(errors,"error-5");
					//set result to false
					result = false;
				}
				//else if it validated
				else {
					//set field class to original
					noError(element);
					errors = removeError(errors,element_id);
				}
				break;

			//if element is message
			case "message":
				//validate field
				//'validate' will either contain true or some error number
				var validate = validateMsg(element);

				//if it doesn't return true
				if (validate != true) {
					//change field class to change background
					error(element);
					//add the error into `errors` array
					errors = addError(errors,validate);
					//set result to false
					result = false;
				}
				//else if it validated
				else {
					//set field class to original
					noError(element);
					errors = removeError(errors,element_id);
				}
				break;
		}		
	}

	//set errors to view in comment form
	showErrors(errors);

	return result;
}

function saveFieldValues(form) {
	//loop through form elements
	for (var i=0; i < form.elements.length; i++) {
		//use shortened var name
		var field    = form.elements[i];
		var field_id = field.getAttribute("id");
		
		switch (field_id) {
			case "name":
				//remove any whitespace
				var value = trim(field.value);
				//if value actually has a value stored
				if (value.length > 0) {
					//save cookie
					createCookie(field_id,value,365);
				}
				break;
			case "website":
				//remove any whitespace
				var value = trim(field.value);
				//if value actually has a value stored
				if (value.length > 0) {
					//save cookie
					createCookie(field_id,value,365);
				}
				break;
			case "email":
				//remove any whitespace
				var value = trim(field.value);
				//if value actually has a value stored
				if (value.length > 0) {
					//save cookie
					createCookie(field_id,value,365);
				}
				break;
		}
	}
}

function prepareForm() {
	for (var i=0; i < document.forms.length; i++) {
		var thisform = document.forms[i];
		//when form is submitted
		thisform.onsubmit = function() {
			//if validation fails, return false
			if (!validateForm(this)) {
				return false;
			}
			//else save data in cookies and return true
			else {
				saveFieldValues(this);
				return true;
			}
		}
	}
}
