// Generic pattern validation function
function isValid(pattern, str) {
	return pattern.test(str)
}

// Checks for any letter
function hasLetter(str) {
	var letterexp = /[a-z]/i;
	return letterexp.test(str)
}

// Checks for any characters except newline
function hasChar(str) {
	var charexp = /./;
	return charexp.test(str)
}

function stripNonDigits(str) {
// strips out all non-digits
// returns back new string of only digits
	return str.replace(/[^0-9]/g,'');
}

function isSelectEmpty(selectObj, index) {
	var obj = eval(selectObj);
	if (obj.selectedIndex == index)
		return true;
	return false;
}

// returns back error message, blank if no errors
function isRadioChecked(radioObj) {	
	var obj = eval(radioObj);
	var msg = "";
	for (var i=0; i<obj.length; i++) {
	  if (obj[i].checked) {
	    return msg;
	  }
	}
	msg = "Please select a " + obj[0].name + " value.\n";
	return msg;
}

function isValidEmail(str) {
	// var emailexp = /^\w+@\w+(\.\w+)+$/;
	// var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]{3}$/i;
	var emailexp = /^[A-Za-z_0-9'\.\-]+@[A-Za-z_0-9'\.\-]+(\.\w+)+$/;
	var vcheck = isValid(emailexp, str);
	return vcheck
}

function isValidZipcode(str) {
	var zipexp = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/;

	var vcheck = isValid(zipexp, str);
	return vcheck
	// msg = "\"" + str + "\" is an invalid zip code!  Please use format xxxxx(-xxxx).\n";
} 

function isValidPhone(str) {
	var phonexp =  /^\d{10}$/;
	var newphone = "";
	
	// check the phone entry by first stripping off all non-digits
	if (hasChar(str)) {	
		newphone = stripNonDigits(str);	
		var notvalid = !isValid(phonexp, newphone);
	}
	if (newphone="" || notvalid) {
		return false;	// may have forgotten to enter area code
	} else {
		return true;
	}
}