
//  JavaScript functions to support the survey.
//	The survey pops up when the use clicks on a nav bar link.
//  But after they see it once, we don't want it to pop up 
//	over and over, so these functions set a cookie if user has 
//	seen the popup survey - if not it shows them the survey
//  
//	send_user_to_page takes 2 urls as arguments: 
//										1. survey.cgi?...&...&...,  
//										2. the default, where the link would usually go
function send_user_to_page( survey, no_survey, target ) {
	if( readCookie() ) {	// Cookie has been set - send them to the right page
		if(target == "blank") {	
			window.open(no_survey,"","","","","");// optional new window
		} else if (target == "top") {
			top.location.replace(no_survey);		// replaces frameset with no-frames window
		} else {
			top.location=no_survey;		// main frame
		}
	} else {				// Cookie has not been set - send them to the right page
		setCookie(); 		// Set a cookie so user doesn't have the survey open twice
		window.open(survey,"","width=400,toolbar=0,height=310,scrollbars=1");	// always a new window for survey
		if(target == "blank") {	
			window.open(no_survey,"","","","","");// optional new window
		} else {
			top.location.replace(no_survey);		// replaces frameset with no-frames window
	//	} else {
		//	parent.frames[2].location=no_survey;		// main frame
		//}
		}
	}
}

function readCookie() { 
    var the_cookie = document.cookie;
    var the_cookie = unescape(the_cookie);
    var broken_cookie = the_cookie.split(":");
    if(broken_cookie[1]) {
		return true;
	} else {
		return false;
	}    
}

function WM_readCookie(name) {
  // if there's no cookie, return false else get the value and return it
  if(document.cookie == '') return false;
  else return unescape(WM_getCookieValue(name));
}

function WM_getCookieValue(name) {
  // Declare variables.
  var firstChar, lastChar;

  // Get the entire cookie string. (This may have other name=value pairs in it.)
  var theBigCookie = document.cookie;

  // Grab just this cookie from theBigCookie string.

  // Find the start of 'name'.
  firstChar = theBigCookie.indexOf(name);

  // If you found it,
  if(firstChar != -1)
{
  // skip 'name' and '='.
  firstChar += name.length + 1;

// Find the end of the value string (i.e. the next ';').
    lastChar = theBigCookie.indexOf(';', firstChar);

    if(lastChar == -1) lastChar = theBigCookie.length;

    // Return the value.
    return theBigCookie.substring(firstChar, lastChar);

  } else {
    // If there was no cookie, return false.
    return false;
  }
}

function setCookie()
{
	var has_taken_survey = true;
	var the_cookie = "wm_javascript=" + escape("username:" + has_taken_survey);
	var the_date = new Date("December 31, 2050");
    var the_cookie_date = the_date.toGMTString();
	the_cookie = the_cookie + ";expires=" + the_cookie_date;
	document.cookie = the_cookie;
}

