/*
 * contactable 1.0 - jQuery Ajax contact form
 *
 * 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.contactable = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'Email',
			message : 'Message',
			recipient : 'sales@acetravels.com',
			subject : 'A feedback message',
			recievedMsg : 'Thankyou for your feedback',
			notRecievedMsg : 'Sorry but your feedback could not be sent, please try again later',
			disclaimer: 'We are always improving our site, so your feedback is highly valued.',
			footer: 'Please feel free to get in touch. We appreciate your feedback.'
		};
//<span class="red"> * </span>
		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			$(this).html('<div id="contactable"></div><form id="feedbackForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><input type="hidden" id="recipient" name="recipient" value="'+defaults.recipient+'" /><input type="hidden" id="subject" name="subject" value="'+defaults.subject+'" /><p><b>'+defaults.disclaimer+'</b></p><br /><p><label for="name" style="width: 50px;">Name </label><input id="name" class="contact" name="name" style="width:260px;" /></p><p><label for="email" style="width: 50px;">E-Mail </label><input id="email" style="width:260px;" class="contact" name="email" /></p><p><div><div class="Clear"><label for="user_friendly">User Friendly</label><input class="star" type="radio" name="user_friendly" value="1" title="No" checked="checked"/><input class="star" type="radio" name="user_friendly" value="2" title="Maybe"/><input class="star" type="radio" name="user_friendly" value="3" title="Yes"/><input class="star" type="radio" name="user_friendly" value="4" title="Yes"/><input class="star" type="radio" name="user_friendly" value="5" title="Yes"/></div><div class="Clear"><label for="site_information">Valued Information</label><input class="star" type="radio" name="site_information" value="1" checked="checked"/><input class="star" type="radio" name="site_information" value="2"/><input class="star" type="radio" name="site_information" value="3"/><input class="star" type="radio" name="site_information" value="4"/><input class="star" type="radio" name="site_information" value="5"/></div><div class="Clear"><label for="site_design">Site Look</label><input class="star" type="radio" name="site_design" value="1" checked="checked"/><input class="star" type="radio" name="site_design" value="2"/><input class="star" type="radio" name="site_design" value="3"/><input class="star" type="radio" name="site_design" value="4"/><input class="star" type="radio" name="site_design" value="5"/></div><div class="Clear"><label for="site_bugs">Error Free</label><input class="star" type="radio" name="site_bugs" value="1" checked="checked"/><input class="star" type="radio" name="site_bugs" value="2"/><input class="star" type="radio" name="site_bugs" value="3"/><input class="star" type="radio" name="site_bugs" value="4"/><input class="star" type="radio" name="site_bugs" value="5"/></div><div class="Clear"><label for="overall_rating">Overall Rating</label><input class="star" type="radio" name="overall_rating" value="1" checked="checked"/><input class="star" type="radio" name="overall_rating" value="2"/><input class="star" type="radio" name="overall_rating" value="3"/><input class="star" type="radio" name="overall_rating" value="4"/><input class="star" type="radio" name="overall_rating" value="5"/></div></div></p><br /><p><label for="comment">Your Feedback </label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p>Please enter the character as displayed.</p><p><label>&nbsp;</label><img id="captcha" src="'+globalJSLiveURL+'/captcha/image.php" alt="" border="0"/><input name="trip_security_code" type="text" id="trip_security_code"  style=" width:75px;" /><span id="err"></span></td></p><p><label>&nbsp;</label></p><p><input class="submit" type="submit" value="Send"/></p><p>'+defaults.footer+'</p></div></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#feedbackForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=387px"}, "slow"); 
				$('#feedbackForm').animate({"marginLeft": "+=390px"}, "slow"); 
			}, 
			function() {
				$('#feedbackForm').animate({"marginLeft": "-=390px"}, "slow");
				$(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			//var code	=	$('#security_code').val();
			//alert(code);
			//validate the form 
			$("#feedbackForm").validate({
				
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					comment: {
						required: true
					},
					trip_security_code :{
						required: true,
						remote : "captcha/validate.php"
					}


				},
				//set messages to appear inline
				messages: {
					name: "Enter your name",
					email: "Enter Email Address",
					comment: "Enter Comment",
					trip_security_code : "Please correct the security code"
				},				
				submitHandler: function() {
					
					
					
					$('.holder').hide();
					$('#loading').show();
					$.get('mail.php',{recipient:$('#recipient').val(), subject:$('#subject').val(), name:$('#name').val(), email:$('#email').val(), user_friendly:$("input[name='user_friendly']:checked").val(), site_information:$("input[name='site_information']:checked").val(), site_design:$("input[name='site_design']:checked").val(), site_bugs:$("input[name='site_bugs']:checked").val(), overall_rating:$("input[name='overall_rating']:checked").val(), comment:$('#comment').val()},
					function(data){
						$('#loading').css({display:'none'}); 						
						if( data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});
				}	

				
			});
		});
	};
	//end the plugin call 
})(jQuery);


