(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()) == 'Type your postcode here') {
							return false;
						}
						
						return true;
					}
					
					else {
						var state = true;
						self.find('.personal input[type="text"], .personal textarea').each(function (index, obj) {
							if (trim($(obj).val()) == '' || trim($(obj).val()) == $(this).data('placeholder')) {
								state = false;
							}
						});
						return state;
					}
				};
		
		this.find('.personal ul .text-field').each(function () {
			$(this).data('placeholder', $(this).val());
		});
		
		// set some defaults		
		var settings = {
			distanceToSlide: '-600px'
		};
		$.extend(settings, options);
			
		this.find('.button').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;
			}
		});		
	};
}(jQuery));
