// 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 = "Personal Info - Enter a valid email address.\n";
	}
	
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   stmnt += "Personal Info - 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.dpmfrm;
	
	error += checkfeild(path.first_name.value, "Personal Info - First name cannot be empty.\n");
	error += checkfeild(path.last_name.value, "Personal Info - Last name cannot be empty.\n");
	error += checkfeild(path.address.value, "Personal Info - Address cannot be empty.\n");
	error += checkfeild(path.city.value, "Personal Info - City cannot be empty.\n");
	error += checkfeild(path.postal.value, "Personal Info - Postal code cannot be empty.\n");
	error += checkemail(path.email.value);
	error += checktel(path.area_tel.value, path.tel.value, "Personal Info - Phone");
	error += checkradio(path.emp_type, "Do you prefer - Choose an employment type.");

	//error += checkradio(path.position, "Position - Position.");
//loop and populate position hidden feild#####################################################################

var positions = new Array();
var counter = 0;

//only works if there are more than on property listed
		if(path.position.length > 0){
			for (i=0, n=path.position.length; i<n; i++) {
			  if (path.position[i].checked) {
				positions[counter] = path.position[i].value;
				counter++;
			  }
			}
		}else if(path['position'].checked){
			//option for all provinces other than pei
			positions[0] = path['position'].value;	
		}

path.positions.value = positions;


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

//checking Location and property location depending on property and province choice #############################
//preset arrays for fixed content
var province_array = {"PEI":"pei", "Nova Scotia":"ns", "New Brunswick":"nb", "Quebec":"quebec"};
var prop_array = {"Tim Hortons":"tim", "Wendy's":"wendys", "Holiday Inn":"holiday", "Royal Park":"royal"};


	//ensure a province has been selected
	error += checkradio(path.loc_province, "Location - Choose a province.");
	
//the beginning of my headache
if(checkradio(path.loc_province, "False") == ""){	

			//loop and check to see which selection was made
			for (i=0, n=path.loc_province.length; i<n; i++) {
			  if (path.loc_province[i].checked) {
				 var checkvalue = path.loc_province[i].value;
				 break;
			  }
			}	
			
//for selected province loop and determin which properties are choosen if any
//if none choosen report error
var province_var = province_array[checkvalue];
var province = province_array[checkvalue] + "_property";
			
		//loop and check all checkboxes and store checked boxes in array.	
var properties = new Array();
var counter = 0;

//resetting the hidden feild so no variables get doubled
path.hidden_properties.value = "";

//only works if there are more than on property listed
		if(path[province].length > 0){
			for (i=0, n=path[province].length; i<n; i++) {
			  if (path[province][i].checked) {
				properties[counter] = path[province][i].value;
				counter++;
			  }
			}
		}else if(path[province].checked){
			//option for all provinces other than pei
			properties[0] = path[province].value;	
			
		}

			
			//if no properties selected
			if(properties == ""){
				error += "Location - Please select a property.\n";
			}else{
				
				//loop and ensure a location has been selected for the property.
				//for the number of selections concatinate the next groups name and check for any selections
				//selections do not need to be stored just made
				for(x=0; x < properties.length; x++){
					
				var checkvalue = "";
				
					if((properties[x]=="Tim Hortons")||(properties[x]=="Wendy's")){
						//concatinate name of property to check
						var property_name = province_var + "_" + prop_array[properties[x]] + "_loc";
						
						//pre load checkvalue with nothing
						
						
						//check property and ensure a locaton has been selected
						for (i=0, n=path[property_name].length; i<n; i++) {
						  if (path[property_name][i].checked) {
							  checkvalue += path[property_name][i].value+', ';
						  }
						}	
						/**/
						//if nothing send error
						if(checkvalue == ""){
							error += "Location - Please select a location for "+properties[x]+".\n";
						}
						
						
						
						
					}//end if tims or wendys
					
					//populate hidden feild from here
						path.hidden_properties.value += "-"+properties[x]+":"+checkvalue
					
				}//end for
				
				
			}//end if
			
}//end if
	
	
//####################################################################################################################
	error += checkradio(path.worked_dpm, "Most recent jobs - Have you worked for D.P. Murphy in the past?");
	
//if user has worked for dpm in the past check that they specified where and when
	for (i=0, n=path.worked_dpm.length; i<n; i++) {
	  if (path.worked_dpm[i].checked) {
		 var checkvalue = path.worked_dpm[i].value;
		 break;
	  }
	}	
	if(checkvalue == "yes"){
		error += checkfeild(path.worked_dpm_where.value, "Most recent jobs - Please enter where you worked for D.P. Murphy.\n");
		error += checkfeild(path.worked_dpm_when.value, "Most recent jobs - Please enter when you worked for D.P. Murphy.\n");
	}
//###############################################################################
	
	error += checkfeild(path.school_grade.value, "Educational Background - Please enter highest grade.\n");
	error += checkfeild(path.school_name.value, "Educational Background - Please enter school name.\n");
	error += checkradio(path.school_attending, "Educational Background - Please indicate wether you are still attending school.");
	
	error += checkfeild(path.ref_name0.value, "References - Reference 1 name cannot be blank.\n");
	error += checkfeild(path.ref_relate0.value, "References - Reference 1 relationship cannot be blank.\n");
	error += checktel(path.ref_area_tel0.value, path.ref_tel0.value, "References - Reference 1");
	
	error += checkfeild(path.ref_name1.value, "Reference 2 name cannot be blank.\n");
	error += checkfeild(path.ref_relate1.value, "Reference 2 relationship cannot be blank.\n");
	error += checktel(path.ref_area_tel1.value, path.ref_tel1.value, "References - Reference 2");
	
	error += authorize(path.authorization);
	
	//check files extension to ensure .rtf or .pdf
	var uploadme = path.File.value;
	if(uploadme != ""){
		var exploded = uploadme.split('.');
		var exrension = exploded[exploded.length-1];
		
		if((exrension == "rtf")||(exrension == "pdf")){
			
		}else{
			error += "Attach Resume - Only PDF or RTF allowed.\n"+exrension;
		}
	}
	
	if(error != ""){
		alert(error);
	}else{
		path.submit();
	}
	
}





