/**********************************************************
*	Functions designed to enable cross browser functionality with IE5+,
*  IE4+ and NS6+
*  Last Update 5/07/2002 Matt Shrader
**********************************************************/

var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinNS6 = (isMinNS4 && navigator.userAgent.indexOf("Gecko")>=0) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4==1 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;
var isMinIE6 = (isMinIE4 && navigator.appVersion.indexOf("6.")) >= 0 ? 1 : 0;
var isDOM = (document.getElementById) ? 1 : 0;


//Cross browser get function - returns single element or null if not found
function get(elementName){
	if (isDOM == 1){
		
		if(document.getElementById(elementName)){
				return document.getElementById(elementName);
		}
		else{
			return null;
		}
		
	}
	else if (isMinIE4 == 1){
		if(document.all(elementName)[0]){
			return document.all(elementName)[0];
		}
		else{
			return document.all(elementName);
		}
		
	}//if
}//function

/** getObjectInForm - Developed to support fwdSubmit requests that occur on 
	  * pages that have more than one function in more than one form.  returns null if no element with 
		* NAME or ID Attributes exists within the passed form.
		* @param form object refrence to form you want to look in.  Form must have ID attribute and 
		*	that ID must be unique to the page 
		* @param elementName - Normally "ACTION" but could be anything. Must be either the NAME or ID
		*	Attribute value in the form.
		* @ return javascript object element
		* @author Matt Shrader
		* @createDate 5/7/2002
		* @see fwdSubmit()
*/
function getObjectInForm(form, elementName){
	
	if (isDOM == 1){
		for(x=0; x< form.elements.length; x++){
			var element = form.elements[x];
			if(element.getAttribute("name") == elementName || element.getAttribute("id") == elementName){return element;}
		}
		return null;
	}
}//function


// return collection of document elements having the tagname specified.  
// search can be limited by passing a source other than 'document'
function getAllByName(source, tagName){
	if (isDOM == 1){
		return source.getElementsByTagName(tagName);
	}
	else if (isMinIE4 == 1){
		return source.all.tags(tagName)
	}//if
}


// return first of collection of document elements having the tagname specified.  
// search can be limited by passing a source other than 'document'
function getFirstByTagName(source, tagName){
	return getAllByName(source, tagName).item(0);
}

//searches within scope source for all elements that have the tagName Specified with the attribute specified equal to the attrValue;
function getAllByAttributeValue(source, tagName, attr, attrValue){
	var sourceList = getAllByName(source, tagName);	
	var matchList = new Array();
	var matchCount = 0;
	for(x=0; x<sourceList.length; x++){
		matchItem = sourceList.item(x);
		if(matchItem.getAttribute(attr) !=null){
			if(matchItem.getAttribute(attr).toLowerCase() == attrValue.toLowerCase()){
				matchList[matchCount] = matchItem;
				matchCount ++;
			}//if
		}//if
	}//for
	return matchList;
}//getAllBYAttributeValue

//given a source element walk up the document tree till tagname found where attribute is passed value
function getParent(source, tag){

 while(source.tagName != tag){
    //protect from going off the top
    if (source.parentNode == document){return null;}
    source = source.parentNode;
    }
  return source;  
}

//get the width of the current window
function windowWidth(){
	if(document.all){
		return parseInt(document.body.clientWidth);
	}
	else{
		return parseInt(window.innerWidth);
	}
}


//get the height of the current window	
function windowHeight(){
	if(document.all){
		return document.body.clientHeight;
	}
	else{
		return parseInt(window.innerHeight);
	}
}

	//Switches Action to appropriate user action and sends form to server.
	function fwdSubmit(form, action){
			var correctAction = getObjectInForm(form, "Action");
			correctAction.setAttribute("value", action);
			form.submit();
	}
	

