﻿function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}


// Set Cookie
function setCookie(c_name,value,expiredays)
{
    if(navigator.cookieEnabled)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
}


// Erase Cookie
function eraseCookies()
{
    if(navigator.cookieEnabled)
        setCookie("RECENTITEMS", "", 1);
}

// Show Cookie
function showCookie()
{
    if(navigator.cookieEnabled)
        window.alert(document.cookie.toString());
}

// Show Current Page Location
function showCurrentPageLocation()
{
    window.alert(document.URL + " --> "+ document.title);
}

function generateRecentItemsTable()
{  
 var table = "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
 var recentItems = getCookie("RECENTITEMS");
 if(recentItems == undefined || recentItems == null || recentItems.length == 0)
    return "";
 var links = recentItems.split("|");
 if(links.length > 0)
 for(var index=links.length - 1; index >= 0; index--)
 {
    var linkDetails = links[index].split(">");
        table +="<tr><td>";
        table +="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='http://www.marketingmechanisms.com/images/carrot.gif'>"
        table +="&nbsp;&nbsp;&nbsp;"
		table +="<a href='" + linkDetails[1] + "'>" + linkDetails[0] + "</a>";
        table +="</td></tr>";
 }
 table += "</table>";
 return table;
}


// Save Current Page To Recently Viewed Items
function saveCurrentPageToRecentlyViewedItemsList()
{
    if(navigator.cookieEnabled)
    {
        if(getCookie("RECENTITEMS").indexOf(window.location, 0) == -1)// Proceed if page is not in recently viewed items list.
        {
            var title = document.getElementById("sectionTitleID") != null ? document.getElementById("sectionTitleID").innerHTML : "";
            if(title == "")
                title = (document.title != '' || document.title != document.URL) ? document.title : "";
            var url = document.URL;
            
            if (title == "") // If no subtitle or page title -- do not register page;
            return;
            
            if(getCookie("RECENTITEMS") == "")   // 1st value
                setCookie("RECENTITEMS", title + ">" + url, 1);
            else                                 // additional values
                setCookie("RECENTITEMS", getCookie("RECENTITEMS")+ "|" + title + ">" + url, 1);
            
            var links = getCookie("RECENTITEMS").split("|");
            if(links.length > 5)
            {
                var newCookieValue = "";
                for(var index=1; index <links.length; index++)
                {
                    if(index != 1)
                        newCookieValue += "|";
                    newCookieValue += links[index] 
                }
                setCookie("RECENTITEMS", newCookieValue, 1);
            }      
        }
    } 
}


function showRecentlyViewedItems()
{
    var msg;
    if (navigator.cookieEnabled)  
        document.getElementById('RecentItems').innerHTML = generateRecentItemsTable(); 
    else
    {
      msg = "This web part requires browser cookies. Your browser cookies are disabled. Please enable your browser cookies.";
      document.getElementById('RecentItems').innerHTML = msg;
    } 
}
