//allows any DIV are to be populated with recent items
//recent items list must be available in a drop down list
function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function SetCookie (name, value) {
    var expDays = 365;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    
    expires = exp;
    
    var ckVal = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + exp)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
    document.cookie = ckVal;
}

function setRecentSelections(linkURL, linkText, recentListID, showCount) {
        var linkURL = linkURL;    //the URL of the link that was selected
        var linkText = linkText;  //the Text of the link that was selected
    var recentListID = recentListID;    //DIV id of where the recent titles should be displayed.
    var showCount = showCount - 1;    //the amount of Recent Items to show
    
    var cookieValCurr = "";
    var cookieValNew = "";
    if(GetCookie(recentListID)) { cookieValCurr = GetCookie(recentListID); } else { cookieValCurr = ""; }
    arr_cookieValCurr = cookieValCurr.split("|", showCount);
   
    //populate div with recent items
    try {
        cookieValNew = linkURL + "&" + linkText;
        //test to make sure new value is unique and not null
        if(!(cookieValCurr.match(cookieValNew) || cookieValNew == 0 || cookieValNew == null)) {
            //add the new value to the cookie
            cookieValNew = cookieValNew + "|" + arr_cookieValCurr.join("|");
            SetCookie(recentListID, cookieValNew);
        }
    } catch (err) {
        //alert("ERROR: Your selection couldn't be saved as a cookie.\n\n" + err.descritption);
    }
}

function getRecentItems(recentListId) {
    var recentListId = recentListId;    //DIV id of where the recent titles should be displayed.
    if(GetCookie(recentListId)) {
        var o_recentListId;
        var cookieVal = GetCookie(recentListId);
        var htmlContent = "";
        
        o_recentListId = document.getElementById(recentListId);
        arr_cookieVal = cookieVal.split("|");
        for(i=0; i < arr_cookieVal.length-1; i++) {
            var splitIndex = arr_cookieVal[i].indexOf("&");
            var linkText = arr_cookieVal[i].substr(splitIndex + 1);
            var linkURL = arr_cookieVal[i].substr(0,splitIndex);
            htmlContent = htmlContent + "<li><a href='" + linkURL + "'>" + linkText + "</a></li>";
        }
        o_recentListId.innerHTML = htmlContent;         
    }
}