/* Author: 

*/

$(document).ready(function() {

    $("a.big-ref").fancybox();

    $('#contactform').submit(function() {
		isValid = true;
		errors = [];
		
		if ($('input#clientname').val() === '') {
			isValid = false;
			errors.push($('input#clientname'));
			$('input#clientname').removeClass('error');
		}
		
		if (!validateEmail($('input#email').val())) {
			isValid = false;
			errors.push($('input#email'));
			$('input#email').removeClass('error');
		} 
		
		if ($('#message').val() == '') {
			isValid = false;
			errors.push($('#message'));
			$('#message').removeClass('error');
		}
		
		if (isValid) {
	        $.post('test_mail_basic.php', $("#contactform").serialize(),
	        function(data) {
	            alert(data);
				$('form#contactform').get(0).reset();
				$('#kontakt p.error').remove();
	        })
		} else {
			if ($('#kontakt p.error').length == 0) {
				$('#contactform').before($('<p class="error">Upps, da ist etwas schiefgelaufen. Gleich noch mal probieren!</p>'));
			}
			$(':input', '#contactform').each(function(){
				$(this).removeClass('error');
			})
			$.each(errors, function() {
				$(this).addClass('error');
			});
		}
        return false;
    })


    $("a[href*=#]").click(function() {
        var trgt = this.hash.slice(1);
        if (trgt.length) {
            var x = $('a[name="' + trgt + '"]')
            var offset = x.offset().top;
            $('html,body').animate({
                scrollTop: offset
            },
            1000);
            return false;
        }
    });
});

function validateEmail($email) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var empty = $email === '';
	if( !emailReg.test( $email ) || empty) {
		return false;
	} else {
		return true;
	}
}






















