//-- Preload images to cure the browser flickering issue
if (document.images) {
	var aryImages = new Array(
		"images/spacer.gif",
		"images/obituaries_over.jpg",
		"images/funerals_over.jpg",
		"images/veterans_over.jpg"
	);
	var aryPreload = new Array();
	for (var img = 0; img < aryImages.length; img++) {
		aryPreload[img] = new Image();
		aryPreload[img].src = aryImages[img];
	}
}

Array.prototype.contains = function(obj) {
	var i = this.length;
	while (i--) {
		if (this[i] === obj) {
			return true;
		}
	}
	return false;
}

function toggleFAQ(faqId) {
	try {
		if ($("#faq" + faqId).hasClass("selected")) {
			//-- hide answer
			$("#faq" + faqId).removeClass("selected");
			$("#faq" + faqId + " .question a").attr("title", "Click to view answer");
			$("#faq" + faqId + " .answer").slideUp("fast");
		} else {
			//-- comment next three lines to allow multiple open faqs
			$(".faqs li.selected .question a").attr("title", "Click to view answer");
			$(".faqs li.selected .answer").slideUp("fast");
			$(".faqs li.faq").removeClass("selected");
			//-- show answer
			$("#faq" + faqId).addClass("selected");
			$("#faq" + faqId + " .question a").attr("title", "Click to hide answer");
			$("#faq" + faqId + " .answer").slideDown("fast");
		}
	} catch (err) { }
	return false;
}

function validateGuestbookForm(theForm) {
	var isValid = true;
	isValid = validateFormField(isValid, theForm.strName, "Your name is required!");
	isValid = validateFormField(isValid, theForm.strEmail, "Your email address is required!");
	isValid = validateFormFieldEmail(isValid, theForm.strEmail, "Your email address is invalid!");
	isValid = validateFormField(isValid, theForm.strComments, "Your comments are required!");
	isValid = validateFormField(isValid, theForm.strSecurityPhrase, "You must enter the security phrase!\n\nPlease note that the phrase is case sensitive.");
	isValid = validateFormFieldEquals(isValid, theForm.strSecurityPhrase, theForm.strSecurityPhraseValue, "You must enter the correct security phrase!\n\nPlease note that the phrase is case sensitive.");
	return isValid;
}

function validateFormField(isValid, theField, strMessage) {
	if (isValid && theField) {
		var strType = theField.type;
		try {
			if (theField[0].type != undefined) {
				strType = theField[0].type;
			}
		} catch (er) { }
		var strValue = "";
		var isChecked = false;
		if (strType.indexOf("select") < 0) {
			if (strType.indexOf("checkbox") < 0 && strType.indexOf("radio") < 0) {
				strValue = new String(theField.value);
			} else {
				for (var i=0; i<theField.length; i++) {
					if (theField[i].checked) {
						strValue = new String(theField[i].value);
						isChecked = true;
						break;
					}
				}
			}
		} else {
			strValue = theField.options[theField.selectedIndex].value;
			strValue = (strValue.replace(/^\s+|\s+$/g, "") == "-1" ? "" : strValue);
		}
		if (strValue.replace(/^\s+|\s+$/g, "") == "" && !isChecked) {
			try {
				theField.focus();
			} catch (er) { }
			if (strMessage.replace(/^\s+|\s+$/g, "") != "") {
				alert(strMessage);
			}
			return false;
		}
		return true;
	}
	return isValid;
}

function validateFormFieldEmail(isValid, theField, strMessage) {
	if (isValid && theField) {
		var strType = theField.type;
		var strValue = (new String(theField.value)).replace(/^\s+|\s+$/g, "");

		if (strValue.replace(/^\s+|\s+$/g, "") == "") {
			try {
				theField.focus();
			} catch (er) { }
			if (strMessage.replace(/^\s+|\s+$/g, "") != "") {
				alert(strMessage);
			}
			return false;
		}
		var objRegExp = /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/g;
		if (!objRegExp.test(strValue)) {
			try {
				theField.focus();
			} catch (er) { }
			if (strMessage.replace(/^\s+|\s+$/g, "") != "") {
				alert(strMessage);
			}
			return false;
		}
		return true;
	}
	return isValid;
}

function validateFormFieldEquals(isValid, theField1, theField2, strMessage) {
	if (isValid && theField1 && theField2) {
		if (theField1.value.replace(/^\s+|\s+$/g, "") != theField2.value.replace(/^\s+|\s+$/g, "")) {
			if (strMessage.replace(/^\s+|\s+$/g, "") != "") { alert(strMessage); }
			try {
				theField1.focus();
			} catch (er) { }
			return false;
		}
		return true;
	}
	return isValid;
}

var maxLengthField;
function imposeMaxLength(field, event) {
	//-- maxlength attribute must be set
	try {
		if (field.value.replace(/^\s+|\s+$/g, "") == "") {
			return true;
		}
		event = event || window.event;
		code = event.which || event.keyCode;
		//-- allow backspace, tab, shift, ctrl, home, end, arrows, del
		var aryCodes = [ 8, 9, 16, 17, 35, 36, 37, 38, 39, 46 ];
		if (aryCodes.contains(code)) {
			return true;
		}
		var maxlen = (field.getAttribute ? parseInt(field.getAttribute("maxlength")) : "");
		if (isNaN(maxlen)) {
			return true;
		}
		if (field.value.length >= maxlen) {
			field.value = field.value.substring(0, maxlen-1);
			maxLengthField = field;
			setTimeout("maxLengthField.scrollTop = maxLengthField.scrollHeight;", 0);
		}

		return (field.value.length < maxlen);
	} catch (err) {
		//alert(err.description);
		return true;
	}
}

