var undefined;
var CurrentMenuSelectorID;
var CurrentMenuFormID;

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
		try {
			oldonload();
		} catch(err) {/*do nothing it's just IE being the worst browser in the world*/}
      }
      func();
    }
  }
}

function addDOMLoadEvent(func) {
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", func, false);
	} else {
		addLoadEvent(func);
	}
}

function ActivateFlash(id)
{
	var object = document.getElementById(id);
	if (navigator.appName.indexOf("MSIE") != -1) {
		if (object) object.outerHTML = object.outerHTML;
	}
}

try {
	if (typeof(Node)!="undefined") {
		// try to extend the DOM definition of Element to include an insertAfter method
		Node.prototype.insertAfter = function (node, referenceNode) {
			var n = referenceNode.nextSibling;
			if (n) return this.insertBefore(node, n);
			else return this.appendChild(node);
		}
		Node.prototype.getChildrenByTagName = function (tagName) {
			var listing = this.getElementsByTagName(tagName);
			var retval = new Array();
			var x;
			for (x=0;x<listing.length;x++) {
				if (listing[x].parentNode == this) {
					retval.push(listing[x]);
				}
			}
			return retval;
		}
	}
} finally {
	// no matter what, define a helper function that can do the same
	function insertAfter(parent, node, referenceNode) {
		var n = referenceNode.nextSibling;
		if (n) return parent.insertBefore(node, n);
		else return parent.appendChild(node);
	}
	function getChildrenByTagName(parent, tagName) {
		var listing = parent.getElementsByTagName(tagName);
		var retval = new Array();
		var x;
		for (x=0;x<listing.length;x++) {
			if (listing[x].parentNode == parent) {
				retval.push(listing[x]);
			}
		}
		return retval;
	}
}
		
function PrintStyleLinks(el) {
	var a = el.getElementsByTagName("A");
	var x;
	var href;
	for (x=0;x<a.length;x++) {
		if (a[x].href) {
			var t = document.createElement("SPAN");
			t.className = "PrintOnly";
			href = a[x].href+"";
			t.innerHTML = " ["+href.replace(/\//g, "/ ")+"] ";
			// try to do this the DOM style way
			if (a[x].parentNode.insertAfter) a[x].parentNode.insertAfter(t, a[x]);
			// if that's not avialable, use the helper function
			else insertAfter(a[x].parentNode, t, a[x]);
		}
	}
}
		

function BuildPrintFooter() {
	var d = new Date();
	var protocol = "http";
	
	if (("https:" == document.location.protocol)) {
		// return false;
		protocol = "https";
	}

	var p = document.createElement("P");
	p.appendChild(document.createTextNode("The following page was generated on "+d.toLocaleString()));
	p.appendChild(document.createElement("BR"));

	var b = document.createElement("B");
	b.appendChild(document.createTextNode("Title: "));
	p.appendChild(b);
	p.appendChild(document.createTextNode(document.title));
	p.appendChild(document.createElement("BR"));

	b = document.createElement("B");
	b.appendChild(document.createTextNode("URL: "));
	p.appendChild(b);
	p.appendChild(document.createTextNode(document.location));
	p.appendChild(document.createElement("BR"));
	p.appendChild(document.createElement("BR"));
	
	b = document.createElement("B");
	b.appendChild(document.createTextNode("What's that image?"));
	p.appendChild(b);
	p.appendChild(document.createTextNode("  It's a QR (Quick Response) code.  Take a picture of the QR code with your smart phone and view a page on your phone's Web browser instead of re-typing the URL."));
	
	var i = document.createElement("IMG");
	i.src = protocol+"://chart.apis.google.com/chart?cht=qr&chl="+escape(document.location)+"&chs=160x160";
	p.insertBefore(i, p.firstChild);
	
	p.id = "PrintInfoBox";
	p.className="PrintOnly";
	
	var body = document.getElementsByTagName("BODY");
	if (body) {
		body[0].appendChild(p);
	}
}

		function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }

        function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get
                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }

		function InStr(strSearch, charSearchFor)
		/*
		InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
		was found in the string str.  (If the character is not found, -1 is returned.)
		Requires use of:
			Mid function
			Len function
		*/
		{  
			for (i=0; i < String(strSearch).length; i++)
			{
				if (charSearchFor == Mid(strSearch, i,  String(charSearchFor).length))
				{
					return i;
				}
			}
			return -1;
		}

		function GetURLToken(url, token) {
			retval = "";
			startpos = InStr(url, token+"=");
			if (startpos==-1) return null;
			startpos += String(token+"=").length;
			endpos = startpos;
			while ((url.charAt(endpos)!='&')&&(url.charAt(endpos)!='#')&&(endpos<url.length)) {
				retval = retval+url.charAt(endpos);
				endpos++;
			}
			return retval;
		}
		
		
