// JavaScript Document


//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 phon number
function checkPhone(strng, vFeild){
	var stmnt = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   stmnt += "The "+vFeild+" phone number contains illegal characters.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "The "+vFeild+" phone number is the wrong length. Make sure you included an area code.\n";
	}
	
		if(stmnt != ""){
		return 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);
}


//function to loop and populate a designated feild with all checked answers from a specified checkbox group
function loopPop(targetInput, targetOutput){
	
	var vArray = new Array();
	var counter = 0;

			for (i=0, n=targetInput.length; i<n; i++) {
			  if (targetInput[i].checked) {
				vArray[counter] = " "+targetInput[i].value;
				counter++;
			  }
			}
			
		

targetOutput.value = vArray;
	
	
}

//ensure the user has checked the authorization checkbox
function authorize(check_path, stmnt){
	if(!check_path.checked){
		return stmnt;
	}else{
			return "";
	}
}


//function to check the whole form
function check_whole_form(){	

var error = "";
var path = document.volfrm;
//alert("woking");
// Validation #####################################################################

error += checkfeild(path.name.value, "Please enter your name.\n"); 									//Name
error += checkfeild(path.address.value, "Please enter your address.\n"); 							//Address
error += checkfeild(path.city.value, "Please enter your city.\n"); 									//City
error += checkfeild(path.province.value, "Please enter your province.\n"); 							//Province
error += checkfeild(path.country.value, "Please enter your country.\n"); 							//Country

error += checkfeild(path.birth.value, "Please enter your birth date.\n"); 							//Birth date
error += checkPhone(path.tel_home.value, "Home"); 													//Phone
//No guarentee they have a work phone so no need to validate										//Phone Work
//No guarentee they have an extension so no need to validate										//Work extension
error += checkemail(path.email.value); 																//Email

error += checkfeild(path.emerg_name.value, "Please enter your emergency contact's name.\n"); 		//emergency contect Name
error += checkPhone(path.emerg_tel.value, "Emergency"); 											//Emergency contact Number

loopPop(path.availability, path.availability_hidden);												//Dates Available
error += checkfeild(path.availability_hidden.value, "Please select the dates you are available.\n"); //Dates Available Check
loopPop(path.avail_time, path.avail_time_hidden);													//Times Available
error += checkfeild(path.avail_time_hidden.value, "Please select the times you are available.\n"); 	//Times Available check

error += checkradio(path.experience, "Please indicate whether you have any previous experience."); //Eperience
if(path.experience.value == "Yes"){
	error += checkfeild(path.experience_describe.value, "Please describe any experience.\n"); 		//Experience description
}

error += checkradio(path.licence, "Please indicate whether you have a drivers licence."); 			//Drivers licence
if(path.licence.value == "Yes"){
	error += checkfeild(path.licence_class.value, "Please enter your licence class.\n"); 			//Drivers licence Class
}

loopPop(path.interest, path.interest_hidden);														//Volunteers Interests
error += checkfeild(path.interest_hidden.value, "Please select your areas of interest.\n"); 		//Check Intersts

//error += authorize(path.accept, "You must agree to our terms and conditions to proceed.");			// Terms and conditions


//#####################################################################

	
	if(error != ""){
		alert(error);
	}else{
		path.submit();
	}
	
}