/* compatibility.js * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Written by: Keith Borgmann                                                  *
* Date: 2008-07-18															  *
* Version: 1.1.1                                                              *
* Description:                                                                *
*	A number of js functions designed to work with most browsers.             *
*                                                                             *
* Functions:                                                                  *
*   bool = hideElement(elementName, hide);                                    *
*   obj  = element(objName[, index]);                                         *
*                                                                             *
* Notes:                                                                      *
*	As of v1.1.0:                                                             *
*	-'window_onLoad() {...}' is not supported                                 *
*		(use window.onload = function() {...}                                 *
*   -element() requires an id attribute (not name)                            *
*		(form objects should be accessed via document.formName.inputName...)  *
*   -elem() new version of element											  *
*	-element.eArray() is deprecated and removed                               *
*	-hideDiv() is deprecated and removed                                      *
*   -hideElement() has been moved to util.js                                  * 
*                                                                             *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*	TODO: update function descriptions
		isNumeric
		hasValidID
		element
		keyToUpper
		filterKeys
		getKeyCode
		disableForm
		getBrowserSize
		animateButton
		addChild
		replaceClass
		setWindowSize
*/


//GLOBAL VARIABLES AND CONSTANTS///////////////////////////////////////////////
var gBrowserType = "unknown";

if (document.all) {						//IE
	gBrowserType = "internet explorer";
} else if (document.layers) {			//Netscape
	gBrowserType = "netscape";
} else if (document.getElementById) {	//Mozilla
	gBrowserType = "mozilla";
}

//MINI FUNCTIONS///////////////////////////////////////////////////////////////
function isNumeric(val){return(parseFloat(val,10)==(val*1));} //source: www.mattkruse.com/javascript/validations/source.html
function hasValidID(objElement) {return (objElement.id!=null && objElement.id!="");} //source: me


//MAIN FUNCTIONS///////////////////////////////////////////////////////////////

/*F'n:   element
  Desc:  Get an element with the specified id (name attribute will not work)
		 Compatible with IE, Netscape* & Mozilla.  (*not tested)
  Usage: obj = element("objName"[, index]);  //instead of: obj = document.all.objName[index];
  Notes: elements should be named as id='objName[index]' (eg: id='dateField' or id='dateField0')
*/
function element(objName, index) {
	if (typeof(objName)!="string")
		return null;
	if ( typeof(index)=="number" )
		objName+=index.toString();


	/*if (document.all) {						//IE
		eval( "obj = document.all." + objName);
	} else if (document.layers) {			//Netscape
		obj = document.layers[objName];
	} else if (document.getElementById) {	//Mozilla
		var el = document.getElementById(objName);
		obj = el;
	}*/

	//Get a reference to the object (browser dependant)
	var obj=null;
	if (gBrowserType=="internet explorer") {
		eval( "obj = document.all." + objName);
	} else if (gBrowserType=="netscape") {
		obj = document.layers[objName];
	} else if (gBrowserType=="mozilla") {
		obj = document.getElementById(objName);
	}

	return obj;
}


/*F'n:   elem
  Desc:  Get an element with the specified id (name attribute will not work)
		 Compatible with all browsers.
  Notes: use this instead of function elements, unless VERY OLD browser compatibility is required.
*/
function elem(objName) {
	return document.getElementById(objName);
}

/*F'n:    hideElement
  Desc:   Hide/show elements.
  Compat: Compatible with IE, Netscape* & Mozilla  (*not tested)
  Usage:  ishidden = hideDiv(divname, true|false|"toggle"|"get");
  Notes:  use html attribute "style='display:none;'" to start as hidden.
*/	//Moved to util.js; now called as: hideElement(element("<name>")[, hide]);
/*function hideElement(elementName, hide) {
	var obj = element(elementName);
	if (obj==null)
		return false;

	var style = (gBrowserType=="netscape") ? obj : obj.style;

	//Determine what state to set the object
	if (hide=="toggle" || hide=="get") {
		state = style.display;
		if (hide=="toggle") 
			state = (state=="none")?"":"none";
	} else {
		var state = (hide==null||hide==true)?"none":"";
	}

	//Change the state of the object
	if (hide!="get")
		style.display = state;

	return (state=="none");  //returns current state
}*/

