var insitecookie="fresnobee_user_auth";
var threshold=20, 
daystokeep=180;
var loginpage="http://dwb.fresnobee.com/user_registration/login/?goto=";
var aud = new Array;
aud['yahoo.com'] = 'yahoo_user';

// GENERIC COOKIE FUNCTIONS
 
 function getCookieVal(offset) {
  var endstr=document.cookie.indexOf(";",offset);
  if (endstr==-1)
  endstr=document.cookie.length;
  return unescape(document.cookie.substring(offset,endstr));
 }
 
 function GetCookie(cname) {
 var arg=cname+"="; var alen=arg.length;
 var clen=document.cookie.length; var i=0;
 while (i<clen) {
    var j=i+alen;
    if (document.cookie.substring(i,j)==arg) return getCookieVal (j); i=document.cookie.indexOf("",i)+ 1;
    if (i==0) break;
 } return null;
 }
 
 function SaveCookie(cname,cvalue,cdays,cpath) {
        ex = new Date; ex.setTime(ex.getTime() + (cdays*86400000));
        if (cpath == null) { cpath = "/" } else { cpath = "; path=" + cpath }
// Split the hostname into an array so www.example.com => ("www","example","com")
       domain_array = location.hostname.split(".");
// Use the last two elements of the array to get your tier 2 domain
       t1=domain_array.pop();
       t2=domain_array.pop();
       sitedom= "." + t2 + "." + t1;
       document.cookie=cname + '=' + cvalue + '; expires=' + ex.toGMTString() + cpath + "; domain="+sitedom;
}

// Threshold management: This keeps count and redirects to log in
if (GetCookie(insitecookie) && GetCookie('pagecount')) { SaveCookie('pagecount',0,.005,'/')} ;
if (!GetCookie(insitecookie)) {
if (!GetCookie('pagecount')) {
       SaveCookie('pagecount',1,daystokeep,'/');
   } else {
       incval = eval(GetCookie('pagecount')) + 1;
       SaveCookie('pagecount',incval,daystokeep,'/') ;
       if (incval>threshold) location=loginpage+escape(document.location);
   }
}
