// --------------------- VALIDATE FORM ---------------- //
// SET GLOBAL CHECKFORM VARIABLE
var checkForm = 0;
function ValidateForm(myform,msg) {
	var elem = document.getElementById(myform).elements;
	for(var i = 0; i < elem.length; i++) {
		if (elem[i].type == "checkbox") {
			if (elem[i].checked == false && elem[i].className == "required") {
				for(var a = 0; a < elem.length; a++) {
					if (elem[a].value == "" && elem[a].className.indexOf("required") != -1) {
						elem[a].style.backgroundColor = "#e1ffe7";
						elem[a].style.borderColor = "#999999";
					} else {
						elem[a].style.backgroundColor = "#ffffff";
						elem[a].style.borderColor = "#cccccc";
					}
				}
				checkForm = 1;
				if (!msg) {
					msg = 'Please fill in all required fields & checkboxes';
				}
				alert(msg);
				return false;
			}
		} else {
			if (elem[i].value == "" && elem[i].className.indexOf("required") != -1) {
				for(var a = 0; a < elem.length; a++) {
					if (elem[a].value == "" && elem[a].className.indexOf("required") != -1) {
						elem[a].style.backgroundColor = "#e1ffe7";
						elem[a].style.borderColor = "#999999";
					} else {
						elem[a].style.backgroundColor = "#ffffff";
						elem[a].style.borderColor = "#cccccc";
					}
				}
				checkForm = 1;
				if (!msg) {
					msg = 'Please fill in all required fields';
				}
				alert(msg);
				return false;
			}
		}
	}
	return true;
}
// --------------------- VALIDATE CAPTCHA ---------------- //
function valCaptcha(myform) {
	ValidateForm(myform);
	if (checkForm == 0) {
		res = $.ajax({
			type: "GET",
			url: "/handle_recaptcha_validate.php?challenge=" + document.forms[myform].elements['recaptcha_challenge_field'].value + "&response=" + document.forms[myform].elements['recaptcha_response_field'].value,
			async: false
		}).responseText;
		if (res == 0) {
			alert("The varification characters you typed were incorrect. Please try again.");
			Recaptcha.reload();
			document.forms[myform].elements['recaptcha_response_field'].focus();
		} else {
			document.forms[myform].submit();
		}
	} else {
		checkForm = 0;
	}
}
