$(document).ready(function(){
	var v = jQuery("#quoteForm").validate({
		invalidHandler: function(e, validator) {
			$('td').removeClass('error');
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
					$('#error_list').empty().text(message);
				$('#form_fill').addClass('errors');
			} else {
				$('#form_fill').removeClass('errors');
			}
		},
		rules: {
			captcha_1: {
				required: true,
				remote: '/misc/captcha/valid_captcha.php'
			},			
			customer_name: {
				required: true,
				minWords: 1
			},
			company_name: {
				required: true,
				minWords: 1
			},
			phone_number: {
				required: true,
				minWords: 1
			},
			email_address: {
				required: true,
				email: true
			},
			details: {
				required: true,
				minWords: 1
			}
		},
		messages: {
			captcha_1: "&nbsp;",
			customer_name: {
				required: "&nbsp;",
				minWords: "&nbsp;"
			},
			company_name: {
				required: "&nbsp;",
				minlength: "&nbsp;"
			},
			phone_name: {
				required: "&nbsp;",
				minlength: "&nbsp;"
			},
			email_address: {
				required: "&nbsp;",
				minlength: "&nbsp;"
			},
			details: {
				required: "&nbsp;",
				minWords: "&nbsp;"
			}
		},
		errorPlacement: function(error, element) {
				$(element).parents('tr').children(':first').addClass('error');
		}
	});
	$('#quote_form input,#quote_form textarea').focus(function() { 
		$(this).parents('tr').children(':first').addClass('selected'); 
	}).blur(function(){ 
		$(this).parents('tr').children(':first').removeClass('selected'); 
	});
});

function showResponse(responseText, statusText)  { 
	if (statusText == 'success') {
		$('#response_container').empty().append(responseText);
		Cufon.replace('#response');
	} else {
		alert('status: ' + statusText + '\n\nresponseText: \n' + responseText); 
	}
} 

function refreshing(id) {
	$('#captcha_image_'+id).html('<a href="#" onclick="return refreshimg('+id+');" title="Click to refresh image"><img src="/images/captcha_'+id+'.jpg?' + Math.random() + '" width="206" height="32" alt="Captcha image" /></a>');
	return false;
}
