(function($){ 
email = function() {
	
	
	/*Countdown characters*/
	$('.word_count').live("keyup",(function(){				   
		// get new length of characters
		var new_length = (3000 - $(this).val().length);
		// update
		if (new_length <= 0){
			$(this).val($(this).val().substring(0, 3000));
			new_length = 0;
		}
		$('.char_remain #numchars').html(new_length);
	}));
				
				
				
	$("#emailus_page a.reset").live("click",(function(e){
		e.preventDefault();
		$('#emailus_page #error').remove();
		$('#emailus_page input:text, #emailus_page textarea').val("");
		$('#emailus_page select').attr("selectedIndex", "0");
		$('#emailus_page select').css({"background-color":"#DFEFFF", color:"#0052AC"});
		if(window.devicePixelRatio && (navigator.platform.indexOf("Mac") > -1))
			$('select').css("background-color","transparent");
		$('#emailus_page select, #emailus_page input, #emailus_page textarea').animate({ 
								   borderBottomColor: '#0D5BB1',
								   borderTopColor: '#0D5BB1',
								   borderLeftColor: '#0D5BB1',
								   borderRightColor: '#0D5BB1'}, 100);
		$('#emailus_page .errorbox').remove();
		$('#emailus_page .checkbox').attr("checked","");
		$('.char_remain #numchars').html(3000);
	}));
	
	
	$("#emailus_page form").live("submit",(function(){
		$('#emailus_page #error').remove();
		
		//if only whitespace submitted
		$("#emailus_page #fname, #emailus_page #lname, #emailus_page #address, #emailus_page #phone, #emailus_page #city, #emailus_page #state, #emailus_page #zip, #emailus_page #comment_box").each(function(){
			if($(this).val().match(/[\S]+/) == null)
				$(this).val("");
		});
		
		//store missing, invalid state
			var missingFName = ($("#emailus_page #fname").val() == "") ? true: false;
			var missingLName = ($("#emailus_page #lname").val() == "") ? true: false;
			var missingAddress = ($("#emailus_page #address").val() == "") ? true: false;
			var missingCity = ($("#emailus_page #city").val() == "") ? true: false;
			var missingState = ($("#emailus_page #state").val() == "") ? true: false;
			var missingZip = ($("#emailus_page #zip").val() == "") ? true: false;
			if ($("#emailus_page #zip").val().length < 5)
				missingZip = true;
			var missingEmail = ($("#emailus_page #email").val() == "") ? true: false;
			var missingComment = ($("#emailus_page #comment_box").val() == "") ? true: false;	
			
			var invalidmail = !isValidEmail($("#emailus_page #email").val());
			var missing = missingFName || missingLName || missingAddress || missingCity || missingZip || missingEmail || missingComment || missingState;
			
			
			//create error div
			var error_string = "<div id='error'><ul>";
									  if (missing)
										error_string += "<li>Please fill in all required fields.</li>";
									  if (invalidmail && (!missingEmail))
										error_string += "<li>Please enter a valid email address.</li>";
									  error_string += "</ul></div>";
									   
			//animate outlines
			if (missingFName){
				$("#emailus_page #fname").animate( borderColors, speed);
			}
			if (missingLName){
				$("#emailus_page #lname").animate( borderColors, speed);
			}
			if (missingCity){
				$("#emailus_page #city").animate( borderColors, speed);
			}
			if (missingState){
				$("#emailus_page #state").animate( {
									   borderBottomColor: '#FF0000',
									   borderTopColor: '#FF0000',
									   borderLeftColor: '#FF0000',
									   borderRightColor: '#FF0000', 
									   backgroundColor:"#ffc4c4",
									   color:"#F00"}, speed);
			}
			if (missingZip){
				$("#emailus_page #zip").animate( borderColors, speed);
			}
			if (missingAddress){
				$("#emailus_page #address").animate( borderColors, speed);
			}
			if (missingComment){
				$("#emailus_page #comment_box").animate( borderColors, speed);
			}
			if (missingEmail || invalidmail){
				$("#emailus_page #email").animate( borderColors, speed);
			}
			
			//listener for focus (remove red)
			$("#emailus_page #fname, #emailus_page #lname, #emailus_page #city, #emailus_page #zip, #emailus_page #address, #emailus_page #email, #emailus_page #comment_box").focus(function(){$(this).animate({ 
									   borderBottomColor: '#0D5BB1',
									   borderTopColor: '#0D5BB1',
									   borderLeftColor: '#0D5BB1',
									   borderRightColor: '#0D5BB1'}, 500);																	
			});
			$("#emailus_page #state").focus(function(){
				$(this).css({"background-color":"#DFEFFF", color:"#0052AC"});
				$(this).animate({ 
								   borderBottomColor: '#0D5BB1',
								   borderTopColor: '#0D5BB1',
								   borderLeftColor: '#0D5BB1',
								   borderRightColor: '#0D5BB1'}, 400);																	
			});
			
			//append error message and end function
			if (missing || invalidmail){
				$('#emailus_page .required').append(error_string);
				$('html,body').animate({scrollTop: 435}, 1000);
				return false;
			}										   
	}));
	
	
	$("#emailus_page a.submit").live("click",(function(e){
		e.preventDefault();
		$("#emailus_page form").submit();
	}));
}
})(jQuery);					
