/*
 * bug (based on contactable) 1.0 - jQuery Ajax contact form with validation
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2009-08-24 $
 *
 */
 
//extend the plugin
(function($){
	//define the new for the plugin ans how to call it	
	$.fn.bug = function(options) {
		//set default options  
		var defaults = {
			subject : 'A Message',
			recievedMsg : 'Thankyou for your message',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later'
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		
		//make sure we check for the default name
		jQuery.validator.addMethod("checkBugName", function(value, element) {
			if($('#bugname').val() == 'Your name*') { 
				return false;
			}
			return true;
		}, "Please enter your name"); 
		
		//make sure we check for the default email
		jQuery.validator.addMethod("checkBug", function(value, element) {
			if($('#bugmessage').val() == 'Bug/Problem*') {
				return false; 
			}
			return true;
		}, "Please describe the bug"); 
		
		//act upon the element that is passed into the design    
		return this.each(function(options) {
					
			//validate the form 
			var validator =  $("#bugForm").validate({
				//set the rules for the fild names
				rules: {
					bugname: {
						required: true,
						minlength: 2,
						checkBugName: true
					},
					bugmessage: {
						required: true,
						minlength: 2,
						checkBug: true
					}
				},
				//set messages to appear inline
				messages: {}, 
				submitHandler: function() {
					$('.holderbugform').hide();
					$('#completebug').show();
					$('#loadingbug').show();
					$.get('mail.php',{formtypebug: $('#formtypebug').val(), subject:defaults.subject, name:$('#bugname').val(), comment:$('#bugmessage').val()},
					function(data){ 
						$('#loadingbug').css({display:'none'}); 
						if( data == 'success') {
							$('#callbackbug').show().append(defaults.recievedMsg);
						} else {
							$('#callbackbug').show().append(defaults.notRecievedMsg); 
						}
					});		
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);

