$(document).ready(function(){

	textAreaDefault = "Ask us about your business on the web, quotes or anything else you would like to know about.";
		
	$( '#dropUsALine' ).prepend( '<div id="formMsg"></div>' );
	
	$("#contactMsg")
		.val( textAreaDefault )
		.focus(function(){
			if ($(this).val() == textAreaDefault ) {
				$(this).val("");
			}
		})
		.blur(function(){
			if ($(this).val() == "") {
				$(this).val( textAreaDefault );
			}
	});
	
	$("input, textarea")
		.css("color", "#999")
		.focus(function(){
			$(this).css("color", "#000");
		})
		.blur(function(){
			if( $( this ).val() == textAreaDefault ){
				$(this).css("color", "#999");
			}
	});
	
	
	$('#dropUsALine').submit(function(){
		
		 allowSubmit = true;
		
		if( $( '#formMsg' ).length ){
			$( '#formMsg' ).slideUp().empty();
		}
			
		errors = {
			fields: '',
			printMsg: '',
			contactMethod:  'a phone number or an email address',
			msg: 'a short message',
			name: 'your name'
		};
		
		// just check that there's something there. Leave heavy lifting to server for now
		function simpleValidation( val ){
			if( val.length <= 0 || val == textAreaDefault ){
				return false;
			}
			return val;
		}
		
		function printErrors(){
			
			$( errors.fields ).each( function(i){
				$( this ).addClass( 'error' );
			});
			
			$( '#formMsg' ).html( errors.printMsg + "</p>" ).slideDown();
		}
		
		function appendError( val ){
			if( errors.printMsg != "" ){
				errors.printMsg += ", " + val;
			}else{
				errors.printMsg = "<h5>Oops. Something's missing:</h5><p>" + val;
			}
		}
		
		name = simpleValidation( $( '#name' ).val() );
		phone = simpleValidation( $( '#phone' ).val() );
		email = simpleValidation( $( '#email' ).val() );
		message = simpleValidation( $( '#contactMsg' ).val() );
		
		if( name == false ){
			errors.fields += '#name, ';
			appendError( errors.name );
			allowSubmit = false;
		}
		
		if( phone == false && email == false ){
			errors.fields += '#phone, #email, ';
			appendError( errors.contactMethod );
			allowSubmit = false;
		}
		
		if( message == false ){
			errors.fields += '#contactMsg, ';
			appendError( errors.msg );
			allowSubmit = false;
		}
		
		
		if( allowSubmit != true ){
			printErrors();
			return false;
		}
		
		formValues = {
			name: name,
			phone: phone,
			email: email,
			message: message
		};
		
		$.ajax({
			type: 'POST',
			url: '/dropusaline.php',
			data: formValues,
			error: function(msg){
				$( '#dropUsALine fieldset, #dropUsALine h4' ).slideUp( 'medium');
				$( '#formMsg' ).addClass( 'error' ).html( '<h5>Error</h5><p>We apologize. Something happened... Maybe with your internet connection?  Please <a href="' + window.location + '#">refresh this window</a> and try again. Or <a href="mailto:design@tangwebstudio.com">send us an email</a>.</p>' ).slideDown();
			},
			success: function(msg){
				$( '#dropUsALine fieldset, #dropUsALine h4' ).slideUp( 'medium');
					$( '#formMsg' ).addClass( 'success' ).html( '<h5>Thank you.</h5><p>We will be in touch.</p>' ).slideDown();
			}
		});
		
		return false;
	
	});
	
});
