/*
 * The following functions CreateCookie, ReadCookie and CheckForCookies
 * are all called from default and search page, to check for the existance
 * of whether cookies are available.
 */
 
/* The time paramater is in milliseconds */
function CreateCookie(name, value, time)
{
    if (time)
    {
	    var date = new Date();
	    date.setTime(date.getTime()+ time);
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

/* This function attempts to read the cookie */
function ReadCookie(sCookieName)
{
    var sNameEQ = sCookieName + "=";
    var peCookies = document.cookie.split(';');
    for(var i = 0; i < peCookies.length; i++)
    {
	    var peCookie = peCookies[i];
	    while (peCookie.charAt(0) == ' ')
	        peCookie = peCookie.substring(1, peCookie.length);
	    if (peCookie == sCookieName || peCookie.indexOf(sNameEQ) == 0)
	        return true;
	}
	return false;
}

function CheckForCookies()
{
    // To check for a cookie - we need to create a temporary one
    // and then attempt to read it
    CreateCookie("PETestCookie", "", (1*60*1000)); // That will expire in 1 minute
    
    // Now attempt to read it
    if(ReadCookie("PETestCookie") == false)
        return false;
    
    // Now delete it
    // To clear the cookie - just recreate it with an expiry of yesterday
    // The browser will then see that it has expired and then delete it.
    CreateCookie("PETestCookie", "", (-1*24*60*60*1000)); // Set expiry to a day old
    
    return true;
}

/*
 * The following section of functions are called from within the search page.
 */
function DoesIdExist(sExistingIds, sId)
{
    var sOldSplit = sExistingIds.split(',');
    for(var i = 0; i < sOldSplit.length; i++)
    {
        var sOldId = sOldSplit[i];
        if(sOldId == sId)
            return true;
    }
    return false;
}

/* Function that returns unique provider ids */
function GetUniqueIds(sExistingIds, sNewIds)
{
    if (sExistingIds == "")
        return sNewIds;
        
    var sIDS = sExistingIds;
    
    var sNewSplit = sNewIds.split(',');
    for(var i = 0; i < sNewSplit.length; i++)
    {
        var sId = sNewSplit[i];
        // If the id doesn't already exist in the list then add it
        if(!DoesIdExist(sExistingIds, sId))
            sIDS += ("," + sId);
    }
    
    return sIDS;
}

/*
 *
 * Cookies are saved in the format of
 * name=value;expiry date;path
 * for example "PESearchCookie=1,3,10;expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/"
 */
function GetExistingCookieValue(sCookieName)
{
    var sNameEQ = sCookieName + "=";
    var peCookies = document.cookie.split(';');
    for(var i = 0; i < peCookies.length; i++)
    {
	    var peCookie = peCookies[i];
	    while (peCookie.charAt(0) == ' ')
	        peCookie = peCookie.substring(1, peCookie.length);
	    if (peCookie.indexOf(sNameEQ) == 0)
	        return peCookie.substring(sNameEQ.length, peCookie.length);
    }
    
    return "";
}

function SaveToShoppingBasket(sCookieName, sIDS)
{
    if(sIDS == "")
        return false; // Nothing to do
        
    var sExistingIds = GetExistingCookieValue("PESearchCookie");
    var sNewIds = GetUniqueIds(sExistingIds, sIDS);
    // Create the cookie with the list of ids
    var expires = "";   // The cookie will expire when the browser closes
    document.cookie = sCookieName + "=" + sNewIds + expires +"; path=/";
    
    return true;
}

function SelectLanguage(iLangId)
{
    CreateCookie("PELanguageCookie", iLangId);
    return;
}