﻿/// <summary>
/// Finds the element that has specified part of a string name of a certain type
/// e.g. "Arrow"
/// </summary>
function GetElement(element, partialName, type) {
    if (element.tagName == type) if (element.id.indexOf(partialName) != -1) return element;
    if (element.childNodes.length < 1) return null;
    var elementArray = element.childNodes;
    var nodeCount = element.childNodes.length;

    for (var i = 0; i < nodeCount; i++) {
        childElement = GetElement(elementArray[i], partialName, type);
        if (childElement != null) return childElement;
    }
    return null;
}

//Gets the Table element given one of the table elements
function GetTableElement(childElement, tag) {
    var element = childElement;
    var tElement = null;

    //Get To the table
    while (element != null) {
        if (element.tagName == tag) {
            tElement = element;
            break;
        }
        element = element.parentElement;
    }

    return tElement;
}


/// <summary>
/// Changes the SubPanel image to the normal style
/// </summary>
function MouseOutSubPanel(subpanel) {
    subpanel.mouse = '';
    UpdateSubPanelImage(subpanel);
} 
/// <summary>
// Updates the SubPanel image based on the currently selected styles
/// </summary>
function UpdateSubPanelImage(subpanel) {
    // Find the arrow image and update it
    var image = GetElement(subpanel, "Arrow", "IMG");
    if (image != null) {
        var root = image.src;
        root = root.substring(0, root.lastIndexOf('/') + 1);
        image.src = root + subpanel.mode + subpanel.mouse + 'Button.png';
    }
}
/// <summary>
/// Hides or expands the SubPanel
/// </summary>
function MouseClickSubPanel(subpanel) {
    subpanel.mode = subpanel.mode == 'Collapse' ? 'Expand' : 'Collapse';
    UpdateSubPanelImage(subpanel);
    var table = GetTableElement(subpanel, 'TBODY');
    var display = subpanel.mode == 'Collapse' ? '' : 'none';
    var childrenLen = table.children.length - 1;
    for (var r = 2; r < childrenLen; ++r) {
        table.children[r].style.display = display;
    }
}

/// <summary>
/// Changes the SubPanel image to the hover over style
/// </summary>
function MouseOverSubPanel(subpanel) {
    if (typeof (subpanel.mode) != 'string') subpanel.mode = 'Collapse';
    subpanel.mouse = 'Over';
    UpdateSubPanelImage(subpanel);
}

