$(document).ready(function(){
	//Run validation on submit
	$('#contactUsForm #submit').click( function(){
		//If validation failed do not submit
		if( !validateForm('#contactUsForm') ){
			if( $('.required').next().length > 0 ){
				$('.required').next().attr('aria-hidden', 'false');
			}
			if( $('.requiredEmail').next().length > 0 ){
				$('.requiredEmail').next().attr('aria-hidden', 'false');
			}
			return false;
		}
	});
	$('#joinEmailList #submitEmail').click( function(){
		//If validation failed do not submit
		if( !validateForm('#joinEmailList') ){
			if( $('.requiredEmail').next().length > 0 ){
				$('.requiredEmail').next().attr('aria-hidden', 'false');
			}
			return false;
		}
	});
	//if someone is typing in a required field with validation active, recheck field
	$('.required').keyup( function(){
		if(	$(this).next().length > 0 ){
			if( basicRequired($(this).attr("value")) ){
				$(this).next().remove();
			}
		}
	});
	//if someone is typing in a required field with validation active, recheck field (email specific)
	$('.requiredEmail').keyup( function(){
		if(	$(this).next().length > 0 ){
			if( $(this).next().text().indexOf("Valid") == 0 ){
				if( emailRequired($(this).attr("value")) ){
					$(this).next().remove();
				}
			}
			else{
				if( basicRequired($(this).attr("value")) ){
					$(this).next().remove();
				}
			}
		}
	});
	flowers();
});
 
//On submit for validator
function validateForm(formId){
	var notValid = false;
	//find all required fields and validate them individually
	$(formId).find('.required').each( function(){
		var error = "";
		var value = $(this).attr('value');
		if( $(this).next().length > 0 ){
			$(this).next().remove();
		}
		if( !basicRequired( value) ){
			$(this).after("<span role=\"complementary\" aria-hidden=\"true\" aria-live=\"assertive\">This field is required</span>");
			notValid = true;
		}
	});
	//find all required email fields and validate them individually
	$(formId).find('.requiredEmail').each( function(){
		var error = "";
		var value = $(this).attr('value');
		if( $(this).next().length > 0 ){
			$(this).next().remove();
		}
		if( !basicRequired( value) ){
			error = "<span role=\"complementary\" aria-hidden=\"true\" aria-live=\"assertive\">This field is required</span>";
		}
		if( error=="" ){
			if( !emailRequired(value) ){
				error = "<span role=\"complementary\" aria-hidden=\"true\" aria-live=\"assertive\">Valid email is required</span>";
			}
		}
		if( error != "" ){
			$(this).after(error);
			notValid = true;
		}
	});
	//If any fields were not valid return false to kill submit
	if(notValid){
		return false;
	}
	return true;
}
 
//check required field to see if empty
function basicRequired(value){
	if( value == "" || value == " " ){
		return false;
	}
	return true;
}
 
//BASIC: check required email to see if valid
function emailRequired(value){
	if( !(value.indexOf('@') > 0) || !(value.indexOf('.') > 2)){
		return false;
	}
	return true;
}

function flowers(){
	$('#flowerLarge, #flowerSmall').hide();
	$('#flowerLarge').slideDown(750, function(){
		$('#flowerSmall').slideDown(600);
	});
}