// hack to fix IE6 text selection bug
function hackheight() {
	if ((navigator.userAgent).indexOf("MSIE") != -1) {
		// This doesn't seem to hurt non-IE, but just in case
		// document.write(navigator.userAgent);
		document.body.style.height = 0;
		document.body.style.height = document.documentElement.scrollHeight+'px';
		// alert("hackheight");
	}
}

// Add trim to strings:
function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.trim = strtrim;
	
function isEmpty(iStr) {
	return (iStr == null || iStr == "" || iStr.trim() == "")
}

// valid email
function isValidEmail(sEmail) {
 // sEmail must contain at least one @ and at least one period, neither at beginning or end
	var iPos = sEmail.indexOf("@");
	if (iPos <= 0 || iPos == sEmail.length-1) {
		alert("Please enter a valid Email address.");
		return false;
	}
	iPos = sEmail.indexOf(".");
	if (iPos <= 0 || iPos == sEmail.length-1) {
		alert("Please enter a valid Email address.");
		return false;
	}
	return true;
}

// valid phone
function isValidPhone(sPhone) {
 // sPhone must contain at least 7 characters, could be smarter
	if (sPhone.trim().length < 7) {
		alert("Please enter a valid Phone number.");
		return false;
	}
	return true;
}

// Individual forms validation

// Add variable to forms
function addSecret(frmForm) {
	//alert("Name: " + frmForm.Name.value);
	var newvar = document.createElement("input");
	newvar.setAttribute("type", "hidden");
	newvar.setAttribute("name", "Secret");
	newvar.setAttribute("value", "MB20060707");
	frmForm.appendChild(newvar);
	return true;
}

// Guest registry form:
function isValidGuest(frmForm) {
 // Name and either Phone or Email must be present
 // If not empty, Phone must be reasonable
 // If not empty, EmailFrom must pass isValidEmail
 // If newsletter is requested, must have Email
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your name.");
		return false;
	}
	var sPhone =  frmForm.Phone.value;
	if (!isEmpty(sPhone)) {
		if (!isValidPhone(sPhone)) {
			return false;
		}
	}
	var sAddress = frmForm.Address.value;
	var sEmail = frmForm.EmailFrom.value;
	if ((!isEmpty(sEmail)) || (frmForm.Newsletter.checked)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	if (isEmpty(sPhone) && isEmpty(sEmail) && isEmpty(sAddress)) {
		alert("Please enter Phone, Address, or Email.");
		return false;
	}
	return addSecret(frmForm);
}

// Outreach collections form:
function isValidOutreach(frmForm) {
 // Name and Description must be non-empty
 // If not empty, Phone must be reasonable
 // If not empty, EmailFrom must be reasonable
 // Must be at least one of Phone or EmailFrom
 // Disable http: in Description to help avoid spam
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter Name of Applicant.");
		return false;
	}
	var sPhone =  frmForm.Phone.value;
	if (!isEmpty(sPhone)) {
		if (!isValidPhone(sPhone)) {
			return false;
		}
	}
	var sEmail = frmForm.EmailFrom.value;
	if (!isEmpty(sEmail)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	if (isEmpty(sPhone) && isEmpty(sEmail)) {
		alert("Please enter Phone or Email.");
		return false;
	}
	var sDescription= frmForm.Description.value.toLowerCase();
	if (isEmpty(sDescription)) {
		alert("Please enter a Description.");
		return false;
	}
	if (sDescription.indexOf("http:") != -1) {
		alert("Description not allowed.");
		return false;
	}
	
	return true;
}


// Auction donation form:
function isValidAuction(frmForm) {
 // Name and Donation Title must be non-empty
 // It not empty, Phone must be at least 7 characters
 // If not empty, EmailFrom must pass isValidEmail
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your Name.");
		return false;
	}
	var sDonation = frmForm.Donation.value;
	if (isEmpty(sDonation)) {
		alert("Please enter the Donation Title, a one-line description.");
		return false;
	}
	var sPhone =  frmForm.Phone.value;
	if (!isEmpty(sPhone)) {
		if (!isValidPhone(sPhone)) {
			return false;
		}
	}
	var sEmail = frmForm.EmailFrom.value;
	if (!isEmpty(sEmail)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	return true;
}

// Vision form:
function isValidVision(frmForm) {
 // Name and Description must be non-empty
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your Name.");
		return false;
	}
	var sDescription = frmForm.Description.value;
	if (isEmpty(sDescription)) {
		alert("Please enter your vision.");
		return false;
	}
	return true;
}

// Remembrance form:
function isValidRemembrance(frmForm) {
 // Name and Description must be non-empty
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your Name.");
		return false;
	}
	var sDescription = frmForm.Description.value;
	if (isEmpty(sDescription)) {
		alert("Please enter your remembrance.");
		return false;
	}
	return true;
}

// Simple Gifts Signup form:
function isValidSGCH(frmForm) {
 // Name must be non-empty
 // If not empty, Phone must be reasonable
 // If not empty, EmailFrom must be reasonable
 // Must be at least one of Phone or EmailFrom
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your Name.");
		return false;
	}
	var sPhone =  frmForm.Phone.value;
	if (!isEmpty(sPhone)) {
		if (!isValidPhone(sPhone)) {
			return false;
		}
	}
	var sEmail = frmForm.EmailFrom.value;
	if (!isEmpty(sEmail)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	if (isEmpty(sPhone) && isEmpty(sEmail)) {
		alert("Please enter Phone or Email.");
		return false;
	}
	
	return true;
}