function ExpandTable(Helper, mode, relevantQuotes) {
    var table = document.getElementById('MainTable');
    var foundIndex = -1;

    var rowsLen = table.rows.length;
    for (var i = 0; i < rowsLen; ++i) {
        if (table.rows[i].id == "RowToInsertAttributesBefore") {
            foundIndex = i;
            break;
        }
    }

    var rowsToCreate = Helper.GetRankCount();
    var cellsToSpawn = 0;
    var uiPage = -1;
    var cellsToSpawn = relevantQuotes.length + 1;

    switch (mode) {
        case "lifeCompare":
        case "healthCompare":
        case "criticalIllnessCompare":
        case "shortTermCompare":
        case "limitedMedcialCompare":
        case "dentalCompare":
            {
                uiPage = Enums.UIPage.Compare;
            }
            break;

        case "lifeSelect":
        case "healthSelect":
        case "criticalIllnessSelect":
        case "shortTermSelect":
        case "limitedMedicalSelect":
        case "dentalSelect":
            {
                uiPage = Enums.UIPage.Select;
            }
            break;
    }

    var allAttributes = Helper.GetRanks();
    var commonAttributes = new Array();

    // This is to add all the Common Attributes to the relevant cell as a tooltip
    for (var k = 0; k < allAttributes.length; k++) {
        if (allAttributes[k].IsDynamic == false) {
            commonAttributes.Add(allAttributes[k]);

            var attributeName = allAttributes[k].DisplayName;
            var rowName = attributeName.trim() + "Cell";

            var tempRegExp = new RegExp("\\s", "g");
            rowName = rowName.replace(tempRegExp, "");

            var cell = document.getElementById(rowName);

            if (cell != null) {
                SetupHeaderTooltip(cell, allAttributes[k], null, mode);
            }
        }
    }

    var lastInsertedIndex = foundIndex - 1;

    for (var i = 0; i < rowsToCreate; ++i) {
        var rank = Helper.GetRank(i);

        // only process the Attribute if it is a dynamic attribute
        if (rank.IsDynamic == true) {

            var rowName = "attrib" + rank.AttributeName + "row";

            //look if row is already there. if it is, don't create the row
            var row = document.getElementById(rowName);
            if (row == null) {
                var indexToInsertAt = 0;

                indexToInsertAt = foundIndex + rank.Rank;

                if (indexToInsertAt > foundIndex) {
                    indexToInsertAt = lastInsertedIndex + 1;
                }

                row = table.insertRow(indexToInsertAt);
                row.id = rowName;

                lastInsertedIndex = indexToInsertAt;
            }
            else {
                //row is already there, so remove the cells and recreate them
                for (var c = 0; c < cellsToSpawn; ++c) {
                    var cell = row.cells[c];
                    if (cell) {
                        while (cell.children.length > 0) {
                            cell.removeChild(cell.children[cell.children.length - 1]);
                        }
                    }
                }
            }

            for (var j = 0; j < cellsToSpawn; ++j) {
                var cell = row.cells[j];
                if (!cell) {
                    cell = row.insertCell(-1);
                }
                switch (j) {
                    case 0:
                        {
                            cell.className = "ItemHeaders";
                            cell.innerHTML = rank.DisplayName;

                            //set the tooltip and the google link
                            SetupHeaderTooltip(cell, rank, relevantQuotes[j], mode);
                            break;
                        }
                    default:
                        {
                            var quote = relevantQuotes[j - 1];
                            var attrib = quote.Plan.InputAttributes.getValue(rank.AttributeName);
                            if (attrib == null) {
                                attrib = new Array();
                                attrib.InputType = -1;
                            }
                            if (attrib != null) {
                                var inputType = attrib.InputType;
                                var attribName = attrib.AttributeName;

                                var createSelect = false;
                                var createSpan = false;
                                var createText = false;

                                switch (inputType) {
                                    case Enums.AttributeType.InputRange:
                                        {
                                            createText = true;
                                            break;
                                        }
                                    case Enums.AttributeType.InputList:
                                        {
                                            createSelect = true;
                                            createSpan = true;
                                            break;
                                        }
                                    case -1:
                                        {
                                            createSpan = true;
                                            break;
                                        }
                                }

                                var newSelect = null;
                                var newSpan = null;
                                var newText = null;


                                if (createSelect == true) {
                                    newSelect = document.createElement('SELECT');
                                    newSelect.id = GenerateDropDownName(i, j - 1);
                                    cell.appendChild(newSelect);

                                    if (rank.DisplayName == "Insurance Class") {
                                        newSelect.className = "LargeDropDown";
                                    }
                                    else {
                                        newSelect.className = "MediumDropDown";
                                    }
                                }

                                if (createSpan == true) {
                                    newSpan = document.createElement('SPAN');
                                    newSpan.id = GenerateSpanName(i, j - 1);
                                    newSpan.innerText = "N/A";
                                    cell.appendChild(newSpan);
                                }

                                if (createText == true) {
                                    newText = document.createElement('INPUT');
                                    newText.id = GenerateTextName(i, j - 1);
                                    cell.appendChild(newText);
                                }

                                if (attribName == Enums.AttributeName.Network) {

                                    //Add the network link 
                                    var networkLink = document.createElement('A');
                                    networkLink.id = 'NetworkLink' + (j - 1);
                                    networkLink.target = "_blank";
                                    cell.appendChild(networkLink);

                                    var dummy = document.createElement('SPAN');
                                    var spaceText = document.createTextNode(" ");
                                    dummy.appendChild(spaceText);
                                    cell.appendChild(dummy);

                                    //Add the network image 
                                    var networkImage = document.createElement('IMG');
                                    networkImage.src = '../Images/ViewProviders.gif'
                                    networkImage.id = 'NetworkImage' + (j - 1);
                                    cell.appendChild(networkImage);
                                }
                            }
                            else {
                                var dummyLabel = document.createElement('SPAN');
                                dummyLabel.innerText = "N/A";
                                cell.appendChild(dummyLabel);
                            }
                        }
                }
            }
        }
    }
}