(function($){
$(document).ready(function() {
	$("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").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Your email address'); 
		} 
	});
	$("form.validate #email").blur(function() {
		if ($("form.validate #email").val() == 'Invalid email.  Try again.') {
			$("form.validate #email").val('Your email address');
			$("form.validate #email").attr('style','');
		} 
	});
	$("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").blur(function() {
		if ($(this).val() == '') {
			$(this).val('First name'); 
		} 
	});
	$("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").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Last name'); 
		} 
	});
	$("form.validate #city").focus(function() {
		if ($(this).val() == "City") {$(this).val('');} 
		if ($(this).val() == "Invalid city.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate #city").blur(function() {
		if ($(this).val() == '') {
			$(this).val('City'); 
		} 
	});
	$("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").blur(function() {
		if ($(this).val() == '') {
			$(this).val('ZIP code'); 
		} 
	});
	$("form.validate #phone").focus(function() {
		if ($(this).val() == "Phone number") {$(this).val('');} 
		if ($(this).val() == "Invalid phone number.  Try again.") {
			$(this).val(''); 
			$(this).attr('style',''); 
		} 
	});
	$("form.validate #phone").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Phone number'); 
		} 
	});
});
})(jQuery);

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 check_phone(id,default_value,error_text) {
    var el = document.getElementById(id);
    var val = el.value;
    var isValid = true;
    val = val.replace(/[^0-9]/g, ''); 
    if (val.length<10 || val==default_value || val==error_text) {
      //-IMPROVE phone number JS check to require 10 digits
      isValid = false;
    }
    if( !isValid ) { 
      el.value = error_text;
      el.style.color = 'red';
    }
    return isValid;
}

function check_max_length(id,default_value,error_text,max) {
    var el = document.getElementById(id);
    var val = el.value;
    var isValid = true;
    if (val.length>max || 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 = 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('lastname', 'Last name');
    }
    return (c && d);
}

function check_full(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);
    // var e = check_phone('phone', 'Phone number', 'Invalid phone number.  Try again.', 11);
    if(d) {
        clear_if_default('firstname', 'First name');
        clear_if_default('lastname', 'Last name');
        clear_if_default('zip', 'ZIP code');
        clear_if_default('phone', 'Phone number');
     }    
    return (d);
}

function check_lgen(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('city', 'City', 'Invalid city.  Try again.', 0);
    var e = valid_email(the_form);
    return (a && b && c && d && e);
}