function hasChildElement(element, tagName){
    var children=Element.immediateDescendants(element);
    for (var i=0; i<children.length; i++){
        if (Element.match(children[i], tagName))
            return true;
    }
    return false;
}

function getImmediateChildElement(element, tagName){
    var children=Element.immediateDescendants(element);
    for (var i=0; i<children.length; i++){
        if (Element.match(children[i], tagName))
            return children[i];
    }		
    return null;
}

/*

function replaced by the non-prototype version below, doesn't seem to work in IE 5.5

function getChildElement(element, tagName){
		var children=Element.descendants(element);
    for (var i=0; i<children.length; i++){
        if (Element.match(children[i], tagName))
            return children[i];
    }		
    return null;
}
*/

function getChildElement(element, tagName){
	for (var i=0;i<element.childNodes.length;i++){
		var node=element.childNodes[i];
		if (node.nodeType==1 && node.nodeName.toLowerCase()==tagName.toLowerCase())
			return node;
	}
	for (var i=0;i<element.childNodes.length;i++){
		var node=element.childNodes[i];
		if (node.nodeType==1){
			var childNode=getChildElement(node, tagName);
			if (childNode!=null)
				return childNode;
		}
	}
	return null;
}

function hasNestedChildElement(element, tagNames){
    for (var i=0; i<tagNames.length; i++){
        element=getChildElement(element, tagNames[i]);
        if (element==null)
            return false;
    }
    return true;
}

function hide(id){
	$(id).setStyle({visibility:"hidden"});
}

function show(id){
	$(id).setStyle({visibility:"visible"});
}

//from http://www.dustindiaz.com/getelementsbyclass
function getElementsByClass(className,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+className+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showElementNear(elementToMove, targetElement, offsetX, offsetY){
	var position=Position.cumulativeOffset($(targetElement));	
	$(elementToMove).setStyle({left:(position[0]+offsetX)+"px",top:(position[1]+offsetY)+"px",visibility:"visible"});
}

function swapImage(element){
	element.oSrc=element.src;
	element.src=getFrame(element.src, 2);
}

function swapBack(element){
	element.src=element.oSrc;
}

function swapDown(element){
	element.src=getFrame(element.oSrc, 3);
}

function getFrame(imageSrc, frame){
	var index=imageSrc.lastIndexOf(".");
	return imageSrc.substring(0, index)+"_f"+frame+imageSrc.substring(index);
}

function loadStylesheet(stylesheet){
	document.write("<link href=\"stylesheets/"+stylesheet+"\" rel=\"stylesheet\" type=\"text/css\"/>");
}

function loadJavascriptFile(javascriptFile){
	document.write("<script type=\"text/javascript\" src=\"javascripts/"+javascriptFile+"\"></script>");
}

//modified from http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getWindowSize() {	
	var windowSize=new WindowSize();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    windowSize.width = window.innerWidth;
    windowSize.height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowSize.width = document.documentElement.clientWidth;
    windowSize.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    windowSize.width = document.body.clientWidth;
    windowSize.height = document.body.clientHeight;
  }
  return windowSize;
}

function WindowSize(width, height){
	this.width=width;
	this.height=height;
}

// taken from http://www.mojavelinux.com/articles/javascript_hashes.html
function Hash(){
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	this.remove = function(in_key){
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_value;
	}

	this.get = function(in_key) {
		return this.items[in_key];
	}

	this.set = function(in_key, in_value){
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}

			this.items[in_key] = in_value;
		}
	   
		return in_value;
	}

	this.containsKey = function(in_key){
		return typeof(this.items[in_key]) != 'undefined';
	}
}

function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}
