

// 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_USERTYPE = "usrtp";
var PERSONCOOKIE_NAME = "fln";
var PERSONCOOKIE_EMAIL = "email";
var PERSONCOOKIE_ZIP = "zip";
var PERSONCOOKIE_SEARCHCOUNT = "scnt";
var PERSONCOOKIE_LISTINGS = "lid";
var PERSONCOOKIE_ALERTS = "alerts";
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";



 //========================================================        
 // the equivalent of .net string.format
 //========================================================
    function stringFormat()
    { 
        var a= arguments;
         if(a.length == 0) 
            return null;
        
        var i = a[0];
        for(var b = 1, cnt=a.length; b < cnt; b++)
            i = i.replace(RegExp("\\{" + (b - 1) +"\\}", "gi"), a[b]);
            
        return i;
     }
     

//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 UrlDecode(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
//article ids are numeric
function readArticlesPersCookie()
{
    var arrArticles = readPersonCookie(PERSONCOOKIE_ARTICLES);
    
    if (arrArticles){
         return arrArticles;
    }
    else{
            return undefined;
    }
      
}

//video ids are not numeric
function readVideosPersCookie()
{
    
    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 alert listing ids, otherwise undefined
function readAlertsPersCookie()
{
    var arrResult = readPersonCookie(PERSONCOOKIE_ALERTS);
    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))
    {
         //read from second cookie
         myCookie = readCookie(PERSONCOOKIE2);
    }
    else
    {
        //read from main cookie 
        myCookie = readCookie(PERSONCOOKIE);
    }
    
    
    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; 
        
    }
}

 //========================================================        
 // update or insert a new value intr-o temp cookie
 // 
 //========================================================

function setHomeStyleCookie(objHomestyle, homeStyleIdKeyName, itemIdKeyName){
    document.cookie = stringFormat("TempC=uact=13&{0}={1}&{2}={3};path=/;",homeStyleIdKeyName, objHomestyle.idstyle, itemIdKeyName, objHomestyle.idlisting); 
}

function readTempCookie(key){
    var myCookie;

    //read from main cookie 
    myCookie = readCookie("TempC");
    
    
    if(myCookie)
    {
        var value = "";
        arrTempCookie = myCookie.split('&');
        for (var i = 0; i < arrTempCookie.length; i ++)
        {
            var keyvalue = arrTempCookie[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 = UrlDecode(ca[i]);
		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].toUpperCase() == element.toUpperCase() )
				{
				    return i;
				}
		}
	}
		return -1;
	}
//

function getBookmarkCookieValue() {
    var cookieVal = "";
    var cookieExists = false;
    
    if (document.cookie.length > 0)
    {
        var c_start = document.cookie.indexOf("BMSuccessSuppressCookie=");
        
        if (c_start != -1) {
            cookieExists = true;
            
            var c_end = document.cookie.indexOf(";", c_start);
            
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            
            cookieVal = document.cookie.substring(c_start + "BMSuccessSuppressCookie=".length, c_end);
        }
    }
    
    return cookieVal;
}

function setBookmarkCookieValue(cookieVal) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+365);
    document.cookie = "BMSuccessSuppressCookie=" + Number(cookieVal) + "; expires=" + exdate.toGMTString() + "; path=/";
}

function setRecommendationRequestDeleteCookie(cookieVal) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 365);
    document.cookie = "RecommendationRequestDeleteCookie=" + cookieVal + "; expires=" + exdate.toGMTString() + "; path=/";
}

function setRecommendationResponseDeleteCookie(cookieVal) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 365);
    document.cookie = "RecommendationResponseDeleteCookie=" + cookieVal + "; expires=" + exdate.toGMTString() + "; path=/";
}

function setSearchInteractionCookie(cookieVal) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 1);
    document.cookie = "SearchInteraction=" + encodeURIComponent(cookieVal) + "; expires=" + exdate.toGMTString() + "; path=/";
}

function setDoNotShowRemoveAlert(cookieVal) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 1);
    document.cookie = "DoNotShowRemoveAlertModal=" + encodeURIComponent(cookieVal) + "; expires=" + exdate.toGMTString() + "; path=/";
}

function UrlDecode(str) {
	return decodeURIComponent(str.replace(/\+/g,  " "));
}

function UrlEncode(str) {
	return encodeURIComponent(str);
}

function readCookieByKey(name, key) {
    var myCookie;

    myCookie = readCookie(name);

    if (myCookie) {
        var value = "";
        arrTempCookie = myCookie.split('&');
        for (var i = 0; i < arrTempCookie.length; i++) {
            var keyvalue = arrTempCookie[i].split('=');
            if (key == keyvalue[0]) {
                value = keyvalue[1];
                var arrValues = value.split('|');
                return arrValues;
            }
        }
    }
    else {
        return null;
    }
}


























