/*
WM_setCookie(), WM_readCookie(), WM_killCookie()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: nadav@wired.com
*/


// This next little bit of code tests whether the user accepts cookies.
var WM_acceptsCookies = false;
if(document.cookie == '') {

    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.

    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {

	WM_acceptsCookies = true; 

    }// If it succeeds, set variable

} else { // there was already a cookie

  WM_acceptsCookies = true;

}

function WM_setCookie (name, value, hours, path, domain, secure) {

    if (WM_acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.

	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // WM_setCookie


function WM_readCookie(name) {

    if(document.cookie == '') { // there's no cookie, so go no further

	return false; 

    } else { // there is a cookie

	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // WM_readCookie

function WM_killCookie(name, path, domain) {

  var theValue = WM_readCookie(name); // We need the value to kill the cookie

  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }

} // WM_killCookie

// rest of Javascript here is by Karen Chapdelaine

function getReferrer() {   
    return document.referrer
}

var WM_hasCookie = WM_readCookie('refererall'); 

var SecondVisit = false;

    if (WM_hasCookie) {

        SecondVisit = true;

    } else {

        setReferral()
    }

function setReferral() {

SecondVisit = false;

var MyUrl = getReferrer();

WM_setCookie('refererall', MyUrl, 1, '/', '.craftmatic.com'); 

}

// javascript code for popups

var WM_hasCookie = WM_readCookie('NoPopup'); 

var NoPopup1 = false;
//alert(NoPopup1);
    if (WM_hasCookie) {

        NoPopup1 = true;

    }

function setPopup() {

NoPopup1 = true;

// below sets a cookie for 720 hours, i.e., 30 days, for craftmatic.com
// with a value of Nopopup = true.  

WM_setCookie('NoPopup', NoPopup1, 720, '/', '.craftmatic.com'); 

}


function checkPopup() {

homeUrl = "craftmatic.com";
lastUrl = document.referrer;

  //if ( lastUrl.indexOf(homeUrl) != -1 ) {   
   // NoPopup1 = true;
  //}

runPopup()

}

function runPopup() {

   if (NoPopup1 != true)  {

// You will probably want to change the window size, etc., below, but do so 
// with care.  DO NOT remove the single quotes around the scrollbars...thru
// height=300, as these are passed directly to the window and properly 
// interpreted there, but are not valid values for direct interpretation 
// by window.open.  Instead, change and/or remove and/or replace values 
// in this section while treating it as a separate string.

window.open('http://www.craftmatic.com/html/US/contest.html','','scrollbars=yes,status=yes,resize=yes,width=550,height=500');
       
  }

}

function stopPop() {
NoPopup1 = true;
}
