//
//  Cookie Functions - Second Helping  (21-Jan-96)
//  Written by:  Bill Dortch, hIdaho Design <bdortch@netw.com>
//  The following functions are released to the public domain.
//
//  The Second Helping version of the cookie functions dispenses with
//  my encode and decode functions, in favor of JavaScript's new built-in
//  escape and unescape functions, which do more complete encoding, and
//  which are probably much faster.
//
//  The new version also extends the SetCookie function, though in
//  a backward-compatible manner, so if you used the First Helping of
//  cookie functions as they were written, you will not need to change any
//  code, unless you want to take advantage of the new capabilities.
//
//  The following changes were made to SetCookie:
//
//  1.  The expires parameter is now optional - that is, you can omit
//      it instead of passing it null to expire the cookie at the end
//      of the current session.
//
//  2.  An optional path parameter has been added.
//
//  3.  An optional domain parameter has been added.
//
//  4.  An optional secure parameter has been added.
//
//  For information on the significance of these parameters, and
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.php    
//
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
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 to create or update a cookie.
//    name - String object object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
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)) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to current date/time)
//    name - String object containing the cookie name
//
function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

//////////////////////////
// end cookie functions //
//////////////////////////

function makeArray(n) {					// creates a blank array
	this.length = n;
	return this;
}

var totalQuotes = 13;					// total number of quotes in list below minus 1
var allQuotes = new makeArray(totalQuotes);		// create array for quotes
var today = new Date()					// variable for cookie expiration date
var time = today.getTime()
today.setTime(time + 365.24 * 24 * 60 * 60 * 1000)	// calculate time for one year from today for cookie expiration
var exp = today.toGMTString()				// store cookie expiration date

// the quotes, add as many as you like, however you must update the totalQuotes variable above
allQuotes[0]="Nationwide, consumer prices rose 3.4 percent in 2005, with 40 percent of the increase blamed on the biggest jump in energy costs since 1990, according to the Labor Department.<br><br><br><b>Seattle Post - Intelligencer, January 19, 2006</b>"
allQuotes[1]="The era of low and fairly stable energy prices appears to have ended.<br><br><br><br><br><br><b>The Providence Journal, January 25, 2006</b>"
allQuotes[2]="Analysts are expecting moderate inflation for 2006 but they caution that a lot will depend on whether energy prices retreat or whether those costs keep rising and start to spill out to other parts of the economy.<br><br><b>AP Business, January 18, 2006</b>"
allQuotes[3]="-the future of natural gas pricing could be a trans-Atlantic tug of war...<br><br><br><b>New York Times, March 14, 2006</b>"
allQuotes[4]="The longer we wait to make (energy) choices and implement solutions, the fewer options we will have in the future. <br><br><br><b>Burlington Free Press, February 23, 2006</b>"
allQuotes[5]="Not taking action to reduce energy expenditures, develop new energy efficient technologies and provide incentives for industry and consumers to cut energy costs is proving extremely costly and making our future less secure.<br><br><br><b>San Francisco Chronicle, February 21, 2006</b>"
allQuotes[6]="With rising energy prices squeezing profits, corporate managers are looking for alternatives. And there are signs that conservation measures - even those that don't cost of a lot of money - are beginning to pay off.<br><br><br><br><b>CNBC MSNBC, March 29, 2006</b>"
allQuotes[7]="Some 65 percent of U.S. companies think that escalating energy prices pose a potential roadblock to their company's growth over the next 12 months.<br><br><br><br><br><b>Price Waterhouse Coopers MSNBC, March 29, 2006</b>"
allQuotes[8]="For many local businesses, the potential for energy savings comes to a screeching halt at those basic elements of production: large expensive machines.<br><br><br><br><b>Newsday, March 21, 2006</b>"
allQuotes[9]="Small businesses have to be more energy-efficient. If you don't, you're wasting money.<br><br><br><br><br><br><b>Richard Kessel, Long Island Power Authority</b><br><b>Newsday, March 21, 2006</b>"
allQuotes[10]="Electric-rate sticker shock seen<br><br><br><br><br><br><br><br><b>John G. Edwards Las Vegas Review Journal, April 13, 2006,</b>"
allQuotes[11]="High energy prices are not temporary, they are the new reality.<br><br><br><br><br><br><br><b>American Council for an Energy Efficient Economy, April 13, 2006</b>"
allQuotes[12]="Forty percent (of economists polled) said higher energy prices are the No. 1 risk for the economy.<br><br><b>USA TODAY Survey, April 2006</b>"
allQuotes[13]="The potential savings are huge. Industrial use accounts for about a third of energy consumed in the U.S., according to the Dept. of Energy estimates. And by cutting back on just 20 percent of that consumption, American businesses could save close to $19 billion a year at 2004 prices.<br><b>National Association of Manufacturers MSNBC, March 29, 2006</b>"

function rotateQuotes() {						// them main function
	usedQuotes = GetCookie("NetbyMattQuotes");			// get cookie of used quotes

	if (usedQuotes == null) {usedQuotes = "0";}			// create cookie if it does not exist

	var totalUsed = 0;						// variable to count total number of used quotes

	for (i = 0; i <= totalQuotes; i++)				// loop through usedQuotes string
	{
		if (usedQuotes.charAt(i) == "1") {totalUsed++;}		// increment totalUsed if used quote found
	}

	if (totalUsed > totalQuotes - 10) {usedQuotes = "0";}		// compare total used to total quotes (subtract 10 to keep recursion low on next loop)

	if (usedQuotes.length != totalQuotes + 1)			// check for proper length of cookie
	{
		while (usedQuotes.length <= totalQuotes)		// adjust length of cookie
		{
			usedQuotes += "0";				// add unused quote to cookie
		}
	}
	quoteNum = Math.round(Math.random() * totalQuotes);		// choose random number for quote
	
	while (usedQuotes.charAt(quoteNum) == "1")			// check random number against usedQuotes
	{
		quoteNum = Math.round(Math.random() * totalQuotes);	// choose new quote if already used
	}

	usedQuotes = usedQuotes.substring(0, quoteNum) + "1" + usedQuotes.substring(quoteNum + 1, usedQuotes.length);	// add used quote to cookie
	
	SetCookie("NetbyMattQuotes", usedQuotes, exp);			// store cookie

	if (navigator.appVersion.indexOf("MSIE")!=-1) {			// check for IE or Netscape and update <span> tag accordingly
		document.all.Quote.innerHTML = allQuotes[quoteNum];
	} else {
		quote = document.getElementById("Quote");
		quote.innerHTML = allQuotes[quoteNum];
	}
	setTimeout("rotateQuotes()", 15000);				// call rotateQuotes() in 10 seconds
}

document.onLoad = rotateQuotes();					// start rotating quotes when document loads