/* ***********************************************
	AJAX FUNCTIONS
   *********************************************** */
   
function GetHTTPObject(){
	var xmlhttp;
	if (window.ActiveXObject)
	{
		xmlhttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;
		return xmlhttp;
	}
	// code for Mozilla, etc.
	else if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = null;
		}
		return xmlhttp;
	} else {
		//alert('Your browser cannot handle this script');
		return null;
	}
}

function AJAXGenericLoad(TargetObject, URL) {
	if (TargetObject && URL) {
		var xmlhttp = GetHTTPObject();
		xmlhttp.open("GET", URL);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				TargetObject.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
}

/* ***********************************************
    MENU ITEM EDITING FUNCTIONS
   *********************************************** */
   
function CMSClearMenu()
{
	var menudiv = document.getElementById("Menu");
	if (menudiv) {
		menudiv.innerHTML = "";
	}
}

function CMSMenuAddToTop(url)
{
	var menudiv = document.getElementById("Menu");
	if (menudiv && url) {
		var tempdiv = document.createElement("DIV");
		if (menudiv.firstChild) {
			menudiv.insertBefore(tempdiv, menudiv.firstChild);
		} else {
			menudiv.appendChild(tempdiv);
		}
		AJAXGenericLoad(tempdiv, url);
	}
}

function CMSMenuAddToBottom(url)
{
	var menudiv = document.getElementById("Menu");
	if (menudiv && url) {
		var tempdiv = document.createElement("DIV");
		menudiv.appendChild(tempdiv);
		AJAXGenericLoad(tempdiv, url);
	}
}

function CMSMenuLoginForm()
{
	CMSMenuAddToTop("/php/loginform.php");
}

function popUp(URL, w, h) {
	winProp = 'width='+w+',height='+h+',toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0';
	day = new Date();
	id = day.getTime();
	//eval("page" + id + " = window.open(URL, '" + id + "', winProp);");
	eval("page" + id + " = window.open(URL, 'pwin', winProp);");
}

/* ****************************************
   DETECTION FUNCTIONS (move to resources.js later)
   **************************************** */
   
var cssTransitionsSupported = false;
(function() {
    var div = document.createElement('div');
    div.innerHTML = '<div style="-webkit-transition:color 1s linear;-moz-transition:color 1s linear;"></div>';
    cssTransitionsSupported = (div.firstChild.style.webkitTransition !== undefined) || (div.firstChild.style.MozTransition !== undefined);
    delete div;
})();
   
function supports_video() {
	// firefox mobile can play Ogg on all devices, so that's a yes regardless of platform
	if (navigator.userAgent.indexOf("Fennec") >= 0) return true;
	// webOS, iPad, iPhone, and Android can't auto-play, so that's a no
	if (navigator.userAgent.indexOf("webOS") >= 0) return false;
	if (navigator.userAgent.indexOf("hpwOS") >= 0) return false;
	if (navigator.userAgent.indexOf("iPad") >= 0) return false;
	if (navigator.userAgent.indexOf("iPhone") >= 0) return false;
	if (navigator.userAgent.indexOf("iPod") >= 0) return false;
	if (navigator.userAgent.indexOf("Android") >= 0) return false;
	return !!document.createElement('video').canPlayType;
}

function supports_h264_baseline_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}

function supports_ogg_theora_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/ogg; codecs="theora, vorbis"');
}

function supports_webm_video() {
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/webm; codecs="vp8, vorbis"');
}

function IsMobileDevice() {
	var ua = navigator.userAgent;
	// Apple
	if ((ua.indexOf("iPhone")!=-1) ||
		(ua.indexOf("iPad")!=-1) ||
		(ua.indexOf("iPod")!=-1)) return true;
	// Android
	if (ua.indexOf("Android")!=-1) return true;
	// webOS
	if ((ua.indexOf("webOS")!=-1) ||
		(ua.indexOf("hpwOS")!=-1))return true;
	// Blackberry
	if ((ua.indexOf("BlackBerry")!=-1)) return true;
	// Firefox Mobile
	if ((ua.indexOf("Fennec")!=-1)) return true;
	// Windows Phone
	if ((ua.indexOf("IEMobile")!=-1)) return true;
	return false;
}

addDOMLoadEvent(function() {
	var f = document.getElementById("Footer");
	if (IsMobileDevice() && f) {
		var s = "<a href=\"/mobile/\">Mobile Site</a>";
		f.innerHTML = f.innerHTML + " | " + s;
	}
});
