$(document).ready(function(){
	$("#contact_form_submit").click(function(){					   				   
		var hasError = false;

/*
		$(".error").hide();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		

		*/
		var emailToVal = $("#contactFieldEmail").val();

/*
		if(emailToVal == '') {
			$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {	
			$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
			hasError = true;
		}
*/		
		var subjectVal = $("#contactFieldSubject").val();
/*
		if(subjectVal == '') {
			$("#subject").after('<span class="error">You forgot to enter the subject.</span>');
			hasError = true;
		}
*/		
		var messageVal = $("#contactFieldMessage").val();
/*		if(messageVal == '') {
			$("#message").after('<span class="error">You forgot to enter the message.</span>');
			hasError = true;
		}
		
*/		
		if(hasError == false) {
			//$(this).hide();
			//$("#sendEmail li.buttons").append('<img src="/wp-content/themes/default/images/template/loading.gif" alt="Loading" id="loading" />');
			
       $.ajax({   
            //this is the php file that processes the data and send mail   
            url: "/joy/mains/ask_question",    
               
            //GET method is used   
            type: "POST",   
  
            //pass the data            
            data: { email: emailToVal, subject: subjectVal, message: messageVal },        
               
            //Do not cache the page   
            cache: false,   
               
            //success   
            success: function (html) {                 
                //if process.php returned 1/true (send mail success)   
                if (html==1) {                     
                    //hide the form   
					$("#contactForm").fadeOut(200); 
					
					$( "#sendEmail" )[ 0 ].reset();
					
//                    $('.form').fadeOut('slow');                    
                       
                    //show the success message   
  //                  $('.done').fadeIn('slow');   
//                       $("#contactForm").before('<h1>Success</h1><p>Your email was sent.</p>').
 //                  setTimeout(, 2000);
				 
				setTimeout(function() { $("#contactFormResult").fadeIn(200); }, 1000);
				
				setTimeout(function() { $("#contactFormResult").fadeOut(400); }, 5000);
				
                       
                //if process.php returned 0/false (send mail failed)   
                } else alert('Sorry, unexpected error. Please try again later.');                  
            }          
        });  
/*			
			$.post("/joy/mains/ask_question",
   				{ email: emailToVal, subject: subjectVal, message: messageVal },
   					function(data){
						$("#contactForm").slideUp("normal", function() {				   
							
							$("#contactForm").before('<h1>Success</h1><p>Your email was sent.</p>');											
						});
   					}
				 );
				 */ 
		}
		
		return false;
	});						   
});
