$(document).ready(function() {

	$(document).everyTime(4000, function() {
		rotateBanner();
	});
	
	rotateBanner = function() {
        if ($('#first').is(":visible")) {
            $('#first').hide();
            $('#second').fadeIn('slow');
        } else {
            $('#second').hide();
            $('#first').fadeIn('slow');
        }
    }
    
    $('#state-locator').change(function() {
    	
    	if ($(this).val() != '') {
    		document.location.href = 'participating-vet-clinics.php?state=' + $(this).val();
    	}
    	
    });
    
    $('#book-a-free-joint-health-consult').submit(function() {
    	
    	var error = false;
    	
    	$('#err_firstname').hide();
    	$('#err_surname').hide();
    	$('#err_suburb').hide();
    	$('#err_email').hide();
    	$('#err_invalidemail').hide();
		$('#err_phone').hide();
    	$('#err_dogname').hide();
    	$('#err_dogage').hide();
    	$('#err_dogbreed').hide();
		
    	if (trim($('#f_firstname').val()) == '') {
    		$('#err_firstname').show();
    		error = true;
    	}
    	if (trim($('#f_surname').val()) == '') {
    		$('#err_surname').show();
    		error = true;
    	}
    	if (trim($('#f_suburb').val()) == '') {
    		$('#err_suburb').show();
    		error = true;
    	}
    	if (trim($('#f_email').val()) == '') {
    		$('#err_email').show();
    		error = true;
    	} else if (!validEmail(trim($('#f_email').val()))) {
    		$('#err_invalidemail').show();
    		error = true;
    	}
    	if (trim($('#f_phone').val()) == '') {
    		$('#err_phone').show();
    		error = true;
    	}
    	if (trim($('#f_dogname').val()) == '') {
    		$('#err_dogname').show();
    		error = true;
    	}
    	if (trim($('#f_dogage').val()) == '') {
    		$('#err_dogage').show();
    		error = true;
    	}
    	if (trim($('#f_dogbreed').val()) == '') {
    		$('#err_dogbreed').show();
    		error = true;
    	}
    	
    	return !error;
    	
    });
    
    $('#contact-us').submit(function() {
    	
    	var error = false;
    	
    	$('#err_name').hide();
    	$('#err_phone').hide();
    	$('#err_email').hide();
    	$('#err_invalidemail').hide();
    	$('#err_enquiry').hide();
		
    	if (trim($('#f_name').val()) == '') {
    		$('#err_name').show();
    		error = true;
    	}
    	if (trim($('#f_phone').val()) == '') {
    		$('#err_phone').show();
    		error = true;
    	}
    	if (trim($('#f_email').val()) == '') {
    		$('#err_email').show();
    		error = true;
    	} else if (!validEmail(trim($('#f_email').val()))) {
    		$('#err_invalidemail').show();
    		error = true;
    	}
    	if (trim($('#f_enquiry').val()) == '') {
    		$('#err_enquiry').show();
    		error = true;
    	}
    	
    	return !error;
    	
    });
    
    validEmail = function(email) {
       var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
       return reg.test(email);
    }
    
    trim = function(stringToTrim) {
    	return stringToTrim.replace(/^\s+|\s+$/g,"");
    }


});
