﻿var txtIdTest = 0;
var btnIdTest = 0;

function filterText(tbValue) {
    // Remove commas, pipes and underscores from the text since they are being used as array delimiters 
    tbValue.value = tbValue.value.replace(/,/g, "");
    tbValue.value = tbValue.value.replace(/_/g, "");
    tbValue.value = tbValue.value.replace(/\174/g, "");
}

function processTestInfo(testId, testTypeId, testScoreId, testDateId) {
    var testscore = "";
    var tableContents = document.getElementById("TestScoreTable");
    if (tableContents != null) {
        var testRows = tableContents.getElementsByTagName("tr");
        var testInfoHidden;
        var testRowsLength = testRows.length;

        for (var i = 0; i < testRowsLength; i = i + 1) {
            if (testRows[i].getElementsByTagName("input").length > 0) {
                var collectionText = testRows[i].getElementsByTagName("input");
                var collectionList = testRows[i].getElementsByTagName("select");

                if (collectionList[0].value != 0 || collectionList[1].value != 0 || collectionText[0].value != "" || collectionText[1].value != "") {
                    testscore = testscore + collectionList[0].value + "_TestHidden,";
                    testscore = testscore + collectionList[1].value + "_TestTypeHidden,";
                    testscore = testscore + collectionText[0].value + "_TestScoreHidden,";
                    testscore = testscore + collectionText[1].value + "_TestDateHidden";
                    // Do not add a "|" at the end of the last group of data so that a split will not create one additional data row
                    if ((i + 1) < testRowsLength)
                        testscore = testscore + "|";
                }
            }
        }

        for (var i = 0; i < document.forms[0].elements.length; i++) {
            var element = document.forms[0].elements[i];
            if (element.id.substring(element.id.length - 14, element.id.length) == '_TestScoreInfo') {
                testInfoHidden = document.getElementById(element.id);
                testInfoHidden.value = testscore;
            }
        }
    }
}

function setCascadingDropDownValuesTestScore(handler, sourceDropDown, destinationDropDown, selectedTestType) {
    removeOptions(destinationDropDown);
    var field = document.getElementById(sourceDropDown);
    $.post(handler, { "testId": findSelectedValue(field) }, function(data) {
        changeDestinationOptionsTestScore(destinationDropDown, data);
        selectDropDownOption(document.getElementById(destinationDropDown), selectedTestType);
       }, "text");

}

function changeDestinationOptionsTestScore(destinationDropDown, data) {
    dom = LoadXmlString(data);
    var tree = dom.documentElement;
    var nodes = tree.childNodes;
    addOptions(destinationDropDown, nodes);
}

