

/**
 * Contact form 
 * 
 * Ajax call for 
 */
$(document).ready(
	function() 
	{
		
		$('#ContactForm_name').focus(
			function() 
			{
				$(this).toggleClass("highlight");
			}
		),
		$('#ContactForm_name').blur(
			function() 
			{
				$(this).toggleClass("highlight");
			}
		),
		$('#ContactForm_email').focus(
				function() 
				{
					$(this).toggleClass("highlight");
				}
		),
		$('#ContactForm_email').blur(
			function() 
				{
					$(this).toggleClass("highlight");

			}
		),
		$('#ContactForm_body').focus(
				function() 
				{
					$(this).toggleClass("highlight");
				}
		),
		$('#ContactForm_body').blur(
			function() 
				{
					$(this).toggleClass("highlight");

			}
		),	
		
		$("#messageWrapper").hide();
		
		
		// the click
		$('#sendContactForm').click(
		function() 
		{
			$("#messageWrapper").show();
			$.ajax(
				{
		            type: 'POST',
		            data: $(this).parents('form').serialize(),
		            cache: false,
		            url: 'index.php?r=site/sendContactForm',
		            dataType: 'json',
	            
		            beforeSend: function() 
		            {
						$("#contactFormMessage").show();
  						$("#messageWrapper").addClass("loading");
  						$("#contactFormMessage").css('height', "40px");
  						$("#contactFormMessage").css('background', "url(" + IMAGE_URL +"ajax-loader.gif) no-repeat");
  						$("#contactFormMessageText").html('Die Daten werden gepr&uuml;ft.');
	      			},
	      		
	      			complete: function() 
	      			{
        				
  						$("#messageWrapper").removeClass("loading");
  						$("#contactFormMessage").css('background', "white");
  						//$("#contactFormMessage").css('background', '#fff');
  						// first remove all possible error messages from previous validations
	      				//$("#contactFormMessageText").html('Die Daten wurden übermittelt.');
  				    },
  				    
  				    success: function(data)
  				    {
  				    	$("#contactFormMessageText").html('Die Daten wurden übermittelt.');
  				    	//$("#messageWrapper").show();
  				    	//$("#messageWrapper").removeClass("loading");
  				    	
  				    	
  				    	// first remove all possible error messages from previous validations
  		            	$("#nameError").empty();
  		            	$("#emailError").empty();
  		            	$("#bodyError").empty();
  		            	
  		            	
  		                // check if there are any errors
  		                if(exists(data.errors))
  		                {
  		                    if(exists(data.errors.name))
  		                    {
  		                	    $("#nameError").html(data.errors.name);
  		                	    $("#nameError").css("display", "block");
  		                    }                    
  		                    else
  		                    {
  		                    	$("#nameError").empty();
  		                    	$("#nameError").css("display", "none");
  		                    }
  		                    
  		                    // email
  		                    if(exists(data.errors.email))
  		                    {
  		                	    $("#emailError").html(data.errors.email);
  		                	    $("#emailError").css("display", "block");
  		                    }
  		                    else
  		                    {
  		                    	$("#emailError").empty();
  		                    	$("#emailError").css("display", "none");
  		                    }
  		                    
  		                    // body (=message content)
  		                    if(exists(data.errors.body))
  		                    {
  		                	    $("#bodyError").html(data.errors.body);
  		                	    $("#bodyError").css("display", "block");
  		                    }
  		                    else
		                    {
		                    	$("#bodyError").empty();
		                    	$("#bodyError").css("display", "none");
		                    }
  		                }
  		                else
  		                {
  		                }
  		                $("#contactFormMessageText").html(data.message);
  				    	
  				    } // end success
				}
				);
				return false;
				
			} // end click function
		);
		
	}
);


/**
 * Newslettter form 
 * 
 * ajax call for buttons "subscribe" and "unsubscribe" 
 */
$(document).ready(
	function() 
	{
		$("#newsletterFeedback").hide();
		
		// click on button subscribe
		$('#buttonSubscribe').click(
			function() 
			{
				$.ajax(
					{
			            type: 'POST',
			            data: $(this).parents('form').serialize(),
			            cache: false,
			            url: 'index.php?r=site/subscribeNewsletter',
			            dataType: 'json',
		            
			            beforeSend: function() 
			            {
							$("#newsletterFeedback").show();
							$("#newsletterFeedback").css('background', "url(" + IMAGE_URL + "ajax-loader.gif) no-repeat");
	  						$("#newsletterFeedback").html('Die Daten werden gepr&uuml;ft.');
		      			},
		      		
		      			complete: function() 
		      			{
		      				$("#newsletterFeedback").css('background', "");
	  				    },
	  				    
	  				    success: function(data)
	  				    {
	  				    	$("#newsletterFeedback").html(data.message);
	  				    } // end success
					}
				);
				return false;
					
			} // end click function
		);
		
		// click on button subscribe
		$('#buttonUnsubscribe').click(
				
			function() 
			{
				$.ajax(
					{
			            type: 'POST',
			            data: $(this).parents('form').serialize(),
			            cache: false,
			            url: 'index.php?r=site/unsubscribeNewsletter',
			            dataType: 'json',
		            
			            beforeSend: function() 
			            {
							$("#newsletterFeedback").show();
							$("#newsletterFeedback").css('background', "url(" + IMAGE_URL +"ajax-loader.gif) no-repeat");
	  						$("#newsletterFeedback").html('Die Daten werden gepr&uuml;ft.');
		      			},
		      		
		      			complete: function() 
		      			{
		      				$("#newsletterFeedback").css('background', "");
	  				    },
	  				    
	  				    success: function(data)
	  				    {
	  				    	$("#newsletterFeedback").html(data.message);
	  				    } // end success
					}
				); 
				return false;
				
					
			} // end click function
		);
	}
);


/**
 * Rollover effect for send button of the contact form
 */
$(document).ready(function() {
    $('#sendContactForm').hover(
        function(){ // Change the input image's source when we "roll on"
            $(this).removeClass("buttonSmallOK");
            $(this).addClass("buttonSmallOKActive");
        },
        function(){ // Change the input image's source back to the default on "roll off"
    	    $(this).removeClass("buttonSmallOKActive");
            $(this).addClass("buttonSmallOK");
        }
    );
});



            


function isValidEmail(email)
{
    filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(email)) {
      // Yay! valid
      return true;
    }
    else
      {return false;}
        
}