/*F'n:    keyToUpper
  Desc:   onkeypress, turn key to uppercase
  Compat: IE & Mozilla.
  Usage:  onKeyPress='return keyToUpper(event)'
*/
function keyToUpper(e) {
	if (gBrowserType=="mozilla") {
		if (e.charCode >= 97 && e.charCode <= 122 && !e.ctrlKey) {
			__mozillaInsert(e.target, e.charCode-32);
			return false;
		}	
	} else if (gBrowserType=="internet explorer") {
		if (e.keyCode >= 97 && e.keyCode <= 122)
			e.keyCode-=32;
		return true;
	}
	return true;
}

/*F'n:    filterKeys
  Desc:   onkeypress, only allow specified keys & key-range.
  Compat: IE & Mozilla.
  Usage:  onKeyPress='return filterKeys(event, "-.", 48, 57);'	//allows keys 0-9, -, .
  Notes:  cut & paste will not follow filter rules.

  More complex: onKeyPress='return ( filterKeys(event, null, 48, 57) && !filterKeys(event, "5") );'
		--> 0-9, but not 5
*/
function filterKeys(e, strKeys, intFrom, intTo) {
	var keycode = 0;

	//Browser specific code---
	if (gBrowserType=="mozilla") {
		keycode = e.charCode;
		if (keycode < 20 || e.ctrlKey)
			return true;	//allow whitespace (except spacebar) & ctrl+<key>
	} else if (gBrowserType=="internet explorer")
		keycode = e.keyCode;
	else
		return true;  //don't attempt to filter keys for other browsers

	//Universal code---
	if (typeof(intFrom)=="number") {  //range may be excluded
		if (keycode >= intFrom && keycode <= intTo) //within requested range
			return true;
	}
	if (typeof(strKeys)=="string") {
		if (strKeys.indexOf( String.fromCharCode(keycode) )!=-1 )	//within strKeys
			return true;
	}

	return false;
}

/*F'n:    getKeyCode
  Desc:   returns the browser-compatible key-code of the last key pressed
  Compat: IE & Mozilla.
  Sample: onKeyPress='alert( getKeyCode(event) );'
*/
function getKeyCode(e) {
	var keycode = 0;
	if (gBrowserType=="internet explorer") {
		keycode = e.keyCode;
	} else if (gBrowserType=="mozilla") {
		keycode = e.charCode;
	}
	return keycode;
}

/*F'n:    disableForm
  Desc:   disable all of a form's elements
  Compat: IE & Mozilla.
  Usage:  disableForm(form, true|false);
*/
function disableForm(form, disable) {
	if (disable==null)
		disable = true;

	if (gBrowserType=="mozilla") {
		var child = form.elements;
	
		if (disable) {
			for (var q=0; q< child.length; q++)
				child[q].setAttribute("disabled", "");  //disable attribute needs no value
		} else {
			for (var q=0; q< child.length; q++)
				child[q].removeAttribute("disabled");
		}

	} else if (gBrowserType=="internet explorer") {
		form.disabled = disable;	//gray-out all text in the form
		for (var x=0; x<form.elements.length; x++)
			form.elements[x].disabled = disable;
	}
}


