function validateForm(theForm) {
	var errors = "The following errors were found:\n";
	if(theForm.solutionsseeked){theForm.solutionsseeked.value = findCheckboxes();}
	
	errors += validateRecipient(theForm.recipient);
	errors += validateLastName(theForm.lastname);
	errors += validateFirstName(theForm.firstname);
	errors += validateEmail(theForm.sender);
	
	if(theForm.rcompany) {errors += validateCompany(theForm.rcompany);}
	if(theForm.rtelephone) {errors += validateTelephone(theForm.rtelephone);}
	if(theForm.title) {errors += validateTitle(theForm.title);}
	if(theForm.raddress1) {errors += validateAddress(theForm.raddress1);}
	if(theForm.rcity) {errors += validateCity(theForm.rcity);}
	if(theForm.rstate) {errors += validateState(theForm.rstate);}
	if(theForm.rzipcode) {errors += validateZip(theForm.rzipcode);}

		
	if(errors != "The following errors were found:\n") {
		alert(errors);
		return false;
	} else {
		theForm.submit();
	}
}

function validateRecipient(fld) {
    var error = "";
  	
    if (fld.value.length == 0) { 
        error = "- Please select a department.\n"
    } else {
    }
    return error;   
}

function validateLastName(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter your last name.\n"
    } else {
    }
    return error;   
}

function validateFirstName(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter your first name.\n"
    } else {}
    return error;   
}



function validateCompany(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter the name of your company.\n"
    } else {}
    return error;   
}

function validateTitle(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter your title.\n"
    } else {
    }
    return error;   
}

function validateAddress(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter your address.\n"
    } else {
    }
    return error;   
}

function validateCity(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please enter your city.\n"
    } else {
    }
    return error;   
}

function validateState(fld) {
    var error = "";
  
    if (fld.value.length == 0) { 
        error = "- Please select a state.\n"
    } else {
    }
    return error;   
}

function trim(s){return s.replace(/^\s+|\s+$/, '');} 

function validateTelephone(fld) {
    var error = "";
  	rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
  	
    if ((!rePhoneNumber.test(fld.value))) { 
        error = "- Phone Number Must Be Entered As: (555) 555-1234.\n"
    } else {}
    return error;   
}

function validateZip(fld) {
    var error = "";
  	reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
  	
    if (!reZip.test(fld.value)) { 
        error = "- Please enter a 5 or 9 digit zip code in XXXXX-XXXX format.\n"
    } else {
    }
    return error;   
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\'\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        error = "- Please enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "- Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "- The email address contains illegal characters.\n";
    } else {}
    return error;
}

function violationsForm(theForm) {
	var stdSubjectHdr = "JaggedPeak.com Violations Report: ";
	var enteredSubjectHdr = theForm.subject.value;
	
	if(theForm.message.value == 0) {
		alert("You must enter a message in order to send this e-mail.")
		return false;
	} else {
		if(theForm.sender.value == 0) {theForm.sender.value = "anonymous@jaggedpeak.com";}
		if(theForm.subject.value == 0) {
			theForm.subject.value = stdSubjectHdr + "No Subject Provided";
		} else {
			theForm.subject.value = stdSubjectHdr + enteredSubjectHdr;
		}
		theForm.submit();
	}
}

function findCheckboxes() {
	var inputs = document.getElementsByTagName("input");
	var checkboxes = new Array();
	var solutions = "";
	var j = 0;
	
	for (i=0; i<inputs.length; i++) {
		if (inputs[i].type == "checkbox") {
			checkboxes[j] = inputs[i];
			j++;
		}
	}
	
	for (i=0; i<checkboxes.length; i++) {
		if (checkboxes[i].checked == 1) {
			solutions += checkboxes[i].value + ",";
		}
	}
	return solutions;
}
	