//
//  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('purinamills_popup');
	if (popup == null)
	{
		var now = new Date();
    	var dateStart = new Date(2009, 7, 31);	// Month starts at 0
    	var dateEnd = new Date(2009, 8, 2);		// Month starts at 0

    	var msgStart = dateStart.getTime() + (1000*60*60*16); //2-5-2009 12:00PM CST
    	var msgEnd = dateEnd.getTime() + (1000*60*60*0); //3-10-2009 12:00AM CST
    
    	if (now.getTime() >= msgStart && now.getTime() <= msgEnd)
    	{
      	var strUrl = window.location.href;
      	if (strUrl.toLowerCase().indexOf("/default.aspx") > -1 || strUrl.toLowerCase() == "http://ahdws533.cnxlol.com/purinamills/" || strUrl.toLowerCase() == "http://stage533.landolakes.com/purinamills/" || strUrl.toLowerCase() == "http://www.purinamills.com" || strUrl.toLowerCase() == "http://www.purinamills.com/" || strUrl.toLowerCase() == "http://www.purina-mills.com" || strUrl.toLowerCase() == "http://www.purina-mills.com/" || strUrl.toLowerCase() == "http://purinamills.com" || strUrl.toLowerCase() == "http://purinamills.com/" || strUrl.toLowerCase() == "http://purina-mills.com" || strUrl.toLowerCase() == "http://purina-mills.com/")
        	{
				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='';
					document.layers["FlashMovie"].display='none';
				}
				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='';
					document.all["FlashMovie"].style.display='none';
				}
			}
		}
	}
}

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() + (1 * 60 * 60 * 1000)); // 1 hour from now 
		//expdate.setTime (expdate.getTime() + (1 * 60 * 1000)); // 1 minute from now 
		SetCookie("purinamills_popup", "cookie to hide popup", expdate, "/");
	//}
	//else
	//{
	//	DeleteCookie("purinamills_popup");
	//}
    
	if (document.layers)
	{
		document.layers["Popup"].display='none';
		document.layers["PopupBackground"].display='none';
		document.layers["FlashMovie"].display='';
	}
	else
	{
		document.all["Popup"].style.display='none';
		document.all["PopupBackground"].style.display='none';
      document.all["FlashMovie"].style.display='';
	}
}
