var name; var email; var subject; var description; var valid; var labels;
function validateContact() {
	document.getElementById("contactForm").onsubmit = validate;
}
function validate() {
	clearErrors();
	if(name.value == "") {showError(name); valid = 0;}
	if(email.value == "") {showError(email); valid = 0;}
	else if(email.value != "") {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email.value) == false) {
			showError(email, "invalid");
			valid = 0;
		}
	}
	if(subject.value != "If you're human, don't type here") {showError(subject); valid = 0;}
	if(descr.value == "") {showError(descr); valid = 0;}
	if(valid == 1) {return true;}
	else {return false;}
}
function clearErrors() {
	valid = 1;
	name = document.getElementById("name");
	email = document.getElementById("email");
	subject = document.getElementById("subject");
	descr = document.getElementById("descr");
	labels = document.getElementsByTagName("label");
	for(var q = 0; q < labels.length; q++) {
		if((labels[q].attributes['for'].nodeValue == "name") || (labels[q].attributes['for'].nodeValue == "email") || (labels[q].attributes['for'].nodeValue == "descr")) {labels[q].style.backgroundPosition = "0 0";}
		if(labels[q].attributes['for'].nodeValue == "subject") {labels[q].style.display = "none";}
	}
}
function showError(obj, msg){
	if(obj != subject) {
		for(var q = 0; q < labels.length; q++) {
			if(labels[q].attributes['for'].nodeValue == obj.name) {
				if(msg == "invalid") {labels[q].style.backgroundPosition = "0 50%";}
				else {labels[q].style.backgroundPosition = "0 100%";}
			}
		}
	}
	else {
		for(var q = 0; q < labels.length; q++) {
			if(labels[q].attributes['for'].nodeValue == "subject") {labels[q].style.display = "block";}
		}
	}
}