// pageChanged & sizeChanged functions are needed in every model file
    // other functions for model should also be in here to avoid conflicts
    var results = new function() {
        var generalResultsTxt,
            averageTxt,
            completionTxt,
            startTimeTxt,
            durationTxt,
            interactivityResultsTxt,
            globalResultsTxt,
            nameTxt,
            scoreTxt,
            weightingTxt,
            completedTxt,
            detailsTxt,
            specificResultsTxt,
            yourAnswerTxt,
            correctAnswerTxt,
            downloadPdfTxt,
            correctTooltip,
            inCorrectTooltip,
            completeTooltip,
            inCompleteTooltip,
            NACompleteTooltip,
            detailsTooltip,
            noDetailsTooltip,
            userNameDialogTitle,
            userNamePrompt,
            okButtonTxt,
            numLoaded = 0,
            xtresults,
            normalcolumns,
            username,
            normalrows = [],
            detailstablescolumns = [],
            detailstablesrows = [];


        // function called every time the page is viewed after it has initially loaded
        this.pageChanged = function() {

            this.getLabels();
            // Always regenerate page
            $("#" + classIdentifier + " .questionScores").html("");
            $("#" + classIdentifier + " .fullResults").html("");
            this.init();

        };

        // function called every time the size of the LO is changed
        this.sizeChanged = function() {

        };

        this.loadJS = function() {
            if (numLoaded < 2) {
                var fileToLoad;
                if (numLoaded == 0) {
                    fileToLoad = "common_html5/js/jspdf.min.js";
                }
                else
                {
                    fileToLoad = "common_html5/js/jspdf.plugin.autotable.min.js";
                }

                $.getScript(x_templateLocation + fileToLoad)
                    .done(function(script, textStatus) {
                        numLoaded++;
                        results.loadJS();
                    })
                    .fail(function( jqxhr, settings, exception ) {
                        console.log("Failed to load results jspdf scripts");
                    });
            }
        };

        this.replaceArrow = function(str)
        {
            if (typeof(str) === 'string') {
                return str.replace(/-->/g, '<i class="fa fa-long-arrow-right"></i>');
            }
            else
            {
                return str;
            }
        };

        this.init = function(classIdentifier, trackingState) {

            this.getLabels(classIdentifier);


            // ignores superscript support data in xml as it will do it automatically with <sub> <sup> tags

            var showzeroweight = false;
            xtresults = XTResults(false, trackingState);
            var altnormalrow = false;
            var altfullrow = false;
            $("#" + classIdentifier + " .averageScore").html(xtresults.averageScore + "%");
            $("#" + classIdentifier + " .completion").html(xtresults.completion + "%");
            $("#" + classIdentifier + " .totalDuration").html(xtresults.totalDuration + "s");
            $("#" + classIdentifier + " .startTime").html(moment(new Date(xtresults.start)).format('YYYY-MM-DD HH:mm:ss'));
            var detailstables=0;
            var firstnormalrow=true;
            var detailstable = $("<table class='details'>");
            var scoretxt;


            xtresults.interactions.forEach(function(x)
            {
                if (x.type == 'result' || (!showzeroweight && x.completed == "true" && x.weighting == 0))
                {
                    // Skip
                    return;
                }
                var interactiveToken, interactiveTokenTxt;
                if(x.type != 'page')
                {
                    interactiveToken = '<i class="fa fa-x-circle blue" title="' + detailsTooltip + '"><span class="ui-helper-hidden-accessible">' + detailsTooltip + '</span></i>';
                    interactiveTokenTxt = "details";
                }
                else
                {
                    interactiveToken = '<i class="fa fa-x-circle-o blue" title="' + noDetailsTooltip + '"><span class="ui-helper-hidden-accessible">' + noDetailsTooltip + '</span></i>';
                    interactiveTokenTxt = "nodetails";
                }
                var completedToken, completedTokenTxt;
                if(x.completed == "true")
                {
                    completedToken = '<i class="fa fa-x-tick" title="' + completeTooltip + '"><span class="ui-helper-hidden-accessible">' + completeTooltip + '</span></i>';
                    completedTokenTxt = "completed";
                }
                else if (x.completed == "false")
                {
                    completedToken = '<i class="fa fa-x-cross" title="' + inCompleteTooltip + '"><span class="ui-helper-hidden-accessible">' + inCompleteTooltip + '</span></i>';
                    completedTokenTxt = "notcompleted";
                }
                else {
                    completedToken = '<i class="fa fa-minus" title="' + NACompleteTooltip + '"><span class="ui-helper-hidden-accessible">' + NACompleteTooltip + '</span></i>';
                    completedTokenTxt = '-';
                }
                if (x.type == 'page')
                {
                    scoretxt = '-';
                }
                else
                {
                    scoretxt = x.score + '%';
                }
                if(xtresults.mode == 'normal-results'){
                    $("#" + classIdentifier + " .specific").show();
                    $("#" + classIdentifier + " .specificResultsTxt").hide();
                    if (firstnormalrow)
                    {
                        $("#" + classIdentifier + " .questionScores").append("<th>" + nameTxt + "</th><th>" + scoreTxt + "</th><th>" + durationTxt + "</th><th>" + weightingTxt + "</th><th>" + completedTxt + "</th>");
                        normalcolumns = [x_GetTrackingTextFromHTML(nameTxt, ""), scoreTxt, durationTxt, weightingTxt, completedTxt];
                        firstnormalrow = false;
                    }
                    $("#" + classIdentifier + " .questionScores").append("<tr " + (altnormalrow ? "class='alt'" : "") + "><td>" + x.title + "</td><td class='td-center'>" + scoretxt + "</td><td class='td-center'>" + x.duration + "s</td><td class='td-center'>" + x.weighting + "</td><td class='td-center'>" + completedToken + "</td></tr>");
                    var normalrow = [x_GetTrackingTextFromHTML(x.title, ""), scoretxt, x.duration, x.weighting, completedTokenTxt];
                    normalrows.push(normalrow);
                    altnormalrow = !altnormalrow;
                }
                else if(xtresults.mode == 'full-results' ) {
                    $("#" + classIdentifier + " .specific").show();
                    $("#" + classIdentifier + " .specificResultsTxt").show();
                    if (firstnormalrow)
                    {
                        normalcolumns = [x_GetTrackingTextFromHTML(nameTxt, ""), scoreTxt, durationTxt, weightingTxt, completedTxt, detailsTxt];
                        $("#" + classIdentifier + " .questionScores").append("<th>" + nameTxt + "</th><th>" + scoreTxt + "</th><th>" + durationTxt + "</th><th>" + weightingTxt + "</th><th>" + completedTxt + "</th><th>" + detailsTxt + "</th>");
                        firstnormalrow = false;
                    }
                    $("#" + classIdentifier + " .questionScores").append("<tr " + (altnormalrow ? "class='alt'" : "") + "><td>" + x.title + "</td><td class='td-center'>" + scoretxt + "</td><td class='td-center'>" + x.duration + "s</td><td class='td-center'>" + x.weighting + "</td><td class='td-center'>" + completedToken + "</td><td class='td-center'>" + interactiveToken + "</td></tr>");
                    var normalrow = [x_GetTrackingTextFromHTML(x.title, ""), scoretxt, x.duration, x.weighting, completedTokenTxt, interactiveTokenTxt];
                    normalrows.push(normalrow);
                    altnormalrow = !altnormalrow;
                    altfullrow = false;
                    if (x.subinteractions.length > 0 && x.type != 'page') {
                        detailstable.append("<tr><th class='correct'></th><th class='question'>" + x.title + "</th><th class='answer'>" + yourAnswerTxt + "</th><th class='correctanswer'>"+ correctAnswerTxt +"</th></tr>");
                        var detailstablecolumns = ["", x_GetTrackingTextFromHTML(x.title, ""), yourAnswerTxt, correctAnswerTxt];
                        var detailstablerows = [];
                        altfullrow = false;
                        x.subinteractions.forEach(function (y) {
                            var question = y.question;
                            var learnerAnswer = y.learnerAnswer;
                            var correctAnswer = y.correctAnswer;
                            if (learnerAnswer == correctAnswer || y.correct) {
                                detailstable.append("<tr " + (altfullrow ? "class='alt'" : "") + "><td class='correct'>" + "<i class='fa fa-x-tick' title='" + correctTooltip + "'><span class='ui-helper-hidden-accessible'>" + correctTooltip + "</span></td><td class='question'>" + question + "</td><td class='answer'>" + results.replaceArrow(learnerAnswer) + "</td><td class='correctanswer'>" + results.replaceArrow(correctAnswer) + "</td></tr>");
                                var detailstablerow = ["correct",  x_GetTrackingTextFromHTML(question, ""), x_GetTrackingTextFromHTML(learnerAnswer, ""), x_GetTrackingTextFromHTML(correctAnswer, "")];
                                detailstablerows.push(detailstablerow);
                            }
                            else {
                                detailstable.append("<tr " + (altfullrow ? "class='alt'" : "") + "><td class='correct'>" + "<i class='fa fa-x-cross' title='" + inCorrectTooltip + "'><span class='ui-helper-hidden-accessible'>" + inCorrectTooltip + "</span></td><td class='question'>" + question + "</td><td class='answer'>" + results.replaceArrow(learnerAnswer) + "</td><td class='correctanswer'>" + results.replaceArrow(correctAnswer) + "</td></tr>");
                                var detailstablerow = ["incorrect", x_GetTrackingTextFromHTML(question, ""), x_GetTrackingTextFromHTML(learnerAnswer, ""), x_GetTrackingTextFromHTML(correctAnswer, "")];
                                detailstablerows.push(detailstablerow);
                            }
                            altfullrow = !altfullrow;
                        });
                        $("#" + classIdentifier + " .fullResults").append(detailstable);
                        detailstable = $("<table class='details'>");
                        detailstablescolumns.push(detailstablecolumns);
                        detailstablesrows.push(detailstablerows);
                    }
                }
            });
            // call this function in every model once everything's loaded
        };

        this.getLabels = function(classIdentifier){

            generalResultsTxt = "General Results";
            averageTxt =  "Average: ";
            completionTxt =  "Completion: ";
            durationTxt = "Duration: ";
            startTimeTxt = "Start Time";
            interactivityResultsTxt = "Interactivity Results";
            globalResultsTxt = "Global Results";
            nameTxt = "Name";
            scoreTxt = "Score";
            weightingTxt = "Weighting";
            completedTxt = "Completed";
            detailsTxt = "Details";
            specificResultsTxt = "Specific Results";
            yourAnswerTxt = "Your Answer";
            correctAnswerTxt = "Correct Answer";
            downloadPdfTxt = "Download PDF File";
            correctTooltip = "Correct";
            inCorrectTooltip = "Incorrect";
            completeTooltip = "Complete";
            inCompleteTooltip = "Incomplete";
            NACompleteTooltip = "Not applicable";
            detailsTooltip =  "Details available";
            noDetailsTooltip = "No details available";
            userNameDialogTitle = "Enter user name";
            userNamePrompt = "User name:";
            okButtonTxt = "OK";
            $("#" + classIdentifier + " .dialog-form").attr("title", userNameDialogTitle);
            $("#" + classIdentifier + " .namelabel").html(userNamePrompt);
            $("#" + classIdentifier + " .generalResultsTxt").html(generalResultsTxt);
            $("#" + classIdentifier + " .averageTxt").html(averageTxt);
            $("#" + classIdentifier + " .durationTxt").html(durationTxt);
            $("#" + classIdentifier + " .durationTxt1").html(durationTxt);
            $("#" + classIdentifier + " .startTimeTxt").html(startTimeTxt);
            $("#" + classIdentifier + " .completionTxt").html(completionTxt);
            $("#" + classIdentifier + " .interactivityResultsTxt").html(interactivityResultsTxt);
            $("#" + classIdentifier + " .globalResultsTxt").html(globalResultsTxt);
            $("#" + classIdentifier + " .nameTxt").html(nameTxt);
            $("#" + classIdentifier + " .scoreTxt").html(scoreTxt);
            $("#" + classIdentifier + " .weightingTxt").html(weightingTxt);
            $("#" + classIdentifier + " .completedTxt").html(completedTxt);
            $("#" + classIdentifier + " .detailsTxt").html(detailsTxt);
            $("#" + classIdentifier + " .specificResultsTxt").html(specificResultsTxt);
            $("#" + classIdentifier + " .button").html(downloadPdfTxt);
        }

        this.pdfDownload = function(skip) {

            if (skip!==true && x_currentPageXML.getAttribute("givename"))
            {
                $("#" + classIdentifier + " .dialog-form").dialog( "open" );
                return;
            }
            var pdf = new jsPDF('p', 'mm', 'a4');

            var checkmark = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAABMlBMVEVixWJjxWNkxmRmxmZnx2dox2hpyGlryGtsyWxwynBxy3Fzy3N0zHR1zHV2zXZ4zXh5zXl5znl7znt8z3x9z31+z36A0ICB0YGD0YOE0oSH04eJ04mL1IuN1Y2Q1pCR15GT15OW2JaZ2Zmc25ye256h3KGj3aOm3qan3qeo36ip36mr4Kus4ayt4a2u4a6v4a+w4rCx4rGz47O147W15LW25La35Le45bi55bm65rq75ru85ry95r29572+576/57/A6MDB6MHD6cPF6sXG6sbI68jL7MvO7c7T79PZ8dnd8t3e897f89/g8+Di9OLj9ePk9eTm9ubo9+jp9+nq9+rw+fDw+vDx+vHy+vL0+/T1+/X2/Pb3/Pf4/Pj5/fn6/fr7/fv7/vv8/vz9/v3+//7///9s6wO5AAAGAUlEQVR42u3de3cTZRDH8R8VUSr1hoJRQax4ARQxCvWC4AXEOgqCCKhNmjSZ9/8W/KPQ5rK72cszu888M/sCsvv5ntOek83uDNj4AQ/gATyAB/AAHsADeAAP4AE8gAfwAB7AA3gAD+ABPIAH8AAewAN4AA/gATyAB/AAeo/J7u6e2QCDO5vPA8DamZ+fGAzweBMzx9v3jQWY9rFwbI4tBRiextJx/JGdADsnkHE899BKgJ11ZB5rf9kIkOcvLAAL/qIC6QTYeQmoUSCZAINCf34BGPHnFoAVf14BmPHnFIAdf3YBGPJnFoAlf1YB/QEGJ4AGBWDLv1wAxvxLBWDNv1gA5vwLBWDPP18ABv1zBWDRP1tAb4DBBhCgAGz6DwvAqB9Ye6g5QHM/cPSx3gDDAH5gfaw1QBg/cF5pgFB+4IHKAMOXQ/nxrsYAAf3Av/oCBPXjF3UBwvrxvrYAgf14UVmA0H5gCtt+7MG2HxNFAYavhPdr+hMQ8W/o+Sco4sfHagLI+LGtJYCQH2MlAaT8F5V8GRq+KuM/8p+OAFJ+/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+mrRCsnN5Pb2iibOc9fzCycrCioh/euLqRki1/tHPAysKKi7q3P7CCYp22YWtXysrD6oi6dz1/83IK0b72wQ8PDFglWE3RyUNfv/P4betWtDJb49JLbnzg5e37e4asKpwn6VT/iePHfjpytLvn55MDQ5Hz5P8DhvmwHOoqHAAAAABJRU5ErkJggg==";
            var nodetails = "data:image/png;base64, ";
            var nacomplete = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAAAAAB5Gfe6AAACXUlEQVR42u3cP2+OURgG8DsdGhExGIhJOjQx1sQuzAYdJGwk/QQ2m8FGIsG7SDB4J4PBYrF1YBCDhZCWN/4kjaoORZ2HvmharQ/QXr/rG1y/nJznGc59VxeeAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwBYE+PL8wZ0b17Zvrt+89/hD+x/A3NW9lZCRc882A5g/UzkZf7IB4G5lZfLrOoAfpyst+z6uAVg+WnnZMbsK0E5UYnbN/QW4VJkZX/4N8KJSc34I0A7GAtTbFYDp3P41uQJwJBig5rv6nNy/+l09igY43tWFaIBqdSwbYLH2ZwO8r9FsgJkKzysAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2zyva082wKAmsgE+1VQ2wPfqR/cf68Ifyl3sqkXfgi+76nrB/Q+0XwBLI7kAD4dDU7dj+x9qQ4B2OBXg3Z+5wYXdmf3vr47ODiLfzF9ZMzw9CDwDvXXj8wtp98Do9D/7A9qtqK/hqcWNKzSWejH/hGffbL5Epc30pya2t8LOsZOXn36zRgcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABbLj8BUGokzEYTmjMAAAAASUVORK5CYII=";

            var source = $('#" + classIdentifier + " .pdfContent').clone();

            //source.find("#" + classIdentifier + " .general_summary").prepend("<tr><th>ABC</th><th>DEF</th></tr>");
            var margins = {
                top: 20,
                bottom: 20,
                left: 20,
                width: 170
            };
            /*
            pdf.fromHTML(
                source.html(), // HTML string or DOM elem ref.
                margins.left, // x coord
                margins.top, { // y coord
                    'width': margins.width// max width of content on PDF
                },

                function (dispose) {
                    name = x_params.name + "_" + XTResults().start;
                    pdf.save(name.replace(/ /g, "_"));

                }, margins
            );
            */
            var vertpos = 20;
            pdf.setFontSize(14);

            if (x_currentPageXML.getAttribute("givename"))
            {
                pdf.text(username, 10, vertpos);
                vertpos += 5;
            }
            pdf.text(generalResultsTxt, 10, vertpos);
            vertpos += 0.2;
            pdf.lines([[190,0]], 10, vertpos);
            vertpos += 3;
            //pdf.setFontSize(11);
            var general_table = $("#" + classIdentifier + " .general_summary");
            if (general_table != null) {
                var elem = document.getElementById("general_summary");
                var res = pdf.autoTableHtmlToJson(elem);
                pdf.autoTable(res.columns, res.data, {
                    showHeader: false,
                    startY: vertpos,
                    margin: {horizontal: 10},
                    bodyStyles: {valign: 'top'},
                    styles: {overflow: 'linebreak', columnWidth: 'wrap'}
                });
                vertpos = pdf.autoTable.previous.finalY + 10;

            }
            if (xtresults.mode == 'normal-results')
            {
                pdf.text(interactivityResultsTxt, 10, vertpos);
                vertpos += 5;
                pdf.setFontSize(12);
                pdf.text(globalResultsTxt, 10, vertpos);
                vertpos += 5;
                var images = [];
                var imgIndex = 0;
                pdf.autoTable(normalcolumns, normalrows, {
                    startY: vertpos,
                    margin: {horizontal: 10},
                    bodyStyles: {valign: 'top'},
                    styles: {overflow: 'linebreak', columnWidth: 'wrap'},
                    headerStyles: { fillColor: [50, 50, 50] },
                    columnStyles:{
                        0: {halign: 'left', columnWidth : 100},
                        1: {halign: 'right', columnWidth : 22.5},
                        2: {halign: 'right', columnWidth : 22.5},
                        3: {halign: 'right', columnWidth : 22.5},
                        4: {halign: 'right', columnWidth : 22.5}
                    },
                    drawCell: function(cell, opts) {
                        if (opts.column.dataKey === 4) {
                            var img;
                            if (cell.raw === "completed")
                            {
                                img = checkmark;
                            }
                            else if (cell.raw == "notcompleted")
                            {
                                img = cross;
                            }
                            else
                            {
                                img = nacomplete;
                            }
                            cell.text = '';
                            images.push({
                                url: img,
                                x: cell.textPos.x - 11,
                                y: cell.textPos.y,
                                used: false
                            });
                            imgIndex++;
                        }
                    },
                    addPageContent: function() {
                        for (var i = 0; i < images.length; i++) {
                            if (!images[i].used) {
                                pdf.addImage(images[i].url, 'png', images[i].x, images[i].y + 0.4, 2.4, 2.4);
                                images[i].used = true;
                            }
                        }
                    }



                });
                vertpos = pdf.autoTable.previous.finalY + 5;
            }

            if (xtresults.mode == 'full-results')
            {
                var i;
                pdf.text(interactivityResultsTxt, 10, vertpos);
                vertpos += 7;
                pdf.setFontSize(12);
                pdf.text(globalResultsTxt, 10, vertpos);
                vertpos += 7;
                var images = [];
                var imgIndex = 0;
                pdf.autoTable(normalcolumns, normalrows, {
                    startY: vertpos,
                    margin: {horizontal: 10},
                    bodyStyles: {valign: 'top'},
                    styles: {overflow: 'linebreak', columnWidth: 'wrap'},
                    headerStyles: { fillColor: [50, 50, 50] },
                    columnStyles:{
                        0: {halign: 'left', columnWidth : 77.5},
                        1: {halign: 'right', columnWidth : 22.5},
                        2: {halign: 'right', columnWidth : 22.5},
                        3: {halign: 'right', columnWidth : 22.5},
                        4: {halign: 'right', columnWidth : 22.5},
                        5: {halign: 'right', columnWidth : 22.5}
                    },
                    drawCell: function(cell, opts) {
                        if (opts.column.dataKey === 4) {
                            var img;
                            if (cell.raw === "completed")
                            {
                                img = checkmark;
                            }
                            else if (cell.raw == "notcompleted")
                            {
                                img = cross;
                            }
                            else
                            {
                                img = nacomplete;
                            }
                            cell.text = '';
                            images.push({
                                url: img,
                                x: cell.textPos.x-11,
                                y: cell.textPos.y,
                                used: false
                            });
                            imgIndex++;
                        }
                        if (opts.column.dataKey === 5) {
                            var img;
                            if (cell.raw === "details")
                            {
                                img = details;
                            }
                            else
                            {
                                img = nodetails;
                            }
                            cell.text = '';
                            images.push({
                                url: img,
                                x: cell.textPos.x - 13.5,
                                y: cell.textPos.y,
                                used: false
                            });
                            imgIndex++;
                        }
                    },
                    addPageContent: function() {
                        for (var i = 0; i < images.length; i++) {
                            if (!images[i].used) {
                                pdf.addImage(images[i].url, 'png', images[i].x, images[i].y + 0.4, 2.4, 2.4);
                                images[i].used = true;
                            }
                        }
                    }

                });
                vertpos = pdf.autoTable.previous.finalY + 10;
                pdf.text(specificResultsTxt, 10, vertpos);
                vertpos+=7;
                for (i=0; i<detailstablescolumns.length; i++)
                {
                    var images = [];
                    var imgIndex = 0;
                    pdf.autoTable(detailstablescolumns[i], detailstablesrows[i], {
                        startY: vertpos,
                        margin: {horizontal: 10},
                        bodyStyles: {valign: 'top'},
                        styles: {overflow: 'linebreak', columnWidth: 'wrap'},
                        headerStyles: { fillColor: [128, 128, 128] },
                        columnStyles: {
                            0: {columnWidth: 10},
                            1: {columnWidth: 60},
                            2: {columnWidth: 60},
                            3: {columnWidth: 60}
                        },
                        drawCell: function(cell, opts) {
                            if (opts.column.dataKey === 0) {
                                var img;
                                if (cell.raw === "correct")
                                {
                                    img = checkmark;
                                }
                                else
                                {
                                    img = cross;
                                }
                                cell.text = '';
                                images.push({
                                    url: img,
                                    x: 12,
                                    y: cell.textPos.y,
                                    used: false
                                });
                                imgIndex++;
                            }
                        },
                        addPageContent: function() {
                            for (var i = 0; i < images.length; i++) {
                                if (!images[i].used) {
                                    pdf.addImage(images[i].url, 'png', images[i].x, images[i].y + 0.4, 2.4, 2.4);
                                    images[i].used = true;
                                }
                            }
                        }

                    });
                    vertpos = pdf.autoTable.previous.finalY;
                }
            }
            var name = x_params.name + "_" + XTResults().start + ".pdf";
            pdf.save(name.replace(/ /g, "_"));
        }
    };



