function json2nav() {
    // console.clear();

    $.fn.getTextQuestion = function(element_id) {
        var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
        var textQuestionId = 'textQuestionName_' + element_id;
        var marks = document.getElementById('Text_Question_Marks_Id_' + element_id).value;
        // var questionType = 'Text';
        var questionType = document.getElementById('question_type_' + element_id).value;


        return {
            "question": questionTitle,
            "question_Id":textQuestionId,
            "marks":marks, 
            "question_type":questionType         
        }
    }

    $.fn.getChoiceQuestion = function(element_id) {

        var questionTitle = document.getElementById('textQuestionName_' + element_id).querySelector('.ql-editor').innerHTML;
        var choiceQuestionId = 'textQuestionName_' + element_id;
        var marks = document.getElementById('Choice_Question_Marks_Id_' + element_id).value;
        // var questionType = 'Group';
        var questionType = document.getElementById('question_type_' + element_id).value;



        return {
            "question": questionTitle,
            "question_Id":choiceQuestionId,
            "marks":marks, 
            "question_type":questionType
        }
    }


    $.fn.getQuestionsData = function(element_id, type = 'question') // type for single question or group question 
    {
        // console.log('getQuestionsData - '+element_id);

        var $questionArea = (type == 'question' ? $('#question_area_' + element_id) : $('#group_question_area_' + element_id));
        // console.log('getQuestionsData');
        // console.log($questionArea);

        var questionsArr = [];
        $questionArea.children("div").each(function(index, element) {

            var questionRowID = (element.id).split("_").slice(-1)[0];
            // console.log('row question' + questionRowID);
            var question_type = document.getElementById('question_type_' + questionRowID);
            // check whether current element is single question or question group
            if (typeof question_type !== null && question_type != undefined) {
                if (document.getElementById('question_type_' + questionRowID).value == 'text') {
                    //get text type question data
                    var textQuestion = $.fn.getTextQuestion(questionRowID);
                    questionsArr.push(textQuestion);

                } else {
                    // console.log('get choice question');
                    //get choice type question data
                    var getChoiceQuestion = $.fn.getChoiceQuestion(questionRowID);
                    questionsArr.push(getChoiceQuestion)
                }
            } else // handle question group
            {
                // console.log('QUESTION GROUP');
                // console.log('row question' + questionRowID);
                var questionGroupData = $.fn.getQuestionQroupData(element);
                questionsArr.push(questionGroupData)

            }


        });

        return questionsArr
    }

    $.fn.getQuestionQroupData = function(element) {
        // console.log('getSectionData');
        // console.log(element.id);
        const rand_id = (element.id).split("_").slice(-1)[0];
        // console.log(rand_id);
        // console.log('section_title_text_'+rand_id);
        var groupQuestion = {}
        var group_title = document.getElementById('subSection_title_text_' + rand_id);
        var group_attempt_any = document.getElementById('Question_Attempt_Any_Id_' + rand_id);
        var group_marks = document.getElementById('Question_Group_Marks_Id_' + rand_id);

        groupQuestion['group_title'] = ((typeof group_title !== null && group_title !== 'undefined') ? (group_title !== null ? group_title.querySelector('.ql-editor').innerHTML : null) : null);
        groupQuestion['question_group_id'] = 'subSection_title_text_' + rand_id;
        groupQuestion['group_attempt_any'] = ((typeof group_attempt_any !== null && group_attempt_any !== 'undefined') ? (group_attempt_any !== null ? group_attempt_any.value : null) : null);
        groupQuestion['group_marks'] = ((typeof group_marks !== null && group_marks !== 'undefined') ? (group_marks !== null ? group_marks.value : null) : null);
        groupQuestion['question_type'] = 'question_group';
        if (group_title !== null) {
            var questionsData = $.fn.getQuestionsData(rand_id, 'group');
            groupQuestion['questions'] = questionsData;
            return groupQuestion;
        }

    }


    $.fn.getSectionDataNavigation = function(element) {
        // console.log('getSectionData');
        // console.log(element.id);
        const rand_id = (element.id).split("_").slice(-1)[0];
        // console.log(rand_id);
        // console.log('section_title_text_'+rand_id);
        var section = {}
        var section_title = document.getElementById('section_title_text_' + rand_id);
        var section_marks = document.getElementById('Section_Marks_Id_' + rand_id);
        var section_attempt_any = document.getElementById('Section_Attempt_Any_Id_' + rand_id);
        // console.log('section title in nav script');
        // console.log(section_title);
        // section['section_title'] = ((section_title !== null && section_title !== 'undefined') ? (section_title !== null ? section_title.querySelector('.ql-editor').innerHTML : null) : null);
        section['section_title'] = (section_title && section_title.querySelector('.ql-editor') ? section_title.querySelector('.ql-editor').innerHTML : null);
        section['section_id'] = 'section_title_text_' + rand_id;
        section['section_attempt_any'] = ((typeof section_attempt_any !== null && section_attempt_any !== 'undefined') ? (section_attempt_any !== null ? section_attempt_any.value : null) : null);
        section['section_marks'] = ((typeof section_marks !== null && section_marks !== 'undefined') ? (section_marks !== null ? section_marks.value : null) : null);

        if (section_title !== null) {
            var questionsData = $.fn.getQuestionsData(rand_id);
            // console.log('Section Data');
            // console.log(questionsData);
            section['questions'] = questionsData;
            return section;
        }

    }

    var formData = {};

    var question_paper_title = document.getElementById('Question_title').querySelector('.ql-editor').innerHTML;
    formData['question_paper_title'] = question_paper_title;
    formData['Question_title_id'] = 'Question_title';
    formData['sections'] = [];
    // console.log(formData);
    // return False;

    var $parentSectionElement = $("#addNewSection");
    $parentSectionElement.find(".section").each(function(index, element) {
        // console.log(element);
        //	if(index == 2){return false;}
        var sectionData = $.fn.getSectionDataNavigation(element);
        // console.log('sectionData:',sectionData);
        if (sectionData !== null && sectionData !== undefined) {
            formData['sections'].push(sectionData);
        }

    });

    // console.log((formData));
    // console.log(JSON.stringify(formData, null, 1));
    localStorage.setItem("JSON_FOR_NAV", JSON.stringify((formData)));
    // console.log('JSON_FOR_NAV');
    // console.log('*********************************************************************************');
    // console.log(JSON.stringify(formData));
    // console.log('*********************************************************************************');

    // console.log(OBJtoXML(({'question_paper' : formData})));
}
