//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
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);
}

function SetCookie (name,value,expires,path,domain,secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=.purinamills.com" : "") +
        ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=.uregina.ca" : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

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 getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset);

    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function PopupShow() {
    document.PopupForm.chkPopupHide.checked = false;

    var popup = GetCookie('popup');
    if (popup == null)
    {
        var now = new Date();
    	var dateStart = new Date(2009, 1, 5);	// Month starts at 0
    	var dateEnd = new Date(2009, 3, 15);		// Month starts at 0

    	var msgStart = dateStart.getTime() + (1000*60*60*12); //2-5-2009 12:00PM CST
    	var msgEnd = dateEnd.getTime() + (1000*60*60*0); //4-15-2009 12:00AM CST
    
    	if (now.getTime() >= msgStart && now.getTime() <= msgEnd)
    	{
	    var wHeight = screen.height + 120;
	    var wWidth = screen.width;

	    if (document.layers)	//Non-IE
	    {
	        document.layers["Popup"].display='';
	        
	        //var wHeight = window.innerHeight;
	        //var wWidth = window.innerWidth;
	        
	        document.layers["PopupBackground"].height = wHight;
	        document.layers["PopupBackground"].width = wWidth;
	        document.layers["PopupBackground"].display='';
	    }
	    else	//IE
	    {
	        document.all["Popup"].style.display='';
	       
	        //var wHeight = document.body.offsetHeight;
	        //var wWidth = document.body.offsetWidth;

	        document.all["PopupBackground"].style.height = wHeight;
	        document.all["PopupBackground"].style.width = wWidth;
	        document.all["PopupBackground"].style.display='';
	    }
        }
    }
}

function PopupClose() {
    if (document.PopupForm.chkPopupHide.checked)
    {
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
	//expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
	expdate.setTime (expdate.getTime() + (1 * 60 * 1000)); // 24 hrs from now 
    	SetCookie("popup", "cookie to hide popup", expdate, "/");
    }
    else
    {
    	DeleteCookie("popup");
    }
    
    if (document.layers)
    {
        document.layers["Popup"].display='none';
        document.layers["PopupBackground"].display='none';
    }
    else
    {
        document.all["Popup"].style.display='none';
	document.all["PopupBackground"].style.display='none';
    }
}
