/////////////////////////////////////////////////////////
//
// (lastname AND firstname) AND
// ((phone OR Phone2) OR (address AND city AND zip) OR emailaddress) AND
// (one or more activites)
//
// prefix functions defined here "vf_" for "volunteer form"
//
/////////////////////////////////////////////////////////

function vf_newline() {var s = "\n";	return s;}
function vf_value(s) {var e = ff_getElementByName(s);	return e.value;}
function vf_isblank(s) {var s = vf_value(s);	return s.length == 0;}

// zip
function vf_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." + vf_newline();
		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." + vf_newline();
			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'." + vf_newline();
			return error;
		}
	}
	return error;
}

// email
function vf_checkEmail(strng) {
	var error = "";
	var emailFilter = /^.+@.+\..{2,3}$/;

	if (!(emailFilter.test(strng))) { 
		error = "Please enter a valid email address." + vf_newline();
	} else {	//test email for illegal characters
	
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
	
		if (strng.match(illegalChars)) {
			error = "The email address contains illegal characters." + vf_newline();
		}
	}
	return error;
}

// phone number - strip out delimiters and check for 10 digits

function vf_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." + vf_newline();
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code." + vf_newline();
	} 
	return error;
}

function vf_anychecked(a) {
	var e;

	for(var i = 0; i < a.length; i++) {
		e = ff_getElementByName(a[i]);
	
		if (e.checked) return true;
	}
	return false;
}

function vf_ischecked(a) {
	var e;

	e = ff_getElementByName(a);
	if (e.checked) return true;
	return false;
}

function validate_form() {
	return vf_validate_volunteer();
}

function vf_validate_volunteer() {
	var error = "";

//	var tasks = new Array("host","office","phone","yard_sign","literature","put_up_signs","knock","endorse","post_cards","gotv","relay");
//
// 2009-09-02 - remove check for "host a gathering"
//	
	var tasks = new Array("office","phone","yard_sign","literature","put_up_signs","knock","endorse","post_cards","gotv","relay");

	// lastname and firstname

	if (vf_isblank("psa_first_name")) {
		error += "Please enter your first name." + vf_newline();
	}

	if (vf_isblank("psa_name")) {
		error += "Please enter your last name." + vf_newline();
	}

	// have one or more of the following contact methods

	// true if phone OR phone2 filled in
	var bphone = !vf_isblank("psa_phone") || !vf_isblank("psa_phone_2");

	// true if email filled in
	var bemail = !vf_isblank("psa_email");

	// true if city AND address AND zip, ALL FILLED IN
	var baddress = (!vf_isblank("psa_address") && !vf_isblank("psa_city") && !vf_isblank("psa_zip"));

	// contact user by phone and/or email
	var bemailme = vf_ischecked("email");
	var bcallme = vf_ischecked("telephone");

 //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." + vf_newline();

	} else {	// phone or email or address supplied 
		if (bphone) {	// phone specified, validate input
			if (!vf_isblank("psa_phone")) error += vf_checkPhone(vf_value("psa_phone"));
			if (!vf_isblank("psa_phone_2")) error += vf_checkPhone(vf_value("psa_phone_2"));
		}
		if (bemail) { 	// email specified, validate email
			error += vf_checkEmail(vf_value("psa_email"));
		} 
		
		if (!vf_isblank("psa_address") || !vf_isblank("psa_city") || !vf_isblank("psa_zip"))	{		// one or more address components entered... validate address
			if (vf_isblank("psa_address") || vf_isblank("psa_city") || vf_isblank("psa_zip")) {			// must have address AND city AND zip to be valid
				error += "Please enter street address AND city AND zip." + vf_newline();
			} else {	// should check zip here
				error += vf_validateZIP (vf_value("psa_zip"));
			}
		}	
	}
	
	// one or more activities

	if (!vf_anychecked(tasks)) {
		error += "Please check at least one activty." + vf_newline();
	}

	if (bcallme && !bphone) {
		error += "Please specify a phone number." + vf_newline();
	} 
	if (bemailme && !bemail) {
		error += "Please specify an email address." + vf_newline();
	}

	return error;
}


function validate_newsletter_form() {
	return vf_validate_newsletter();
}

function vf_validate_newsletter() {
	var error = "";

//	var tasks = new Array("host","office","phone","yard_sign","literature","put_up_signs","knock","endorse","post_cards","gotv","relay");
//
// 2009-09-02 - remove check for "host a gathering"
//	
////	var tasks = new Array("office","phone","yard_sign","literature","put_up_signs","knock","endorse","post_cards","gotv","relay");

	// lastname and firstname

	if (vf_isblank("psa_first_name")) {
		error += "Please enter your first name." + vf_newline();
	}

	if (vf_isblank("psa_name")) {
		error += "Please enter your last name." + vf_newline();
	}

	// have one or more of the following contact methods

	// true if phone OR phone2 filled in
	var bphone = !vf_isblank("psa_phone") || !vf_isblank("psa_phone_2");

	// true if email filled in
	var bemail = !vf_isblank("psa_email");

	// true if city AND address AND zip, ALL FILLED IN
	var baddress = (!vf_isblank("psa_address") && !vf_isblank("psa_city") && !vf_isblank("psa_zip"));

	// contact user by phone and/or email
	var bemailme = vf_ischecked("email");
	var bcallme = vf_ischecked("telephone");

 //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." + vf_newline();

	} else {	// phone or email or address supplied 
		if (bphone) {	// phone specified, validate input
			if (!vf_isblank("psa_phone")) error += vf_checkPhone(vf_value("psa_phone"));
			if (!vf_isblank("psa_phone_2")) error += vf_checkPhone(vf_value("psa_phone_2"));
		}
		if (bemail) { 	// email specified, validate email
			error += vf_checkEmail(vf_value("psa_email"));
		} 
		
		if (!vf_isblank("psa_address") || !vf_isblank("psa_city") || !vf_isblank("psa_zip"))	{		// one or more address components entered... validate address
			if (vf_isblank("psa_address") || vf_isblank("psa_city") || vf_isblank("psa_zip")) {			// must have address AND city AND zip to be valid
				error += "Please enter street address AND city AND zip." + vf_newline();
			} else {	// should check zip here
				error += vf_validateZIP (vf_value("psa_zip"));
			}
		}	
	}
	
	// one or more activities

////	if (!vf_anychecked(tasks)) {
////		error += "Please check at least one activty." + vf_newline();
////	}

	if (bcallme && !bphone) {
		error += "Please specify a phone number." + vf_newline();
	} 
	if (bemailme && !bemail) {
		error += "Please specify an email address." + vf_newline();
	}

	return error;
}
