//******************************************************************************
//FUNCTION NAME: setCookie
//PURPOSE: Sets the cookies on mobile bookmarks
//FUNCTION INTERFACE: function setCookie(c_name,value,expiredays)
//                    c_name: name of bookmark
//                    value: page being bookmarked
//                    expireDays: days til expire.
//******************************************************************************
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);

    // setting cookie value. Must set path
    if(document.cookie.indexOf(c_name + "=") < 0) // check to see if cookie exists.
    {
        document.cookie=c_name+ "=" +escape(value)+ ";path=/" +
        ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());

    }
}

//******************************************************************************
// FUNCTION NAME: appendCookie
// FUNCTION PURPOSE: appends incoming cookie values to the existing cookie
// FUNCTION INTERFACE: function appendCookie(c_name,value,expiredays)
//                    c_name: name of bookmark
//                    value: page being bookmarked
//                    expireDays: days til expire.
//******************************************************************************
function appendCookie(c_name, value, expiredays)
{
    var exdate=new Date();
    var tempOldCookie;
    exdate.setDate(exdate.getDate()+expiredays);

    var oldCookieValue = getCookie(c_name);

    if(oldCookieValue == -1) {
        
        // no cookie value so lets set value
        setCookie(c_name, value, expiredays);
        return;
    }

    // check to see if value already exists in book mark and if so lets ignore.
    var currentTagTokens = oldCookieValue.split( "," );
    for ( var i = 0; i < currentTagTokens.length; i++ )
      {
          if(currentTagTokens[i] == value) {
            return; // exists already so no point add again
          }
      }

    // add new cookie with appended value.
    document.cookie=c_name+ "=" +escape(value + "," + oldCookieValue)+ ";path=/" +
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name) {
    var start = document.cookie.indexOf( c_name + "=" );

    if(start != -1)
     {
        var len = start + c_name.length + 1;
        var end = document.cookie.indexOf(";", len);

        if (end==-1)
            end=document.cookie.length;

        return unescape(document.cookie.substring( len, end ));
    } else
    {
        return -1; // denotes nothing found.
    }
    
}


function isCookieSet(c_name, value) {
	
	var oldCookieValue = getCookie(c_name);
	// check to see if value already exists in book mark and if so lets ignore.
    
	var currentTagTokens = String(oldCookieValue).split(",");
    
    for ( var i = 0; i < currentTagTokens.length; i++ ) {
      if(currentTagTokens[i] == value) {
        return true;
      }
    }	
	
	return false;
}

function deleteCookie(cookieName) {
	document.cookie = cookieName + '=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/'; // delete cookie
}
