function validateAndSend() {
	// invoked when saving a new or existing product
	var f = document.forms['newsletter'];

	// perform validation
	if (!validate(f.email, 'email address', ['required', 'email'])) return;
	f.submit();	
}
// given a form input and an array of checks, perform data validation checks
function validate(input, label, checks) {
	for (i=0; i<checks.length; i++) {
		// replace first occurrence of | with (, and any other occurrences of | with ,
		// if there were no occurrences of | to begin with, add ( to the end of the check
		newCheck = checks[i].replace(/\|/, '(');
		newCheck = newCheck.replace(/\|/g, ', ');
		if (newCheck == checks[i]) {
				newCheck = newCheck + '(';
		} else {
				newCheck = newCheck + ', ';
		}
		valid = eval (newCheck + " input, label);");
		if (valid != true) {
			var theType = getType(input);
			if (theType != 'radio') {
				if (theType != 'select-one') {
						input.select();
				}
				input.focus();
			}
			alert(valid);
			return false;
		}
	}
	return true;
}
// given a form input object, return its type
function getType(input) {
    if (!input.type) { return input[0].type; }
    else { return input.type; }
}       
// given a form input, return its value
function getValue(input) {
	var inputType = getType(input);
	if (inputType == 'select-one') {
		return trim(input[input.selectedIndex].value);
	} else if (inputType == 'radio') {
		for (var i=0; i<input.length; i++) {
			if (input[i].checked)
				return trim(input[i].value);
		}
	} else if (inputType == 'checkbox') {
		if (input.checked)
			return trim(input.value);
		else
			return 0;
	} else {
		return trim(input.value);
	}
	return 0;       // this will be returned if, for example, 'input' is a radio, and nothing is selected
}
// given a form input, set its value
function setValue(input, theValue) {
	var inputType = getType(input);
	if (inputType == 'select-one') {
		for (var i=0; i<input.length; i++) {
			if (input[i].value == theValue)
				input.selectedIndex = i;
		}
	} else if (inputType == 'radio') {  
		for (var i=0; i<input.length; i++) {
			if (input[i].value == theValue)
				input[i].checked = true;
		}
	} else {
		input.value = theValue;
	}
}
        
// input value is required; it cannot be blank
function required(input, label) {
	var theType = getType(input);
	switch (theType) {
		case 'radio':
			valid = false;
			for (i=0; i<input.length; i++) {
				valid = valid || input[i].checked;
			}
			break;
		default:
			valid = (getValue(input) != '');
	} 
	if (!valid) {
		if (theType == 'select-one')
			return ('Please choose a ' + label + ' before continuing.');
		else
			return ('Please enter your ' + label + ' before continuing.');
	} else {        
		return true;
	}
}

// trims whitespace from input
function trim(str) {
	return (str.replace(/^\W+/,'')).replace(/\W+$/,'');
}
 
// remove all whitespace from input
function strip(input) {
	var str = getValue(input);
	var str2 = (str.replace(/^\W+/,'')).replace(/\W+$/,'');
	input.value = str2;
}

// return whether or not the input value is a valid email address
function email(input, label) {
	var emailStr = trim(input.value);
	if (emailStr == '') { return true; }

	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);
	
	if (matchArray == null) {
		return ('Your ' + label + ' seems incorrect.');
	}

	var user = matchArray[1];
	var domain = matchArray[2];
	if (user.match(userPat) == null) {
		return ('Your ' + label + ' seems incorrect - check the username.');
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i = 1; i = 4; i++) {
			if (IPArray[i] > 255) {
				return ('Your ' + label + ' seems incorrect - check the IP address.');
			}
		}
		return true;
	} else {
	
		var domainArray = domain.match(domainPat);
		if (domainArray == null) {
			return ('Your ' + label + ' seems incorrect - check the domain name.');
		}
	 
		var atomPat = new RegExp(atom,"g");
		var domArr = domain.match(atomPat);
		var len = domArr.length;
		if (domArr[len-1].length < 2 || domArr[len-1].length > 3) {
			return ('Your ' + label + ' seems incorrect - check the domain.');
		}
	
		if (len < 2) {
			return ('Your ' + label + ' seems incorrect - check the hostname.)');
		}
	}
	return true;
}
