$(document).ready(function(){

	// Homepage Slideshow
	$(document).ready(function() {
		if ($('#slide').length>0){
    	$('#slide').cycle({
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
		}
	})


	$('ul#nav li').hover(function() {
		$(this).addClass('over')
	}, function() {
		$(this).removeClass('over');
	});


    $('#enquiry').submit(function () {
	    var errorcount = validate_form();

	    //check the error count
	    if (errorcount === 0) {
	        //alert(errorcount);
	        //all good - submit the form and carry on
	        //$(this).text('huzzah!');
	        //var form = $(this).parents('form:first');
	        //var form=$('.reviewform');
	        //alert(form.attr('name'));
	        //form.submit();
	        $.ajax({
	            url: "mailto_db.php",
	            type: "POST",
	            data: $('#enquiry').serialize()
	        });
	        $('#enquiry').html("<h4 align='center'>We have received your contact, thanks!</h4>");
	    } else if (errorcount === 1) {
	        //something's wrong.
	        $('body').animate({ scrollTop: 0 }, 'slow');
	        $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
	    } else if (errorcount > 1) {
	        $('body').animate({ scrollTop: 0 }, 'slow');
	        $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
	    }
	    return false;
	});





});


function validate_form() {

    //required fields checkermajigger

    //reset the error count
    errorcount = 0;
    $('p.error').remove();
    $('.error').removeClass('error');


    //for textboxes, textareas and selects
    $('label:contains("*")').each(function () {
        //if a label has a * in it, and the next form element is empty
        if ($(this).next('input, select, textarea').val() == "" || $(this).next('fieldset').children('input, select, textarea').val() == "") {
            //add .error to the label
            $(this).addClass('error');

            //OFFSET (IF IT'S REQUIRED)
            //get the label's position (relative to the top)
            //var offset = $(this).position();
            //var offset_correction = -80;
            //var offset_correction = $('#offset_correction').html();
            //$(this).text(offset.top);

            //add .error to the field as well
            $(this).next('input, select, textarea').addClass('error');
            $(this).next('fieldset').children('input, select, textarea').addClass('error');

            //WITH OFFSET show an error message with <label> + "is required" (hidden by default)
            //$(this).next('input, select, textarea').after('<p id="'+$(this).attr('for')+'_error" class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');
            //$(this).next('fieldset').children('input, select, textarea').after('<p class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');

            //WITHOUT OFFSET
            $(this).next('input, select, textarea').after('<p id="' + $(this).attr('for') + '_error" class="error">' + $(this).text().replace('*', '') + '  required</p>');

            //add one to the error count

            errorcount = errorcount + 1;

            //show all the error messages
            $('p.error').slideDown('slow');
        }
    });


    return errorcount;

}
