<!-- // hide from older browsers
/**
 * validate.js - Javascript Validation
 * Script by @Mell
 * Do not redistribute or sell
**/

// validation function
function validateForm() {
	hideAllErrors(); // call hideAllErrors function, so we can hide the current errors on the page
	var countErrors = 0;
	
	if(!(document.getElementById("required_fields"))) {} // for elements are marked required
	else {
		var requiredInput = document.getElementById("required_fields").value.split(","); // split elements in required by comma into an array
		for(i=0;i<requiredInput.length;i++) { // iterate through all required fields
			if(document.getElementById(requiredInput[i]).value == "") { // if empty, show error
				document.getElementById(requiredInput[i]).className = "field_error";
				document.getElementById(requiredInput[i]+"Error").className = "visible_error"; // set class of that error to visible, so it would be displayed
				document.getElementById(requiredInput[i]).select(); // select that input area
				if(countErrors == 0)
					document.getElementById(requiredInput[0]).focus(); // focus on input area, so text cursor appears
				countErrors++;
			}
		}
	}
	// similar operations for other validation options
	if(!(document.getElementById("intOnly_fields"))) {}
	else {
		var intOnly = document.getElementById("intOnly_fields").value.split(",");
		for(i=0;i<intOnly.length;i++) {
			if(isNaN(document.getElementById(intOnly[i]).value) && document.getElementById(intOnly[i]).value != "") { // if input is Not A Number and not empty, perform next operations
				document.getElementById(intOnly[i]).className = "field_error";
				document.getElementById(intOnly[i]+"Error1").className = "visible_error";
				document.getElementById(intOnly[i]).select();
				if(countErrors == 0)
					document.getElementById(intOnly[0]).focus();
				countErrors++;
			}
		}
	}
	if(!(document.getElementById("validEmail_fields"))) {}
	else {
		var validEmail = document.getElementById("validEmail_fields").value.split(",");
		for(i=0;i<validEmail.length;i++) {
			if(!checkEmail(document.getElementById(validEmail[i]).value) && document.getElementById(validEmail[i]).value != "") { // if input is not a valid email (passed through function) and not empty, perform next operations
				document.getElementById(validEmail[i]).className = "field_error";
				document.getElementById(validEmail[i]+"Error2").className = "visible_error";
				document.getElementById(validEmail[i]).select();
				if(countErrors == 0)
					document.getElementById(validEmail[0]).focus();
				countErrors++;
			}
		}
	}
	if(!(document.getElementById("validURL_fields"))) {}
	else {
		var validURL = document.getElementById("validURL_fields").value.split(",");
		for(i=0;i<validURL.length;i++) {
			if(!checkURL(document.getElementById(validURL[i]).value) && document.getElementById(validURL[i]).value != "") { // if input is not a valid url (passed through function) and not empty, perform next operations
				document.getElementById(validURL[i]).className = "field_error";
				document.getElementById(validURL[i]+"Error3").className = "visible_error";
				document.getElementById(validURL[i]).select();
				if(countErrors == 0)
					document.getElementById(validURL[0]).focus();
				countErrors++;
			}
		}
	}
	if(!(document.getElementById("validOptions_fields"))) {}
	else {
		var validOptions = document.getElementById("validOptions_fields").value.split(",");
		var select = 0;
		for(i=0;i<validOptions.length;i++) {
			for(j=0;j<document.getElementsByName(validOptions[i]).length;i++) {
				if(document.getElementsByName(validOptions[i])[j].checked) {
					select++;
				}
				if(select == 0){
					document.getElementById(validOptions[i]+"Error4").className = "visible_error";
					countErrors++;
				}
			}
		}
	}
	if(countErrors > 0)
		return false;
}

// email validation function
function checkEmail(email) { // accepts email as parameter
    var emailValid = "^[a-zA-Z0-9\_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\.]+$"; // regular expression to validate by
    if(!email.match(emailValid)) {return false;} // if comparison to expression returns true, return true
	else {return true;}
}

// url validation function
// similar to email function, just different regular expression
function checkURL(url) {
	var urlValid = "^http:\/\/([a-z0-9]+\.)?[a-zA-Z0-9\-]+\.[a-z\.\/\?&]+$";
    if(!url.match(urlValid)) {return false;}
	else {return true;}
}

// hide the errors function
function hideAllErrors() {
	if(!(document.getElementById("required_fields"))) {}
	else { // if required is on the page
		var requiredInput = document.getElementById("required_fields").value.split(","); // split required by comma to get different fields into an array
		for(i=0;i<requiredInput.length;i++) { // iterate through fields
			document.getElementById(requiredInput[i]).className = "field";
			document.getElementById(requiredInput[i]+"Error").className = "hidden_error"; // change class if error element to hidden, cannot be seen
		}
	}
	// similar operations for other validation options
	if(!(document.getElementById("intOnly_fields"))) {}
	else {
		var intOnly = document.getElementById("intOnly_fields").value.split(",");
		for(i=0;i<intOnly.length;i++) {
			document.getElementById(intOnly[i]).className = "field";
			document.getElementById(intOnly[i]+"Error1").className = "hidden_error";
		}
	}
	if(!(document.getElementById("validEmail_fields"))) {}
	else {
		var validEmail = document.getElementById("validEmail_fields").value.split(",");
		for(i=0;i<validEmail.length;i++) {
			document.getElementById(validEmail[i]).className = "field";
			document.getElementById(validEmail[i]+"Error2").className = "hidden_error";
		}
	}
	if(!(document.getElementById("validURL_fields"))) {}
	else {
		var validURL = document.getElementById("validURL_fields").value.split(",");
		for(i=0;i<validURL.length;i++) {
			document.getElementById(validURL[i]).className = "field";
			document.getElementById(validURL[i]+"Error3").className = "hidden_error";
		}
	}
	if(!(document.getElementById("validOptions_fields"))) {}
	else {
		var validOptions = document.getElementById("validOptions_fields").value.split(",");
		for(i=0;i<validOptions.length;i++) {
			document.getElementById(validOptions[i]+"Error4").className = "hidden_error";
		}
	}
}

// -->
