// 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() == "")
}

function string2float(iStr) {
 //convert to float, ignoring anything other than numbers and one decimal

	var ret1 = "";
	var seenPeriod = false;
	for (var i = 0; i < iStr.length; i++) {
		var oneChar = iStr.charAt(i);
		if (oneChar == "." && !seenPeriod) {
			seenPeriod = true;
			ret1 += oneChar;
		}
		if (oneChar >= "0" && oneChar <= "9") {
			ret1 += oneChar;
		}
	}
	return parseFloat(ret1);
}

function isCurrency(iStr) {
 //what I think PayPal accepts:
 //numbers only with optional leading dollar sign, one comma (max $5,000), 
 //one decimal point, and max 2 decimal places

	iStr = iStr.trim();
	var seenComma = false;
	var seenPeriod = false;
	for (var i = 0; i < iStr.length; i++) {
		var oneChar = iStr.charAt(i);
		if (oneChar == "$" && i == 0) {
			continue;
		}
		if (oneChar == "," && !seenComma) {
			seenComma = true;
			continue;
		}
		if (oneChar == "." && !seenPeriod) {
			seenPeriod = true;
			continue;
		}
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
		if (seenPeriod && (i < iStr.length-2)) {
			//more than 2 decimal places
			return false;
		}
	}
	return true;
}

function email_obscure (username, domain, ext, content) {
 // Build mailto tag that's hard to harvest, given user, domain, and optional content
 // If content is not given, user and domain are used

	var atsign = "&#64;";
	var dot = "&#46;";
        var addr = username + atsign + domain + dot + ext;
	if (!content) {
		content = addr;
	}
        document.write( 
          "<" + "a" + " " + "href=" + "mail" + "to:" + addr + ">" + content + "<\/a>"
	);
}


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 info on contact form:
function isValidInput(frmForm) {
 // Name required
 // Phone must be >= 7 characters if not empty
 // Email must be isValidEmail if not empty
 // At least Address, Phone, or Email is required
 // Disable http: in Description to help avoid spam
	var sName = frmForm.Name.value;
	if (isEmpty(sName)) {
		alert("Please enter your Name.");
		return false;
	}
	var sPhone =  frmForm.Phone.value;
	if (!isEmpty(sPhone)) {
		if (sPhone.trim().length < 7) {
			alert("Please enter a valid Phone number.");
			return false;
		}
	}
	var sEmail = frmForm.EmailFrom.value;
	if (!isEmpty(sEmail)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	var sAddress = frmForm.Address.value;
	if (isEmpty(sAddress) && isEmpty(sPhone) && isEmpty(sEmail)) {
		alert("Please enter your Phone or Email so I can get back to you.");
		return false;
	}
	var sComments = frmForm.Comments.value.toLowerCase();
	if (!isEmpty(sComments)) {
		if (sComments.indexOf("http:") != -1) {
			alert("Web addresses are not allowed in Comments.");
			return false;
			}
	}
	return true;
}

// Valid online info on contribute form:
function isValidContributeOnline(frmForm) {
 // Occupation, Employer required
 // Phone required, must be >= 7 characters
 // Amount $1.00 to $1000
	var sPhone =  frmForm.item_number_1.value;
	if (isEmpty(sPhone)) {
		alert("Please enter your Phone number.");
		return false;
	}
	if (sPhone.trim().length < 7) {
		alert("Please enter a valid Phone number.");
		return false;
	}
	if (isEmpty(frmForm.os0_1.value)) {
		alert("Please enter your Occupation.");
		return false;
	}
	if (isEmpty(frmForm.os1_1.value)) {
		alert("Please enter your Employer.");
		return false;
	}
	var sAmount = frmForm.amount_1.value;
	if (isEmpty(sAmount) || !isCurrency(sAmount)) {
		alert("Please enter a valid Amount.");
		return false;
	}
	cAmount = string2float(sAmount);
	if (cAmount > 5000) {
		alert("Please enter an Amount of $5000 or less.");
		return false;
	}
	if (cAmount < 1) {
		alert("Please enter an Amount of at least $1.00.");
		return false;
	}
	frmForm.amount_1.value = cAmount;
	return true;
}

// Valid mail info on contribute form:
function isValidContributeMail(frmForm) {
 // Name, Address, City State Zip required
 // Email must be isValidEmail if not empty
 // Also checks in isValidContributeOnline
	var Name = frmForm.Name.value
	if (isEmpty(Name)) {
		alert("Please enter your Name.");
		return false;
	}
	var Address = frmForm.Address.value
	if (isEmpty(Address)) {
		alert("Please enter your Address.");
		return false;
	}
	if (isEmpty(frmForm.CityStateZip.value)) {
		alert("Please enter your City, State, and Zip.");
		return false;
	}
	var sEmail = frmForm.EmailFrom.value;
	if (!isEmpty(sEmail)) {
		if (!isValidEmail(sEmail)) {
			return false;
		}
	}
	if (!isValidContributeOnline(frmForm)) {
		return false;
	}
	return true;
}

//print the contribute form:
function printContribute(frmForm) {
	if (isValidContributeMail(frmForm)) {
		window.print();
	}
	return true;
}
