﻿function getList() {
    $.ajax(
        {
            type: 'POST',
            url: SITE_URL + "/List/Get",
            success: function (data) {
                var list = eval("(" + data + ")");
                my_list_display(list);
            }
        }

    );
}

function my_list_add(category, id, type, name) {
    $.ajax(
        {
            type: 'POST',
            url: SITE_URL + "/List/Add",
            data: { Type: type, ID: id, Name: name, Category: category },
            success: function (data) {
                getList();
                
                if (document.getElementById("del_" + category + "_" + id + "_" + type)) {
                    document.getElementById("add_" + category + "_" + id + "_" + type).style.display = 'none';
                    document.getElementById("del_" + category + "_" + id + "_" + type).style.display = 'block';
                }

            }
        }

    );

    }

    function my_list_del_2(category, id, type) {
        $.ajax(
        {
            type: 'POST',
            url: SITE_URL + "/List/DeleteInList",
            data: { Category: category, ID: id, Type: type },
            success: function (data) {
                getList();
                if (document.getElementById("add_" + category + "_" + id + "_" + type)) {
                    document.getElementById("del_" + category + "_" + id + "_" + type).style.display = 'none';
                    document.getElementById("add_" + category + "_" + id + "_" + type).style.display = 'block';
                }
            }
        }

        );
   }

   function my_list_del_all() {
       $.ajax(
        {
            type: 'POST',
            url: SITE_URL + "/List/DeleteAll",
            success: function (data) {
               getList();

                var add_buttons = document.getElementsByName("addbuttons");
                for (var i = 0; i < add_buttons.length; i++)
                    add_buttons[i].style.display = "block";
                var del_buttons = document.getElementsByName("delbuttons");
                for (var i = 0; i < del_buttons.length; i++)
                    del_buttons[i].style.display = "none";	
            }
        }

        );

      
   }

function my_list_display(list) {
    var my_list_total = 0;

    for (var i = 1; i <= 5; i++) {
        if (document.getElementById("list_display_" + i).hasChildNodes()) {
            tmpEl1 = document.getElementById("list_display_" + i).firstChild;
            while (tmpEl1) {
                tmpEl2 = tmpEl1.nextSibling;
                tmpEl1.parentNode.removeChild(tmpEl1);
                tmpEl1 = tmpEl2;
            }
        }
    }

    for (var i = 0; i < 5; i++) {
        document.getElementById("list_count_" + (i + 1)).innerHTML = list[i].length;
        my_list_total += list[i].length;

        for (var j = 0; j < list[i].length; j++) {
            tmpList = document.getElementById("list_display_" + (i + 1));

            tmpListItem = document.createElement("LI");

            tmpDiv = document.createElement("DIV");
            tmpDiv.setAttribute("class", "listing-box");

            tmpSpan = document.createElement("SPAN");
            tmpSpan.setAttribute("title", "Delete this listing");
            tmpSpan.setAttribute("class", "delete-listing");
            tmpSpan.onclick = new Function("my_list_del_2(" + i + ", " + list[i][j].ID + ", " + list[i][j].Type + ");");
            tmpDiv.appendChild(tmpSpan);

            tmpSpan = document.createElement("SPAN");
            tmpSpan.setAttribute("title", list[i][j].Name);
            tmpSpan.setAttribute("style", "display:block");
            tmpSpan.setAttribute("class", "hyperlink");

            tmpTxt = document.createTextNode(list[i][j].Name);
            tmpSpan.appendChild(tmpTxt);
            tmpDiv.appendChild(tmpSpan);

            tmpInput = document.createElement("INPUT");
            tmpInput.setAttribute("class", "manage-listing");
            tmpInput.setAttribute("name", "");
            tmpInput.setAttribute("type", "checkbox");
            tmpInput.setAttribute("value", "");
            tmpInput.onclick = new Function("my_list_check(this, " + i + ", " + list[i][j].ID + ");");
            tmpDiv.appendChild(tmpInput);
            if (list[i][j].Checked == true)
                tmpInput.checked = true;

            tmpListItem.appendChild(tmpDiv);
            tmpList.appendChild(tmpListItem);
        }
    }
    document.getElementById("my_list_total_header").innerHTML = my_list_total;
    document.getElementById("my_list_total_footer").innerHTML = my_list_total;
}

function my_list_check(e, category, id) {
    check = e.checked;
    $.ajax(
        {
            type: 'POST',
            url: SITE_URL + "/List/update_check",
            data: { Category: category, ID: id, Checked: check },
            success: function (data) {
            }
        }

    );
}
function compare(type) {
    var count = Number(document.getElementById("list_count_" + type).innerHTML);
    if (count == 0) {
        alert("You have nothing to compare in that category.  Please add some items to your list and try again.");
        return false;
    }
    var action = "";
    switch (type - 1) {
        case 0: action = "Drugs"; break;
        case 1: action = "Devices"; break;
        case 2: action = "Injectables"; break;
        case 3: action = "SkinCare"; break;
        case 4: action = "Sunscreen"; break;
    }
    // Check to make sure there are at least 2 items checked
    // Go to the sbs page.
    window.location = SITE_URL + "/Sbs/" + action;
}

function my_list_view_all() {
    var total = Number(document.getElementById("my_list_total_header").innerHTML);

    if (total == 0) {
        alert("Your MyList is empty.  Please add items before using this function.");
        return false;
    }
    window.location = SITE_URL + "/Search/ListAll";
}

