//Form validation for the JRH Asset Management Employment Opportunities Page
//Developer: Tait Chambers, WebDuck Designs 2007
//This is only the initial validation, assuming the information passes here, it will be validated again via PHP


/*
function to validate the contact form input, if each field validates correctly the form will submit. If there is an error, 
the error array will be added to and the form will not submit. The errors for the appropriate fields will display instead
*/
function empValidate(){
	//first establish all the variables
	var fname, lname, phone, email, street, city, state, zip, fnameTest, lnameTest, testNumber, zipNumber;
	//turn the errors variable into a new array with a length of 5
	var errors = new Array();
	
	var allow = '';
	
	//assign the values to each variable corresponding to the appropriate field
	fname = document.submitInfo.f_name.value;
	lname = document.submitInfo.l_name.value;
	phone = document.submitInfo.phone_number.value;
	email = document.submitInfo.email_addy.value;
	street = document.submitInfo.street_addy.value;
	city = document.submitInfo.res_city.value;
	state = document.submitInfo.res_state.value;
	zip = document.submitInfo.zip_code.value;
	
	if(document.submitInfo.resume){
		var resume = document.submitInfo.resume.value;
	}
	
	if(document.submitInfo.specialty_type){
		var specialty = document.submitInfo.specialty_type.value;
	}

	

//below are the regular expressions for looking validation comparison
	var pattern = /[0-9]/;  //regular expression looking for a digit between 0 and 9
	var emailPattern = /\w+@\w+\.\w{1,3}/; //regular expression for an email address to adhere to
	var zippattern = /[0-9\-]/;  //regular expression looking for a digit between 0 and 9 or a dash
	var phonepattern = /[0-9\(\)\-\s]/;  //regular expression looking for a digit between 0 and 9
	
	//validate the first name field 
	//first check to make sure the field is not empty
	if(fname == ''){
		document.getElementById('fnameError').style.display = "inline";
		errors[0] = 'yes';
	}else{
		//check to see if a number was entered
		for(j=0; j<fname.length; j++){
			fnameTest = fname.charAt(j);
			if(pattern.test(fnameTest)){
				errors[0] = 'yes';
				break;
			}
		}
		if(errors[0] == 'yes'){
			document.getElementById('fnameError').style.display = "inline";
		}else{
			document.getElementById('fnameError').style.display = "none";
			errors[0] = '';
		}
	}


	//validate the last name field 
	//first check to make sure the field is not empty
	if(lname == ''){
		document.getElementById('lnameError').style.display = "inline";
		errors[1] = 'yes';
	}else{
		//check to see if a number was entered
		for(j=0; j<lname.length; j++){
			lnameTest = lname.charAt(j);
			if(pattern.test(lnameTest)){
				errors[1] = 'yes';
				break;
			}
		}
		if(errors[1] == 'yes'){
			document.getElementById('lnameError').style.display = "inline";
		}else{
			document.getElementById('lnameError').style.display = "none";
			errors[1] = '';
		}
	}



	//validate the specialty field if one exists
	//first make sure the field is not empty or the default value
	if(document.submitInfo.specialty_type){
		if(specialty == 'choose'){
			document.getElementById('specialtyError').style.display = "inline";
			errors[2] = 'yes';
		}else{
			document.getElementById('specialtyError').style.display = "none";
			errors[2] = '';
		}
	}



	//validate the phone fields. 
	//Make sure all three fields are not empty
	if(phone == ''){
		errors[3] = 'yes';
		document.getElementById('phoneError').style.display = "inline";
	}else{
		if(phone.length < 10 || phone.length > 14){
		errors[3] = 'yes';
		document.getElementById('phoneError').style.display = "inline";
		}else{		
			//the phone number field is not empty so continue validation
			for(i=0; i<phone.length; i++){
				testNumber = phone.charAt(i);
				if(!phonepattern.test(testNumber)){   //check to see if the current digit does NOT match the pattern. If it dont' then it is not a number
					errors[3] = 'yes';
					break;
				}
			}
		}
		if(errors[3] == 'yes'){
			document.getElementById('phoneError').style.display = "inline";
		}else{
			document.getElementById('phoneError').style.display = "none";
			errors[3] = '';
		}
	}


	
	//validate the email field.
	//make sure the email field is not empty
	if(email == ''){
		document.getElementById('emailError').style.display = "inline";
		errors[4] = 'yes';
	}else{
		//the field is not empty so time to compare it against the email reg expression
		if(!emailPattern.test(email)){
			errors[4] = 'yes';
			document.getElementById('emailError').style.display = "inline";
		}else{
			document.getElementById('emailError').style.display = "none";
			errors[4] = '';
		}
	}



	//validate street address
	//just make sure it is not empty
	if(street == ''){
		document.getElementById('streetError').style.display = "inline";
		errors[5] = 'yes';
	}else{
		document.getElementById('streetError').style.display = "none";
		errors[5] = '';
	}
	
	//validate city address
	//just make sure it is not empty
	if(city == ''){
		document.getElementById('cityError').style.display = "inline";
		errors[6] = 'yes';
	}else{
		document.getElementById('cityError').style.display = "none";
		errors[6] = '';
	}



	//validate the state field
	//first make sure the field is not empty or the default value
	if(state == 'choose'){
		document.getElementById('stateError').style.display = "inline";
		errors[7] = 'yes';
	}else{
		document.getElementById('stateError').style.display = "none";
		errors[7] = '';
	}
	
	
	//validate the zip code field
	if(zip == ''){
		document.getElementById('zipError').style.display = "inline";
		errors[8] = 'yes';
	}else{
		if(zip.length < 5 || zip.length >= 11){
			document.getElementById('zipError').style.display = "inline";
			errors[8] = 'yes';
		}else{
			for(i=0; i<zip.length; i++){
				zipNumber = zip.charAt(i);
				if(!zippattern.test(zipNumber)){   //check to see if the current digit does NOT match the pattern. If it dont' then it is not a number
					errors[8] = 'yes';
					break;
				}
			}
		}
		if(errors[8] == 'yes'){
			document.getElementById('zipError').style.display = "inline";
		}else{
			document.getElementById('zipError').style.display = "none";
			errors[8] = '';
		}
	}



	//validate the comments/resume enter field if one exists
	if(document.submitInfo.resume){
		if(resume == ''){
			errors[9] = 'yes';
			document.getElementById('commentsError').style.display = "inline";
		}else{
			errors[9] = '';
			document.getElementById('commentsError').style.display = "none";
		}
	}




	//check the errors array
	for(i=0; i<errors.length; i++){
		if(errors[i] == 'yes'){
			allow = 'no';
			break;
		}
	}
	
	//check the allow flag. This flag determines whether or not to submit the information
	if(allow == 'no'){
		return false;
	}else{
		return true;
	}
}



//simple validation to make sure the check box aknowledgement of truthful information is checked
function verifyCheck(){
	
	if(!document.resumeSubmit.agreement.checked){
		document.getElementById('checkError').style.display = "inline";
		document.getElementById('checkError').focus();
		return false;
	}else{
		document.getElementById('checkError').style.display = "none";
		document.resumeSubmit.action='../includes/process-applicant.php';
		document.resumeSubmit.submit();	
		return true;
	}
}
