var esvr = 'http://comm.newspress.com/npcommerce/';
var WM_acceptsCookies = false;

if(document.cookie == '') {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	WM_acceptsCookies = true; 
    }// If it succeeds, set variable
} else { // there was already a cookie
  WM_acceptsCookies = true;
}

//This version has a bug if one cookie's name is a subset of another cookie's name
function WM_readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // WM_readCookie

function myunescape (str)
{
	str = '' + str;
	while (true)
	{
		var i = str . indexOf ('+');
		if (i < 0)
			break;
		str = str . substring (0, i) + ' ' + str . substring (i + 1, str . length);
	}
	return unescape (str);
}


function args_init ()
{
	args = new Array ();
	var argstring = window . location . search;
	if (argstring . charAt (0) != '?')
		return;
	argstring = argstring . substring (1, argstring . length);
	argarray = argstring . split ('&');
	var i;
	var singlearg;
	for (i = 0; i < argarray . length; ++ i)
	{
		//document.write(' loop '+ i);
                singlearg = argarray [i] . split ('=');
		if (singlearg . length != 2)
			continue;
		var key = myunescape (singlearg [0]);
		var value = myunescape (singlearg [1]);
		//document.write(' key '+ key + ' value ' + value);
		args [key] = value;
	}
}

function user_check_subscriber()
{
	var emp = WM_readCookie('NPEmployee');
	if ((emp == 'true')  || (emp == '1')) return;
	var psub = WM_readCookie('NPPrintSubscriber');
	if ((psub == 'true')  || (psub == '1')) return;
	var esub = WM_readCookie('NPElectronicSubscriber');
	if ((esub == 'true')  || (esub == '1')) return;
	args_init();
	if (typeof args["tref"] != 'undefined') return;

	var reg = WM_readCookie('NPRegistered');	
	if ((reg == 'true')  || (reg == '1'))
		location.replace(esvr+'subscribeForm.htf?hst='+location.host+'&fwd='+location.pathname+'&esub='+esub+'&psub='+psub+'&from=cksub&v=11');
          else
		location.replace(esvr+'registerForm.htf?hst='+location.host+'&fwd='+location.pathname);
}

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function format_date(dt) {
    return (dt.getMonth()+1)+'/'+dt.getDate()+'/'+dt.getYear();
}

function good_domain() {
    if (location.host == 'www.sbcoast.com')
       {
	location.replace('http://www.newspress.com'+location.pathname);
        return false;
        }
    else
       return true; 

}

function update_check()
{
   var ecRandom = Math.round(10000*Math.random()) + 1;
   var here = 'ok';
   var npup = WM_readCookie('NPUpdate');
   var skip = (new Date() < new Date(npup));
   if (skip) return true;
   args_init();
   if (typeof args["nref"] != 'undefined') return true;
   if (typeof args["tref"] != 'undefined') return true;
   here = 'needs';
   var t1= format_date(new Date());
   location.replace(esvr+'updateUser.htf?hst='+location.host+'&fwd='+location.pathname+'&cook='+WM_acceptsCookies+'&from=cksub&v=11'+'&h='+here+'&cookie='+document.cookie+'&t='+t1+'&u='+npup+'&r='+ecRandom);
   return false;
}

if (good_domain() && update_check())
  user_check_subscriber();