function XTResults(fullcompletion, trackingState) {
        var completion = 0;
        var nrcompleted = 0;
        var nrvisited = 0;
        var completed;
        $.each(trackingState.completedPages, function (i, completed) {
            // indices not defined will be visited anyway.
            // In that case 'completed' will be undefined
            if (completed) {
                nrcompleted++;
            }
            if (typeof(completed) != "undefined") {
                nrvisited++;
            }
        })

        if (nrcompleted != 0) {
            if (!fullcompletion) {
                completion = Math.round((nrcompleted / nrvisited) * 100);
            }
            else {
                completion = Math.round((nrcompleted / trackingState.toCompletePages.length) * 100);
            }
        }
        else {
            completion = 0;
        }

        var results = {};
        results.mode = "full-results";

        var score = 0,
            nrofquestions = 0,
            totalWeight = 0,
            totalDuration = 0;
        results.interactions = Array();

        for (i = 0; i < trackingState.interactions.length - 1; i++) {


            score += trackingState.interactions[i].score * trackingState.interactions[i].weighting;
            if (trackingState.interactions[i].ia_nr < 0 || trackingState.interactions[i].nrinteractions > 0) {

                var interaction = {};
                interaction.score = Math.round(trackingState.interactions[i].score);
                interaction.title = trackingState.interactions[i].ia_name;
                interaction.type = trackingState.interactions[i].ia_type;
                interaction.correct = trackingState.interactions[i].result;
                interaction.duration = Math.round(trackingState.interactions[i].duration / 1000);
                interaction.weighting = trackingState.interactions[i].weighting;
                interaction.subinteractions = Array();

                var j = 0;
                for (j; j < trackingState.toCompletePages.length; j++) {
                    var currentPageNr = trackingState.toCompletePages[j];
                    if (currentPageNr == trackingState.interactions[i].page_nr) {
                        if (trackingState.completedPages[j]) {
                            interaction.completed = "true";
                        }
                        else if (!trackingState.completedPages[j]) {
                            interaction.completed = "false";
                        }
                        else {
                            interaction.completed = "unknown";
                        }
                    }
                }

                results.interactions[nrofquestions] = interaction;
                totalDuration += trackingState.interactions[i].duration;
                nrofquestions++;
                totalWeight += trackingState.interactions[i].weighting;

            }
            else if (results.mode == "full-results") {
                var subinteraction = {};

                var learnerAnswer, correctAnswer;
                switch (trackingState.interactions[i].ia_type) {
                    case "match":
                        var resultCorrect=false;
                        for (var c = 0; c < trackingState.interactions[i].correctOptions.length; c++) {
                            var matchSub = {}; //Create a subinteraction here for every match sub instead
                            correctAnswer = trackingState.interactions[i].correctOptions[c].source + ' --> ' + trackingState.interactions[i].correctOptions[c].target;
                            source = trackingState.interactions[i].correctOptions[c].source;
                            if (trackingState.interactions[i].learnerOptions.length == 0) {
                                learnerAnswer = source + ' --> ' + ' ';
                            }
                            else {
                                for (var d = 0; d < trackingState.interactions[i].learnerOptions.length; d++) {
                                    if (source == trackingState.interactions[i].learnerOptions[d].source) {
                                        learnerAnswer = source + ' --> ' + trackingState.interactions[i].learnerOptions[d].target;
                                        break;
                                    }
                                    else {
                                        learnerAnswer = source + ' --> ' + ' ';
                                    }
                                }
                            }

                            matchSub.question = trackingState.interactions[i].ia_name;
                            matchSub.correct = resultCorrect;
                            matchSub.learnerAnswer = learnerAnswer;
                            matchSub.correctAnswer = correctAnswer;
                            results.interactions[nrofquestions - 1].subinteractions.push(matchSub);
                        }

                        break;
                    case "text":
                        learnerAnswer = trackingState.interactions[i].learnerAnswers;
                        correctAnswer = trackingState.interactions[i].correctAnswers;
                        break;
                    case "multiplechoice":
                        learnerAnswer = trackingState.interactions[i].learnerAnswers[0] != undefined ? trackingState.interactions[i].learnerAnswers[0] : "";
                        for (var j = 1; j < trackingState.interactions[i].learnerAnswers.length; j++) {
                            learnerAnswer += "\n" + trackingState.interactions[i].learnerAnswers[j];
                        }
                        correctAnswer = "";
                        for (var j = 0; j < trackingState.interactions[i].correctAnswers.length; j++) {
                            if (trackingState.interactions[i].correctAnswers[j] != undefined) {
                                if (correctAnswer.length > 0)
                                    correctAnswer += "\n";
                                correctAnswer += trackingState.interactions[i].correctAnswers[j];
                            }
                        }
                        break;
                    case "numeric":

                        learnerAnswer = trackingState.interactions[i].learnerAnswers;
                        correctAnswer = "-";  // Not applicable
                        //TODO: We don't have a good example of an interactivity where the numeric type has a correctAnswer. Currently implemented for the survey page.
                        break;
                    case "fill-in":
                        learnerAnswer = trackingState.interactions[i].learnerAnswers;
                        correctAnswer = trackingState.interactions[i].correctAnswers;
                        break;
                }
                if (trackingState.interactions[i].ia_type != "match") {
                    subinteraction.question = trackingState.interactions[i].ia_name;
                    subinteraction.correct = trackingState.interactions[i].result.success;
                    subinteraction.learnerAnswer = learnerAnswer;
                    subinteraction.correctAnswer = correctAnswer;
                    results.interactions[nrofquestions - 1].subinteractions.push(subinteraction);
                }
            }
        }
        results.completion = completion;
        results.score = score;
        results.nrofquestions = nrofquestions;
        results.averageScore = getScaledScore(trackingState) * 100;
        results.totalDuration = Math.round(totalDuration / 1000);
        results.start = trackingState.start.toLocaleString();

        //$.ajax({
        //    type: "POST",
        //    url: window.location.href,
        //    data: {
        //        grade: results.averageScore / 100
        //    }
        //});
        return results;
    }

