// JavaScript Document

//CONTACT JAVASCRIPT
$(function(){
  $("form").submit(function(){               //This initiates the ajax request, if javascript is 
	  Email = $("#EmailAddress").val();	
	  Name = $("#Name").val();
	  Comments = $("#UserComments").val();
	  $("#Table1").fadeOut(1000, function(){
	  	  $("<div id='LoadingOverlay'/>").hide().appendTo('#ContactForm').fadeIn(250); 	 									  
	  });					  
	  $.ajax({
		  type: "post",
		  url: "Contact_FormHandler.php",
		  data: {email: Email, name: Name, comments:Comments},
		  timeout: 15000,                                        //If a response doesn't come within 15 seconds, abort ajax attempt.
		  error: function(XMLHttpRequest,timeout){               //The function to run in the event of a timeout.    
			  	  to_response = "<p class='errorWarning'>An error occurred when saving your request, please try again.  If this problem persists, please send an email to Support@WCAssist.com so we can correct this ASAP.</p>"
				  $('#LoadingOverlay').fadeOut(250,function(){
					$(this).remove();
				  });  
				  $('<div id="Response" class="responseClass">'+to_response+'</div>').hide().appendTo("#ContactForm").fadeIn(1000);
			      $("<div id='Refresh' class='responseClass'><p><a href ='#'>Click Here</a> to try your request again.</div></p>").hide().appendTo("#ContactForm").fadeIn(1000);
				  $('#Refresh').click(function(){
						$('#Response, #Refresh').fadeOut(500,function(){
							$('#Response, #Refresh').remove();										  
							$("#Table1").fadeIn(500);											  
						});
  				   });	 
				 },
		  success: function(r){
			  setTimeout (function(){
				  $('#LoadingOverlay').fadeOut(250,function(){
					$(this).remove();										
				  });			   
				  $('<div id="Response" class="responseClass">'+r+'</div>').hide().appendTo("#ContactForm").fadeIn(1000);
				  $("<div id='Refresh' class='responseClass'><p><a href ='#'>Click Here</a> to enter a new inquiry.</div></p>").hide().appendTo("#ContactForm").fadeIn(1000);
				  $('#Refresh').click(function(){
						$('#Response, #Refresh').fadeOut(500,function(){
							$('#Response, #Refresh').remove();										  
							$("#Table1").fadeIn(500);											  
						});
  				   });      
				  },1000); //End setTimeout
		  }//End success:
	  }); //End $.ajax
	  return false;
  });

})