﻿
var d = document;
//following values identify nodes in XML doc
//they are shortened to compact the code
//but otherwise adhere to usage of jkl-parsexml.js file
var qu = "questionnaire";
var s = "section";
var ss = "sections";
var q = "question";
var qs = "questions";
var a = "answer";
var as = "answers";

function WriteQuaireRadioButtons() {
    for (sec = 0; sec <= data[qu][ss][s].length - 1; sec++) {// section loop
        // following line is a workaround:
        // if there is only one section, then it is not treated as an array and loops break, 
        // so we keep a minimum of two sections and simply don't display number two
        if (data[qu][ss][s][sec]["display"] == "true") {
            if (data[qu][ss]["showtitles"] == "true") {
                d.write("<tr><td><strong>");
                d.write(data[qu][ss][s][sec]["title"]); // write section title
                d.write("</strong><br/><br/></td></tr>");
            }
            d.write("<tr><td>\n<ol start='" + data[qu][ss][s][sec]["startnum"] + "'>"); // open (numbered) list 
            for (que = 0; que <= data[qu][ss][s][sec][qs][q].length - 1; que++) {	// question loop
                d.write("<br/><li>" + data[qu][ss][s][sec][qs][q][que]["title"] + "<br/>"); // write question
                for (ans = 0; ans <= data[qu][ss][s][sec][qs][q][que][as][a].length - 1; ans++) {	// answer loop
                    d.write("<input type='radio' value='" + ans + "' ID='rdoS" + sec + "G" + que + "A" + ans + "' NAME='rdoGrpS" + sec + "Q" + que + "'>"); //start radio button
                    if (parseInt(data[qu][ss][s][sec][qs]["startanswersat"]) >= 0) {//if >=0 then show, otherwise hide
                        d.write((ans + parseInt(data[qu][ss][s][sec][qs]["startanswersat"]))); // write answer number
                    }
                    d.write("&nbsp;" + data[qu][ss][s][sec][qs][q][que][as][a][ans]); //write answer text
                    d.write("</input><br/>"); // close radio button
                }
            }
        }
    }
}

function ValidateQuaireInput() {
    var totalNumQus = 0;
    var numChecked = 0;

    for (sec = 0; sec <= data[qu][ss][s].length - 1; sec++) {	// section loop
        // following line is a workaround:
        // if there is only one section, then it is not treated as an array and loops break, 
        // so we keep a minimum of two sections and simply don't display number two
        if (data[qu][ss][s][sec]["display"] == "true") {
            for (que = 0; que <= data[qu][ss][s][sec][qs][q].length - 1; que++) {	// question loop
                document.getElementById("unansS" + sec + "Q" + que).style.display = 'none'; //turn off warnings
                thisQueAnswered = false // reset answer counter
                for (ans = 0; ans <= data[qu][ss][s][sec][qs][q][que][as][a].length - 1; ans++) {	// answer loop
                    button = eval('document.forms["theQuaireForm"].rdoGrpS' + sec + "Q" + que)[ans]; //check this radio button
                    if (button.checked) {
                        thisQueAnswered = true //we're good this question has an answer
                        numChecked++; //add that up
                        hdnFld = eval('document.forms["sendAnswers"].s' + sec + 'q' + que)//populate hidden text for passing to querystring
                        hdnFld.value = ans;
                    }
                }
                totalNumQus++;
                if (!thisQueAnswered) {
                    document.getElementById("unansS" + sec + "Q" + que).style.display = 'block';
                }
            }
        }
    }
    if (numChecked == totalNumQus) {
        sendAnalyticsEvent("Checklist-Submission")//interacts with Omniture code
        document.forms["sendAnswers"].submit()
    }
}

function WriteQuaireResults() {
    for (sec = 0; sec <= data[qu][ss][s].length - 1; sec++) {	// section loop
        // following line is a workaround:
        // if there is only one section, then it is not treated as an array and loops break, 
        // so we keep a minimum of two sections and simply don't display number two
        if (data[qu][ss][s][sec]["display"] == "true") {
            var tally = 0;
            if (data[qu][ss]["showtitles"] == "true") {
                d.write("<tr><td><strong>" + data[qu][ss][s][sec]["title"] + "</strong><br/><br/></td></tr>");
            }
            d.write("<tr><td>\n<ol start='" + data[qu][ss][s][sec]["startnum"] + "'>");
            for (que = 0; que <= data[qu][ss][s][sec][qs][q].length - 1; que++) {	// question loop
                d.write("<li>" + data[qu][ss][s][sec][qs][q][que]["title"] + "<br/>");
                for (ans = 0; ans <= data[qu][ss][s][sec][qs][q][que][as][a].length - 1; ans++) {	// answer loop
                    if (GrabFromQueryString('s' + sec + 'q' + que) == ans) {
                        document.write('<img src="image/box_checked.gif" width="13" height="13" />');
                        tally += ans;
                    }
                    else {
                        document.write('<img src="image/box_uncheck.gif" width="13" height="13" />');
                    }
                    if (parseInt(data[qu][ss][s][sec][qs]["startanswersat"]) >= 0) {//if >=0 then show, otherwise hide
                        d.write("&nbsp;&nbsp;" + ans);
                    }
                    d.write("&nbsp;&nbsp;" + data[qu][ss][s][sec][qs][q][que][as][a][ans] + "<br/>");
                }
            }
            if (typeof (data[qu][ss][s][sec]["comment1"]) != "undefined") {
                d.write("<p>" + data[qu][ss][s][sec]["comment1"] + tally + ".</p>"); //the tally
            }
            if (typeof (data[qu][ss][s][sec]["comment2"]) != "undefined") {
                d.write("<p>" + data[qu][ss][s][sec]["comment2"] + "</p>"); //flat comment
            }
            d.write("</ol>");
        }
    }
}

function GrabFromQueryString(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) {
        return ""
    }
    else {
        return results[1];
    }
}

