/*
 * contactable 1.2.1 - 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 2010-01-18 $
 *
 */
 
//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: 'Nume',
			email: 'Email',
			message : 'Mesaj',
			subject : 'Cerere de oferta',
			telefon: 'Telefon',
			oras: 'Oras',
			produse: 'Produse',
			bucati: 'Numar bucati',
			recievedMsg : 'Multumim pentru cererea de oferta. Va vom contacta cat de curand.<br><img height="1" width="1" alt="" src="http://www.googleadservices.com/pagead/conversion/973826232/imp.gif?label=8QvZCLC3xwMQuNGt0AM&amp;guid=ON&amp;script=0"/>',
			notRecievedMsg : 'Ne pare rau dar formularul nu a putut fi trimis. Te rugam sa mai incerci.',
			disclaimer: 'Pentru alte detalii ne puteti contacta la tel: 0726-332.696, 0723-628.112 sau 0751-768.939',
			titlu: document.title,
			locatia: document.location.href,
			hideOnSubmit: true
		};

		//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="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">Nume <span class="red"> * </span></label><br /><input id="name" class="contact" name="name" /></p><p><label for="email">E-Mail <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="telefon">Telefon <span class="red"> * </span></label><br /><input id="telefon" class="contact" name="telefon" /></p><p><label for="oras">Oras <span class="red"> * </span></label><br /><input id="oras" class="contact" name="oras" /></p><p><label for="produse">Cod produs/produse/servicii <span class="red"> * </span></label><br /><input id="produse" class="contact" name="produse" /></p><p><label for="bucati">Numar bucati </label><br /><input id="bucati" class="contact" name="bucati" /></p><p><label for="comment">Detalii suplimentare </label><br /><textarea id="comment" name="comment" class="comment" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="Trimite"/></p><p class="disclaimer">'+defaults.disclaimer+'</p></div></form>');
			//show / hide function
			$('div#contactable').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginRight": "-=5px"}, "fast"); 
				$('#contactForm').animate({"marginRight": "-=0px"}, "fast");
				$(this).animate({"marginRight": "+=387px"}, "slow"); 
				$('#contactForm').animate({"marginRight": "+=390px"}, "slow"); 
			}, 
			function() {
				$('#contactForm').animate({"marginRight": "-=390px"}, "slow");
				$(this).animate({"marginRight": "-=387px"}, "slow").animate({"marginRight": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			$('div#newcerere').toggle(function() {
				$('#overlay').css({display: 'block'});
				$('#contactForm').animate({"marginRight": "-=0px"}, "fast");
				$('#contactable').animate({"marginRight": "-=0px"}, "fast");
				$('#contactForm').animate({"marginRight": "+=390px"}, "slow");
				$('#contactable').animate({"marginRight": "+=382px"}, "slow");  
			}, 
			function() {
				$('#contactForm').animate({"marginRight": "-=390px"}, "slow");
				$('#contactable').animate({"marginRight": "-=382px"}, "slow");
				$('#overlay').css({display: 'none'});
			});
			//validate the form 
			$("#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					telefon: {
						required: true,
						minlength: 2
					},
					oras: {
						required: true,
						minlength: 2
					},
					produse: {
						required: true,
						minlength: 2
					},
					bucati: {
						required: false
					},
					comment: {
						required: false
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						telefon: "",
						oras: "",
						produse: "",
						bucati: "",
						comment: ""
					},			

				submitHandler: function() {
					$('.holder').hide();
					$('#loading').show();
					$.post('../mail.php',{subject:defaults.subject, name:$('#name').val(), email:$('#email').val(), comment:$('#comment').val(), telefon:$('#telefon').val(), oras:$('#oras').val(), produse:$('#produse').val(),titlu:defaults.titlu,locatia:defaults.locatia, bucati:$('#bucati').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if( data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
							if(defaults.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								$('#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
								$('div#contactable').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$('#overlay').css({display: 'none'});	
							}
						} else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
		});
	};
})(jQuery);


