$(document).ready(function () {
    if (typeof sessionStorage['selected'] != 'undefined' && formID > 0) {
        if ($.isEmptyObject(__FORMOBJECT) && sessionStorage['selected'] == 'form_actions') {
            sessionStorage['selected'] = 'form_field';
        }
        var obj = $("[data='" + sessionStorage['selected'] + "']")
        obj.addClass('active');
        var rel = obj.attr('rel');
        $("#add-field,#form-settings,#form-actions").addClass('hide');
        $("#" + rel).removeClass('hide');
        if (sessionStorage['selected'] == 'form_field') {
            $("#add-field").removeClass('hide');
        }
        $(".form-control a").removeClass('active');
        $("[rel='" + rel + "']").addClass('active');
    }
    /* Settings Page */
    $(".fields_elements").click(function () {
        var elementid = $(this).attr('rel');
        var label = $(this).text();
        var emails = $("#userEmailFields").val();
        if (emails != '') {
            $("#userEmailFields").val(emails + ", [-" + label + "-{" + elementid + "}]");
        } else {
            $("#userEmailFields").val(emails + " [-" + label + "-{" + elementid + "}]");
        }
        __FORM_SETTINGS['userEmailFields'] = $("#userEmailFields").val();
    });
    $(".field-list").click(function () {
        if ($(this).siblings(".select-fld_ul").is(':visible')) {
            $(this).removeClass("active");
            $(".select-fld_ul").hide();
        } else {
            $(this).siblings(".select-fld_ul").slideDown("fast");
            $(this).addClass("active");
        }
    });
    $('body').click(function (e) {
        if ($(".select-fld_links").has(e.target).length === 0) {
            $(".select-fld_ul").hide();
            $(".field-list").removeClass("active");
        }
    });
    switch ($("[name=afterFormSubmission]:checked").val()) {
    case 'NoAction':
        $(".li_afterFormSubmission_text").addClass('hide');
        $(".li_afterFormSubmission_textarea").addClass('hide');
        break;
    case 'RedirectToUrl':
        $(".li_afterFormSubmission_textarea").addClass('hide');
        $(".li_afterFormSubmission_text").removeClass('hide');
        break;
    case 'ShowText':
        $(".li_afterFormSubmission_textarea").removeClass('hide');
        $(".li_afterFormSubmission_text").addClass('hide');
        break;
    }
    $("[name=afterFormSubmission]").change(function () {
        switch (this.value) {
        case 'NoAction':
            $(".li_afterFormSubmission_text").addClass('hide');
            $(".li_afterFormSubmission_textarea").addClass('hide');
            break;
        case 'RedirectToUrl':
            $(".li_afterFormSubmission_textarea").addClass('hide');
            $(".li_afterFormSubmission_text").removeClass('hide');
            break;
        case 'ShowText':
            $(".li_afterFormSubmission_textarea").removeClass('hide');
            $(".li_afterFormSubmission_text").addClass('hide');
            break;
        }
    })
    if ($("[name='sendConfirmMailToSubmitter[]']").prop('checked')) {
        $(".li_sendConfirmMailToSubmitter").removeClass('hide');
    } else {
        $(".li_sendConfirmMailToSubmitter").addClass('hide');
    }
    $("[name='sendConfirmMailToSubmitter[]']").change(function () {
        if (this.checked) {
            $(".li_sendConfirmMailToSubmitter").removeClass('hide');
        } else {
            $(".li_sendConfirmMailToSubmitter").addClass('hide');
        }
    })
    $(document).on('click', '#disableForm', function () {
        if ($(this).val() != 'never') {
            $(".li_disableForm").removeClass('hide');
        } else {
            $(".li_disableForm").addClass('hide');
        }
    }).ready(function () {
        if ($("#disableForm").val() != 'never') {
            $(".li_disableForm").removeClass('hide');
        } else {
            $(".li_disableForm").addClass('hide');
        }
    });
	
	 $(document).on('click', '#enableAgreement', function () {
        if ($(this).val() != 'No') {
            $(".li_agreementText").removeClass('hide');
        } else {
            $(".li_agreementText").addClass('hide');
        }
    }).ready(function () {
        if ($("#enableAgreement").val() != 'No') {
            $(".li_agreementText").removeClass('hide');
        } else {
            $(".li_agreementText").addClass('hide');
        }
    });
	
	
    /* $(document).on('keyup', "#formName", function () {
        __FORM_SETTINGS['formName'] = $(this).val();
        $("#formTitle span:first").text($(this).val());
    }) */
    $(".form-tab").click(function () {
        $(".form-tab-div").addClass('hide');
        var rel = $(this).attr('rel');
        $("#" + rel).removeClass('hide');
        $(".form-control a").removeClass('active');
        $("[rel='" + rel + "']").addClass('active');
        if (typeof $(this).attr('data') != 'undefined') {
            sessionStorage['selected'] = $(this).attr('data');
        }
    });
    $(".form-control a").click(function () {
        if (!$(this).hasClass('preview') && !$(this).hasClass('add-field-init')) {
            if (typeof $(this).attr('data') != 'undefined') {
                sessionStorage['selected'] = $(this).attr('data');
            }
            $(".form-control a").removeClass('active');
            $(this).addClass('active');
        }
    });
    $(".pin").click(function () {
        if ($(".cnt-setting").hasClass('pos-element')) {
            $(".cnt-setting").removeClass("pos-element");
            $(".dynamicForm-container").removeClass("expand");
        } else {
            $(".cnt-setting").addClass("pos-element").draggable({
                    cursor: "move"
                });
            $(".dynamicForm-container").addClass("expand");
            $(".dynamicForm-container").addClass("rmv-add-fld");
        }
        toggleAddNewField();
    });
    $('.minim').click(function () {
        if (!$(".inner-field").hasClass('minimise')) {
            $(".inner-field").slideUp();
            $(".inner-field").addClass('minimise');
        } else {
            $(".inner-field").slideDown();
            $(".inner-field").removeClass('minimise');
        }
        toggleAddNewField();
    });
    $(document).on('mouseenter', '#importPredefinedOptions', function () {
        $("#importPredefinedOptions").colorbox({
                inline: true,
                innerWidth: 650,
                href: '#pre-oprions-popup',
                title: lblImportPredefinedChoices,
                innerHeight: 330
            });
    }).on('click', '#importPredefinedOptions', function () {
        $("#addPredefinedChoice").attr('elementType', $(this).attr('data'));
    });
    $(document).on('click', '#addPredefinedChoiceCancel', function () {
        $.colorbox.close();
    });
    $(document).on('mouseenter', '.pre-options-list', function () {
        $(this).sortable({
                enable: 'enable',
                cursor: 'move',
                placeholder: "option-placeholder",
                update: function (event, ui) {
                    var key = $(this).attr('rel');
                    updateOptionOrder(key);
                }
            });
    });
    $("#pre-options a").click(function () {
        $("#pre-options a").removeClass('active');
        $(this).addClass('active');
        var pre_type = $(this).attr('rel');
        $("#predefinedOptions").val((pre_options[pre_type]).join("\n"));
    });
});

function updateOptionOrder(key) {
    var formObjectKey = $(".pre-options-list").attr('rel');
    $(".pre-options-list li").each(function (index) {
        var itemKey = $(this).attr('key');
        __FORMOBJECT[formObjectKey]['data']['defaults']['items'][itemKey]['optionOrder'] = index;
    });
}

function removeEditMode() {
    $(".formElement").removeClass('edit-mode');
}

function toggleAddNewField() {
    if ($(".inner-field").hasClass('minimise')) {
        $(".dynamicForm-container").removeClass("rmv-add-fld");
    } else {
        $(".dynamicForm-container").addClass("rmv-add-fld");
    }
}

function reorderList() {
    for (key in __FORMOBJECT) {
        __FORMOBJECT[key]['order'] = $("[uniqueid='" + __FORMOBJECT[key]['uniqueID'] + "']").index();
    }
}