/* attachMouseBehavior function to attach mouse behavior to any set of objects
** @author Matt Shrader
** @param source - the javascript object that is the container for the tags you wish to attach events to
** @param blockBegin - tells the method where in the collection to begin the event attachment
** @param tag - the tag name of the objects to be altered - must be upper case for some reason
** @param mouseover - the name of the script object used for the mouseover event ( pass the function name with no parens or quotes )
** @param mouseout - see mouseover
** @param click - see above
*/
function attachMouseBehavior(source, tag, blockBegin, mouseover, mouseout, click ){
  var objCollection = getAllByName(source,tag);
  for (x=blockBegin; x<=objCollection.length -1;x++){
		obj = objCollection.item(x);
		if(document.all){
				if (isMinIE4 ==1 && isMinIE5 != 1 &&  isMinIE6 != 1){
					//no op for IE4 - must be written into the table
				}
				else{
          //IE 5+
					if(mouseover != null){ var overEvent = obj.attachEvent("onmouseover", mouseover);}
					if(mouseout != null ){ var outEvent = obj.attachEvent("onmouseout", mouseout);}
					if (click != null){
            source.style.cursor=(isMinIE5==1)?"hand":"pointer";
						var outEvent = obj.attachEvent("onclick", click);
						obj.title= tooltip;
					}//if
				}//if
			}//if
			else{
        //NS6+
				if(mouseover != null){ obj.addEventListener("mouseover", mouseover, false);}
				if(mouseout != null ){ obj.addEventListener("mouseout", mouseover, false);}
				if (click != null){
          source.style.cursor="pointer"
					obj.addEventListener("click", click, false);
					obj.title= tooltip;
				}//if
			}//if
		}//for
	}//attachMouseBehavior
  
  /*arraySwap swaps the position of target1 and target2 in the specified array
  * @param target1 int
  * @param target2 int
  * @param  array  Array - the array to operate on
  */
  function arraySwap(target1, target2, array){
    var tmpNode = array[target2];
    array[target2] = array[target1];
    array[target1] = tmpNode;
  }

/** Convience method to resolve the correct reference to the browsers event object.  Access
	** to this object is critical and (of course) varies between browsers. Should not be needed by 
	** Java Developers but put here as it is used everywhere.
**/
	function resolveEventTargetObject(e){
	var whichInput; 
		
		if(document.all){
			whichInput = window.event.srcElement;
		}
		else{
			whichInput = e.currentTarget;
		}
		return whichInput;	
	}//function
	
	function resolveEventObject(e){
		if(document.all){e==window.event;}
		return e;
	}


/** Used to determine if the keyCode passed in is in the alphanumeric part of the keyboard
		* Arrow Keys, enter key, tab key etc return false.  
		* @param keyCode Integer value of the ascii code of the key to be checked. Obtainable from event.keyCode
		* @return boolean.
*/ 
function isAlphaNumericKey(keyCode){
// Do I need to deal with function keys?
	switch(keyCode){
	case 8:
	case 9:
	case 13:
	case 16:
	case 17:
	case 18:
	case 19:
	case 20:
	case 27:
	case 33:
	case 34:
	case 35:
	case 36:
	case 37:
	case 38:
	case 39:
	case 40:
	case 45: 
	case 46:
	case 91:
	case 92:
	case 93:
	case 144:
		return false;
		break;
default:
return true;
break;

	}
}


//cross broser function for turning the cursor into a hand ( like an anchor )	
function toggleCursorOver(which){
if (document.all){
		which.style.cursor = 'hand';
	}
	else{
		which.style.cursor = 'pointer';
	}

}

//returns cursor to system default.	
function toggleCursorOut(which){
	which.style.cursor="default";		
}

// return whether the given element belongs to the given class
function elementBelongsToClass(element, classId) {
	// element's class should have the classId somewhere it it's classname.
	// should prefixed and postfixed by whitespace if it's not at the beginning or end
	return (new RegExp('.*(^|\\s+)' + classId + '(\\s+|$).*')).test(element.className); 
}
