/* 
 *      Connect.js file that handle the submission of the connect form.
 */


var connectForm = new function () {
    return {
        name: null,
        email: null,
        subject: null,
        message: null,
        nameField: null,
        emailField: null,
        subjectField: null,
        messageField: null,
        thisForm: null,

        init: function() {
            
            // hold the reference to the connectForm object
            thisForm = this;

            // bind the submit function to the send button
            $('#sendButton').live('click', this.submit);
            
            // store the text fields as variables
            $('input.name').live('focus blur', thisForm.removeError);
            $('input.email').live('focus blur', thisForm.removeError);
            $('input.subject').live('focus blur', thisForm.removeError);
            $('textarea.message').live('focus blur', thisForm.removeError);

            // bind the duplicate textfield function to make the value of two identical fields the same
            $('input.name').live('keyup blur', function(){
                $('input.name').val($(this).val());
            });
            $('input.email').live('keyup blur', function(){
                $('input.email').val($(this).val());
            });
            $('input.subject').live('keyup blur', function(){
                $('input.subject').val($(this).val());
            });
            $('textarea.message').live('keyup blur', function(){
                $('textarea.message').val($(this).val());
            });
        },
        submit: function() {
            // set the properties with the values in the text fields
            thisForm.name = escape($('input.name').val());
            thisForm.email = escape($('input.email').val());
            thisForm.subject = escape($('input.subject').val());
            thisForm.message = escape($('textarea.message').val());
            
            // serialize the email data and send it to sendEmail.php via ajax
            var dataToSend = $('#emailForm').serialize();

            if (thisForm.validate()) {
                $.ajax({
                    url: 'control/sendEmail.php',
                    data: dataToSend,
                    dataType: 'text',
                    type: 'get',
                    success: function() {
                        alert('Email sent!');
                    }
                });
            }
        },
        validate: function() {
            var valid = true;
            var errorMessage;
            if (thisForm.name == '') {
                $('input.name').addClass('error');
                valid = false;
            }
            if (thisForm.email == '') {
                $('input.email').addClass('error');
                valid = false;
            }
            if (thisForm.subject == '') {
                $('input.subject').addClass('error');
                valid = false;
            }
            if (thisForm.message == '') {
                $('textarea.message').addClass('error');
                valid = false;
            }

            if (!valid) {
                $('div#errorMessage').html('* Fields cannot be blank.');
            }
            if (!Validator.email(thisForm.email) && thisForm.email != '') {
                $('input.email').addClass('error');
                $('div#errorMessage').append('<br />* Email is invalid.');
                valid = false;
            }

            return valid;
        },
        removeError: function() {
            var className = $(this).attr('class').split(' ')[0];
            $('input.' + className + ', textarea.' + className).removeClass('error');
            $('div#errorMessage').html('');
        }

    }
}();

$(function() {
    connectForm.init();

});




