// 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 = "Veuillez inscrire une adresse de courrier électronique valide.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += "L’adresse de courrier électronique contient des caractères inadmissibles.\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 += "Le numéro de téléphone "+vFeild+" contient des caractères inadmissibles.\n";
	}
	
	if (!(stripped.length == 10)) {
	stmnt += "Le numéro de téléphone "+vFeild+" n’est pas de la bonne longueur. Assurez-vous d’inscrire un indicatif régional.\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, "Tout accompagné d\’une astérisque est exigé.\n"); 									//Name
error = checkfeild(path.address.value, "Tout accompagné d\’une astérisque est exigé.\n"); 							//Address
error = checkfeild(path.city.value, "Tout accompagné d\’une astérisque est exigé.\n"); 									//City
error = checkfeild(path.province.value, "Tout accompagné d\’une astérisque est exigé.\n"); 							//Province
error = checkfeild(path.country.value, "Tout accompagné d\’une astérisque est exigé.\n"); 							//Country

error = checkfeild(path.birth.value, "Tout accompagné d\’une astérisque est exigé.\n"); 							//Birth date
//error += checkPhone(path.tel_home.value, "à domicile"); 													//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, "Tout accompagné d\’une astérisque est exigé.\n"); 		//emergency contect Name
//error += checkPhone(path.emerg_tel.value, "en cas d’urgence"); 											//Emergency contact Number

loopPop(path.availability, path.availability_hidden);												//Dates Available
error = checkfeild(path.availability_hidden.value, "Tout accompagné d\’une astérisque est exigé.\n"); //Dates Available Check
loopPop(path.avail_time, path.avail_time_hidden);													//Times Available
error = checkfeild(path.avail_time_hidden.value, "Tout accompagné d\’une astérisque est exigé.\n"); 	//Times Available check

error = checkradio(path.experience, "Tout accompagné d\’une astérisque est exigé."); //Eperience
if(path.experience.value == "Yes"){
	error = checkfeild(path.experience_describe.value, "Tout accompagné d\’une astérisque est exigé.\n"); 		//Experience description
}

error = checkradio(path.licence, "Tout accompagné d\’une astérisque est exigé."); 			//Drivers licence
if(path.licence.value == "Yes"){
	error = checkfeild(path.licence_class.value, "Tout accompagné d\’une astérisque est exigé.\n"); 			//Drivers licence Class
}

loopPop(path.interest, path.interest_hidden);														//Volunteers Interests
error = checkfeild(path.interest_hidden.value, "Tout accompagné d\’une astérisque est exigé.\n"); 		//Check Intersts

//error = authorize(path.accept, "Tout accompagné d\’une astérisque est exigé.\n");			// Terms and conditions



error += checkemail(path.email.value); 																//Email
error += checkPhone(path.tel_home.value, "à domicile"); 													//Phone
error += checkPhone(path.emerg_tel.value, "en cas d\’urgence"); 											//Emergency contact Number


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

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