$(document).ready(function() {
	$("form.validate .email, form.validate #email").focus(function() {
		if ($(this).val() == "Your Email Address") {$(this).val('');} 
		if ($(this).val() == "Invalid email.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate .email, form.validate #email").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Your Email Address'); 
		} 
	});
	$("form.validate .email, form.validate #email").blur(function() {
		if ($("form.validate .email, form.validate #email").val() == 'Invalid email.  Try again.') {
			$("form.validate .email, form.validate #email").val('Your Email Address');
			$("form.validate .email, form.validate #email").attr('style','');
		} 
	});
	$("form.validate .firstname, form.validate #firstname").focus(function() {
		if ($(this).val() == "First name") {$(this).val('');} 
		if ($(this).val() == "Invalid name.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate .firstname, form.validate #firstname").blur(function() {
		if ($(this).val() == '') {
			$(this).val('First name'); 
		} 
	});
	$("form.validate .lastname, form.validate #lastname").focus(function() {
		if ($(this).val() == "Last name") {$(this).val('');} 
		if ($(this).val() == "Invalid name.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate .lastname, form.validate #lastname").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Last name'); 
		} 
	});
	$("form.validate .zip, form.validate #zip").focus(function() {
		if ($(this).val() == "Zip code") {$(this).val('');} 
		if ($(this).val() == "Invalid zip code.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate .zip, form.validate #zip").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Zip code'); 
		} 
	});
	$("form.validate .textarea, form.validate #textarea").focus(function() {
		if ($(this).val() == "Enter message...") {$(this).val('');} 
		if ($(this).val() == "Invalid message.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate .textarea, form.validate #textarea").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Enter message...'); 
		} 
	});
});

function valid_email(the_form) {
    var el = the_form.email;
    var val = el.value;
    var isValid = true;
    var Regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!Regex.test(val)) {
      isValid = false;
    }
    if( !isValid ) { 
      el.value = 'Invalid email.  Try again.';
      el.style.color = 'red';
    }
  return isValid;
}

function check_length(id,default_value,error_text,min) {
    var el = document.getElementById(id);
    var val = el.value;
    var isValid = true;
    if (val.length<=min || val==default_value || val==error_text) {
      isValid = false;
    }
    if( !isValid ) { 
      el.value = error_text;
      el.style.color = 'red';
    }
  return isValid;
}

function clear_if_default(id, default_value){
    var el = document.getElementById(id);
    var val = el.value;
    if (el.value == default_value) {
      el.value = '';
    }
}

function check_form(the_form) {
    var a = check_length('firstname', 'First name', 'Invalid name.  Try again.', 0);
    var b = check_length('lastname', 'Last name', 'Invalid name.  Try again.', 0);
    var c = check_length('zip', 'Zip code', 'Invalid zip code.  Try again.', 0);
    var d = check_length('textarea', 'Enter message...', 'Invalid message.  Try again.', 0);
    var e = valid_email(the_form);
    return (a && b && c && d && e);
}

function check_form2(the_form) {
    var a = check_length('firstname', 'First name', 'Invalid name.  Try again.', 0);
    var b = check_length('lastname', 'Last name', 'Invalid name.  Try again.', 0);
    var c = check_length('zip', 'Zip code', 'Invalid zip code.  Try again.', 0);
    var d = valid_email(the_form);
    return (a && b && c && d);
}

function check_form3(the_form) {
    var c = check_length('zip', 'Zip code', 'Invalid zip code.  Try again.', 0);
    var d = valid_email(the_form);
    if(c && d) {
        clear_if_default('firstname', 'First name');
        clear_if_default('lastname', 'Last name');
    }
    return (c && d);
}

function check_form4(the_form) {
    var a = valid_email(the_form);
    return (a);
}