
if (!(rc('MYPORTAL'))) {
	if (rc('edata')) {
			deleteCookie('edata')
		}
	if (rc('xedata')) {
			deleteCookie('xedata')
		}
}


// readCookie
function rc(name) {
  var nameEQ = name + "=",
      ca = document.cookie.split(';'),
      re = /\s*(.*)/,
      i, c, l;
  for(i=0, l=ca.length; i<l; i++) {
    c = ca[i].match(re)[1];
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
// deleteCookie
function dc(name) {
	createCookie(name, '', -1);
}

/**
  Read the MYPORTAL cookie into a convient hash
*/
function basicGetPortalData() {
  var hash = false;
  var keep_vars = ['guid', 'zip', 'gt', 'em'];
  
  var data = (document.cookie || '').match(/\s*MYPORTAL=([^;]*);?/);
  if (data) {
    var hash = {}, pairs = data[1].split('&');
    for (var i=0, l=pairs.length; i<l; i++) {
      var nv = pairs[i].split('=');
      nv[1] = nv[1].replace(/\+/g, ' ');
      hash[nv[0]] = unescape(nv[1]);
    }
    // Store some prefs for future use after MYPORTAL goes the way of the dodo...
    Prefs.bundle(function() {
      keep_vars.each(function(v) {
	  	if(hash[v]) Prefs.set('portal.' + v, hash[v], Prefs.SERVER);
      });
    });
  } else {
    var authed = (document.cookie || '').match(/\s*session@comcast.net=([^;]*);?/);
    if (authed) {
      var hash = {};
      keep_vars.each(function(v) {
        hash[v] = Prefs.get('portal.' + v, '');
      });
    }
  }
  
  return hash; 
}

function getPortalData() {
  var hash = basicGetPortalData();
  return (window.getPortalData = function() { return hash; })();
}

/**
  Deletes the EDATA cookies if the user is logged out
*/
if (!getPortalData()) {
	deleteCookie('edata');
	deleteCookie('xedata');
	deleteCookie('entitlementPageViewCount');
}

/**
  Check if the user is the primary account
*/
function isPrimary() {
  var is_primary = false;
  var req = new Ajax.Request('/user/authkey/checkaccount/',
    {
      method: 'get',
      asynchronous: false
    });
  
  if(req.responseIsSuccess()) {
    try {
      var json = stripComments(req.transport.responseText);
      var data = eval('(' + json + ')');
      is_primary = data.is_primary;
    } catch(e) { }
  }
  
  return (this.isPrimary = function() { return is_primary })();
}


/**
 Retrieve the authkey from the server. The data is loaded once and then cached
*/
function getAuthKey() {
  var authkey = Prefs.get('authkey', '');
  
  if (authkey == '') {
    var req = new Ajax.Request(
      '/user/authkey/',
      {
        method: 'get',
        parameters: Ajax.parameters({ ts: parseInt((new Date).getTime() / 1000)}),
        asynchronous: false
      }
    );
    
    if (req.responseIsSuccess()) {
      try {
        var json = req.transport.responseText.replace('/*', '').replace('*/', '');
        var data = eval('(' + json + ')');
        //return (this.getAuthKey = function() { return data.authkey })();
        authkey = data.authkey;
      }
      catch (e) { /* Do Nothing */ }
    }
  }
  
  if (authkey != '') {
    Prefs.set('authkey', authkey, Prefs.SESSION_COOKIE);
    return (this.getAuthKey = function() { return authkey })();
  }
  
  return '';
}
