/* These functions are used to validate form data. */

// This function is designed to validate a 15 digit phone number.
function validPhone(phone_number) {
	while(phone_number.indexOf(" ")!=-1) {
		phone_number = phone_number.replace(" ","");
	}
	while(phone_number.indexOf("(")!=-1) {
		phone_number = phone_number.replace("(","");
	}
	while(phone_number.indexOf(")")!=-1) {
		phone_number = phone_number.replace(")","");
	}
	while(phone_number.indexOf(".")!=-1) {
		phone_number = phone_number.replace(".","");
	}
	while(phone_number.indexOf("-")!=-1) {
		phone_number = phone_number.replace("-","");
	}

	if (isNaN(phone_number) || phone_number.length>15) {
		return false;
	}else {
		return true;
	}// else
}// validPhone(...)

// This function is designed to validate a zip code.
function validZip(zip) {
	while(zip.indexOf(" ")!=-1) {
		zip = zip.replace(" ","");
	}
	while(zip.indexOf("-")!=-1) {
		zip = zip.replace("-","");
	}

	if (isNaN(zip) || (zip.length!=5 && zip.length!=9)) {
		return false;
	}else {
		return true;
	}// else
}// validZip(...)

// This function is designed to validate an email address by verifying the presence of the "@" and "." in the address.
function validEmail(email_address) {
	//KLR changed 09-22-2004, so it uses better email validating
   if (email_address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}// validEmail(...)

// This function is designed to return the selected value in a radio button array.
function getRadioValue(radioObject) {
	value = ""
	for (i=0;i<radioObject.length;i++) {
		if (radioObject[i].checked){
			value = radioObject[i].value;
			break;
		}// if (radi... 
	}//  for (i-...
	return value;
}// getRadioValue(...)

// This function displays a custom error message for forms that only validate the
// postal address when the user has indicated postal as a preferred contact method.
function validPostalChoice(address,city,state,zip) {
    msg = "You have chosen to be contacted by postal mail.  Please ";
    return validPostalTest(address,city,state,zip,msg);
}

// This function displays a generic error message for forms that simply require
// a postal address.
function validPostal(address,city,state,zip) {
    msg = "Please ";
    return validPostalTest(address,city,state,zip,msg);
}

// This function validates the postal mail address.
function validPostalTest(address,city,state,zip,msg) {
	started = false;
	if(msg == "") {
	    msg = "Please ";
	}
	msg2 = "";
	if(address.length<=0) {
		msg = msg + "enter your address";
		started = true;
	}
	if(city.length<=0) {
		if(started) {
			msg2 = "city";
		} else {
			msg = msg + "enter a city";
			started = true;
		}
	}
	if(state.length<=0) {
		if(started && msg2.length>0) {
			msg = msg + ", " + msg2;
			msg2 = "select a state";
		} else {
			if(started) {
			    msg2 = "select a state";
			} else {
			    msg = msg + "select a state";
			    started = true;
			}
		}
	}
	if(!validZip(zip)) {
		if(started && msg2.length>0) {
			msg = msg + ", " + msg2;
			msg2 = "enter a valid postal code";
		} else {
			if(started) {
			    msg2 = "enter a valid postal code";
			} else {
			    msg = msg + "enter a valid postal code";
			    started = true;
			}
		}
	}
	
	if(msg2!="") {
		msg = msg + " and " + msg2;
	}
	msg = msg + ".";
	
	if(started) {
		alert(msg);
		return false;
	} else {
	    return true;
	}
}

function oneChecked(oForm, groupName) { 
	var el, i = 0, grp = oForm[groupName]; 
	if (typeof grp != 'undefined') { 
		if (typeof grp.length != 'undefined') { 
			while (el = grp[i++]) if (el.checked) break; 
			if (i > grp.length) { 
				return false; 
			} 
		} else if (!grp.checked) { 
			return false; 
		} 
	} 
	return true; 
} 