// "Internal" function to return the decoded value of a cookie
function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//  Function to return the value of the cookie specified by "name".
//  name - String object containing the cookie name.
//  returns - String object containing the cookie value, or null if
//  the cookie does not exist.

function GetCookie(name) {
  var arg = name + "=";
  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 to correct for 2.x Mac date bug.
//  IMPORTANT:  This function should only be
//  called *once* for any given date object!
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

// Set a cookie
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// Check to see if user accepts cookies (for daily poll) warn if no
function PollCheckCookie ( pdate ) {
	var expdate = new Date ();

	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!

	// get some of the date objects
        var yr = expdate.getYear();
        var mon = expdate.getMonth();
        var day = expdate.getDate();

	// set other parts to "end-of-day"
        hr=23;
        min=59;
        sec=59;

	// check to see if the poll is current
	var curr_date=0;
        curr_date += (mon+1)*10000;
        curr_date += day*100;
        curr_date += yr%100;

        var int_pdate=parseInt(pdate,10);
        if(int_pdate != curr_date) {
                alert("We're sorry...The polling period for this topic has expired and the final votes are being counted. Your page will now be refreshed to display the poll for the day's new topic. If it's just after midnight, please check back later this morning for the new topic.");
                window.location.reload(true);
                return false;
        }

        // set up the expiration date of the real cookie
        var expdate2 = new Date (yr,mon,day,hr,min,sec);

	expdate.setTime (expdate.getTime() + (60 * 1000)); // 1 minute from now
	SetCookie('WN_test_poll','1',expdate,'/','.att.net');

	// After you've set it, try to read it
	if (GetCookie('WN_test_poll') != "1") {
		alert("Sorry!  You've chosen not to accept cookies and therefore we can't register your vote.");
		return false;
	}
	else {
                SetCookie('POLL','TAKEN',expdate2,'/','.att.net');
        }
}

