/* generic cookie functions shared by software, music, and video */
/* be careful when editing this file */

function checkCookies(cookieNames,nameDelimiter) {
     // split the cookie names by comma
     // and look for existence of each cookie.
     if(cookieNames.length == 0) {
         return false;
     }

     // comma is a separator.
     var cookieNameArray = cookieNames.split(nameDelimiter);
     if(null != cookieNameArray) {
         for(var i=0;i<cookieNameArray.length;i++) {
             if(false == checkCookie(cookieNameArray[i])) {
                 return false;
             }
         }
     }
     return true;
}

 // This function checks if the cookie identified
 // by the name of the cookie exists. If it does
 // exists, it returns true else false.
function checkCookie(cookieName) {
     // customary null check.
     if(null == cookieName || cookieName.length == 0) {
         return false;
     }

     // get cookie data.
     var cookieData = getCookie(cookieName);
     if(null != cookieData) {
         return true;
     }
     return false;
}

 // get cookie data.
function getCookie(cookieName) {
     // customary null check.
     if(null == cookieName || cookieName.length == 0) {
         return null;
     }

     //check if any cookies are on page
     if (document.cookie.length > 0) {
         //look for our cookie
         var begin = document.cookie.indexOf(cookieName+"=");
         if (begin != -1) {
             begin += cookieName.length+1;
             end = document.cookie.indexOf(";", begin);
             if (end == -1) {
                 end = document.cookie.length;
             }

             // here is the cookie information.
             return unescape(document.cookie.substring(begin, end));
         }
     }
     return null;
}

function writeCookies(delimitedCookieNames,
                       delimitedCookieValues,
                       delimiter,
                       expirationDays) {
     // as usual sanity checks.
     if(null == delimitedCookieNames || delimitedCookieNames.length == 0) {
         return false;
     }

     // set default delimiter if its not provided.
     if(null == delimiter || delimiter.length == 0) {
         delimiter = ",";
     }

     // make sure cookie values are provided.
     if(null == delimitedCookieValues || delimitedCookieValues.length == 0) {
         return false;
     }

     // lets split the names and values into an array.
     var cookieNameArray = delimitedCookieNames.split(delimiter);
     var cookieValueArray = delimitedCookieValues.split(delimiter);

     if(null != cookieNameArray && null != cookieNameArray &&
        cookieNameArray.length == cookieValueArray.length) {
         for(var i=0;i<cookieNameArray.length;i++) {
             if(false == writeCookie(cookieNameArray[i],
                                      cookieValueArray[i],
                                      expirationDays)) {
                 return false;
             }
         }
     } else {
         return false;
     }
     return true;
}

