/* <![CDATA[ */

jQuery.fn.clearForm = function() {

  return this.each(function() {

    if (this.tagName.toLowerCase() == "form") {

      return jQuery(":input",this).clearForm();

    }

    switch (this.type) {

      case "password":

      case "textarea":

      case "text":

        this.value = "";

        break;

      case "select-multiple":

      case "select-one":

        this.selectedIndex = -1;

        break;

      case "radio":

      case "checkbox":

        this.checked = false;

        break;

    }

  });

};



jQuery(document).ready(function(){

		jQuery.each(jQuery('.efs-ajax-form'),function(){
			jQuery(jQuery(':text'),jQuery(this).children('input'))
				.focusin(function(){
			 		//jQuery('.'+jQuery(this).attr('id')+'formError').remove();
					//if(jQuery(this).val()=="")	
 					//jQuery(this).prev('label').css('display','inline').fadeOut(1000);
					console.log(jQuery(this));
					if(jQuery(this).val()=="")
					if(jQuery(this).hasClass('inputError'))
					jQuery(this).toggleClass('inputError');
					}); 
				
			jQuery(jQuery(':text'),jQuery(this).children('input'))
				.focusout(function(){
				 	//if(jQuery(this).val()=="")	
 					//jQuery(this).prev('label').fadeIn(1000);
					console.log(jQuery(this));
					if(jQuery(this).val()=="")
					jQuery(this).toggleClass('inputError');
					}); 
				
			 jQuery(this).bind({

				  submit: function() {

		

		jQuery(this).find('.error').remove();

		var hasError = false;

		

		jQuery('.required').each(function() {

			if(jQuery.trim(jQuery(this).val()) == '') {

				var labelText = jQuery(this).prev('span.label').text();

				jQuery(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');

				jQuery(this).addClass('inputError');

				hasError = true;

			} else if(jQuery(this).hasClass('email')) {

				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

				if(!emailReg.test(jQuery.trim(jQuery(this).val()))) {

					 var labelText = jQuery(this).prev('label').text();

					 jQuery(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');

					jQuery(this).addClass('inputError');

					hasError = true;

				}

			}

		});

		if(!hasError) { 

	

	var $this = jQuery(this);

	jQuery($this).append('<div class="'+jQuery($this).attr('id')+'-sending loading" style="position:absolute; top:0; left:0; width:'+jQuery($this).width()+'px; height:'+jQuery($this).height()+'px;"></div>');

  	var admin_ajaxurl = jQuery('#efs_ajax_admin_url').val();

	var data = jQuery($this).serialize();

	jQuery.post(admin_ajaxurl, data, function(response){

				jQuery($this).append('<div class="'+jQuery($this).attr('id')+'-sending loading" style="position:absolute; top:0; left:0; width:'+jQuery($this).width()+'px; height:'+jQuery($this).height()+'px;"></div>');



		var res = jQuery.parseJSON(response);

		jQuery(this).delay(1500,function() {

			jQuery($this).fadeOut('fast', function() {

				var resp = '';

				switch(res.respond){

					case'0':

					resp = 'Sorry your message was not sent! Please try again!';

					break;

					case'1':

					resp = 'Thank You! Please allow 24 hours for our response!';

					jQuery($this).clearForm();

					break;

					}

				jQuery($this).find('.inputError').removeClass('inputError');	

				jQuery('#efs-ajax-form').after('<p class="efs-respond">' + resp + '</p><!--json-->');

				

				jQuery($this).fadeIn(100, function() {

					

					jQuery('.efs-respond').fadeOut(5000, function() {

						var $respond = jQuery(this);

						jQuery('.'+jQuery($this).attr('id')+'-sending').fadeOut(2000, function() {

							jQuery(this).remove();

							jQuery($respond).remove();

							

							});

						});

					

					});

					

				});

			});

		});

		}

  	return false; 

	 }

 

			

			

			});

	});



	

  

});

/* ]]> */
