/***************************************************
		FORM VALIDATION JAVASCRIPT
***************************************************/
jQuery(document).ready(function($){
	$('form#contact-form').submit(function() {
		$('form#contact-form .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
            	var labelText = $(this).prev('label').text();
            	$(this).parent().prepend('<div class="error">*Required Field</div>');
            	$(this).addClass('inputError');
            	hasError = true;
            } else if($(this).hasClass('email')) {
            	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            	if(!emailReg.test(jQuery.trim($(this).val()))) {
            		var labelText = $(this).prev('label').text();
            		$(this).parent().prepend('<div class="error">*Invalid Field</div>');
            		$(this).addClass('inputError');
            		hasError = true;
            	}
            } else if($(this).is('textarea')) {
            
			    var wordcount = $(this).val().split(/\b[\s,\.-:;]*/).length;
			    if (wordcount > 250) {
			        $(this).parent().prepend('<div class="error">*250 words max</div>');
            		$(this).addClass('inputError');
            		hasError = true;
			    } 
            }
		});
		if(!hasError) {
			$('form#contact-form input.submit').fadeOut('normal', function() {
				$(this).parent().append('');
			});
			var formInput = $(this).serialize()+'&brenShake=cesc';
			$.post($(this).attr('action'),formInput, function(data){
				if(data==1) { 
					var reportHTML = '<div class="success overline" style="margin-top:50px"><h2>Thank You!</h2></div><p>Your message/application was successfully sent. We will be in contact with you soon.</p>';
				} else {
					var reportHTML = '<div class="success overline" style="margin-top:50px"><h2>Ooops!</h2></div><p>Something has gone wrong.  Try again later or give us a call instead.</p>';
				}
				$('form#contact-form').slideUp("fast", function() {
					$(this).before(reportHTML);
				});
			});
		} else {
		
			$('form#contact-form #brenMainError').remove();
			$('form#contact-form').prepend('<div id="brenMainError" class="error">*Errors exist.  See below for details</div>');
		
		}
		
		$("html, body").animate({ scrollTop: 0 }, "slow");

		return false;

	});
});





/***************************************************
		COMMENT FORM VALIDATION JAVASCRIPT
***************************************************/
jQuery(document).ready(function($){
	$('form#comment-form').submit(function() {
		$('form#comment-form .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			if(jQuery.trim($(this).val()) == '') {
            	var labelText = $(this).prev('label').text();
            	$(this).parent().append('<span class="error">Required</span>');
            	$(this).addClass('inputError');
            	hasError = true;
            } else if($(this).hasClass('email')) {
            	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            	if(!emailReg.test(jQuery.trim($(this).val()))) {
            		var labelText = $(this).prev('label').text();
            		$(this).parent().append('<span class="error">Invalid</span>');
            		$(this).addClass('inputError');
            		hasError = true;
            	}
            }
		});
		if(!hasError) {
			$('form#comment-form input.submit').fadeOut('normal', function() {
				$(this).parent().append('');
			});
			var formInput = $(this).serialize();
			$.post($(this).attr('action'),formInput, function(data){
				$('form#comment-form').slideUp("fast", function() {
					$(this).before('<div class="success overline"><h2>Thanks!</h2></div><p>Your comment was successfully sent.</p>');
				});
			});
		}

		return false;

	});
});