function getdScaledScore(x)
{
    return getdRawScore(x) / (getdMaxScore(x) - getdMinScore(x));
}

function getScaledScore(x)
{
    return Math.round(getdScaledScore(x)*100)/100 + "";
}

function getdRawScore(x)
    {
        if (x.lo_type == "pages only")
        {
            if (getSuccessStatus(x) == 'completed')
                return 100;
            else
                return 0;
        }
        else
        {
            var score = [];
            var weight = [];
            var totalweight = 0.0;
            // Walk passed the pages
            var i=0;
            for (i=0; i<x.nrpages; i++)
            {
                var sit = findPage(i, x);
                if (sit != null && sit.weighting > 0)
                {
                    totalweight += sit.weighting;
                    score.push(sit.score);
                    weight.push(sit.weighting);
                }
            }
            var totalscore = 0.0;
            if (totalweight > 0.0)
            {
                for (i=0; i<score.length; i++)
                {
                    totalscore += (score[i] * weight[i]);
                }
                totalscore = totalscore / totalweight;
            }
            else
            {
                // If the weight is 0.0, set the score to 100
                totalscore = 100.0;
            }
            return Math.round(totalscore*100)/100;
        }
    }

function getRawScore()
{
    return getdRawScore(x) + "";
}

function getdMinScore(x)
{
    if (x.lo_type == "pages only")
    {
        return 0.0;
    }
    else
    {
        return 0.0;
    }
}

