

// Reads Personalization Cookie 
// Irina - Dec. 17


//changed on Dec. 28 - two cookies (videos and articles are stored sep.)

// 'enum' for keys
var PERSONCOOKIE = "FrontDoorUserPers";
var PERSONCOOKIE_USERID = "uid";
var PERSONCOOKIE_NAME = "fln";
var PERSONCOOKIE_EMAIL = "email";
var PERSONCOOKIE_ZIP = "zip";
var PERSONCOOKIE_SEARCHCOUNT = "scnt";
var PERSONCOOKIE_LISTINGS = "lid";
var PERSONCOOKIE_LATESTLISTINGS = "llid";
var PERSONCOOKIE_LATESTSEARCHES= "lsrs";
var PERSONCOOKIE_LATESTVIDEOS = "lvid";
var PERSONCOOKIE_LATESTARTICLES = "laid";


//the keys to read from second cookie
var PERSONCOOKIE2 = "FrontDoorUserPers2";
var PERSONCOOKIE_ARTICLES = "aid";
var PERSONCOOKIE_VIDEOS ="vid";





//returns true if article exists
function articleExists(articleID)
{
  var exists = false;
  arrArticles = readArticlesPersCookie();
  if (null != arrArticles)
  {
      if (inArray(articleID, arrArticles)!= -1)
      {
        exists = true;
      }
  }
  return exists;
    
}
function videoExists(videoID)
{
  var exists = false;
  arrVideos = readVideosPersCookie();
  if (null != arrArticles)
  {
      if (inArray(videoID, arrVideos)!= -1)
      {
        exists = true;
      }
  }
  return exists;
    
}

//returns true if listing exists
function listingExists(listingID)
{
   var exists = false;
  arrListings = readListingsPersCookie();
  if (null != arrListings)
  {
      if (inArray(listingID, arrListings)!= -1)
      {
        exists = true;
      }
  }
  return exists;
}
//returns true if listing exists
function latestlistingExists(listing)
{
   var exists = false;
  arrLatListings = readLatestListingsPersCookie();
  if (null != arrLatListings)
  {
      if (inArray(listing, arrLatListings)!= -1)
      {
        exists = true;
      }
  }
  return exists;
}
//returns the number of saved searches or 0
function readSearchCountPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_SEARCHCOUNT);
    if (arrResult){
        if(arrResult[0]!="")
        {
            return arrResult[0];
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }

}

function DisplayName()
{
   //I don't know if you need to add Welcome,  ...   
    if (readUserPersCookie(PERSONCOOKIE_NAME)=="")
    {
        return readUserPersCookie(PERSONCOOKIE_EMAIL);
    }
    else
    {
        return readUserPersCookie(PERSONCOOKIE_NAME);
    }

}

function readListingCountPersCookie()
{
    if (readListingsPersCookie() != undefined)
    {
        if(readListingsPersCookie()[0]!="")
        {
            return readListingsPersCookie().length;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}
function readArticleCountPersCookie ()
{
    if (readArticlesPersCookie() != undefined)
    {
        if(readArticlesPersCookie()[0] != "")
        {
            return readArticlesPersCookie().length;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

function readVideoCountPersCookie ()
{
    if (readVideosPersCookie() != undefined)
    {
        if(readVideosPersCookie()[0]!= "")
        {
            return readVideosPersCookie().length;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

//returns the array of stored article ids , otherwise undefined
function readArticlesPersCookie()//article ids are numeric
{
    var arrArticles = readPersonCookie(PERSONCOOKIE_ARTICLES);
    
    if (arrArticles){
         return arrArticles;
    }
    else{
            return undefined;
    }
      
}

function readVideosPersCookie()//video ids are not numeric
{
    
    var arrVideos = readPersonCookie(PERSONCOOKIE_VIDEOS);
    
    if (arrVideos){
         return arrVideos;
    }
    else{
            return undefined;
    }
}


//returns the arr of stored listing ids, otherwise undefined
function readListingsPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_LISTINGS);
    if(arrResult){
        return arrResult;
    }
    else {
        return undefined;
    }
    
}

//returns the arr of stored listing ids, otherwise undefined
function readLatestListingsPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_LATESTLISTINGS);
    if(arrResult){
        return arrResult;
    }
    else {
        return undefined;
    }    
}

//returns the arr of stored latest 3 article ids, otherwise undefined
function readLatestArticlesPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_LATESTARTICLES);
    if(arrResult){
        return arrResult;
    }
    else {
        return undefined;
    }    
    
}

//returns the arr of stored  latest 3 video ids, otherwise undefined
function readLatestVideosPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_LATESTVIDEOS);
    if(arrResult){
        return arrResult;
    }
    else {
        return undefined;
    }    
}

//returns the arr of stored  latest 3 Search names, otherwise undefined
function readLatestSearchesPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_LATESTSEARCHES);
    if(arrResult){
        return arrResult;
    }
    else {
        return undefined;
    }    
}
//see 'enum' for keys - use PERSONCOOKIE_USERID ; PERSONCOOKIE_EMAIL; PERSONCOOKIE_ZIP 
//returns the id, email,and zip stored in the cookie or empty string
function readUserPersCookie (key)
{
    var arrResult = readPersonCookie(key);
    if (arrResult)
    {
        return arrResult[0];
    }
    else {
        return "";
    }
    
}


//gloabal functions - reads the person cookie values and returns array of values by cookie key (otherwise null);
//reads the personcookie
//checks is item is inArray - returns the ordinal number of the item

function readPersonCookie(key){
    var myCookie;
   
    if((key==PERSONCOOKIE_ARTICLES)||(key==PERSONCOOKIE_VIDEOS) || (key==PERSONCOOKIE_LATESTARTICLES) ||  (key==PERSONCOOKIE_LATESTVIDEOS))
    {
         myCookie = readCookie(PERSONCOOKIE2);//read from second cookie
    }
    else
    {
        myCookie = readCookie(PERSONCOOKIE);//read from main cookie 
    }
    
    
    if(myCookie)
    {
        var value = "";
        arrPersCookie = myCookie.split('&');
        for (var i = 0; i < arrPersCookie.length; i ++)
        {
            var keyvalue = arrPersCookie[i].split('=');
            if(key==keyvalue[0])
            {
                    value = keyvalue[1];
                    var arrValues = value.split('|');
                    return arrValues;
            }
        }
     }
    else
    {
        return null; 
        
    }
}

  function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = unescape(ca[i]).replace(/\+/g, " ");
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function inArray(element, arr) {
   if(arr != undefined)
   {
		for ( var i = 0, arrl = arr.length; i < arrl; i++ )
		{   
		    if ( arr[i] == element )
				{
				    return i;
				}
		}
	}
		return -1;
	}
//
