$(document).ready(function(){
	//-- MODAL FUNCTIONS
	$('.modalcloser').click(function() {$(this).parent().fadeOut(); $('#overlay').fadeOut();});
	//-- NAVBAR FUNCTIONS
	$('#nav li').hover(
		function() {$(this).children().addClass('hover');},
		function() {$(this).children().removeClass('hover');}
	);
	//-- PRODUCT CATEGORY IMAGE ROLLOVER FUNCTIONS
	$('#productcockpit').hover(
		function() {$(this).attr('src', 'images/ui/productcategories/cockpit-on.jpg');},
		function() {$(this).attr('src', 'images/ui/productcategories/cockpit.jpg');}
	);
	$('#productcabin').hover(
		function() {$(this).attr('src', 'images/ui/productcategories/cabin-on.jpg');},
		function() {$(this).attr('src', 'images/ui/productcategories/cabin.jpg');}
	);
	$('#productelectrical').hover(
		function() {$(this).attr('src', 'images/ui/productcategories/electrical-on.jpg');},
		function() {$(this).attr('src', 'images/ui/productcategories/electrical.jpg');}
	);
	//-- FORM FIELD FOCUS FUNCTIONS
	$('input:text:not([readonly="readonly"]), textarea:not([readonly="readonly"])').each(function(){
		if ($(this).val() == '' || $(this).val().toLowerCase() == $(this).attr('title').toLowerCase()) {
			$(this).val($(this).attr('title'));
			$(this).addClass('fieldhint');
		}
	});
	$('input:text:not([readonly="readonly"]), textarea:not([readonly="readonly"])').focus(function(){
		if ($(this).hasClass('fieldhint')) {
			$(this).val('');
			$(this).removeClass('fieldhint');
		}
	});
	$('input:text:not([readonly="readonly"]), textarea:not([readonly="readonly"])').blur(function(){
		if ($(this).val() == '' || $(this).val().toLowerCase() == $(this).attr('title').toLowerCase()) {
			$(this).val($(this).attr('title'));
			$(this).addClass('fieldhint');
		}
	});
	//-- SURVEY FORM VALIDATION AND PROCESSING
	$('.surveyform #surveysubmit').click(function() {
		$sendable = 1;
		// validation
		$('.required').each(function(){
			if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
				$(this).focus();
				alert('Please fill out the ' + $(this).attr('title') + ' field.');
				$sendable = 0;
				return false;
			}
			if ($(this).attr('id') == 'email') {
				var regexpEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
				if (!$(this).val().match(regexpEmail)) {
					$(this).focus();
					alert('Please enter a valid Email address.');
					$sendable = 0;
					return false;
				}
			}
		});
		// processing
		if ($sendable == 1) {
			$querystring = $('.surveyform').serialize();
			//alert ($querystring); return false;
			$.ajax({
				type: "POST",
				url: "/scripts/customer_service_survey_SUBMIT.php",
				data: $querystring,
				error: function(xhr, textStatus, errorThrown){alert('Error: '+textStatus);},
				success: function() {
					$('.surveyform').html('<div id="surveysuccess"><p>Your message was sent. We will be in touch soon!</p></div>');
					$('#surveysuccess').hide();
					$('#surveysuccess').fadeIn(1000);
				}
			});
			return false;
		}
	});
});
