function isblank(s) {
	for(var i=0; i< s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '')) return false;
	}
	return true;
}
					   
function validateContactMeForm(f) {

	//check that required fields are filled
	var msg;
	var empty_fields = "";
	var errors = "";
	var requiredFields = new Array ("contactFirst", "contactLast", "contactCompany", "contactStreet", "contactCity", "contactPostalCode", "kp_Country", "contactPhone", "contactEmail", "organizationType", "organizationSize");
	var displayNames = new Array ("First Name", "Last Name", "Company", "Street Address", "City", "Zip/Postal Code", "Country", "Phone", "Email Address", "Organization Type", "Organization Size");

	// add the optional questions to the required fields if the question is not blank
	for(i=1; i<=10; i++) {
		var question = document.getElementById("q"+i);
		if (!question =='') {
			if (!question.innerText =='') {
				var eID = "a"+i;
				var e = document.getElementById(eID);
				requiredFields[requiredFields.length] = e.name;
				displayNames[displayNames.length] = "Question " + i;
			}
 		}
	}

	// add the job title to the required fields list if the lead is not an education lead
	var title = document.getElementById("jobTitleField");
	if (title.style.display == '') { 
		requiredFields[requiredFields.length] = "contactJobTitle";
		displayNames[displayNames.length] = "Job Title";
	}
	
	// add state to required fields list if the country has states
	var statesList = document.getElementById("states");
	if (!statesList.disabled) { 
		requiredFields[requiredFields.length] = "kf_State";
		displayNames[displayNames.length] = "State/Province";
	}
	
	// add education fields to the required fields list if this is an education lead
	var orgType = document.getElementById('orgTypeId').value;
	var country = document.getElementById('countryId').value;
	if ((country=='227' && (orgType==4 || orgType==5)) || (country=='38' && orgType==4)) {
		requiredFields[requiredFields.length] = "edInstitution";
		displayNames[displayNames.length] = "Institution";
		requiredFields[requiredFields.length] = "edJobTitle";
		displayNames[displayNames.length] = "Job Title";
		requiredFields[requiredFields.length] = "edCommunicationPref";
		displayNames[displayNames.length] = "\"Contact Me By\" preference";
	}
	
	// check to see if required fields are not filled out
	for (var i=0; i<f.elements.length; i++) {
		e = f.elements[i];
		for (var r=0; r<requiredFields.length; r++) {
			if (e.name == requiredFields[r]) {
				if (e.type == "radio" || e.type == "checkbox") {
					var choices = document.getElementsByName(e.name);
					var choice = -1;
					for(k=0; k<choices.length; k++) {
						if (choices[k].checked) choice = k; 
					}
					if (choice < 0) { empty_fields += "\n" + displayNames[r]; }
					i = i+choices.length;
					continue;
				}
				else {
					if (e.value == null || e.value == "" || isblank(e.value)) {
					   empty_fields += "\n" + displayNames[r];
					   continue;
					}
				}
			}
		}
	}
	
	// if required values are empty, show an alert box naming which ones are not filled out
	if (empty_fields) {
		   alert("The following fields are required: " + empty_fields);
		   return false;
	}

	//show an alert box if the email address is not valid
	with (f.contactEmail) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert("The email address you entered is invalid.");
			return false;
		}
	}
	return true;
}
