var isOwner = false;
function CheckCookie (CookieName) {
    var lf = "\n";
    var CookieString = null;
    if ( cstr == null )
        CookieString = document.cookie;
    else
        CookieString = cstr;
    var CookieSet = CookieString.split (';');
    var SetSize = CookieSet.length;
    var CookiePieces
    var ReturnValue = "";
    var x = 0;
    for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {
      CookiePieces = CookieSet[x].split ('=');
      if (CookiePieces[0].substring (0,1) == ' ') {
        CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
      }
      if (CookiePieces[0] == CookieName) {
        ReturnValue = CookiePieces[1];
      }
     }
     return ReturnValue;
 }

function SetCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function writeTopbar()
{      
	if ( ! auth_isLoggedIn() )
    {
		document.write('<span class="tright trwide"><a href="/account/signup">Register for Free</a> | <a href="/account/signup">Subscribe</a> | <a href="/helpcenter/login/">Login Problems?</a></span><form id="loginform" class="login" action="/account/login" method="post"><strong>Log In:</strong> <input style="display: none;" type="hidden" name="action" value="login" /><input style="display: none;" type="hidden" name="qref" value="'+document.location.pathname+'" /><input type="text" class="text" id="email" name="email" value="'+auth_getMemEmail()+'" title="Your e-mail here" /> <input onkeypress="check_caps(this,event);" class="text" id="password" type="password" name="password" title="Your password here" /> <input name="imageField" class="imgbutton" type="image" src="/images/buttons/login.png" width="43" height="21" border="0" /></form>');
	}
    else
    {
		document.write('<span class="tleft" style="left:12px;"><strong>Hi <a href="/'+auth_getMinisitePath()+'/'+auth_getUserName()+'">'+auth_getUserName()+'</a>!</strong> | <a href="javascript:void(0)" id="mytsrview">My TSR</a> | <a href="javascript:void(0)" id="userview" title="Show/Hide your Dashboard">Dashboard</a>');
		if ( auth_isSubscriber())
		{     
			document.write(' | <a href="/mytsr/subscription/pay">Renew Subscription</a> | <a href="/account/logout">Log Out</a></span>');
            document.write('<span class="tright"><a href="http://forums.thesimsresource.com/private.php" class="pm">' + auth_getPM() + '</a><a href="/'+auth_getMinisitePath()+'/'+auth_getUserName()+'/guestbook/view/" class="guestbook">' + auth_getGuestbook() + '</a><a href="/downloads/download-basket" id="basket" class="basket">'+auth_getBasketCnt()+'</a><a href="/mytsr/account/status" class="days">'+(auth_getDaysLeft() > 0 ? auth_getDaysLeft()-1 : 0)+'</a><a href="/kudos/score" class="kudos">' + auth_getKudos() + '</a></span>');
		}
        else
        {
			document.write(' | <a href="/account/logout">Log Out</a></span><span class="tright"><a href="/mytsr/subscription/pay" class="days">Subscribe Now!</a><a href="http://forums.thesimsresource.com/private.php" class="pm">' + auth_getPM() + '</a><a href="/'+auth_getMinisitePath()+'/'+auth_getUserName()+'/guestbook/view/" class="guestbook">' + auth_getGuestbook() + '</a><a href="/kudos/score" class="kudos">' + auth_getKudos() + '</a></span>');
		}
	}
}

function auth_getGuestbook()
{
	try
	{
		if(CheckCookie('pmandguestbookinfo')=="") return("n/a");
		var data = CheckCookie('pmandguestbookinfo').split("%2C");
		return(data[1]);
	}
	catch(e)
	{
		return("n/a");
	}
}

