add files
[uma.git] / public / js / contact.js
blob785da714bac9d07c446f9e954b706c58d3a6c4ab
1 $(document).ready(function(){
2                         $('#send_message').click(function(e){
3                                 
4                                 //stop the form from being submitted
5                                 e.preventDefault();
6                                 
7                                 /* declare the variables, var error is the variable that we use on the end
8                                 to determine if there was an error or not */
9                                 var error = false;
10                                 var name = $('#name').val();
11                                 var email = $('#email').val();
12                                 var subject = $('#subject').val();
13                                 var message = $('#message').val();
14                                 
15                                 /* in the next section we do the checking by using VARIABLE.length
16                                 where VARIABLE is the variable we are checking (like name, email),
17                                 length is a javascript function to get the number of characters.
18                                 And as you can see if the num of characters is 0 we set the error
19                                 variable to true and show the name_error div with the fadeIn effect. 
20                                 if it's not 0 then we fadeOut the div( that's if the div is shown and
21                                 the error is fixed it fadesOut. 
22                                 
23                                 The only difference from these checks is the email checking, we have
24                                 email.indexOf('@') which checks if there is @ in the email input field.
25                                 This javascript function will return -1 if no occurence have been found.*/
26                                 if(name.length == 0){
27                                         var error = true;
28                                         $('#name_error').fadeIn(500);
29                                 }else{
30                                         $('#name_error').fadeOut(500);
31                                 }
32                                 if(email.length == 0 || email.indexOf('@') == '-1'){
33                                         var error = true;
34                                         $('#email_error').fadeIn(500);
35                                 }else{
36                                         $('#email_error').fadeOut(500);
37                                 }
38                                 if(subject.length == 0){
39                                         var error = true;
40                                         $('#subject_error').fadeIn(500);
41                                 }else{
42                                         $('#subject_error').fadeOut(500);
43                                 }
44                                 if(message.length == 0){
45                                         var error = true;
46                                         $('#message_error').fadeIn(500);
47                                 }else{
48                                         $('#message_error').fadeOut(500);
49                                 }
50                                 
51                                 //now when the validation is done we check if the error variable is false (no errors)
52                                 if(error == false){
53                                         //disable the submit button to avoid spamming
54                                         //and change the button text to Sending...
55                                         $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });
56                                         
57                                         /* using the jquery's post(ajax) function and a lifesaver
58                                         function serialize() which gets all the data from the form
59                                         we submit it to send_email.php */
60                                         $.post("send_email.php", $("#contact_form").serialize(),function(result){
61                                                 //and after the ajax request ends we check the text returned
62                                                 if(result == 'sent'){
63                                                         //if the mail is sent remove the submit paragraph
64                                                          $('#button').remove();
65                                                         //and show the mail success div with fadeIn
66                                                         $('#mail_success').fadeIn(500);
67                                                 }else{
68                                                         //show the mail failed div
69                                                         $('#mail_fail').fadeIn(500);
70                                                         //reenable the submit button by removing attribute disabled and change the text back to Send The Message
71                                                         $('#send_message').removeAttr('disabled').attr('value', 'Submit');
72                                                 }
73                                         });
74                                 }
75                         });    
76                 });