// Use with cookieBasic Functions.js

function editSelectedTopic()
{

    var j;
    var k = 0;
    param = new Array(3);

    var sTemp = new String();
    var topicInfo = new String();
    var editname, editwords, edittype, edittime;

    if(document.topicform.mytopics.options.selectedIndex != -1)
    {

        var index = document.topicform.mytopics.options[document.topicform.mytopics.options.selectedIndex].value;
        var topicCookie = "topic" + index;

        topicInfo = getCookie(topicCookie);

        for(j = 0; j < topicInfo.length; j++)
        {
            if(topicInfo.charAt(j) != "|")
            {
                sTemp += topicInfo.charAt(j);
            }
            else
            {
                param[k] = sTemp;
                sTemp = "";
                k++;
            }
        }
        param[k] = sTemp;

        editname = param[0];
        editwords = param[1];
        edittype = param[2];
        edittime = param[3];

        document.topicform.topicname.value = editname;
        document.topicform.topicwords.value = editwords;
        document.topicform.editindex.value = index;

        for(var i = 0; i < 4; i++)
        {
            if(document.topicform.topictype[i].value == edittype)
                document.topicform.topictype[i].checked = "1";
        }

        for(var i = 0; i < 4; i++)
        {
            if(document.topicform.topictime[i].value == edittime)
                document.topicform.topictime[i].checked = "1";
        }

    // Change "Add Button" to "Update"
        document.topicform.add.value = "Update";

    }
    else
    {

        alert("Please select a topic to edit...");
    }
}

// Delete topic selected in selection box...
function delSelectedTopic()
{

    var index, topicCookie;

   // If a topic is selected...
    if(document.topicform.mytopics.options.selectedIndex != -1)
    {

        index = document.topicform.mytopics.options[document.topicform.mytopics.options.selectedIndex].value;

        topicCookie = "topic" + index;

        delCookie(topicCookie, "/");

    // Reload banner frame with added topic
        var sURL = new String(parent.TopBanner.document.URL);
        if(sURL.indexOf("?") > 0)
            parent.TopBanner.location.replace(parent.TopBanner.document.URL);
        else
            parent.TopBanner.location.replace(parent.TopBanner.document.URL + "?setTab=myTab");

    // Refresh add topic frame
        window.location.reload();

    }
    else
    {
        alert("Please select a topic to delete...");
    }
}

// Adds a topic cookie
function addTopicCookies()
{


    //set to expire 5 years from today
    var expiration = new Date();
    expiration.setTime(expiration.getTime() + (157680000000));

    var index, topicCookie, topicInfo;

    if(document.topicform.editindex.value == "-1")
        index = nextTopicCookie();
    else
        index = document.topicform.editindex.value;

  // If user has maxxed out topics..
    if(index == 11)
    {
        // topic cookie not added...
        alert("You've reached the maximum supported custom topics(10), Please delete a topic before adding more...");

    }
    else
    {

        topicCookie = "topic" + index;

        var topicname = document.topicform.topicname.value;
        var topicwords = document.topicform.topicwords.value;
        var topictype, topictime;

        if(document.topicform.topicname.value == "" || document.topicform.topicwords.value == "")
        {
            alert("Unable to add topic, blank entry for name or keywords...");
        }
        else
        {

            for(var i = 0; i < 4; i++)
            {
                if(document.topicform.topictype[i].checked == "1")
                {
                    topictype = document.topicform.topictype[i].value;
                }
            }

            for(var i = 0; i < 4; i++)
            {
                if(document.topicform.topictime[i].checked == "1")
                {
                    topictime = document.topicform.topictime[i].value;
                }
            }

            topicInfo = topicname + "|" + topicwords + "|" + topictype + "|" + topictime;

            setCookie(topicCookie, topicInfo, expiration, "/");   // "dij/", document.domain

            // Reload banner frame with added topic
            var sURL = new String(parent.TopBanner.document.URL);
            if(sURL.indexOf("?") > 0)
                parent.TopBanner.location.replace(parent.TopBanner.document.URL);
            else
                parent.TopBanner.location.replace(parent.TopBanner.document.URL + "?setTab=myTab");

      // Refresh add topic frame
            window.location.reload();
        }
    }
}


// Find next available topic number ( from 20 maximum )
function nextTopicCookie()
{

    var i, cookieValue, cookieName;

    for(i = 1; i < 12; i++)
    {

        cookieName = "topic" + i;
        cookieValue = getCookie(cookieName);

        if(cookieValue == null)
        {
            break;
        }
    }
    return i;
}


function writeTopics()
{

    var j;
    var k = 0;
    param = new Array(3);

    var sTemp = new String();
    var topicInfo = new String();

//Elements of topic cookie...
    var name, words, type, time;
    var bar;

    var searchURL = "<TD class=\"redOption\">&nbsp;|&nbsp;</TD><td class=\"redOption\"><a class=\"redbar\" href=\"/jsp/SearchResults?Query=";
    var searchURLsuffix;

    var cookieName;
    var topicOption = "";

// 10 topics total allowed

    for(i = 1; i < 11; i++)
    {

        sTemp = "";
        k = 0;
        cookieName = "topic" + i;
        topicInfo = getCookie(cookieName);

    //alert(topicInfo);

        if(topicInfo != null)
        {

            for(j = 0; j < topicInfo.length; j++)
            {
                if(topicInfo.charAt(j) != "|")
                {
                    sTemp += topicInfo.charAt(j);
                }
                else
                {
                    param[k] = sTemp;
                    sTemp = "";
                    k++;
                }
            }

            param[k] = sTemp;

            name = param[0];
            words = escape(param[1]);
            type = "&SearchOption=" + param[2];
            time = "&DateChoice=DateRange&RangeOption=" + param[3];
            bar = "&TopicBar=1";
            searchURLsuffix = "\" target=\"Main\">" + name + "</A></TD>";

            topicOption += searchURL + words + type + time + bar + searchURLsuffix;
        }
    }
    return topicOption;
}


// "Add" button changes to "Update" when editing a topic...
function addUpdateButtons()
{

    document.write("<INPUT class='dijButton' name='add' TYPE='button' VALUE='Add Topic' onClick='addTopicCookies()'>&nbsp;&nbsp;");
    document.write("<INPUT class='dijButton' name='cancel' TYPE='button' VALUE='Cancel' onClick='window.location.reload()'>");
}


// Fills Select list with created topics from cookies
function writeTopicOptions()
{

    var i;
    var cookieValue = new String();
    var cookieName;
    var name = new String();

// 10 topics total allowed

    for(i = 1; i < 11; i++)
    {

        cookieName = "topic" + i;

        cookieValue = getCookie(cookieName);

        if(cookieValue != null)
        {

            name = cookieValue.substring(0, cookieValue.indexOf("|"));

            document.write("<option value='" + i + "'>" + name + "</option>");
        }
    }
}


