$("document").ready(function(){
	var hasError = false;					 
	$(".txt,.state,.zip").each(function(){
		$(this).focus(function(){
			if($(this).val() == 'Required'){
				$(this).val('');	
				$(this).next('.error').fadeOut();
				$(this).css({'border-color':'#666','color':'#333'})
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val('Required');	
				$(this).css({'border-color':'red','color':'red'})
			}	  
		});	
		
		$('#phone').blur(function(){
			var p = $('#phone').val();
			$('#phone').val(p.split(' ').join(''));		   
			 		  
		});
	});
	$("#submitBtn").click(function(){
		hasError = false;
		$(".error").empty();
		$(".txt, .zip, .state").each(function(){
			if($(this).val() == '' || $(this).val() == 'Required'){
				$(this).val('Required');
				$(this).css({'border-color':'red','color':'red'});
				hasError = true;
			}
		});
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var emailToVal = $("#email").val();
		if(!emailReg.test(emailToVal)) {
			$("#email").after('<span class="error">Invalid email address</span>');
			hasError = true;
		} 
		
		var phoneReg = /^[0-9]\d{2}-\d{3}-\d{4}$/;
		var phone = $('#phone');
		phone.val(phone.val().split(' ').join(''));
		if(phone.val().search(phoneReg) == -1 || phone.val() == '') {
			phone.after('<span class="error">Invalid. Please enter as: xxx-xxx-xxxx</span>');
			hasError = true;
		} 
		var state = $('#state');
			var array = ["AL","AS","AZ","AR","CA","CO","CT","DE","DC","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY"];
			if(state.val() == '' || $.inArray(state.val().toUpperCase(),array) == -1){
				state.after('<span class="error">Valid state abbreviation required</span>');
				hasError = true;
			} 
		
		if(isNaN($("#zip").val()) || $("#zip").val().length < 5){
			$("#zip").after('<span class="error">Please enter a 5 digit zip code</span>');
		}
		if(hasError == true){
			return false;	
		} else {
			return true;
		}
	});
});
