// JavaScript Document
// Validate the apply online form

//check a text feild for content
function checkfeild(strng, stmnt){
	if(strng == ""){
			return stmnt;
	}else{
			return "";
	}
}

//check email feild
function checkemail(strng){
var stmnt = "";
var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
		   stmnt = "Please enter a valid email address.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += "Email address contains illegal characters.\n";	
	}
	
	if(stmnt != ""){
		return stmnt;
	}else{
			return "";
	}
}

//check phone numbers
function checktel(area_strng, tel_strng, stmnt){
var error_stmnt = "";
var area_stripped = area_strng.replace(/[\(\)\.\-\ ]/g, '');
var tel_stripped = tel_strng.replace(/[\(\)\.\-\ ]/g, '');

	if (!(area_stripped.length == 3)) {
	error_stmnt += stmnt+", area code must be 3 numbers..\n";
	}
	if (!(tel_stripped.length == 7)) {
	error_stmnt += stmnt+", phone number must be 7 numbers.\n";
	}
	if(error_stmnt != ""){
			return error_stmnt;
	}else{
			return "";
	}
	
}

//check radio buttons
function checkradio(path_radioname, stmnt){
	
	function checkRadio(checkvalue) {
	var errorr = "";
		if (!(checkvalue)) {
		   errorr = stmnt+"\n";
		}
	return errorr;    
	}
	
for (i=0, n=path_radioname.length; i<n; i++) {
	  if (path_radioname[i].checked) {
		 var checkvalue = path_radioname[i].value;
		 break;
	  }
}	
return checkRadio(checkvalue);
}


//ensure the user has checked the authorization checkbox
function authorize(check_path){
	if(!check_path.checked){
		return "Please ensure you have checked the autorization checkbox.\n";
	}else{
			return "";
	}
}




//function to check the whole form
function check_whole_form(){	
	
var error = "";
var path = document.contact_us;

	error += checkfeild(path.name.value, "Please enter your name.\n");
	error += checkemail(path.email.value);	
	error += checktel(path.tel_area.value, path.tel.value, "Please enter a valid phone number");
	
	if(error != ""){
		alert(error);
	}else{
		path.submit();
	}
	
}





