function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function emailValidation(fld) {
    var tfld = trim(fld.value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Red';
        alert("Please enter an email address");
    } else if (!emailFilter.test(tfld)) {
        fld.style.background = 'Red';
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Red';
    } else {
        fld.style.background = 'White';
    }
}

function phoneValidate(fld) { 
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, ''); 
	if (isNaN(parseInt(stripped))) { 
		alert("The phone number contains illegal characters"); 
		fld.style.background = 'Red'; 
	} else if (stripped.length < 10) {  
		fld.style.background = 'Red'; 
	} else {
		fld.style.background = 'White'; 
	}
} 

function firstValidation(fld){
	var alphaExp = /^[a-zA-Z\- ]+$/ ;
	if(!fld.value.match(alphaExp)){ 
		document.appForm.firstname.style.background = 'Red';
	}else{
		document.appForm.firstname.style.background = 'White';
	}
}

function lastValidation(fld){
	var alphaExp = /^[a-zA-Z\- ]+$/ ;
	if(!fld.value.match(alphaExp)){ 
		document.appForm.lastname.style.background = 'Red';
	}else{
		document.appForm.lastname.style.background = 'White';
	}
} 

function introValidation(entered, alertbox){
	// Emptyfield Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	with (entered){
		if (value==null || value.length < 2){
			if (alertbox!=""){
				document.appForm.intro.style.background = 'Red';
			} 
			return false;
		}
		else {
			document.appForm.intro.style.background = 'White';
			return true;
		}
	}
} 

function validateFileExtension(fld) {
	if(!/(\.pdf|\.doc|\.docx)$/i.test(fld.value)) {
		alert("Invalid file type. Please submit a PDF or Word document");
		document.appForm.resume.style.background = 'Red';
		return false;
	}
	else{
		document.appForm.resume.style.background = 'White';
		return true;
	}
}

function validateOnSubmit(fld) {
	
	var tfld = trim(document.appForm.email.value);  // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;	
	
	var stripped = document.appForm.phoneNum.value.replace(/[\(\)\.\-\ ]/g, '');
	
	var alphaExp = /^[a-zA-Z\- ]+$/ ;
	
	if(!/(\.pdf|\.doc|\.docx)$/i.test(document.appForm.resume.value)) {
		alert("Invalid file type. Please submit a PDF or Word document");
		document.appForm.resume.style.background = 'Red';
		return false;
		
		
	} else if(document.appForm.intro.value.length == 0 || document.appForm.intro.value.length < 2) {
        document.appForm.intro.style.background = 'Red';
        alert("Tell us about yourself");
		return false;
		
		
	} else if(document.appForm.email.value == "") {
        document.appForm.email.style.background = 'Red';
        alert("Please enter an email address");
		return false;
    } else if(!emailFilter.test(tfld)) {
        document.appForm.email.style.background = 'Red';
        alert("Please enter a valid email address");
		return false;
    } else if(document.appForm.email.value.match(illegalChars)) {
        document.appForm.email.style.background = 'Red';
        alert("The email address contains illegal characters");
		return false;
		
		 
	} else if (isNaN(parseInt(stripped))) { 
		alert("The phone number contains illegal characters"); 
		document.appForm.phoneNum.style.background = 'Red'; 
		return false;
	} else if (stripped.length < 10) { 
		alert("Enter a valid phone number with the area code"); 
		document.appForm.phoneNum.style.background = 'Red'; 
		return false;
	} else if(document.appForm.phoneNum.value.length == 0) {
        alert("Enter a valid phone number");
		return false;
		
		
	} else if(!document.appForm.lastname.value.match(alphaExp) || document.appForm.lastname.value.length < 2){
		alert("Last name must contain letters, and spaces or dashes"); 
		document.appForm.lastname.style.background = 'Red';
		return false;
		
		
	} else if(!document.appForm.firstname.value.match(alphaExp) || document.appForm.firstname.value.length < 2){
		alert("First name must contain letters, and spaces or dashes"); 
		document.appForm.firstname.style.background = 'Red';
		return false;
		
		
    } else {
        document.appForm.resume.style.background = 'White';
        document.appForm.intro.style.background = 'White';
        document.appForm.email.style.background = 'White';
        document.appForm.phone.style.background = 'White';
        document.appForm.lastname.style.background = 'White';
        document.appForm.firstname.style.background = 'White';
		return true;
    } 
}