$(function(){

    function validateEmail(email) {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    }
    $('#slider img')

	//Do what we need to when form is submitted.	
	$('#form_submit').click(function(e){
	
	//Setup any needed variables.
	var input_name = $('#form_name').val(),
		input_email = $('#form_email').val(),
		input_subject = $('#form_subject').val(),
		input_message = $('#form_message').val(),
		response_text = $('#response');
		//Hide any previous response text 
		response_text.hide();
        e.preventDefault();
		if (input_message =='' || input_email==''|| !validateEmail(input_email)){

            if (input_message==''){
                $('#form_message').addClass('error');
            }
            if(input_email==''|| !validateEmail(input_email)){
                $('#form_email').addClass('error');
            }

            response_text.addClass('error').html('There are errors in the data you submitted. Please correct the marked fields and try again').show();

        }else{
            //Change response text to 'loading...'
            response_text.html('Loading...').show();
            var params={name: input_name, email: input_email, subject: input_subject, message: input_message};
            //Make AJAX request
            console.log(params);
            $.ajax({
                crossDomain: true,
                type: 'POST',
                url: $(this).parent('form').attr('action'),
                data: params,
                success: function(data){
                    console.log(data);
                },
                error: function(data){
                    console.log(data);
                    response_text.html('Error, please try again...');
                }
            });
        }

		
		//Cancel default action
		e.preventDefault();
	});
    $(window).bind('hashchange', function() {
        if(window.location.hash) {
            //alert($(window.location.hash).find('h2'));
            $(window.location.hash).find('h2').trigger('click');
        }
    });

    if(window.location.hash) {
        //alert($(window.location.hash).find('h2'));
        $(window.location.hash).find('h2').trigger('click');
    }

});


