/////////////////////////////////////////////////////////
//
// (lastname AND firstname) AND
// ((phone OR Phone2) OR (address AND city AND zip) OR emailaddress) AND
// (one or more activites)
//
/////////////////////////////////////////////////////////
function value(s) {	var e = ff_getElementByName(s);		return e.value;	}
function isblank(s) {var s = value(s);	return s.length == 0; 	}
function newline() {	var s = "\n";	return s; }

// zip
function validateZIP(field) {
	var error = "";

	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) {
		error = "Please enter your 5 digit or 5 digit+4 zip code.";
		return error;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			error = "Invalid characters in your zip code.";
			return error;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			error = "The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.";
			return error;
		}
	}
	return error;
}

// email
function checkEmail (strng) {
	var error = "";
	var emailFilter = /^.+@.+\..{2,3}$/;

	if (!(emailFilter.test(strng))) { 
		error = "Please enter a valid email address." + newline();
	} else {	//test email for illegal characters
	
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
	
		if (strng.match(illegalChars)) {
			error = "The email address contains illegal characters." + newline();
		}
	}
	return error;
}

// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
	var error = "";
 
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');   //strip out acceptable non-numeric characters

	if (isNaN(stripped)) {
		error = "The phone number contains illegal characters." + newline();
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code." + newline();
	} 
	return error;
}

function volunteerValidate() {
	var error = "";

	// lastname and firstname

	if (isblank("psa_first_name")) {
		error += "Please enter your first name." + newline();
	}

	if (isblank("psa_name")) {
		error += "Please enter your last name." + newline();
	}

	// have one or more of the following contact methods

	// true if phone OR phone2 filled in
	var bphone = !isblank("psa_phone") || !isblank("psa_phone_2");

	// true if email filled in
	var bemail = !isblank("psa_email");

	// true if city AND address AND zip, ALL FILLED IN
	var baddress = (!isblank("psa_address") && !isblank("psa_city") && !isblank("psa_zip"));

	// contact user by phone and/or email

 //alert("emailme=" + bemailme + ", phoneme=" + bcallme);
	
	if (!bphone && !baddress && !bemail) {	// phone, address and email cant all be blank
		error += "Please specify a phone, or full mailing address, or email." + newline();

	} else {	// phone or email or address supplied 
		if (bphone) {	// phone specified, validate input
			if (!isblank("psa_phone")) error += checkPhone (value("psa_phone"));
			if (!isblank("psa_phone_2")) error += checkPhone (value("psa_phone_2"));
		}
		if (bemail) { 	// email specified, validate email
			error += checkEmail (value("psa_email"));
		} 

		if (!isblank("psa_address") || !isblank("psa_city") || !isblank("psa_zip"))	{		// one or more address components entered... validate address
			if (isblank("psa_address") || isblank("psa_city") || isblank("psa_zip")) {			// must have address AND city AND zip to be valid
				error += "Please enter street address AND city AND zip." + newline();
			} else {	// should check zip here
				error += validateZIP (value("psa_zip"));
			}
		}	
	}
	return error;
}

function yardsignValidate() {
	var error = "";

	// lastname and firstname

	if (isblank("psa_name")) {
		error += "Please enter your name." + newline();
	}

	// have one or more of the following contact methods

	// true if phone OR phone2 filled in
	var bphone = !isblank("psa_phone") || !isblank("psa_phone_2");

	// true if email filled in
	var bemail = !isblank("psa_email");

	// true if city AND address AND zip, ALL FILLED IN
	var baddress = (!isblank("psa_address") && !isblank("psa_city") && !isblank("psa_zip"));

	// contact user by phone and/or email

	if (!bphone && !baddress && !bemail) {	// phone, address and email cant all be blank
		error += "Please specify a phone, or full mailing address, or email." + newline();

	} else {	// phone or email or address supplied 
		if (bphone) {	// phone specified, validate input
			if (!isblank("psa_phone")) error += checkPhone (value("psa_phone"));
			if (!isblank("psa_phone_2")) error += checkPhone (value("psa_phone_2"));
		}
		if (bemail) { 	// email specified, validate email
			error += checkEmail (value("psa_email"));
		} 

		if (!isblank("psa_address") || !isblank("psa_city") || !isblank("psa_zip"))	{		// one or more address components entered... validate address
			if (isblank("psa_address") || isblank("psa_city") || isblank("psa_zip")) {			// must have address AND city AND zip to be valid
				error += "Please enter street address AND city AND zip." + newline();
			} else {	// should check zip here
				error += validateZIP (value("psa_zip"));
			}
		}	
	}
	return error;
}