function numbersOnly($str) {
	$s = $str.replace(/\D/g,"");
	return $s;
}

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

function checkPhone($f) {
// optional 2nd argument: "quiet" suppresses alert messages
	$str = $f.value;
	$s = "";
	if ($str.length>0) {
		// remove all non-numbers
		$s = numbersOnly($str);
		if ($s.length != 10) {
			if (arguments[1]!="quiet") {
				bubble_in("<b>Sorry!</b><br />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 proper($str) {
	return $str.substr(0,1).toUpperCase() + $str.substr(1).toLowerCase();
}
	
function checkForm(f) {
	errorFlag = false;
	message = "<b>Sorry!</b><br />There appears to be a problem with your form submission:<br /><br />";
	
// Check for valid email syntax
	if (!checkEmail(f.elements["email"],"quiet") || f.elements["email"].value.length < 1) {
		errorFlag = true;
		message += " - Please enter a valid email address.<br />";
	}
	
// name
	if (f.elements["name"].value.length < 3) {
		errorFlag = true;
		message += " - Please enter your name.<br />";
	}
	
// message
	if (f.elements["message"].value.length < 5) {
		errorFlag = true;
		message += " - Please type your message.<br />";
	}
	
// Final routine	
	if (errorFlag) {
		bubble_in(message, 300);
		return false;
	} else {
		return true;
	}
}


$(document).ready( function() {
	$(document).bind("click", function() { 
		$("#bubble").fadeOut(300);
	});
	$(":input").bind("keydown", function() { 
		$("#bubble").fadeOut(300);
	});
});
