// JavaScript Document

window.onload = function() {
	contactSend();
}

   
/* -- [ contact form validation ] -- */
function valContactForm() {
	
	if(!document.contact_form) {
		return;
	}
	
	var name_value = document.contact_form.name.value;
	var email_value = document.contact_form.email.value;
	var message_value = document.contact_form.message.value;
	var phone_value = document.contact_form.phone.value;
	var email_regex = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	var phone_regex = /^\([0-9]{3}\)\s?[0-9]{3}(-|\s)?[0-9]{4}$|^[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/;		

	var errormsg = '';

	if (name_value == '') {
		errormsg = errormsg + '-You must enter your Name\n';
	}
	
	if (email_value == '' || email_regex.test(email_value) != true) {
		errormsg = errormsg + '-You must enter a valid Email Address\n';
	}

	if (phone_value && phone_regex.test(phone_value) != true) {
		errormsg = errormsg + '-You must enter a valid Phone Number\n';
	}

	if (message_value == '') {
		errormsg = errormsg + '-You must enter a Message\n';
	}
	
	if (errormsg != '') {
		alert('The following errors were found:\n'+errormsg);
		return false;
	}
	
	else { 
		return true; 
	}

}

function contactSend() {
	
	if(!document.contact_form) {
		return;
	}
	
	var contact_form = document.getElementById("contact_form");
	
	contact_form.onsubmit = function() {
		return valContactForm();
	}
}
