(function ($) {
	$.fn.compareClinics = function (options) {
		var self = this,
				gone = false,
				trim = function (stringToTrim) {
					return stringToTrim.replace(/^\s+|\s+$/g,"");
				},
				validate = function () {
					if (gone == false) {
						if (! self.find('.procedure input[type="radio"]').is(':checked')) {
							return false;
						}
						
						if (trim(self.find('.postcode .text-field').val()) == '') {
							return false;
						}
						
						return true;
					}
					
					else {
						var state = true;
						self.find('.personal input[type="text"]').each(function (index, obj) {
							if (trim($(obj).val()) == '') {
								state = false;
							}
						});
						return state;
					}
				};
		
		// set some defaults		
		var settings = {
			distanceToSlide: '-600px'
		};
		$.extend(settings, options);
		
		this.find('#compare').click(function () {	
			if (gone === true) {
				if (validate()) {
					self.submit();
					return gone;
				}
				
				else {
					alert('Please fill out all required fields. We have highlighted any optional fields');	
					return false;
				}
			}
			
			else {
				if (validate()) {
					self.find('.first').animate({
						marginLeft: settings.distanceToSlide
					});
					gone = true;
				}
				
				else {
					alert('Please fill out all of the fields');	
				}
				
				return false;
			}
		});
		
		// make sure that what's entered in other is set
		this.submit(function () {
			var other = self.find('#other-field');
			if (other.is(':checked')) {
				other.val(self.find('#other-specify').val());
			}
		});
	};
}(jQuery));
