//Form validation for the JOBPRO submit employment information 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 appValidate(){
	//form has been submitted, hide the success or unsuccess error messages
	if(document.getElementById('successError')){
		document.getElementById('successError').style.display = "none";
	}
	if(	document.getElementById('UnsuccessError')){
		document.getElementById('UnsuccessError').style.display = "none";
	}
	
	//first establish all the variables
	var fnameField, lnameField, tradeField, emailField, phoneField, phoneField_2, phoneField_3, phone2Field, phone2Field_2, phone2Field_3, resumeField, cityField, zipField, addressField, testDigit, allow, phoneNumber, testNumber, eDigit;
	var p1Errors, p2Errors;
	//turn the errors variable into a new array with a length of 5
	var errors = new Array(5);
	//assign the values to each variable corresponding to the appropriate field
   	tradeField = document.theForm.category.value;
	fnameField = document.theForm.firstname.value;
	lnameField = document.theForm.lastname.value;
	emailField = document.theForm.email.value;
	phoneField = document.theForm.dayphone_1.value;
	phoneField_2 = document.theForm.dayphone_2.value;
	phoneField_3 = document.theForm.dayphone_3.value;
	phone2Field = document.theForm.eveningphone_1.value;
	phone2Field_2 = document.theForm.eveningphone_2.value;
	phone2Field_3 = document.theForm.eveningphone_3.value;
	addressField = document.theForm.address.value;	
	cityField = document.theForm.city.value;
	zipField = document.theForm.zip.value;
	resumeField = document.theForm.resume.value;
	p1Errors = '';
	p2Errors = '';
	p1Empty = '';
	p2Empty = '';
	
	//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}/;
	var alphabet = /[a-zA-Z]/;
	
	//validate the first name field first
	//first check to make sure the field is not empty
	if(fnameField == ''){
		document.getElementById('fnameError').style.display = "block";
		document.theForm.firstname.style.backgroundColor = "#E1ECEB";
		errors[0] = 'yes';
	}else{
		//check to see if a number was entered
		for(j=0; j<fnameField.length; j++){
			testDigit = fnameField.charAt(j);
			if(pattern.test(testDigit)){
				errors[0] = 'yes';
				break;
			}
		}
		if(errors[0] == 'yes'){
			document.getElementById('fnameError').style.display = "block";
     		document.theForm.firstname.style.backgroundColor = "#E1ECEB";
		}else{
			document.getElementById('fnameError').style.display = "none";
        	document.theForm.firstname.style.backgroundColor = "#FFFFFF";
			errors[0] = '';
		}
	}
	
	//validate the last name field
	//first check to make sure the field is not empty
	if(lnameField == ''){
		document.getElementById('lnameError').style.display = "block";
		document.theForm.lastname.style.backgroundColor = "#E1ECEB";
		errors[1] = 'yes';
	}else{
		//check to see if a number was entered
		for(j=0; j<lnameField.length; j++){
			testDigit = lnameField.charAt(j);
			if(pattern.test(testDigit)){
				errors[1] = 'yes';
				break;
			}
		}
		if(errors[1] == 'yes'){
			document.getElementById('lnameError').style.display = "block";
     		document.theForm.lastname.style.backgroundColor = "#E1ECEB";
		}else{
			document.getElementById('lnameError').style.display = "none";
        	document.theForm.lastname.style.backgroundColor = "#FFFFFF";
			errors[1] = '';
		}
	}
	
	//validate the trade field
	//make sure that "choose" is not the selected option
	if(tradeField != ''){
		if(tradeField != 0){
			document.getElementById('tradeError').style.display = "none";
			document.theForm.category.style.backgroundColor = "#FFFFFF";
			errors[2] = '';
		}else{
			document.getElementById('tradeError').style.display = "block";
     		document.theForm.category.style.backgroundColor = "#E1ECEB";
			errors[2] = 'yes';
		}
	}else{
		document.getElementById('tradeError').style.display = "block";
		document.theForm.category.style.backgroundColor = "#E1ECEB";
		errors[2] = 'yes';
	}
		
	//validate the email field.
	//make sure the email field is not empty
	if(emailField == ''){
		document.getElementById('emailError').style.display = "block";
		document.theForm.email.style.backgroundColor = "#E1ECEB";
		errors[3] = 'yes';
	}else{
		//the field is not empty so time to compare it against the email reg expression
		if(!emailPattern.test(emailField)){
			errors[3] = 'yes';
			document.getElementById('emailError').style.display = "block";
		    document.theForm.email.style.backgroundColor = "#E1ECEB";
		}else{
			document.getElementById('emailError').style.display = "none";
            document.theForm.email.style.backgroundColor = "#FFFFFF";
			errors[3] = '';
		}
	}
	
	//validate the phone fields.
	//make sure that at least one of the phone number fields is filled out
	if(phoneField != '' || phoneField_2 != '' || phoneField_3 != '' || phone2Field != '' || phone2Field_2 != '' || phone2Field_3 != ''){
		//Make sure all three fields are not empty for the daytime phone
		if(phoneField != '' || phoneField_2 != '' || phoneField_3 != ''){
			//check to make sure the proper amount of digits is entered in each field
			if(phoneField.length != 3 || phoneField_2.length != 3 || phoneField_3.length != 4){
				p1Errors = 'yes';
			}else{
				//the fields contain the proper amount of digits, check to make sure they are numbers
				//first combine all the fields into one big string so it can be run through making sure there are not non-digits
				phoneNumber = phoneField+phoneField_2+phoneField_3;
				for(i=0; i<phoneNumber.length; i++){
					testNumber = phoneNumber.charAt(i);
					if(!pattern.test(testNumber)){   //check to see if the current digit does NOT match the pattern. If it dont' then it is not a number
						p1Errors = 'yes';
						break;
					}
				}
			}
		}else{
			p1Empty = 'yes';
		}
		//Make sure all three fields are not empty for the evening phone
		if(phone2Field != '' || phone2Field_2 != '' || phone2Field_3 != ''){
			//check to make sure the proper amount of digits is entered in each field
			if(phone2Field.length != 3 || phone2Field_2.length != 3 || phone2Field_3.length != 4){
				p2Errors = 'yes';
			}else{
				//the fields contain the proper amount of digits, check to make sure they are numbers
				//first combine all the fields into one big string so it can be run through making sure there are not non-digits
				phoneNumber = phone2Field+phone2Field_2+phone2Field_3;
				for(i=0; i<phoneNumber.length; i++){
					testNumber = phoneNumber.charAt(i);
					if(!pattern.test(testNumber)){   //check to see if the current digit does NOT match the pattern. If it dont' then it is not a number
						p2Errors = 'yes';
						break;
					}
				}
			}
		}else{
			p2Empty = 'yes';
		}
		if(p1Errors == 'yes' && p2Errors == 'yes'){
			document.getElementById('phoneError').style.display = "block";
			document.theForm.dayphone_1.style.backgroundColor = "#E1ECEB";
			document.theForm.dayphone_2.style.backgroundColor = "#E1ECEB";
			document.theForm.dayphone_3.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_1.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_2.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_3.style.backgroundColor = "#E1ECEB";
			errors[4] = 'yes';
		}
		if(p1Errors == '' && p2Errors == ''){
			document.getElementById('phoneError').style.display = "none";
			document.theForm.dayphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_3.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_3.style.backgroundColor = "#FFFFFF";
		}
		if(p1Errors == 'yes' && p2Errors == '' && p2Empty != 'yes'){
			document.getElementById('phoneError').style.display = "none";
			document.theForm.dayphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_3.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_3.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_1.value = '';
			document.theForm.dayphone_2.value = '';
			document.theForm.dayphone_3.value = '';
		}else if(p1Errors == 'yes' && p2Errors == '' && p2Empty != ''){
			document.getElementById('phoneError').style.display = "block";
			document.theForm.dayphone_1.style.backgroundColor = "#E1ECEB";
			document.theForm.dayphone_2.style.backgroundColor = "#E1ECEB";
			document.theForm.dayphone_3.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_3.style.backgroundColor = "#FFFFFF";
			errors[4] = 'yes';
		}
		if(p1Errors == '' && p2Errors == 'yes' && p1Empty != 'yes'){
			document.getElementById('phoneError').style.display = "none";
			document.theForm.eveningphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_3.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_3.style.backgroundColor = "#FFFFFF";
			document.theForm.eveningphone_1.value = '';
			document.theForm.eveningphone_2.value = '';
			document.theForm.eveningphone_3.value = '';
		}else if(p1Errors == '' && p2Errors == 'yes' && p1Empty != ''){
			document.getElementById('phoneError').style.display = "block";
			document.theForm.eveningphone_1.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_2.style.backgroundColor = "#E1ECEB";
			document.theForm.eveningphone_3.style.backgroundColor = "#E1ECEB";
			document.theForm.dayphone_1.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_2.style.backgroundColor = "#FFFFFF";
			document.theForm.dayphone_3.style.backgroundColor = "#FFFFFF";
			errors[4] = 'yes';
		}
	}else{
		//all the phone number fields are empty so light them all up and display the error
		errors[4] = 'yes';
		document.getElementById('phoneError').style.display = "block";
		document.theForm.dayphone_1.style.backgroundColor = "#E1ECEB";
		document.theForm.dayphone_2.style.backgroundColor = "#E1ECEB";
		document.theForm.dayphone_3.style.backgroundColor = "#E1ECEB";
		document.theForm.eveningphone_1.style.backgroundColor = "#E1ECEB";
		document.theForm.eveningphone_2.style.backgroundColor = "#E1ECEB";
		document.theForm.eveningphone_3.style.backgroundColor = "#E1ECEB";
	}
	
	//validate the street address
	//just make sure it's not empty
	if(addressField == ''){
		document.theForm.address.style.backgroundColor = "#E1ECEB";
		document.getElementById('addressError').style.display = "block";
		errors[5] = 'yes';
	}else{
		document.theForm.address.style.backgroundColor = "#FFFFFF";
		document.getElementById('addressError').style.display = "none";
		errors[5] = '';
	}
	
	//validate the city field.
	//make sure it's not empty or don't have  a number in it
	if(cityField == ''){
		document.theForm.city.style.backgroundColor = "#E1ECEB";
		document.getElementById('cityError').style.display = "block";
		errors[6] = 'yes';
	}else{
		//check for numbers 
		for(j=0; j<cityField.length; j++){
			testDigit = cityField.charAt(j);
			if(pattern.test(testDigit)){
				errors[6] = 'yes';
				break;
			}
		}
		if(errors[6] == 'yes'){
			document.getElementById('cityError').style.display = "block";
			document.theForm.city.style.backgroundColor = "#E1ECEB";
		}else{
			document.getElementById('cityError').style.display = "none";
			document.theForm.city.style.backgroundColor = "#FFFFFF";
		}
	}

	//validate the zipcode field
	//make sure it's not empty then check for digits
	if(zipField.length < 5 || zipField.length > 10){
		document.theForm.zip.style.backgroundColor = "#E1ECEB";
		document.getElementById('zipError').style.display = "block";
		errors[7] = 'yes';
	}else{
		//check to make sure it's numbers not letters
		for(j=0; j<zipField.length; j++){
			testDigit = zipField.charAt(j);
			if(alphabet.test(testDigit)){
				errors[7] = 'yes';
				break;
			}
		}
		if(errors[7] == 'yes'){
			document.getElementById('zipError').style.display = "block";
			document.theForm.zip.style.backgroundColor = "#E1ECEB";
		}else{
			document.getElementById('zipError').style.display = "none";
			document.theForm.zip.style.backgroundColor = "#FFFFFF";
		}
	}
	
	//validate the resume field
	//make sure the field is not empty. Since it's a comments field it can really contain anything so there is not other validation
	if(resumeField == '' || resumeField.length < 50){
		document.getElementById('resumeError').style.display = "block";
    	document.theForm.resume.style.backgroundColor = "#E1ECEB";
		errors[8] = 'yes';
	}else{
		document.getElementById('resumeError').style.display = "none";
    	document.theForm.resume.style.backgroundColor = "#FFFFFF";
		errors[8] = '';
	}
	
	//check the errors array
	for(i=0; i<9; i++){
		if(errors[i] == 'yes'){
			allow = 'no';
		}
	}
	
	//check the allow flag. This flag determines whether or not to submit the information
	if(allow == 'no'){
		return false;

	}else{
		return true;
	}

}