// National Curve Bank// California State University, Los Angeles// Greeting Popup// Matthew Nelson// January 2006// 1. Add the following code to the <head>://    <script language="javascript" type="text/javascript" src="greeting_config.js"></script>//    <script language="javascript" type="text/javascript" src="greeting_code.js"></script>// // 2. Add the following onload attribute to the <body> tag://    <body onload="greeting();">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) {      var endstr = document.cookie.indexOf(";", j);      if (endstr == -1)        endstr = document.cookie.length;      return unescape(document.cookie.substring(j, endstr));    }    i = document.cookie.indexOf(" ", i) + 1;    if (i == 0) break;  }  return null;}function setCookie(name, value) {  var argv = setCookie.arguments;  var argc = setCookie.arguments.length;  var expires = (argc > 2) ? argv[2] : null;  var path = (argc > 3) ? argv[3] : null;  var domain = (argc > 4) ? argv[4] : null;  var secure = (argc > 5) ? argv[5] : false;  document.cookie = name + "=" + escape(value) +      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +      ((path == null) ? "" : ("; path=" + path)) +      ((domain == null) ? "" : ("; domain=" + domain)) +      ((secure == true) ? "; secure" : "");}function deleteCookie(name) {  var exp = new Date();  exp.setTime (exp.getTime() - 1);  var cval = getCookie (name);  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();}function display_birthday() {  var now = new Date();  var today = (now.getMonth() + 1) + "/" + now.getDate();  for (i = 0; i + 1 < birthdays.length; i += 2) {    if (today == birthdays[i]) {      var popup = window.open(birthdayPath + "/" + birthdays[i + 1],          windowTitle, birthdayWindowOptions);      popup.focus();      return true;    }  }  return false;}function display_thought(category) {  var category, min, max;  if (category) {    // Look up parameters for selected category    for (i = 0; i + 2 < thoughts.length; i += 3) {      if (category == thoughts[i]) {        min = Math.min(thoughts[i + 2], thoughts[i + 1]);        max = Math.max(thoughts[i + 1], thoughts[i + 2]);      }    }	if (!min || !max) return;  } else {    // Compute the total number of thoughts    var total = 0;    for (i = 0; i + 2 < thoughts.length; i += 3) {      total += Math.abs(thoughts[i + 2] - thoughts[i + 1] + 1);    }    // Choose a random category and look up parameters    category = Math.floor(Math.random() * (total + 1));    for (i = 0; i + 2 < thoughts.length; i += 3) {      total -= Math.abs(thoughts[i + 2] - thoughts[i + 1] + 1);      if (category >= total) {        category = thoughts[i];        min = Math.min(thoughts[i + 2], thoughts[i + 1]);        max = Math.max(thoughts[i + 1], thoughts[i + 2]);        break;      }	}  }  // Load the count cookie, scan for this category, and increment count  var countString = getCookie(thoughtCookieName);  var counts = new Object();  var count = null;  if (countString != null) {    countString = (countString + "").split("&");    for (i = 0; i < countString.length; ++i) {      var pair = countString[i].split("=");      if (pair.length == 2) {        if (pair[0] == category) {          count = (pair[1] * 1) + 1;          if (count > max)            count = min;          counts[pair[0]] = count;        }        else {          counts[pair[0]] = pair[1];        }      }    }  }  if (countString == null || count == null) {    count = min;    counts[category] = count;  }  // Display the chosen quote  var url = thoughtPath + "/" + category + "/" + category + count + "/" + category + count + ".htm";  var popup = window.open(url, windowTitle, thoughtWindowOptions);  popup.focus();  // Reset cookie  countString = new Array();  for (i in counts) {    countString.push(i + "=" + counts[i]);  }  countString = countString.join("&");  var exp = new Date();  exp.setTime(exp.getTime() + thoughtCookieDuration * 86400000);  setCookie(thoughtCookieName, countString, exp);  return true;}function greeting() {  var disp = getCookie(greetingCookieName);  if (disp == null) {    display_birthday();    // display_birthday() || display_thought();    // Set cookie to expire with browser session    var exp = new Date();	exp.setMilliseconds(0);	exp.setSeconds(0);	exp.setMinutes(0);    exp.setHours(0);	exp.setDate(exp.getDate() + 1);    setCookie(greetingCookieName, "1", exp);  }}
