// JavaScript Document for contact form functions
    $(document).ready(
        function(){
            contactShowHide();
            setupLocationSwitching();
            doAntiBotCheck();
            
            $('textarea#message').autoResize({
				// Quite slow animation:
				animateDuration : 200,
				// More extra space:
				extraSpace : 30
			});
			
			$('#contactmain input:text, #contactmain textarea')
			.bind('focus', function(){ $(this).addClass('active_text'); })
			.bind('blur', function(){ $(this).removeClass('active_text'); });
        }
    );
    
    function contactShowHide() {
        $('div#extendsection').hide();
        
        $('input#btnExtend').click(
            function(){
                btnExtend_click(this);
            }
        );
    }
    
    function btnExtend_click(btn){
        if($('div#extendsection').is(":hidden")){
            $('div#extendsection').slideDown(500);
            $(btn).attr('value','Hide Extended Form');
        }else{
            $('div#extendsection').slideUp(200);
            $(btn).attr('value','Show Extended Form');
        }
        return false;
    }

	
	function setupLocationSwitching() {
		$('div.officecornwall').hide();
		$('div.officemidsomer').hide();
		
		if($('a#showcornwall').hasClass('cactive')){
			$('div.officecornwall').show();
		}
		
		if($('a#showmidsomer').hasClass('cactive')){
			$('div.officemidsomer').show();
		}
		
		$('#showcornwall').click(
			function(){
				 $('div.officemidsomer').hide();
				 $('div.officecornwall').show();
				 $('a#showcornwall').addClass("cactive");
				 $('a#showmidsomer').removeClass("cactive");
			}
		);
   
		$('#showmidsomer').click(
			function(){
				$('div.officecornwall').hide();
				$('div.officemidsomer').show();
				$('a#showcornwall').removeClass("cactive");
				$('a#showmidsomer').addClass("cactive");
			}
		);
	}
	
	function validateForm(btn){
	
		var errors = new Array();

		$("#"+btn).attr("disabled", "true"); 
		$("#email").removeClass('error');
		$("#name").removeClass('error');
		$("#message").removeClass('error');
		$("#formerrors").empty();
	
		/* Check the email field */
		var email = $("#email").val();
		if(email.length < 3){
			errors.push("You must enter an email address");
			$("#email").addClass('error');
		}
		else
		{
			re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
			if(!re.test(email)){
				errors.push("You have entered an invalid email address");
				$("#email").addClass('error');
			}
		}
		
		
		/* Check the name field */
		var name = $("#name").val();
		if(name.length < 2){
			errors.push("You must enter a name");
			$("#name").addClass('error');
		}
		
		/* Check the message field */
		var message = $("#message").val();
		if(message.length < 2){
			errors.push("You must enter a comment or question");
			$("#message").addClass('error');
		}
	
		if(errors.length > 0){
		
			$("#formerrors").append(buildErrorMessage(errors));
			$("#"+btn).removeAttr('disabled')
			
			return false;
		}
	
		return true;
	}
	
	function buildErrorMessage(errors){
	
		var out = "<b>There was a problem sending your message<\/b><ul>";
		
		for(var error in errors){
			out += "<li>" + errors[error] + "<\/li>";
		}
		
		out += "<\/ul>";
	
		return out;
	}
	
	function doAntiBotCheck(){
		var seed = Math.round(Math.random() * 1000);
		var result = Math.ceil((34564 * seed) * Math.sin(0.6328));
	
		$("#stormseed").val(seed);
		$("#stormval").val(result);
	
	}
