﻿var site = {

    init: function () {
        site.onOrOff();
        site.formSubmitListener();
        site.addDatePickers();
    },

    formSubmitListener: function () {
        $('#ordersupplies-form').submit(function (e) {
            $('#ordersupplies-submit').hide();
            site.validateForm($(this), 1);
            return false;
        });

        $('#clientservices-form').submit(function (e) {
            $('#ordersupplies-submit').hide();
            site.validateForm($(this), 2);
            return false;
        });

        $('#bloodtest-form').submit(function (e) {
            $('#ordersupplies-submit').hide();
            site.validateForm($(this), 3);
            return false;
        });

        $('#contact-form').submit(function (e) {
            $('#ordersupplies-submit').hide();
            site.validateForm($(this), 4);
            return false;
        });
    },

    validateForm: function (e, type) {
        var success = true;
        var message = '';
        var missed = 0;

        $('.validate').each(function () {
            $(this).removeClass('alarm-input');
        });

        $('.validate').each(function () {
            if ($(this).val() == '') {
                $(this).addClass('alarm-input');
                success = false;
                missed++;
            } else {
            }
        });

        if (missed > 0) {
            message += 'Please fill in all details marked in yellow.<br />';
        }

        if ($(e).attr('id') == 'bloodtest-form') {
            if ($('#medicalnumber').val() != '') {
                $('.insure-validate').each(function () {
                    $(this).removeClass('alarm-input');
                })
            } else {

                $('.insure-validate').each(function () {
                    $(this).removeClass('alarm-input');
                })

                var insureMissed = 0;

                $('.insure-validate').each(function () {
                    if ($(this).val() == '') {
                        insureMissed++;
                    }
                });

                if (insureMissed == 5 && $('#medicalnumber').val() == '') {
                    success = false;
                    message += 'Fully Completed Insurance Info OR Medicare No. is required.<br />';
                }

                if (insureMissed > 0 && insureMissed != 5) {
                    var i = 0;
                    $('.insure-validate').each(function () {
                        if ($(this).val() == '') {
                            $(this).addClass('alarm-input');
                            success = false;
                            i++;
                        }
                    });

                    if (i > 0) {
                        message += 'Please complete your Insurance Information OR provide a Medicare No.<br />';
                    }
                }

                if (insureMissed == 0) {
                    $('.insure-validate').each(function () {
                        $(this).removeClass('alarm-input');
                    });
                }
            }
        }

        if (success === true) {
            $('#form-message').html('');
            site.submitForm(e, type);
        } else {
            $('#ordersupplies-submit').show();
            $('#form-message').addClass('alarm-message').html(message);
        }
    },

    submitForm: function (e, type) {

        switch (type) {
            case (1):
                {
                    site.submitSupplyOrder(e); break;
                }
            case (2):
                {
                    site.submitClientServiceContact(e); break;
                }
            case (3):
                {
                    site.submitBloodDrawAppt(e); break;
                }
            case (4):
                {
                    site.submitGeneralContact(e); break;
                }
        }
    },

    addDatePickers: function () {
        $('#apptdate1').datepicker();
        $('#apptdate2').datepicker();
    },

    submitSupplyOrder: function (e) {
        $('#indicator').show();
        $('#ordersupplies-submit').hide();
        $.post('/sc_ordersupplies.ashx', e.serialize(), function (d) {
            var messageJSON = $.parseJSON(d);
            if (messageJSON.success == "True") {
                $('#indicator').hide();
                $('#form-message').removeClass('alarm-message').html('Success! An order confirmation email will be delivered to you shortly.');
                site.clearForm('ordersupplies-form');
            } else {
                $('#indicator').hide();
                $('#ordersupplies-submit').show();
                $('#form-message').addClass('alarm-message').html('Failed! Please try again');
            }
        })
    },

    submitClientServiceContact: function (e) {
        $('#indicator').show();
        $('#ordersupplies-submit').hide();
        $.post('/sc_contactclientservices.ashx', e.serialize(), function (d) {
            var messageJSON = $.parseJSON(d);
            if (messageJSON.success == "True") {
                $('#indicator').hide();
                $('#form-message').removeClass('alarm-message').html('Thanks for contacting client services.');
                site.clearForm('clientservices-form');
            } else {
                $('#indicator').hide();
                $('#ordersupplies-submit').show();
                $('#form-message').addClass('alarm-message').html('Failed! Please try again');
            }
        })
    },

    submitBloodDrawAppt: function (e) {
        $('#indicator').show();
        $('#ordersupplies-submit').hide();
        $.post('/sc_contactblooddraw.ashx', e.serialize(), function (d) {
            var messageJSON = $.parseJSON(d);
            if (messageJSON.success == "True") {
                $('#indicator').hide();
                $('#form-message').removeClass('alarm-message').html('Thank you for your interest in our Concierge Phlebotomy Appointments. A representitive will be contacting you as soon as possible.');
                site.clearForm('bloodtest-form');
            } else {
                $('#indicator').hide();
                $('#ordersupplies-submit').show();
                $('#form-message').addClass('alarm-message').html('Failed! Please try again');
            }
        })
    },

    submitGeneralContact: function (e) {
        $('#indicator').show();
        $('#ordersupplies-submit').hide();
        $.post('/sc_contactgeneral.ashx', e.serialize(), function (d) {
            var messageJSON = $.parseJSON(d);
            if (messageJSON.success == "True") {
                $('#indicator').hide();
                $('#form-message').removeClass('alarm-message').html('Thank you for your message!');
                site.clearForm('contact-form');
            } else {
                $('#indicator').hide();
                $('#ordersupplies-submit').show();
                $('#form-message').addClass('alarm-message').html('Failed! Please try again');
            }
        })
    },

    clearForm: function (formId) {
        $(':input', '#' + formId)
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    },

    onOrOff: function () {
        var $page = $('#pagewrap');

        switch (true) {
            case ($($page).hasClass('about')):
                {
                    $('#nav .about').addClass('isOn'); break;
                }
            case ($($page).hasClass('physicians')):
                {
                    $('#nav .physicians').addClass('isOn'); break;
                }
            case ($($page).hasClass('patients')):
                {
                    $('#nav .patients').addClass('isOn'); break;
                }
            case ($($page).hasClass('client')):
                {
                    $('#nav .client').addClass('isOn'); break;
                }
            default:
                {

                }
        }
    }
};

$(document).ready(function () {    
    site.init();
});
