/*
 * quotable 1.0 - jQuery Ajax quote 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.quotable.js 2009-08-24 $
 *
 */
 
//extend the plugin
(function($){
	//define the new for the plugin ans how to call it	
	$.fn.quotable = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'Email',
			phone: 'Phone',
			address: 'Address',
			city: 'City',
			state: 'State',
			zip: 'Zip',
			fax: 'Fax',
			qrecipient : 'michael@citiesdigital.com',
			qsubject : 'Quote Request',
			qrecievedMsg : 'Thank you for your information, we will quote you as soon as possible.',
			qnotRecievedMsg : 'Sorry but your message could not be sent, try again later',
			qdisclaimer: 'Please feel free to get in touch,<br/>we value your quote_feedback'
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		var quoteShowStatus =1;

		
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			//construct the form
			$(this).html('<div id="quotable"></div><div id="quoteForm" class="quoteFeedContainer"><a id="close_quote_feedback" title="Close"><img src="plugin-images/btn-close.png" border="0" alt="Close" class="close" /></a><div class="topCrv"></div><div class="leftHand"><div class="leftshad"><div class="rightHand"><div class="rightshad"><div class="gradient"><form id="quoteFormId" method="" action=""><div id="loading">Please Wait..<br><img src="plugin-images/ajax-loader.gif" border="0"/></div><div class="holder"><input type="hidden" id="recipient" name="recipient" value="'+defaults.qrecipient+'" /><input type="hidden" id="subject" name="subject" value="'+defaults.qsubject+'" /><table><tr><td><input type="checkbox" id="medisoft" name="medisoft" value="Medisoft"></td><td><label for="medisoft">Medisoft</label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input type="checkbox" id="clinical" name="clinical" value="Medisoft Clinical"></td><td><label for="clinical">Medisoft Clinical</label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input type="checkbox" id="chiro" name="chiro" value="Chiro Quick Charts"></td><td><label for="chiro">Chiro Quick Charts</label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input type="checkbox" id="laserfiche" name="laserfiche" value="Laserfiche"></td><td><label for="laserfiche">Laserfiche</label></td></tr></table><label><input type="text" id="name" name="name" /></label><label><input type="text" id="email" name="email" /></label><label><input type="text" id="phone" name="phone" /></label><label><input type="text" id="address" name="address" /></label><label><input type="text" id="city" name="city" style="width: 236px;" /> ,&nbsp; <input type="text" id="state" name="state" style="width: 50px;" /> &nbsp; <input type="text" id="zip" name="zip" style="width: 100px;" /></label><label><input type="text" id="fax" name="fax" /></label><label><input type="submit" name="Submit" value="SEND" class="btn" /><span style=" padding-top:4px;">'+defaults.qdisclaimer+'</span></label></div></form><p class="thankNote" id="quoteCallback"></p></div></div></div></div></div><div class="botCrv"></div></div>');
			
			$('#quoteFormId #name').example('Name');
			$('#quoteFormId #email').example('Email');
			$('#quoteFormId #phone').example('Phone');
			$('#quoteFormId #address').example('Address');
			$('#quoteFormId #city').example('City');
			$('#quoteFormId #state').example('State');
			$('#quoteFormId #zip').example('Zip');
			$('#quoteFormId #fax').example('Fax');
			
		
			//show / hide function
		function quoteShow() {
				$('#overlay').css({display: 'block'});
				$('div#quotable').animate({"marginLeft": "-=5px"}, "fast"); 
				$('#quoteForm').animate({"marginLeft": "-=0px"}, "fast");
				$('div#quotable').animate({"marginLeft": "+=748px"}, "slow"); 
				$('#quoteForm').animate({"marginLeft": "+=745px"}, "slow");
				$('.quoteFeedContainer .close').css({right: '-4px'});
				quoteShowStatus = 0;
			}
		function quoteHide() {
				$('#quoteForm').animate({"marginLeft": "-=745px"}, "slow");
				$('div#quotable').animate({"marginLeft": "-=748px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
				$('.quoteFeedContainer .close').css({right: '0px'});
				quoteShowStatus =1;
			}
			
			$('div#quotable').click(
				function() {
				if(quoteShowStatus==1)
					quoteShow();
				else
					quoteHide();
			
			});
			
			$('a#close_quote_feedback').click(function() {
				quoteHide();
			
			}
			);
			//validate the form 
			$("#quoteFormId").validate({
				//set the rules for the field names
				rules: {
					name: {
						required: true
					},
					email: {
						required: true,
						email: true
					},
					phone: {
						required: true
					},
					address: {
						required: true
					},
					city: {
						required: true
					},
					state: {
						required: true
					},
					zip: {
						required: true
					},
					fax: {
						required: false
					}
				},
				//set messages to appear inline
				messages: {
					name: "Please enter your name",
					email: "Please enter your email",
					phone: "Please enter your phone number",
					address: "Please enter your address",
					city: "Please enter your city",
					state: "Please enter your state",
					zip: "Please enter your zip code",
					fax: "Please enter your fax number"
				},
				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.get('mail.php',{name:$('#name').val(), email:$('#email').val(), phone:$('#phone').val(), address:$('#address').val(), city:$('#city').val(), state:$('#state').val(), zip:$('#zip').val(), fax:$('#fax').val()},
					function(data){
						$('#loading').css({display:'none'});
						$('.holder').show();
						//document.forms['quoteFormId'].reset()
						name:$('#name').val('');
						email:$('#email').val('');
						phone:$('#phone').val('');
						address:$('#address').val('');
						city:$('#city').val('');
						state:$('#state').val('');
						zip:$('#zip').val('');
						fax:$('#fax').val('');
						alert(data);
						if( data == 'success') {
							$('#quoteCallback').show().append(defaults.qrecievedMsg);
						} else {
							$('#quoteCallback').show().append(defaults.qnotRecievedMsg);
						}
					});		
				}
			});
		});
	};
	//end the plugin call 
})(jQuery);