function auth_getPM()
{
	try
	{
		if(CheckCookie('pmandguestbookinfo')=="") return("n/a");
		var data = CheckCookie('pmandguestbookinfo').split("%2C");
		return(data[0]);
	}
	catch(e)
	{
		return("n/a");
	}
}
/*
function auth_getProfileLink() {
    var faname = getSessionVar('faname');
    if(faname)
        return("/artists/" + faname);
    return("/profiles/view.php");
}
*/
function auth_getTranslatorLocale() {
    return getSessionVar('tloc');
}
function auth_isRegistered() {
    return ( CheckCookie('MemEmail') > 0 );    
}
function auth_isLoggedIn() {
    return ( getSessionVar('MemberID') > 0 );
}
function auth_isAdmin(section) {
    return ( CheckCookie('admc') > 0);
}
function auth_isModerator(section) {
    return ( getSessionVar(section+'_moderator') == 'true');
}
function auth_getMemberID() {
    return getSessionVar('MemberID');
}
function auth_getKudos() {
    return getSessionVar('kudos');
}
/*
function auth_kudosItemUnlocked(item) {
    var itms = getSessionVar('KudosItems');
    if(!itms) return(false);
    var items = itms.split(',');
    for(var i=0;i<items.length;i++) if(items[i]==item) return(true);
    return(false);
}
*/
function auth_getMemEmail() {
    return unescape(CheckCookie('MemEmail'));
}
function auth_getUserName() {
    var r = new RegExp(/\+/g);
    var uname = getSessionVar('uname');
    if(uname != null) uname = uname.replace(r," ");
    else uname = "";
    return unescape(uname);
}
function auth_getMinisitePath() {
    var r = new RegExp(/\+/g);
    var path = getSessionVar('minisitepath');
    if(path != null) path = path.replace(r," ");
    else path = "";
    return unescape(path);
}
function auth_getUnratedItems() {
    return getSessionVar("UnratedItems");    
}
/*
function auth_getPM() {
    return (getSessionVar("forummsgs",600) < 1 || getSessionVar("forummsgs",600) == null) ? "0" : getSessionVar("forummsgs",600);    
}*/
function auth_getUnreadGuestbook() {
    return (getSessionVar("guestbookmsgs",600) < 1) ? "0" : getSessionVar("guestbookmsgs",600);
}
function auth_getDaysLeft() {
    //if(getSessionVar("BETA_TESTER")=="1") return(1);
    return getSessionVar('daysLeft');
}
function auth_getBasketCnt() {
    return getSessionVar('dlbasket_cnt');    
}
function auth_isFeaturedArtist() {
    return(getSessionVar('isFeaturedArtist')>0);    
}
function auth_hasCreations() {
    return(getSessionVar('hasCreations')>0);    
}
function auth_isSubscriber(free) {
    if(!auth_isLoggedIn()) return(false);
    //if(auth_isBetaTester()) return(true);
    if(auth_isFeaturedArtist()) return(true);
    return ( auth_isLoggedIn() && getSessionVar('daysLeft') > 0 );
}
/*
function auth_isBetaTester() {
    return(getSessionVar("BETA_TESTER")=="1");
}
*/
/*
function auth_canDownload(free) {
    return ( free || auth_isSubscriber(free));    
}
*/
function auth_canUseWizard(free) {
    return auth_isSubscriber();
}
function auth_sims2packDownload(free) {
    return ( getSessionVar('AltFileFormat') > 0  ); // && (auth_isSubscriber()||auth_isTryout())
}
function auth_wizardEnabled(free) {
    return ( getSessionVar('EnableWizard') > 0 );
}
function auth_canUseBasket(free) {
    return auth_isSubscriber();
}
function getSessionVar(name,timeout) {
    var t = timeout==null?0:timeout;
    var cookieName = "TsrVal_" + t;
    var val = unescape(CheckCookie(cookieName));
    try {
        var obj = eval('(' + val + ')');
    } catch(e) { return(null); }
    if(!obj[name]) return(null);
    return(obj[name]);
}
function getExpansionPacks(gamemask)
{
 	 return ( getSessionVar('ExpansionMasks') );
}