// checks to verify a field entry at the least
function isNotEmpty(elem, whichField) { 
	var str = elem.value;
	// check each required "text" field, to make sure there is an entry and not empty
	if (str == null || str.length == 0) {
		alert("You must enter your [" +  whichField + "] to submit this form.");
		return false;
	} else {
		return true;
	}
	
}


// checks email address for proper format
function isEMailAddr(elem) {
	var str = elem.value;
	str = str.toLowerCase();
	if (str.indexOf("@") > 1) {
		var addr = str.substring(0, str.indexOf("@"));
		var domain = str.substring(str.indexOf("@") + 1, str.length);
		// at least one top level domain is required
		if (domain.indexOf(".") == -1) {
			alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
			return false;
		}
		//parse address protion first, character by character
		for (var i = 0; i < addr.length; i++) {
			oneChar = addr.charAt(i).charCodeAt(0);
			// dot or hyphen not allowed in the first position; dot in last
			if ((i == 0 && (oneChar == 45 || oneChar == 46)) || (i == addr.length - 1 && oneChar == 46)) {
				alert("Please verify the [username] portion of your [email address].  The username is everything before the @ sign.");
				return false;
			}
			// acceptable characters (- . _ 0-9 a-z)
			if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
				continue;
			} else {
				alert("Please verify the [username] portion of your [email address].  The username is everything before the @ sign.");
				return false;
			}
		}
		for (i = 0; i < domain.length; i++) {
			oneChar = domain.charAt(i).charCodeAt(0);
			if ((i == 0 && (oneChar == 45 || oneChar == 46)) || ((i == domain.length - 1 || i == domain.length - 2) && oneChar == 46)) {
				alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
				return false;
			}
			if (oneChar == 45 || oneChar == 46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
				continue;
			} else {
				alert("Please verify the [domain] portion of your [email address]. The domain is everything after the @ sign.");
				return false;
			}
		}
		return true;
	}
	alert("Please verify your [email address] is correct. Review the example address for proper format.");
	return false;
}


// this function is currently not being used
function isPhoneNum(elem) {
	var str = elem.value;
	//parse phone number character by character
	for (var i = 0; i < str.length; i++) {
		oneChar = str.charAt(i).charCodeAt(0);
		// dot or hyphen not allowed in the first position; dot in last
		if ((i == 0 && (oneChar == 46)) || (i == str.length - 1 && oneChar == 46)) {
			alert("You can not put a period in a phone number");
			return false;
		}
		// acceptable characters (- & 0-9 )
		if (oneChar == 43 || oneChar == 45 || oneChar == 46 || (oneChar > 47 && oneChar < 58)) {
			continue;
		} else {
			alert("You can only enter numbers, or hashes into a phone number. International numbers can inclue a plus sign.");
			return false;
		}
		return true;
	}
}


// checks a fieled to make sure only nymbers are being used
function isNumber(elem, whichField) {
	var str = elem.value;
	// checks
	//parse phone number character by character
	for (var i = 0; i < str.length; i++) {
		oneChar = str.charAt(i).charCodeAt(0);
		// acceptable characters (0-9 )
		if (oneChar > 47 && oneChar < 58) {
			continue;
		} else {
			alert("You can only enter numbers into the [" +  whichField + "] field. Please try again.");
			return false;
		}
	}
	return true;
}


function clearEmpty(elem) {
	var theBtn = elem.checked;
	var btnName = elem.name + "o";
	
	if (theBtn == false) { 
		eval('document.survey.' + btnName + '.value = ""');
		return true;
	}
}



function populateRecip() {
	certForm.recipient_address.value = certForm.sender_address.value;
	certForm.recipient_city.value = certForm.sender_city.value;
	certForm.recipient_state.value = certForm.sender_state.value;
	certForm.recipient_zip.value = certForm.sender_zip.value;
	certForm.recipient_phone_home.value = certForm.sender_phone_home.value;
	certForm.recipient_phone_cell.value = certForm.sender_phone_cell.value;
}

function checkGift() {
	var giftFlag = 0;
	for (i=0; i < certForm.gift_amount.length; i++) {
		if(certForm.gift_amount[i].checked == true) {
			if (certForm.gift_amount[i].value == "Other Selected") {
				if (certForm.gift_amount_other.value != "") {
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
		} else if((i == certForm.gift_amount.length - 1) && (giftFlag == 0)) {
			return false;
		}
										
	}	
}

function setOther(flag) {
	var whichRoutine = flag;
	if (whichRoutine == 1) {
		certForm.gift_amount_other.value = "";											
	} else if (whichRoutine == 2) {
		var giftFlag = 0;
		for (i=0; i < certForm.gift_amount.length; i++) {
			if(certForm.gift_amount[i].value != "Other Selected") {
				certForm.gift_amount[i].checked = false;
			} else {
				certForm.gift_amount[i].checked = true;
				certForm.gift_amount_other.focus();
			}
											
		}
	}
}

function clearRecipient() {
	certForm.recipient_firstname.value = "";
	certForm.recipient_lastname.value = "";
	certForm.recipient_address.value = "";
	certForm.recipient_city.value = "";
	certForm.recipient_state.value = "";
	certForm.recipient_zip.value = "";
	certForm.recipient_phone_home.value = "";
	certForm.recipient_phone_cell.value = "";
	certForm.recipient_firstname.focus();
}