// The getCookieVal, GetCookie, SetCookie and DeleteCookie functions were
// written by Bill Dortch and are public domain.

// Any other code on this page is Copyright 2001 by Aaron Ansell and CJ Online
// Feel free to use this game, but keep this copyright notice intact.

// BEGIN 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 (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 SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
// END COOKIE FUNCTIONS

// BEGIN VARIABLES
var startdate = new Date(2001, 06, 23); // 7/23/01 - NOTE: month range is 0-11
var enddate = new Date(2001, 07, 24); // 8/24/01 12:00 am NOTE: month range is 0-11
var odds = 3;	// odds are 1 in 3 that duck will show
// END VARIABLES

// BEGIN FUNCTIONS
function DisableDuck() {
// this function will set a cookie to turn off the duck on a particular page for 24 hours
	var rightnow = new Date();
	var expiry = new Date();
	expiry.setTime( rightnow.getTime() + (24 * 60 * 60 * 1000) );
	SetCookie( 'disabledduck', 'duck is disabled', expiry );
}

function FindDuck() {
var rightnow = new Date();
var url = "http://cjonline.com/duckhunt/find.html?code=" + rightnow.getTime();
var duckhunt = window.open( url, 'duckhunt', 'scrollbars=YES,resizable=yes,height=360,width=285' );
duckhunt.focus;
}
// END FUNCTIONS

// BEGIN DUCK HUNT
var today = new Date();
if ( startdate.getTime() < today.getTime() && today.getTime() < enddate.getTime() ) { 
// displays only between startdate and enddate
	var disabledduck = GetCookie('disabledduck');
	if ( disabledduck == null ) { 
	// displays only if duck hasn't been found in the last 24 hours
		var chance = Math.round( Math.random() * odds );
		if ( chance == 1 ) { 
		// displays only 1 out of \odds\ times
			document.write( "<a href=\"javascript: FindDuck();\" onclick=\"javascript: DisableDuck()\">" );
//			document.write( "<a href=\"/duckhunt/find.html\" onclick=\"javascript: DisableDuck(); var duckhunt = window.open('','duckhunt','scrollbars=YES,resizable=yes,height=360,width=285'); duckhunt.focus();\" target=\"duckhunt\">" );
			document.write( "<IMG SRC=\"http://cjonline.com/duckhunt/find.gif\" width=\"60\" height=\"60\" alt=\"Hunt this duck\" border=\"0\"></a>" );
			document.write( "<a href=\"http://cjonline.com/duckhunt/check.html\" onclick=\"javascript:var duckhunt = window.open('','duckhunt','scrollbars=YES,resizable=yes,height=360,width=285'); duckhunt.focus();\" target=\"duckhunt\">" );
			document.write( "<IMG SRC=\"http://cjonline.com/duckhunt/check.gif\" width=\"60\" height=\"60\" alt=\"Hunt this duck\" border=\"0\"></a>" );
		}
	}
}

// END DUCK HUNT