// functions to write cookie to browser.
//domain specific
function setCookie(NameOfCookie, value, expiredays) {
   var ExpireDate = new Date ();
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

   document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

//global across all gdl domains
function writeCookie(cookieName,cookieValue,expirationDays) {
     // sanity check make sure cookie name and value are provided.
     if(null == cookieName || cookieName.length == 0) {
         return false;
     }

     // check for cookie value.
     if(null == cookieValue || cookieValue.length == 0) {
         return false;
     }

     // if expiration time is not set, set value of 0. Setting
     // expiration time to 0 makes it a session cookie which gets
     // deleted when the browser is closed.
     if(null == expirationDays) {
         expirationDays = 0;
     }

     var expirationTime = new Date ();
     expirationTime.setTime(expirationTime.getTime() + (expirationDays * 24 * 3600 * 1000));
     document.cookie = cookieName + "=" +
                       escape(cookieValue) +
                       ";domain=download.com" +
                       ";path=/" +
                       ((null == expirationDays || expirationDays == 0) ? "" : ";expires=" + expirationTime.toGMTString());
     return true;
}

function deleteCookies(delimitedCookieNames,delimiter) {
     // ofcourse the sanity check.
     if(null == delimitedCookieNames || delimitedCookieNames.length == 0) {
         return false;
     }

     // set default delimiter if one is not provided.
     if(null == delimiter || delimiter.length == 0) {
         delimiter = ",";
     }

     var cookieNameArray = delimitedCookieNames.split(delimiter);
     for(var i=0;i<cookieNameArray.length;i++) {
         delGDLCookie(cookieNameArray[i]);
     }

     return true;
}

// functions to delete cookie based on a cookie name.
//domain specific
function delCookie (NameOfCookie) {
   if (getCookie(NameOfCookie)) {
      document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

//global across all gdl domains
function delGDLCookie (cookieName) {
     if(getCookie(cookieName)) {
         document.cookie = cookieName + "=" +
                           ";domain=download.com" +
                           ";path=/" +
                           "; expires=Thu, 01-Jan-70 00:00:01 GMT";
     }
     return true;
 }

function removePrefCookies() {
    var cookieNames = 'gdlPrefs,mdl_streaming_bandwidth,mdl_streaming_explicit';
    deleteCookies(cookieNames,',');
}

/* for my downloads component*/

function myDownloadsCookie() {
    //alert('myDownloadsCookie - begin');

    this.count = 0;
    this.pidString = '';
    this.rndToken = null;
    this.mdViewed = 0;

        // if there is a cookie get the info
        var dwnlds = getCookie("dwnlds");
        if (null != dwnlds && '' != dwnlds) {
            var dwnldsCookieValueArray = dwnlds.split('|');
            this.count = dwnldsCookieValueArray.length > 0 ? dwnldsCookieValueArray[0] : this.count;
            this.pidString = dwnldsCookieValueArray.length > 1 ? dwnldsCookieValueArray[1] : this.pidString;
            this.rndToken = dwnldsCookieValueArray.length > 2 ? dwnldsCookieValueArray[2] : this.rndToken;
            this.mdViewed = dwnldsCookieValueArray.length > 3 ? dwnldsCookieValueArray[3] : this.mdViewed;
        }

        //if no rnd identifier, generate a random number
        //todo: handle error case on rand function
        if (this.rndToken == null || this.rndToken == 'null') {
            //alert('myDownloadsCookie - getting random number');
            this.rndToken = Math.floor(Math.random()*101)
        }
}
function setMyDownloadsCookie(myDownloadsCookie) {
    var newCookieValue = myDownloadsCookie.count + "|"
        + myDownloadsCookie.pidString + "|"
        + myDownloadsCookie.rndToken + "|"
        + myDownloadsCookie.mdViewed;

    //alert('setMyDownloadsCookie: ' + newCookieValue);
    setCookie('dwnlds', newCookieValue, 3650);
}

/* moved to /html/js/dl/dl_postdl.js */
/*
function setMyDownloadsCookiePDL(myDownloadsCookie) {
    // get pid out of the oid
        var path = window.location.pathname;
        path = path.replace(/.html/, '');
        var oidElements = path.split('-');
        var pid = null != oidElements[2] ? oidElements[2] : '';

        // if pid in not already in the list, append
        if (myDownloadsCookie.pidString.indexOf(pid) < 0) {

                // prepend the new pid to the existing string
                myDownloadsCookie.pidString = '' != myDownloadsCookie.pidString ? pid + '-' + myDownloadsCookie.pidString : pid;

                // if there are more than max pids, remove oldest pids
                var max = 20;
                var pidArray = myDownloadsCookie.pidString.split('-');
                var pidCount = pidArray.length;
                var newPidString = '';
                if (pidCount > max) {
                    for (var i = 0; i<max; i++) {
                            if (i > 0) {newPidString = newPidString + '-';}
                            newPidString = newPidString + pidArray[i];
                    }
                myDownloadsCookie.pidString = newPidString;
                }

                //increment the count
                myDownloadsCookie.count = parseInt(myDownloadsCookie.count) + 1;

        }

    // set (or reset) the cookie
    setMyDownloadsCookie(myDownloadsCookie);
}
*/

/* should be moved to dl front door .js */
function showMyDownloads(myDownloadsCookie) {
    //if they have 3 or more products, and the rnd number is 1-10
    var pidCount = 0;
    if (myDownloadsCookie.pidString != "" && myDownloadsCookie.pidString != null) {
        var pidArray = myDownloadsCookie.pidString.split('-');
        var pidCount = pidArray.length;
    }
    if ( parseInt(pidCount) > 2 && myDownloadsCookie.rndToken < 11) {
          //alert('showing iframe');
          var iframeSrc = "/3655-20_4-0.html?dlcount=" + myDownloadsCookie.count;
          document.getElementById('myDownloadsIframe').style.width = '753px';
          document.getElementById('myDownloadsIframe').style.height = '164px';
          document.getElementById('myDownloadsIframe').style.backgroundColor = '#ffffff';
          document.getElementById('myDownloadsIframe').style.padding = '7px 0 8px 0';
          document.getElementById('mydlMrgnB').style.marginTop = '180px';
          document.getElementById('mydlMrgnC').style.marginTop = '180px';
          document.getElementById('myDownloadsIframe').innerHTML = '<iframe src="' + iframeSrc + '" name="myDownloadsFrame" frameborder="0" scrolling="no" align="absmiddle" height="173px" width="743px"></iframe>';
          myDownloadsCookie.mdViewed = 1;
    }
    // set (or reset) the cookie
    setMyDownloadsCookie(myDownloadsCookie);
}