function addTest(testDropDownId, testTypeHandler, testTypeDropDownId, testScoreTextBoxId, testDate, testDateCalendar, removeButtonText,
        selectedTest, selectedTestType, selectedTestScore, selectedTestDate) {
    var container = document.getElementById("TestScoreTable");
    var testRow = container.insertRow(-1);
    var dupeTestBox;
    var dupeTestTypeBox;
    var dupeTestScoreBox;
    var dupeTestDateBox;
    var dupeTestCalendar;
    var cellNumber = 0;

    //Test field
    dupeTestBox = document.getElementById(testDropDownId).cloneNode(true);
    dupeTestBox.id = "_TestHidden" + eval(txtIdTest += 1);
    selectDropDownOption(dupeTestBox, selectedTest);

    // Set the id for the Test Type field to be used in the onChange event
    dupeTestTypeBox = document.getElementById(testTypeDropDownId).cloneNode(true);
    dupeTestTypeBox.id = "_TestTypeHidden" + eval(txtIdTest += 1);

    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeTestBox);

    $("#" + dupeTestBox.id).attr("dupeTestBox", dupeTestBox.id);
    $("#" + dupeTestBox.id).attr("dupeTestTypeBox", dupeTestTypeBox.id);
    $("#" + dupeTestBox.id).attr("handler", testTypeHandler);

    dupeTestBox.onchange = function() {
        setCascadingDropDownValuesTestScore($(this).attr("handler"), $(this).attr("dupeTestBox"), $(this).attr("dupeTestTypeBox"));
    }

    cellNumber++;

    //Test Type field
    // already created the dupe node and id above
    selectDropDownOption(dupeTestTypeBox, selectedTestType);
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeTestTypeBox);
    cellNumber++;

    setCascadingDropDownValuesTestScore(testTypeHandler, dupeTestBox.id, dupeTestTypeBox.id, selectedTestType);

    selectDropDownOption(dupeTestTypeBox, selectedTestType);

    //Test Score field
    dupeTestScoreBox = document.getElementById(testScoreTextBoxId).cloneNode(true);
    dupeTestScoreBox.id = "_TestScoreHidden" + eval(txtIdTest += 1);
    dupeTestScoreBox.value = selectedTestScore;
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeTestScoreBox);
    cellNumber++;

    //Test date field
    dupeTestDateBox = document.getElementById(testDate).cloneNode(true);
    dupeTestDateBox.id = "_TestDateHidden" + eval(txtIdTest += 1);
    dupeTestDateBox.value = selectedTestDate;
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeTestDateBox);

    dupeTestCalendar = document.getElementById(testDateCalendar).cloneNode(true);
    dupeTestCalendar.id = "_TestDatePopupHidden" + eval(txtIdTest += 1);
    dupeTestCalendar.onclick = function() { calendarPicker(dupeTestDateBox.id); return false; }
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(dupeTestCalendar);
    cellNumber++;

    // Add remove button
    var btnRemove = document.createElement("input");
    btnRemove.type = "button";
    btnRemove.value = removeButtonText;
    btnRemove.id = "remove_" + eval(btnIdTest += 1);
    btnRemove.className = "btnRemove";
    container.rows[container.rows.length - 1].insertCell(cellNumber);
    container.rows[container.rows.length - 1].cells[cellNumber].appendChild(btnRemove);

    reInitializeTest();
}

// Handle the initial state for TestScore container
function initializeGridTestScore(testDropDownList, testTypeDropDownList, testScoreTextBox, testDateBox, testDateCalendarPopup,
    defaultValues, removeButtonLabelText) {
    removeButtonLabel = removeButtonLabelText;

    if (defaultValues == "[ ]")
        defaultValues = "";

    if (defaultValues.length > 0) {
        var test;
        var testScore;
        var testDate;
        var testTypeHandlerHidden;
        var testTypeHandler;

        for (var i = 0; i < document.forms[0].elements.length; i++) {
            var element = document.forms[0].elements[i];

            if (element.id.substring(element.id.length - 23, element.id.length) == '_TestScoreHandlerHidden') {
                testTypeHandlerHidden = document.getElementById(element.id);
                if (testTypeHandlerHidden != null) testTypeHandler = testTypeHandlerHidden.value;
            }
        }

        test = document.getElementById(testDropDownList);
        selectDropDownOption(test, defaultValues[0][0]);
        setCascadingDropDownValuesTestScore(testTypeHandler, testDropDownList, testTypeDropDownList, defaultValues[0][1]);

        testScore = document.getElementById(testScoreTextBox);
        if (testScore != null) testScore.value = defaultValues[0][2];

        testDate = document.getElementById(testDateBox);
        if (testDate != null) testDate.value = defaultValues[0][3];
    }

    for (var i = 1; i < defaultValues.length; i++) {
        addTest(testDropDownList, testTypeHandler, testTypeDropDownList, testScoreTextBox, testDateBox, testDateCalendarPopup,
            removeButtonLabel, defaultValues[i][0], defaultValues[i][1], defaultValues[i][2], defaultValues[i][3]);
    }
}

// Add event for delete a test
function reInitializeTest() {
    var removeButtons = document.getElementById("TestScoreTable").getElementsByTagName("input");
    for (i = 0; i < removeButtons.length; i++) {
        if ((removeButtons[i].type == "button") && (removeButtons[i].className != "glassFormButtonMed")) {
            removeButtons[i].onclick = function() {
                this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
            }
            removeButtons[i].className = "btnRemove";
        }
    }
}