/*F'n:    getBrowserSize
  Desc:   returns the current size of the browser (inner width & height)
  Compat: IE, Netscape* & Mozilla.  (*not tested)
  Usage:  var objSize = getBrowserSize();  w = objSize.width; h = objSize.height;
*/
function getBrowserSize() {
	var width=null;
	var height=null;

	if (gBrowserType=="netscape" ) {
		width = window.innerWidth;
		height = window.innerHeight;
	} else if (gBrowserType=="mozilla") {
		width = Math.max( document.documentElement.clientWidth, document.body.clientWidth);
		height = Math.max( document.documentElement.clientHeight, document.body.clientHeight); 
		
	} else if (gBrowserType=="internet explorer") {
		//if  (document.documentElement.clientHeight)  //is IE 6 Strict; following may not be valid
		width = document.body.scrollWidth;
		height = document.body.scrollHeight;
	}

	return {width:width, height:height};
}

/*F'n:    animateButton
  Desc:   sets a buttonUp and buttonDown image for an <img> tag
  Compat: IE & ??
  Usage:  window_onLoad() { animateButton(name, "up.gif", "down.gif") }
*/
function animateButton(strName, strUp, strDown) {
	var objButton = element(strName);
	if (objButton != null) {
		objButton.src = strUp;
		eval("objButton.onmousedown = function() { this.src=\""+strDown+"\"; }");
		eval("objButton.onmouseout = function() { this.src=\""+strUp+"\"; }");
		eval("objButton.onmouseup = function() { this.src=\""+strUp+"\"; }");
	}
}


/*F'n:    addChild
  Desc:   Appends a child to the desired parent.  Returns a reference to the child
  Compat: IE & ??
  Usage:  addChild(parent, childType[, innerHTML[, childName]]);
  Usage:  (eg) addChild(objRow, 'td', "hello", "td1");
*/
function addChild(parent, childType, innerHTML, childName){
	var child = document.createElement(childType);	//create child
	if (innerHTML!=null)
		child.innerHTML = innerHTML;				//set the content
	if (childName!=null)
		child.setAttribute('id', childName);		//name child
	parent.appendChild(child);						//add to parent (last element)
	return child;
}


/*F'n:    replaceClass
  Desc:   replaces the class (css) for all object with the class "replaceClass"
  Compat: IE & Moz & NS  (not fully tested)
  Notes:  This will replace the class for ALL objects in the document
		  The replaceClass name must be exact ('myClass' != 'myClass red')
*/
function replaceClass(replaceClass, replaceWith) {	
	var elements = null;  //an array of all objects
	var obj=null;
	var id = "";

	if (gBrowserType=="internet explorer") {
		elements = document.all;
	} else if (gBrowserType=="netscape") {
		elements = document.layers;
	} else if (gBrowserType=="mozilla") {
		elements = document.getElementsByTagName('*');
	}

	for (e in elements) {
		obj=elements[e];

		if (typeof(obj)=="object") {	//make sure it's a valid object
			if (obj.className != "") {
				if (obj.className == replaceClass) {	//does it have the specified name?
					obj.className = replaceWith;
				}
			}
		}
	}
}

/*F'n:    setWindowSize
  Desc:   Change the size of the current browser window
  Compat: IE & ???
*/
function setWindowSize(width,height) {
	if (window.outerWidth) {
		window.outerWidth = width;
		window.outerHeight = height;

	}  else if (window.resizeTo) {
		window.resizeTo(width,height);
	}
}





///////////////////////////////////////////////////////////////////////////////
//PRIVATE FUNCTIONS////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Note: the following functions are not intended for direct calls.


//Mozilla only (insert a character at current position)
function __mozillaInsert(target, key) {
	var newKey="";
	var p = target.selectionStart;

	if (typeof(key)=="number")
		newKey = String.fromCharCode(key);
	else if (typeof(key)=="string")
		newKey = key;

	//Insert the key manually
	target.value = target.value.substring(0, p) + newKey +  target.value.substring(p);

	//reset the caret position
	target.selectionStart =p+1;
	target.selectionEnd =p+1;
}


//Code for onDemand loading.  Must be the last lines in the file.
if ( typeof(onDemandJSLoaded)=="undefined") var onDemandJSLoaded = new Object();
onDemandJSLoaded["compatibility.js"] = true;
if ( typeof(onDemandCallback)=="function" ) onDemandCallback();