function getMinScore(x)
{
    return x.getdMinScore() + "";
}

function getdMaxScore(x)
{
    if (x.lo_type == "pages only")
    {
        return 100.0;
    }
    else
    {
        return 100.0;
    }
}

function getMaxScore(x)
{
    return x.getdMaxScore() + "";
}

function findPage(page_nr, x)
{
    var id = makeId(page_nr, -1, 'page', "");
    var i=0;
    for (i=0; i<x.interactions.length; i++)
    {
        if (x.interactions[i].id.indexOf(id) == 0 && x.interactions[i].id.indexOf(id + ':interaction') < 0)
            return x.interactions[i];
    }
    return null;
}

function makeId(page_nr, ia_nr, ia_type, ia_name)
{
    var tmpid = 'urn:x-xerte:p-' + (page_nr + 1);
    if (ia_nr >= 0)
    {
        tmpid += ':' + (ia_nr + 1);
        if (ia_type.length > 0)
        {
            tmpid += '-' + ia_type;
        }
    }

    if (ia_name)
    {
        // ia_nam can be HTML, just extract text from it
        var div = $("<div>").html(ia_name);
        var strippedName = div.text();
        tmpid += ':' + encodeURIComponent(strippedName.replace(/^[a-zA-Z0-9_ ]/g, "").replace(/ /g, "_"));
        // Truncate to max 255 chars, this should be 4000
        tmpid = tmpid.substr(0,255);
    }
    return tmpid;
}

function x_GetTrackingTextFromHTML(html, fallback)
{
    var div = $('<div>').html(html);
    var txt = $.trim(div.text());
    if (txt == "")
    {
        var img = div.find("img");
        if (img != undefined && img.length > 0)
        {
            txt = img[0].attributes['alt'].value;
        }
    }
    if (txt == "")
    {
        txt = fallback;
    }
    return txt;
}

function getSuccessStatus(x)
    {
        if (x.lo_type != "pages only")
        {
            if (getScaledScore(x) > x.lo_passed)
            {
                return "passed";
            }
            else
            {
                return "failed";
            }
        }
        else
        {
            if (getCompletionStatus(x) == 'completed')
            {
                return "passed";
            }
            else
            {
                return "unknown";
            }
        }
    }

function getCompletionStatus(state)
    {
        var completed = true;
        for(var i = 0; i<state.completedPages.length; i++)
        {
            if(state.completedPages[i] == false)
            {
                completed = false;
                break;
            }
            //if( i == state.completedPages.length-1 && state.completedPages[i] == true)
            //{
            //completed = true;
            //
        }

        if (completed)
        {
            return "completed";

        }
        else if(!completed)
        {
            return 'incomplete';
        }
        else
        {
            return "unknown"
        }
    }
