$(document).ready( function(){

	var formValues = [];
	var formTextFields = $(".form").find(".wpcf7-text, textarea");

	// clear text fields on focus, return to default val on blur
	formTextFields.each( function() {
	
		var defaultValue = $(this).val();
		
		formValues.push( defaultValue );
		
		$(this).blur( function() {
			if ( $(this).val() == "" ) {
				$(this).val( defaultValue );
			}
		});
		$(this).focus( function() {
			if ( $(this).val() == defaultValue ) {
				$(this).val( "" );
			}
		});
		
	});
	
	// form submit actions
	$(".wpcf7-form").submit( function() {
	
		// on submit, if field has default value change it to blank for req'd fields to work properly
		formTextFields.each( function(i) {
			if ( $(this).val() == formValues[i] ) {
				$(this).val( "" );
			}
		});	
		
		// after submit, return blank fields to default values
		// we have to run replacement twice because when form is submitted and
		// data is valid, it replaced fields value right way and then a second
		// time when the AJAX response resolves.
		var howManyTimesReplaced = 0;
		var intCount = 0;	// number of times interval was run
		
		var to = setInterval( function() {
			var emptyFieldsReplaced = false;
			formTextFields.each( function() {
				if ( $(this).val() == "" && !$(this).is(":focus") ) {
					$(this).trigger("blur");
					emptyFieldsReplaced = true;
				}
			});
			
			if (emptyFieldsReplaced) {
				howManyTimesReplaced++;
				emptyFieldsReplaced = false;
			}
			if ( howManyTimesReplaced > 1 || intCount > 20 ) {
				howManyTimesReplaced = 0;
				clearInterval(to);
			} else {
				intCount++;
			}
			//console.log("interval run");
		}, 500);
		
	});
	
});
