/* = Form validation
     12 Aug 2008, Greg Kuwaye
/******************************/

function validateForm() {
	var f = document.getElementById("cform"); // form
	var n = document.getElementById("name");
	var s = document.getElementById("subj");
	var e = document.getElementById("eml");
	var p = document.getElementById("ph");
	var m = document.getElementById("msg");
	// if any one of the fields is empty
	if( n.value=="" || s.value=="" || e.value=="" || p.value=="" || m.value=="" ) {
		alert( "One or more fields in the contact form were left blank. All fields are required" );
		return false;
	} else if( !validateEmail(e.value) ) {
		alert( "Oops! It seems like \'" + e.value + "\' isn\'t a valid email address" );
	} else if( !validatePhone(p.value) ) {
		alert( "Oops! It seems like \'" + p.value + "\' isn't a valid phone number" );
	} else {
		f.submit();
	};	
}

// Phone number validation
// Modified from http://www.webcheatsheet.com/javascript/form_validation.php#phone
function validatePhone(ph) {
	var stripped = ph.replace(/[\(\)\.\-\ ]/g, '');
	// if ph begins with 1, remove it
	if( stripped.length == 11 && stripped.charAt(0) == 1 ) stripped = stripped.substring(1);
	// detect non-numbers and correct length
	if (isNaN(parseInt(stripped)) || !(stripped.length == 10) ) {
		return false;
	} else {
    return true;
	};
};

// Email validation
// Modified from http://ntt.cc/2008/05/10/over-10-useful-javascript-regular-expression-functions-to-improve-your-web-applications-efficiency.html
function validateEmail(em) {
	var r = new RegExp(/^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/);
	return r.test(em);
}
