function setCookie(name,value) {
	var domain = location.hostname.split('.');
	domain[0] = '';
	domain = domain.join('.');
	document.cookie = name + "=" + encodeURIComponent(value) + ";domain=" + domain + ";path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = 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;
}

if (hearst_user.logged_in) {
    // services.hearstmags logged in

    // This check is done incase mag_user differs from hearst_user.  Used to default to mag_user.
    // If local site's cookie's user_name is different from user_name found in services.hearstmags,
    // lets log them out of local site ONLY, and the redirect to clearandgo should take care of loggin in to local site
    // using hearst_user!
    var cookie_user_name = getCookie('user_name');
    if( cookie_user_name && cookie_user_name != hearst_user.user_name ) {
         var next_url = window.location;
         window.location.href = '/registration/clearandgo?next_url=' + escape(next_url);
    }

    // now set local site fSpace cookies
   if (!getCookie('fSpaceSSOUserId')) {
      setCookie('fSpaceSSOUserId',hearst_user.user_name);
      setCookie('fSpaceSSOUserEmail',hearst_user.email);
      setCookie('fSpaceSSOUserCheck',hearst_user.encString);
      setCookie('fSpaceSSOExpires',hearst_user.expires);
      window.location.reload(true); // this re-renders page, next getMyHash call will pick up cookies
   }
}

var hearst_user_logged_in = 0;
if (hearst_user.logged_in) {
  hearst_user_logged_in = 1;
}
