/**
 * ValidateQuestionnaire
 * @return true/false result of the questionnaire validation
 *
 * Validate that all questions have been answered and return the validation status
 * result to the caller.
 */
function ValidateQuestionnaire() {
    numQ = 15;

    form = document.getElementById("investQForm");

    for (i=0; i<form.elements.length; i++) {
        elem = form.elements[i];
        if (elem.name.substring(0, 3)=="q1_" && elem.checked == true)
            --numQ;
        if (elem.name.substring(0, 3)=="q2_" && elem.checked == true)
            --numQ;
        if (elem.name.substring(0, 3)=="q3_" && elem.checked == true)
            --numQ;
    }

    return (numQ == 0);
}

/**
 * ComputeTotal
 * @return int Adjusted total for values in the form.
 *
 * Compute the adjusted total for all of the values on the form. The given rules
 * indicate that the total value of answers for section I are x3, the total value
 * of answers for section II are x2, and the total value of answers for section
 * III are x1.
 */
function ComputeTotal() {
    q1_t = 0;
    q2_t = 0;
    q3_t = 0;

    form = document.getElementById("investQForm");

    for (i=0; i<form.elements.length; i++) {
        elem = form.elements[i];

        if (elem.getAttribute('type')=='radio' && elem.checked == true) {
            qname = elem.name;
            qtext = elem.nextSibling.nodeValue;
            form[qname + 'a'].value = qtext;
        }

        if (elem.name.substring(0, 3)=="q1_" && elem.checked == true)
            q1_t += parseInt(elem.value);
        if (elem.name.substring(0, 3)=="q2_" && elem.checked == true)
            q2_t += parseInt(elem.value);
        if (elem.name.substring(0, 3)=="q3_" && elem.checked == true)
            q3_t += parseInt(elem.value);
    }

    adj_t = (q1_t * 3) + (q2_t * 2) + q3_t;

    obj = form.obj_1;
    if (adj_t >= 39 && adj_t <= 72) {
        obj.value = "Income with Capital Preservation";
        obj = obj.nextSibling;
    }
    if (adj_t >= 60 && adj_t <= 95) {
        obj.value = "Income with Moderate Growth";
        obj = obj.nextSibling;
    }
    if (adj_t >= 85 && adj_t <= 115) {
        obj.value = "Growth with Income";
        obj = obj.nextSibling;
    }
    if (adj_t >= 90 && adj_t <= 120) {
        obj.value = "Growth";
        obj = obj.nextSibling;
    }
    if (adj_t >= 105 && adj_t <= 125) {
        obj.value = "Agressive Growth";
        obj = obj.nextSibling;
    }

    return adj_t;
}

/**
 * CheckSubmit
 * Validate the form and, if invalid, display an alert window. Otherwise sumit the form.
 */
function CheckSubmit() {
    formOK = false;

    if (ValidateQuestionnaire()) {
        form = document.getElementById("investQForm");
        form.adjtotal.value = ComputeTotal();
        formOK = true;
    }
    else
        alert("All questions must be answered before the survey\ncan be processed. Thank you.");

    return formOK;
}
