// Copyright (c) 2009 by Dognose Productions (Ken Dykes)
// except for:
//    createCookie(), readCookie() and eraseCookie() which are from http://www.quirksmode.org/js/cookies.html

	//	Enumeration lies at the heart of DOM Scripting:
	//	var lists = document.getElementsByTagName('UL');
	//	for (var i = 0; i < lists.length; i++) {
	//	  lists[i].className = 'menu';
	//	}

	//	for (var i = 0; i < array.length; i++) {
	//	  print(array[i]);
	//	}

	//	for (var key in object) {
	//	  print(object[key]);
	//	}

function ForEach(array, fn) {
	document.write('<br>array.length='+array.length+'<br>');
	for(var i=0; i<array.length; i++) {
		document.write('<br>elem#'+i+': '+array[i]);
	};
	document.write('<br>ForEach returns.');
}

	// ** NOTE:
	// cannot use document.write for debugging because it creates, naturally, a new page/document
	// then any 'elements' you try to find or replace in a page cease to exist - no longer on that page!
	//

function ForEachTag(tagname, fn) {
	var elemlist = document.getElementsByTagName(tagname);
	document.write('<br>ForEachTag: tagname='+tagname+'; array.length='+elemlist.length);
	ForEach(elemlist, fn);
}

function EnumWriter(inline, outputdata) {
	if(!outputdata)		// note: empty string "" == null
		outputdata = 'NULL';
	if(inline)
		document.write('<BR>'+outputdata);
	else {
		document.body.appendChild(document.createElement('BR'));
		document.body.appendChild(document.createTextNode(outputdata));
	}
}

function EnumElements(object, title, inline) {
	var n, nn;

	if(title)
		EnumWriter(inline, '<strong>'+title+'</strong>');

	if(!object) {
		EnumWriter(inline, 'EnumElements: error, object is Null');
		return;
	}

	n = 0;
	for(var i in object) {
		if(!hasOwnProperty(i))		// avoid inherited properties
			continue;
		n++;
		EnumWriter(inline, i);
		nn = 0;
		for(var j in i) {
			if(!hasOwnProperty(j))		// avoid inherited properties
				continue;
			nn++;
			EnumWriter(inline, ' &nbsp; >'+j);
			if(nn > 50)
				break;
		}
		if(n > 500)
			return;
	}
	EnumWriter(inline, ' ');	// want a <br> but '' == NULL to javascript
}

// date stuff
function mkDDHHMM() {
	var today = new Date();

	dd = today.getDate();
	if(dd < 10)
		dd = '0'+dd;
	else	dd = ''+dd;		// convert to string
	var hh = today.getHours();
	if(hh < 10)
		hh = '0'+hh;
	else	hh = ''+hh;
	var mm = today.getMinutes();
	if(mm < 10)
		mm = '0'+mm
	else
		mm = ''+mm;

	return(dd+hh+mm);
}

// If you set the number of days to 0 the cookie is trashed when the user closes the browser.
// If you set the days to a negative number the cookie is trashed immediately.

			// added by kgd, 2010-1-3: optional args: domain, path, comment
function createCookie(name,value,days,domain,path,comment) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));	// milliseconds
		var expires = '; expires='+date.toGMTString();
	}
	else var expires = '';
	if(!path)
		path = '/';
	cookstring = name+'='+value+expires+'; path='+path;
	if(domain) {
		if(domain[0] != '.')	// must have leading dot for domains
			domain = '.'+domain;
		cookstring = cookstring+'; domain='+domain;
	}
	if(comment)
		cookstring = cookstring+'; comment='+comment;
	document.cookie = cookstring;
}

// We're going to search for the name of the cookie, followed by an =. So create 
// this new string and put it in nameEQ:
//	var nameEQ = name + '=';

// Then split document.cookie on semicolons. ca becomes an array containing
// all cookies that are set for this domain and path.
//	var ca = document.cookie.split(';');

// Then we go through the array (so through all cookies):
//	for(var i=0;i < ca.length;i++) {
// Set c to the cookie to be checked.
//	var c = ca[i];
// Trim leading blanks, if any.
//	while (c.charAt(0)==' ') c = c.substring(1,c.length);

// Now string c begins with the name of the current cookie. If this is the name of
// the desired cookie
//	if (c.indexOf(nameEQ) == 0)
// we've found what we were looking for. We now only need to return the value 
// of the cookie, which is the part of c that comes after nameEQ. By returning this 
// value we also end the function: mission accomplished.

function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,'',-1);
}
// from rfc2109.txt:
//   Comment=comment
//      Optional.  Because cookies can contain private information about a
//      user, the Cookie attribute allows an origin server to document its
//      intended use of a cookie.  The user can inspect the information to
//      decide whether to initiate or continue a session with this cookie.
//
//   Domain=domain
//      Optional.  The Domain attribute specifies the domain for which the
//      cookie is valid.  An explicitly specified domain must always start
//      with a dot.
//
//   Max-Age=delta-seconds
//      Optional.  The Max-Age attribute defines the lifetime of the
//      cookie, in seconds.  The delta-seconds value is a decimal non-
//      negative integer.  After delta-seconds seconds elapse, the client
//      should discard the cookie.  A value of zero means the cookie
//      should be discarded immediately.
//
//   Path=path
//      Optional.  The Path attribute specifies the subset of URLs to
//      which this cookie applies.
//
//   Secure
//      Optional.  The Secure attribute (with no value) directs the user
//      agent to use only (unspecified) secure means to contact the origin
//      server whenever it sends back this cookie.
//
//      The user agent (possibly under the user's control) may determine
//      what level of security it considers appropriate for "secure"
//      cookies.  The Secure attribute should be considered security
//      advice from the server to the user agent, indicating that it is in
//      the session's interest to protect the cookie contents.
//
//   Version=version
//      Required.  The Version attribute, a decimal integer, identifies to
//      which version of the state management specification the cookie
//      conforms.  For this specification, Version=1 applies.

		// this is a synchronous sleep that will block/halt the browser while it executes
function sleep(milliseconds) {
	var entrytime = new Date().getTime();
	if(milliseconds > 15000)	// limit blocking to 15 seconds max
		milliseconds = 15000;
	while((new Date().getTime() - entrytime) < milliseconds) {
		;		// do nothing
	}
}

