

/*
 * contactable 1.2 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2009-09-24 $
 *
 */

(function($){
    $(function(){
         $('div#contactable').toggle(
            function(){
                $('#fbf_name').val('Name');
                $('#fbf_name').focus(function(){
                    if(this.value == "Name")
                        this.value = "";
                });
                $('#fbf_name').blur(function(){
                    if(this.value == "")
                        this.value = "Name";
                });
                
                $('#fbf_email').val('Email');
                $('#fbf_email').focus(function(){
                    if(this.value == "Email")
                        this.value = "";
                });
                $('#fbf_email').blur(function(){
                    if(this.value == "")
                        this.value = "Email";
                });
                $('#fbf_comment').val('');     
                $('#fbf_category').val('');
                $('#fbf_quickquestion').val('');
                $('#contactForm #loading').css('display', 'none');
                $('#contactForm #callback').css('display', 'none');
                $('#fbf_category').selectbox({className: 'jquery-selectbox3', itemHeight: 25});
                $('#overlay').css({display:'block'});
                /*$(this).animate({"marginLeft":"-=5px"},"fast");
                $('#contactForm').animate({"marginLeft":"-=0px"},"fast");
                $(this).animate({"marginLeft":"+=233px"},"slow");
                $('#contactForm').animate({"marginLeft":"+=390px"},"slow")*/
                $('#feedbackform').animate({"left":"-232px"},"fast", function(){
                    $('#feedbackform').animate({"left":"0px"},"slow");
                });
            },function(){
                $('#feedbackform').animate({"left":"-225px"},"slow");
                
                $('.category').parents('.jquery-selectbox3').unselectbox();
                /*$('#contactForm').animate({"marginLeft":"-=390px"},"slow");                                                       
                $(this).animate({"marginLeft":"-=233px"},"slow").animate({"marginLeft":"+=5px"},"fast");
                $('#overlay').css({display:'none'});*/
            }
        );
        $('#contactForm').submit(function(){
            sendComment();
            return false;
        });
        $('#submit_feedback_btn').click(function(){
            sendComment();
            return false;
        });
        
        function sendComment(){
            var form = document.contactForm;
            if(form.name.value == '' || form.name.value == 'Name')
            {
                alert('Please enter your name.');
                form.name.focus();
                return;
            }
            if(form.email.value == '' || form.email.value == 'Email')
            {
                alert('Please enter your email.');
                form.email.focus();
                return;
            }else{
                var pattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
                var regex = new RegExp( pattern );
                if(!regex.test( form.email.value ))
                {
                    alert('Please enter a valid email address.');
                    form.email.focus();
                    return;
                }
            }
            if(form.category.value == '')
            {
                alert('Please select a category.');
                form.category.focus();
                return;
            }
            if(form.comment.value == '')
            {
                alert('Please enter your comment.');
                form.comment.focus();
                return;
            }
            if(form.quickquestion.value == '')
            {
                alert('Please answer the question above.');
                form.quickquestion.focus();
                return;
            }
            $('#feedbackform #loading').css('display', 'block');
            var curLocation = encodeURIComponent(document.location.href);
            $.ajax({
                type: 'post',
                url: '/sendfeedback.php',
                data: $(form).serialize() + "&location=" + curLocation,
                dataType: 'html',
                success: function(rsp){      
                    $('#feedbackform #loading').css('display', 'none');        
                    if(rsp == 'answerfailure'){
                        alert("Your answer is not correct.");
                    }else if(rsp == 'success'){
                        $('#feedbackform #callback').html('Thanks for the feedback!');        
                        $('#feedbackform #callback').fadeIn('fast', function(){
                            setTimeout(function(){
                                $('#feedbackform #callback').fadeOut('slow', function(){
                                    $('div#contactable').click();
                                });
                                
                            }, 1000);
                        });
                    }else{
                        $('#feedbackform #callback').html('Error: whatever the error is. Please try again and if you are still having problems, please email <a href="mailto:support@aneros.com">support@aneros.com</a>.');        
                        $('#feedbackform #callback').fadeIn('fast', function(){
							alert("Error Message:\n" + rsp);
                            setTimeout(function(){
                                $('#feedbackform #callback').fadeOut('slow', function(){
                                    //$('div#contactable').click();									
                                });
                                
                            }, 1000);
                        });
						
                    }
                },
                error: function(err)
                {
                    $('#feedbackform #loading').css('display', 'none');        
                    alert(err.responseText);                                   
                }
            });
        }        
    });
})(jQuery);
