function rand(mn, mx) {
	return Math.round(Math.random() * (mx - mn) + mn);
}

function numbersOnly($str) {
	$s = "";
	for ($i=0; $i<$str.length; $i++) {
		if ("1234567890".indexOf($str.charAt($i))>-1) {
			$s += $str.charAt($i);
		}
	}
	return $s;
}

function checkEmail($str) {
// optional 2nd argument: "quiet" suppresses alert messages	
	filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
	if (!filter.test($str)) {
		if (arguments[1]!="quiet") {
			alert("There appears to be a problem with your email address.");
		}
		return false;
	} else {
		return true;
	}
}

function checkPhone($str) {
// optional 2nd argument: "quiet" suppresses alert messages
	$s = "";
	if ($str.length>0) {
		// remove all non-numbers
		$s = numbersOnly($str);
		if ($s.length != 10) {
			if (arguments[1]!="quiet") {
				alert("There appears to be a problem with your Phone Number.");
			}
		}
	}
	$str = "(" + $s.substr(0,3) + ") " + $s.substr(3,3) + "-" + $s.substr(6);
	return $str;
}

function checkZip($str) {
// optional 2nd argument: "quiet" suppresses alert messages
	$s = "";
	if ($str.length>0) {
		// remove all non-numbers
		$s = numbersOnly($str);
		if (!($s.length == 9 || $s.length == 5)) {
			if (arguments[1]!="quiet") {
				alert("There appears to be a problem with your Zip Code.");
			}
		}
	}
	$str = $s.substr(0,5);
	if ($s.length > 5) { $str += "-" + $s.substr(5); }
	return $str;
}

function proper($str) {
	return $str.substr(0,1).toUpperCase() + $str.substr(1).toLowerCase();
}
	
function checkForm(f) {
	lf = String.fromCharCode(13);
	errorFlag = false;
	tempFlag = false;
	message = "Sorry, there appears to be a problem with your form submission.\n\n";
	
// Check for valid email syntax
	if (!checkEmail(f.elements["email"].value,"quiet")) {
		errorFlag = true;
		message += "Please enter a valid email address.\n\n";
	}
	
// Check length of name
	if (f.elements["name"].value.length < 3) {
		errorFlag = true;
		message += "Please enter your name.\n\n";
	}
	
// Check length of city
	if (f.elements["city"].value.length < 3) {
		errorFlag = true;
		message += "Please enter your city.\n\n";
	}
	
// Check state
	if (f.elements["state"].selectedIndex==0) {
		errorFlag = true;
		message += "Please select your state.\n\n";
	}
	
/* Check zip for min 5 characters
	temp = checkZip(f.elements["zip"].value,"quiet");
	if (temp.length < 5) {
		errorFlag = true;
		message += "Please enter a valid Zip Code.\n\n";
	}
*/

// Final routine	
	if (errorFlag) {
		alert(message);
	} else {
		f.submit();
	}

}

function fillRandom() {	// fill form w/ random data for testing
	$lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce elementum magna ut tellus. Vivamus eu arcu at est venenatis accumsan. Sed molestie massa in dolor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Vivamus et leo. Integer adipiscing urna vitae sem. Maecenas ut elit non sapien porttitor pellentesque. Curabitur nunc purus, luctus non, tincidunt in, congue vel, libero. Suspendisse potenti. Duis condimentum nulla a quam. Maecenas lacus odio, convallis nec, dapibus nec, vehicula sed, tellus. Donec in nulla ac odio pretium dictum. Mauris lectus. Proin consequat ultricies risus. Maecenas ullamcorper auctor orci.";
	for ($i=0; $i<document.contact.elements.length; $i++) {
		$f = document.contact.elements[$i];
		$rand = Math.floor(Math.random() * 40);
		if ($f.type == "text") {
			switch($f.name) {
				case "email":
					$f.value = "scottpoulin@hotmail.com";
					break;
				case "phone":
					$f.value = "6175551212";
					break;
				case "zip":
					$f.value = "012345678";
					break;
				default:
					$len = 5 + Math.floor(Math.random() * 15);
					$f.value = $lorem.substr($rand,$len);
					break;
			}	// close switch
		}	// close text type
		
		if ($f.type=="textarea") {
			$len = 30 + Math.floor(Math.random() * 200);
			$f.value = $lorem.substr($rand,$len);
		}
		
	}	// close for loop
	$f = document.contact;
	$f.state.selectedIndex = rand(1, $f.state.options.length - 1);
	$f.phone.value = checkPhone($f.phone.value);
	$f.zip.value = checkZip($f.zip.value);
}
