var formsBasePath = '/';

var contactForm = {
    id        : 'contact_form',
    backend   : formsBasePath + 'forms/forms.php',
    throbberId: 'contact_form_progress',
    submitId  : 'contact_form_submit',

    fields: {
        errorClassName: 'field_error',
        errorIdPreffix: 'contact_form_field_error_',

        setError: function(field, message) {
            jQuery('#' + this.errorIdPreffix + field).text(message).show();
        },
        clearErrors: function() {
            contactForm.get().find('.' + this.errorClassName).text('').hide();
        }
    },
    messages: {
        containerId     : 'contact_form_messages',
        className       : 'contact_form_message',
        errorClassName  : 'contact_form_message_error',
        messageClassName: 'contact_form_message_message',
        delay           : 10,

        add: function(message, error) {
            jQuery('<div>')
            .addClass(this.className)
            .addClass(error ? this.errorClassName : this.messageClassName)
            .text(message)
            .appendTo('#' + this.containerId)
            .delay(this.delay * 1000)
            .fadeOut(1000)
            .queue(function() {
                $(this).remove();
            });
        },
        addError: function(message) {
            contactForm.messages.add(message, true);
        },
        addMessage: function(message) {
            contactForm.messages.add(message, false);
        },
        clearMessages: function(){
            jQuery('#' + this.containerId).empty();
        }
    },
    get: function() {
        if ('undefined' == typeof this.form) {
            this.form = jQuery('form#' + this.id);
        }

        return this.form;
    },
    reset: function() {
        this.get()[0].reset();
    },
    submit: function() {
        contactForm.messages.clearMessages();
        contactForm.fields.clearErrors();
        var submit   = jQuery('#' + this.submitId).attr('disabled', 'disabled');
        var throbber = jQuery('#' + this.throbberId).css('visibility', 'visible');

        jQuery.ajax({
            url     : this.backend,
            data    : this.get().serialize() + '&form_id=' + this.get().attr('id'),
            type    : 'GET',
            cache   : false,
            dataType: 'json',

            success: function(data, textStatus, jqXHR) {
                if ('object' != typeof data || 'undefined' == typeof data.status) {
                    contactForm.messages.addError('Invalid JSON structure');
                }
                else if (data.status != 'OK') {
                    if (data.message) {
                        contactForm.messages.addError(data.message);
                    }

                    jQuery.each(data.fields, function(field, message) {
                        contactForm.fields.setError(field, message);
                    });
                }
                else if (data.message) {
                    contactForm.reset();
                    contactForm.messages.addMessage(data.message);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                contactForm.messages.addError(textStatus);
            },
            complete: function() {
                throbber.css('visibility', 'hidden');
                submit.removeAttr('disabled');
            }
        });
    }
};

var gunSalesForm = {
    id        : 'gun_sales_form',
    backend   : formsBasePath + 'forms/forms.php',
    throbberId: 'gun_sales_form_progress',
    submitId  : 'gun_sales_form_submit',

    fields: {
        errorClassName: 'field_error',
        errorIdPreffix: 'gun_sales_form_field_error_',

        setError: function(field, message) {
            jQuery('#' + this.errorIdPreffix + field).text(message).show();
        },
        clearErrors: function() {
            gunSalesForm.get().find('.' + this.errorClassName).text('').hide();
        }
    },
    messages: {
        containerId     : 'gun_sales_form_messages',
        className       : 'gun_sales_form_message',
        errorClassName  : 'gun_sales_form_message_error',
        messageClassName: 'gun_sales_form_message_message',
        delay           : 10,

        add: function(message, error) {
            jQuery('<div>')
            .addClass(this.className)
            .addClass(error ? this.errorClassName : this.messageClassName)
            .text(message)
            .appendTo('#' + this.containerId)
            .delay(this.delay * 1000)
            .fadeOut(1000)
            .queue(function() {
                $(this).remove();
            });
        },
        addError: function(message) {
            gunSalesForm.messages.add(message, true);
        },
        addMessage: function(message) {
            gunSalesForm.messages.add(message, false);
        },
        clearMessages: function(){
            jQuery('#' + this.containerId).empty();
        }
    },
    get: function() {
        if ('undefined' == typeof this.form) {
            this.form = jQuery('form#' + this.id);
        }

        return this.form;
    },
    reset: function() {
        this.get()[0].reset();
    },
    submit: function() {
        gunSalesForm.messages.clearMessages();
        gunSalesForm.fields.clearErrors();

        this.get().find('input[type=file]').upload(this.backend, this.get().serialize() + '&form_id=' + this.get().attr('id'), function(data) {
            if ('object' != typeof data || 'undefined' == typeof data.status) {
                gunSalesForm.messages.addError('Invalid JSON structure');
            }
            else if (data.status != 'OK') {
                if (data.message) {
                    gunSalesForm.messages.addError(data.message);
                }

                jQuery.each(data.fields, function(field, message) {
                    gunSalesForm.fields.setError(field, message);
                });
            }
            else if (data.message) {
                gunSalesForm.reset();
                gunSalesForm.messages.addMessage(data.message);
            }
        }, 'json');
    }
};

var opticsSEForm = {
    id        : 'optics_se_form',
    backend   : formsBasePath + 'forms/forms.php',
    throbberId: 'optics_se_form_progress',
    submitId  : 'optics_se_form_submit',

    fields: {
        errorClassName: 'field_error',
        errorIdPreffix: 'optics_se_form_field_error_',

        setError: function(field, message) {
            jQuery('#' + this.errorIdPreffix + field).text(message).show();
        },
        clearErrors: function() {
            opticsSEForm.get().find('.' + this.errorClassName).text('').hide();
        }
    },
    messages: {
        containerId     : 'optics_se_form_messages',
        className       : 'optics_se_form_message',
        errorClassName  : 'optics_se_form_message_error',
        messageClassName: 'optics_se_form_message_message',
        delay           : 10,

        add: function(message, error) {
            jQuery('<div>')
            .addClass(this.className)
            .addClass(error ? this.errorClassName : this.messageClassName)
            .text(message)
            .appendTo('#' + this.containerId)
            .delay(this.delay * 1000)
            .fadeOut(1000)
            .queue(function() {
                $(this).remove();
            });
        },
        addError: function(message) {
            opticsSEForm.messages.add(message, true);
        },
        addMessage: function(message) {
            opticsSEForm.messages.add(message, false);
        },
        clearMessages: function(){
            jQuery('#' + this.containerId).empty();
        }
    },
    get: function() {
        if ('undefined' == typeof this.form) {
            this.form = jQuery('form#' + this.id);
        }

        return this.form;
    },
    reset: function() {
        this.get()[0].reset();
    },
    submit: function() {
        opticsSEForm.messages.clearMessages();
        opticsSEForm.fields.clearErrors();
        this.get().find('input[type=file]').upload(this.backend, this.get().serialize() + '&form_id=' + this.get().attr('id'), function(data) {
            if ('object' != typeof data || 'undefined' == typeof data.status) {
                opticsSEForm.messages.addError('Invalid JSON structure');
            }
            else if (data.status != 'OK') {
                if (data.message) {
                    opticsSEForm.messages.addError(data.message);
                }

                jQuery.each(data.fields, function(field, message) {
                    opticsSEForm.fields.setError(field, message);
                });
            }
            else if (data.message) {
                opticsSEForm.reset();
                opticsSEForm.messages.addMessage(data.message);
            }
        }, 'json');
    }
};

var productEnquiringForm = {
    id        : 'product_enquiring_form',
    backend   : formsBasePath + 'forms/forms.php',
    throbberId: 'product_enquiring_form_progress',
    submitId  : 'product_enquiring_form_submit',

    fields: {
        errorClassName: 'field_error',
        errorIdPreffix: 'product_enquiring_form_field_error_',

        setError: function(field, message) {
            jQuery('#' + this.errorIdPreffix + field).text(message).show();
        },
        clearErrors: function() {
            productEnquiringForm.get().find('.' + this.errorClassName).text('').hide();
        }
    },
    messages: {
        containerId     : 'product_enquiring_form_messages',
        className       : 'product_enquiring_form_message',
        errorClassName  : 'product_enquiring_form_message_error',
        messageClassName: 'product_enquiring_form_message_message',
        delay           : 10,

        add: function(message, error) {
            jQuery('<div>')
            .addClass(this.className)
            .addClass(error ? this.errorClassName : this.messageClassName)
            .text(message)
            .appendTo('#' + this.containerId)
            .delay(this.delay * 1000)
            .fadeOut(1000)
            .queue(function() {
                $(this).remove();
            });
        },
        addError: function(message) {
            productEnquiringForm.messages.add(message, true);
        },
        addMessage: function(message) {
            productEnquiringForm.messages.add(message, false);
        },
        clearMessages: function(){
            jQuery('#' + this.containerId).empty();
        }
    },
    get: function() {
        if ('undefined' == typeof this.form) {
            this.form = jQuery('form#' + this.id);
        }

        return this.form;
    },
    reset: function() {
        this.get()[0].reset();
    },
    submit: function() {
        productEnquiringForm.messages.clearMessages();
        productEnquiringForm.fields.clearErrors();
        var submit   = jQuery('#' + this.submitId).attr('disabled', 'disabled');
        var throbber = jQuery('#' + this.throbberId).css('visibility', 'visible');

        jQuery.ajax({
            url     : this.backend,
            data    : this.get().serialize() + '&form_id=' + this.get().attr('id'),
            type    : 'GET',
            cache   : false,
            dataType: 'json',

            success: function(data, textStatus, jqXHR) {
                if ('object' != typeof data || 'undefined' == typeof data.status) {
                    productEnquiringForm.messages.addError('Invalid JSON structure');
                }
                else if (data.status != 'OK') {
                    if (data.message) {
                        productEnquiringForm.messages.addError(data.message);
                    }

                    jQuery.each(data.fields, function(field, message) {
                        productEnquiringForm.fields.setError(field, message);
                    });
                }
                else if (data.message) {
                    productEnquiringForm.reset();
                    productEnquiringForm.messages.addMessage(data.message);
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                productEnquiringForm.messages.addError(textStatus);
            },
            complete: function() {
                throbber.css('visibility', 'hidden');
                submit.removeAttr('disabled');
            }
        });
    }
};

var opticsSubscribeForm = {
    id        : 'optics_subscribe_form',
    backend   : formsBasePath + 'forms/forms.php',
    throbberId: 'optics_subscribe_form_progress',
    submitId  : 'optics_subscribe_form_submit',

    fields: {
        errorClassName: 'field_error',
        errorIdPreffix: 'optics_subscribe_form_field_error_',

        setError: function(field, message) {
            jQuery('#' + this.errorIdPreffix + field).text(message).show();
        },
        clearErrors: function() {
            opticsSubscribeForm.get().find('.' + this.errorClassName).text('').hide();
        }
    },
    messages: {
        containerId     : 'optics_subscribe_form_messages',
        className       : 'optics_subscribe_form_message',
        errorClassName  : 'optics_subscribe_form_message_error',
        messageClassName: 'optics_subscribe_form_message_message',
        delay           : 10,

        add: function(message, error) {
            jQuery('<div>')
            .addClass(this.className)
            .addClass(error ? this.errorClassName : this.messageClassName)
            .text(message)
            .appendTo('#' + this.containerId)
            .delay(this.delay * 1000)
            .fadeOut(1000)
            .queue(function() {
                $(this).remove();
            });
        },
        addError: function(message) {
            opticsSubscribeForm.messages.add(message, true);
        },
        addMessage: function(message) {
            opticsSubscribeForm.messages.add(message, false);
        },
        clearMessages: function(){
            jQuery('#' + this.containerId).empty();
        }
    },
    get: function() {
        if ('undefined' == typeof this.form) {
            this.form = jQuery('form#' + this.id);
        }

        return this.form;
    },
    reset: function() {
        this.get()[0].reset();
    },
    submit: function() {
        opticsSubscribeForm.messages.clearMessages();
        opticsSubscribeForm.fields.clearErrors();
        var thisform = this;
        var submit   = jQuery('#' + this.submitId).attr('disabled', 'disabled');
        submit.data('value', submit.val()).val('Wait...');

        jQuery.ajax({
            url     : this.backend,
            data    : this.get().serialize() + '&form_id=' + this.get().attr('id'),
            type    : 'GET',
            cache   : false,
            dataType: 'json',

            success: function(data, textStatus, jqXHR) {
                if ('object' != typeof data || 'undefined' == typeof data.status) {
                    opticsSubscribeForm.messages.addError('Invalid JSON structure');
                    submit.val(submit.data('value')).removeAttr('disabled');
                }
                else if (data.status != 'OK') {
                    if (data.message) {
                        opticsSubscribeForm.messages.addError(data.message);
                    }

                    jQuery.each(data.fields, function(field, message) {
                        opticsSubscribeForm.fields.setError(field, message);
                    });
                    submit.val(submit.data('value')).removeAttr('disabled');
                }
                else if (data.message) {
                    jQuery.ajax({
                        url     : formsBasePath + 'authentication',
                        data    : thisform.get().serialize() + '&ajax=ajax',
                        type    : 'POST',
                        cache   : false,
                        dataType: 'json',

                        success: function(data, textStatus, jqXHR) {
                            if ('object' != typeof data || 'undefined' == typeof data.status) {
                                opticsSubscribeForm.messages.addError('Invalid JSON structure');
                                submit.val(submit.data('value')).removeAttr('disabled');
                            }
                            else if (data.status != 'OK') {
                                if (data.message) {
                                    opticsSubscribeForm.messages.addError(data.message);
                                }

                                jQuery.each(data.fields, function(field, message) {
                                    opticsSubscribeForm.fields.setError(field, message);
                                });
                                submit.val(submit.data('value')).removeAttr('disabled');
                            }
                            else if (data.message) {
                                opticsSubscribeForm.reset();
                                opticsSubscribeForm.messages.addMessage(data.message);
                                submit.val(submit.data('value')).removeAttr('disabled');
                            }
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            opticsSubscribeForm.messages.addError(textStatus);
                            submit.val(submit.data('value')).removeAttr('disabled');
                        }
                    });
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                opticsSubscribeForm.messages.addError(textStatus);
                submit.val(submit.data('value')).removeAttr('disabled');
            }
        });


    }
};

jQuery(document).ready(function() {
    contactForm.messages.clearMessages();
    contactForm.fields.clearErrors();
    contactForm.get().submit(function(event) {
        event.preventDefault();
        contactForm.submit();
    });
    jQuery('#gun_sales_form_submit').click(function(event) {
        gunSalesForm.submit();
    });
    jQuery('#optics_se_form_submit').click(function(event) {
        opticsSEForm.submit();
    });
    opticsSubscribeForm.get().submit(function(event) {
        event.preventDefault();
        opticsSubscribeForm.submit();
    });
    productEnquiringForm.get().submit(function(event) {
        event.preventDefault();
        productEnquiringForm.submit();
    });

    $('#contact_form input[name="appointment_date"]').datepicker({dateFormat: 'dd/mm/yy'});
    $('#contact_form input[name="appointment_time"]').timepicker({});
});
