/*
 * 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
	var a = Math.ceil(Math.random() * 9);
    var b = Math.ceil(Math.random() * 9);       
    var c = a + b;
    var myNumbers = new Array();
    myNumbers[1] = "unu";
    myNumbers[2] = "doi";
    myNumbers[3] = "trei";
    myNumbers[4] = "patru";
    myNumbers[5] = "cinci";
    myNumbers[6] = "sase";
    myNumbers[7] = "sapte";
    myNumbers[8] = "opt";
    myNumbers[9] = "noua";
    myNumbers[0] = "zero";
(function($){

	//define the new for the plugin ans how to call it	
	$.fn.contactable = function(options) {
		//set default options  
		var defaults = {
			url: 'http://www.efin.ro/index/trimiteIntrebare',
			name: 'Nume',
			email: 'Email',
			message : 'Intrebare',
			subject : 'Intrebare noua de la ' + $("#name").val(),
			submit : 'Trimite',
			recievedMsg : 'Va multumim pentru intrebarea adresata! <br /><br /> In cel mai scurt timp specialistii nostri va vor raspunde la adresa de email mentionata in formular.',
			notRecievedMsg : 'Intrebarea nu a putut fi trimisa, va rugam incercati mai tarziu',
			disclaimer: 'Adresati-ne intrebarea iar specialistii nostri va vor raspunde in cel mai scurt timp!',
			hideOnSubmit: false

		};

		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function() {
			//construct the form
			var this_id_prefix = '#'+this.id+' ';
			
			$(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">'+options.name+'</label><br /><input id="name" class="contact" name="name"/></p><p><label for="email">'+options.email+' </label><br /><input id="email" class="contact" name="email" /></p><p><label for="message">'+options.message+' </label><br /><textarea id="message" name="message" class="message" rows="4" cols="30" ></textarea></p><p id="adunare">Cat face '+ myNumbers[a] + ' + ' + myNumbers[b] +'? <i>(verificare antispam)</i> <input id="BotBootInput" type="text" maxlength="2" size="2" style="width: 130px"/><i>(rezultat in cifre)</i></p><p><input class="submit" type="submit" value="'+options.submit+'" onclick="_gaq.push([\'_trackEvent\', \'trimite intrebare\', \'Click trimite intrebare\', \'/event/trimite_intrebare_ureche\'])" /></p><p class="disclaimer">'+options.disclaimer+'</p></div></form>');
			//show / hide function
			
			$('#ureche_img').toggle(
			function() {
				$(this_id_prefix+'#overlay').css({display: 'block'});
				$(this).animate({"marginRight": "-=5px"}, "fast"); 
				$(this_id_prefix+'#contactForm').animate({"marginRight": "-=0px"}, "fast");
				$(this).animate({"marginRight": "+=387px"}, "slow"); 
				$(this_id_prefix+'#contactForm').animate({"marginRight": "+=390px"}, "slow"); 
			}, 
			function() {
				
				$(this_id_prefix+'#contactForm').animate({"marginRight": "-=390px"}, "slow");
				$(this).animate({"marginRight": "-=387px"}, "slow").animate({"marginRight": "+=5px"}, "fast"); 
				$(this_id_prefix+'#overlay').css({display: 'none'});
			});
			
			//validate the form 
			$(this_id_prefix+"#contactForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					message: {
						required: true
					}
				},
				//set messages to appear inline
					messages: {
						name: "",
						email: "",
						message: ""
					},			

				submitHandler: function() {
					var d = $(this_id_prefix+'#BotBootInput').val();
					if(d != c) {
						$(this_id_prefix+"#BotBootInput").css("background", "#EDBE9C");
						$("<p>Adunarea nu este corecta</p>").after(this_id_prefix+"#adunare");
						return false;
					} else {
						$(this_id_prefix+"#BotBootInput").css("background", "none");
						$(this_id_prefix+'.holder').hide();
						$(this_id_prefix+'#loading').show();
					$.ajax({
					  type: 'POST',
					  url: options.url,
					  data: {subject:options.subject, name:$(this_id_prefix+'#name').val(), email:$(this_id_prefix+'#email').val(), message:$(this_id_prefix+'#message').val()},
					  success: function(data){
						$(this_id_prefix+'#loading').css({display:'none'});
						
							var trimedText = trim(data);
							if( trimedText == 'ok') {
								$(this_id_prefix+'#callback').show().append(options.recievedMsg);
								setTimeout(function(){
								  $(this_id_prefix+'#callback').hide().html('');
								  var cForm = document.forms.contactForm;
								  cForm.reset();
								  $(this_id_prefix+'.holder').show();
								},4000);
								if(options.hideOnSubmit == true) {
									//hide the tab after successful submition if requested
									$(this_id_prefix+'#contactForm').animate({dummy:1}, 2000).animate({"marginRight": "-=450px"}, "slow");
									$(this_id_prefix+'div#contactable').animate({dummy:1}, 2000).animate({"marginRight": "-=447px"}, "slow").animate({"marginRight": "+=5px"}, "fast"); 
									$(this_id_prefix+'#overlay').css({display: 'none'});
									var cForm = document.forms.contactForm;
									cForm.reset();
								}
							} else {
								$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
								var cForm = document.forms.contactForm;
								cForm.reset();
								setTimeout(function(){
									$(this_id_prefix+'.holder').show();
									$(this_id_prefix+'#callback').hide().html('');
								},4000);
							}
						
					  },
					  error:function(){
						  $(this_id_prefix+'#loading').css({display:'none'}); 
						  $(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
						  setTimeout(function(){
							  $(this_id_prefix+'#callback').hide().html('');
								$(this_id_prefix+'.holder').show();
							},2000);
					  }
					});
					
					}
				}
			});
		});
	};
 
})(jQuery);
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
} 
