//------------------------------------------------------------------------------
// jQuery Capture
// (c) 2011 Mike Everhart // Eagle Web Assets, Inc.
//------------------------------------------------------------------------------
//
// Description:
//   Captures the information from a partially submitted form
//   and saves it to a database so that you can follow up
//------------------------------------------------------------------------------
(function($){  
	$.fn.capture = function(options) {  
  	
		// Default Options
		var defaults = {  
			action: 'blur', // The action that will envoke the script
			ajax: 'capture.ajax.php', // location of the php file that will do all the ajax work
			source: window.location,
			email: '#email',
			includeName: true, // Include the user's name? 
			firstname: '#firstname', // ID of the firstname field
			lastname: '#lastname', // ID of the lastname field
			idField: 'jqc_id', // name/id of the field that is appended to the form to hold the database id
			form: 'parent', // The form that we want to append the field to
			debug: false,
			
			// Callback functions
			onStart: function(){}, // when we start
			onComplete: function(){}, // when everything is finished
			onSuccess: function(){}, // if the lead was captured
			onError: function(){} // if there was an error
			
			
		};  
		var options = $.extend(defaults, options);  
		
		function debug(msg) {
      	if(options.debug && window.console){
      		console.log(msg);
      	}
		}
		
		return this.each(function() {  
			obj = $(this); 
			
			// When the selected field is blurred take action!
			obj.bind(options.action, function(){
				
				options.onStart.call(this);
				
				var params = 'email=' + encodeURIComponent($(options.email).val());
				if( options.includeName ) {
					params += '&fname=' + encodeURIComponent($(options.firstname).val()) + '&lname=' + encodeURIComponent($(options.lastname).val());
				}
				params += '&source=' + encodeURIComponent(options.source);
				debug('Parameters: ' + params);
				
				$.ajax({
					url: options.ajax,
					data: params,
					dataType: 'json',
					success: function(m) {
						// If successful, then append a new hidden field
						// to the parent form to hold the ID of this record
						if( m.status == 'success' ) {
							is_success = true;
							// see if the field already exists. If so, update its value
							if( $('#' + options.idField).length > 0 ) {
								$('#' + options.idField).val(m.message);
								
							// if not, add the field to our form	
							} else {
								var form = options.form == 'parent' ? obj.parents('form') : $(options.form);
								form.append('<input id="' + options.idField + '" type="hidden" name="' + options.idField + '" value="' + m.message + '" />');
							}
						} else {
							is_success = false;
						}
					}
				});
				//options.onComplete.call(this);
			});
		});  
	};  
})(jQuery);  
