/////////////////////////////////////////////////////////
//
// (lastname AND firstname) AND
// ((phone OR Phone2) OR (address AND city AND zip) OR emailaddress) AND
// (one or more activites)
//
// prefix functions defined here "sf_" for "survey form"
//
/////////////////////////////////////////////////////////

function sf_newline() {var s = "\n";	return s;}
function sf_value(s) {var e = ff_getElementByName(s);	return e.value;}
function sf_isblank(s) {var s = sf_value(s);	return s.length == 0;}

// zip
function sf_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." + sf_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." + sf_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'." + sf_newline();
			return error;
		}
	}
	return error;
}

// email
function sf_checkEmail(strng) {
	var error = "";
	var emailFilter = /^.+@.+\..{2,3}$/;

	if (!(emailFilter.test(strng))) { 
		error = "Please enter a valid email address." + sf_newline();
	} else {	//test email for illegal characters
	
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
	
		if (strng.match(illegalChars)) {
			error = "The email address contains illegal characters." + sf_newline();
		}
	}
	return error;
}

// phone number - strip out delimiters and check for 10 digits

function sf_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." + sf_newline();
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code." + sf_newline();
	} 
	return error;
}

/************
function sf_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 sf_ischecked(a) {
	var e;

	e = ff_getElementByName(a);
	if (e.checked) return true;
	return false;
}

function sf_validate_survey() {
	var error = "";

	// have ONE OR MORE of the following contact methods

	// true if phone OR phone2 filled in
	var bphone = !sf_isblank("psa_phone") || !sf_isblank("psa_phone_2");

	// true if email filled in
	var bemail = !sf_isblank("psa_email");

	// true if city AND address AND zip, ALL FILLED IN
	var baddress = (!sf_isblank("psa_address") && !sf_isblank("psa_zip"));

	// submit anonymously
	var vanon = my_getRadioButtonChecked("anonymous");	// yes | no
	var banon = (vanon == "yes") ? 1 : 0;

	// receive email newsletters and alerts
	var valert = my_getRadioButtonChecked("alerts");	// yes | no
	var balert = (valert == "yes") ? 1 : 0;

	//
	// Here is my name and contact information (if submitting non-anonymously)
	//
	if (banon == 0) {  
	   
      // lastname and firstname
   
	   if (sf_isblank("psa_first_name")) {
		   error += "Please enter your first name." + sf_newline();
	   }

	   if (sf_isblank("psa_name")) {
		   error += "Please enter your last name." + sf_newline();
	   }

		//
		// phone, address and email cant all be blank
		//
		if (!bphone && !baddress && !bemail) {
			error += "Please specify a phone, a mailing address, or email." + sf_newline();

		} else {	
			//
			// ONE OR MORE of phone or email or address supplied, validate each in turn 
			//
			if (bphone) {	// phone specified, validate input
				if (!sf_isblank("psa_phone")) error += sf_checkPhone(sf_value("psa_phone"));
				if (!sf_isblank("psa_phone_2")) error += sf_checkPhone(sf_value("psa_phone_2"));
			}
			if (bemail) { 	// email specified, validate email
				error += sf_checkEmail(sf_value("psa_email"));
			} 
			
			if (!sf_isblank("psa_address") || !sf_isblank("psa_zip"))	{			// one or more address components entered... validate address
				if (sf_isblank("psa_address") || sf_isblank("psa_zip")) {			// must have address AND zip to be valid
					error += "Please enter street address AND zip." + sf_newline();
				} else {	// should check zip here
					error += sf_validateZIP (sf_value("psa_zip"));
				}
			}	
		}
		//
		// check if user wants to receive newsletter
		//
		if (balert) {
			if (!bemail) {	// no (required) email supplied
				error += "Please specify an email address to receive newsletter." + sf_newline();
			}
		}
	}
	return error;
}

function validate_survey() {
	return sf_validate_survey();
}
