
//Form validation______________________________________

//This script is used to validate 'contact_form.php'

// Define whitespace characters
var whitespace = " \t\n\r";

function isEmpty(s) {
	var i;
	
	if((s == null) || (s.length == 0))
	return true;
		  
	// Search string looking for characters that are not whitespace
	for (i = 0; i < s.length; i++) {
	   
		var c = s.charAt(i);
		
		if (whitespace.indexOf(c) == -1) 
		return false;
	};
	// At this point all characters are whitespace.
	return true;
};

function validate() {
	
	 var elem = document.getElementById("notice");
	 var output = "Did not send: Please don't leave any field blank";

	//Name__________________________________________
	if (isEmpty(document.contact_form.name.value)) {
		elem.firstChild.nodeValue=output;
		document.contact_form.name.focus();
		return false;
	};
	
	//Email__________________________________________
	if (isEmpty(document.contact_form.email.value)) {
		elem.firstChild.nodeValue=output;
		document.contact_form.email.focus();
		return false;
	}
	else {
		if (/[^@]+@[^@]+/.test(document.contact_form.email.value)==false) {
			elem.firstChild.nodeValue="Did not send: Email is not in a valid format";
			document.contact_form.email.focus();
			return false;
		};
	};
	
	//Subject__________________________________________
	if (isEmpty(document.contact_form.subject.value)) {
		elem.firstChild.nodeValue=output;
		document.contact_form.subject.focus();
		return false;
	};
	
	//Message__________________________________________
	if (isEmpty(document.contact_form.message.value)) {
		elem.firstChild.nodeValue=output;
		document.contact_form.message.focus();
		return false;
	};
	
	document.contact_form.btn.disabled = true; 
	elem.firstChild.nodeValue = "Please wait...";
	return true;
};	
