function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function getElementsByClass(searchClass,startNode,htmlTag) {
	var classElements = new Array();
	if ( startNode == null )
		startNode = document;
	if ( htmlTag == null )
		htmlTag = '*';
	var els = startNode.getElementsByTagName(htmlTag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// --------------------------------------------------------
// jumps fields for phone #.  naming convention must be
// prefix1, prefix2, prefix3 where prefix is the name/id of the 
// phone field.
//
// NOTE: in FireFox, if you are getting a SelectedIndex error
// add the following to the input control: autocomplete="off"
// --------------------------------------------------------
function jumpPhone(fld,prefix) {
	switch(fld.name) {
		case prefix + '1':
			if(document.getElementById(prefix + '1').value.length==3) {
				document.getElementById(prefix + '2').focus();
				document.getElementById(prefix + '2').select();
			}
			break;
		case prefix + '2':
			if(document.getElementById(prefix + '2').value.length==3) {
				document.getElementById(prefix + '3').focus();
				document.getElementById(prefix + '3').select();
			}
			break;
	}
}

// --------------------------------------------------------
// clears a select dropdown list
// --------------------------------------------------------
function clearSelectList(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if (obj) {
		var len = obj.length;
		
		while (len > 0) {
			obj.remove(len);
			len--;
		}
	}
}

// --------------------------------------------------------
// returns the value of a select dropdown
// --------------------------------------------------------
function getSelectValue(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	var value=null;
	
	if (obj) {
		value=obj.options[obj.selectedIndex].value
	}
	
	return value;
}

// --------------------------------------------------------
// toggles a checkbox
// --------------------------------------------------------
function toggleCheck(obj,bNoClick){
	obj = $(obj);
	if (obj) {
		// perform the click
		if (!bNoClick) {
			obj.click();
		} else {
			obj.checked=!obj.checked;		
		}
	}
}

// --------------------------------------------------------
// returns true if the passed checkbox is checked
// --------------------------------------------------------
function isChecked(obj) {
	obj = $(obj);
	if (!obj) {
		alert('Could not locate '+obj);
		return false;
	} else {
		return obj.checked;		
	}
}

// --------------------------------------------------------
// returns true if the passed element is visible
// --------------------------------------------------------
function isVisible(obj)
{
	var el = $(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		return (el.style.display!='none');
	}	
}

// --------------------------------------------------------
// shows an element by setting its display value
// --------------------------------------------------------
function showElement(obj) 
{
	var el = $(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		el.style.display='';
	}
}

// --------------------------------------------------------
// hides an element by setting its display value
// --------------------------------------------------------
function hideElement(obj) 
{
	var el = $(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		el.style.display='none';
	}
}

// --------------------------------------------------------
// toggles visibility of the passed element
// --------------------------------------------------------
function toggleElement(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if ( obj.style.display != "none" ) {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = '';
	}
}

// --------------------------------------------------------
// disables the passed element
// --------------------------------------------------------
function disableElement(obj) {
	obj = $(obj);
	
	if (isArray(obj)) {
		for (i=0; i < obj.length; i++) {
			disableElement(obj[i]);
		}
	} else {
		if (obj) {
			obj.disabled = true;	
		} else {
			alert('Could not locate '+obj);
		}
	}
}

// --------------------------------------------------------
// enables the passed element
// --------------------------------------------------------
function enableElement(obj) {
	obj = $(obj);
	
	if (isArray(obj)) {
		for (i=0; i < obj.length; i++) {
			enableElement(obj[i]);
		}
	} else {
		if (obj) {
			obj.disabled = false;	
		} else {
			alert('Could not locate '+obj);
		}
	}
}

// --------------------------------------------------------
// toggles the enable of an element
// --------------------------------------------------------
function toggleEnabled(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if ( obj.disabled == false ) {
		obj.disabled = true;
	} else {
		obj.disabled = false;
	}
}

// disables all elements of a certain class
function disableElementsOfClass(searchClass, startNode, htmlTag) {
	var elements = getElementsByClass(searchClass,startNode,htmlTag);

	if (elements) {
		for (i = 0; i < elements.length; i++) {
			disableElement(elements[i]);
		}
	}
}

// disables all elements of a certain class
function enableElementsOfClass(searchClass, startNode, htmlTag) {
	var elements = getElementsByClass(searchClass,startNode,htmlTag);

	if (elements) {
		for (i = 0; i < elements.length; i++) {
			enableElement(elements[i]);
		}
	}
}

// returns the # of checked elements.  can be a radio group or a checkbox array.
function getCheckedCount(elementName) {
	return getCheckedElements(elementName).length;
}

// returns an array of checked elements.  can be radio or checkboxes.
function getCheckedElements(elementName) {
	var elements = new Array();
	var elementGroup = document.getElementsByName(elementName);

	if (elementGroup) {
		for (i=0; i < elementGroup.length; i++) {
			if (elementGroup[i].checked == true) {
				elements.push(elementGroup[i]);
			}
		}
	} else {
		alert('Unable to locate ' + elementName);
	}

	return elements;
}

// checks all elements of the passed name.  can be a radio group or a checkbox array.
function checkAll(elementName, perform_click) {

	var elementGroup = document.getElementsByName(elementName);
	
	if (elementGroup) {
		for (i=0; i < elementGroup.length; i++) {
			if (!elementGroup[i].checked) {
				if (perform_click) { elementGroup[i].click(); }
				elementGroup[i].checked = true;
			}
		}
	} else {
		alert('Unable to locate ' + elementName);
	}		
}

// unchecks all elements of the passed name.  can be a radio group or a checkbox array.
function uncheckAll(elementName, perform_click) {

	var elementGroup = document.getElementsByName(elementName);
	
	if (elementGroup) {
		for (i=0; i < elementGroup.length; i++) {
			if (elementGroup[i].checked) {
				if (perform_click) { elementGroup[i].click(); }
				elementGroup[i].checked = false;
			}
		}
	} else {
		alert('Unable to locate ' + elementName);
	}		
}

// --------------------------------------------------------
// note this takes a reference to the form named element, the the element id
// ex. clearRadio(document.formName.elementName);
// --------------------------------------------------------
function clearRadio(radioGroup){	
	for (i=0; i < radioGroup.length; i++) {
		if (radioGroup[i].checked == true) {
			radioGroup[i].checked = false
		}
	}
}

// --------------------------------------------------------
// returns the index of the selected radio option
// --------------------------------------------------------
function getRadioCheckedIndex(radioName) {

	var radioGroup = document.getElementsByName(radioName);
	
	if (radioGroup) {
		for (i=0; i < radioGroup.length; i++) {
			if (radioGroup[i].checked == true) {
				return i;
			}
		}
	} else {
		alert('Unable to locate ' + radioName);
	}
		
	return -1;
}

// --------------------------------------------------------
// returns true if the passed radio control has something selected
// --------------------------------------------------------
function isRadioChecked(radioName) {
	return (getRadioCheckedIndex(radioName) >= 0);
}

// --------------------------------------------------------
// returns the value of the selected radio option
// --------------------------------------------------------
function getRadioValue(radioName) {
	var radioGroup = document.getElementsByName(radioName);

	if (radioGroup) {
		for (i=0; i < radioGroup.length; i++) {
			if (radioGroup[i].checked == true) {
				return radioGroup[i].value;
			}
		}
	} else {
		alert('Unable to locate ' + radioName);
	}
	
	return '';
}

// --------------------------------------------------------
// selects a radio option based on value
// --------------------------------------------------------
function setRadioValue(radioName,value) {
	var radioGroup = document.getElementsByName(radioName);

	if (radioGroup) {
		for (i=0; i < radioGroup.length; i++) {
			if (radioGroup[i].value == value) {
				radioGroup[i].checked = true;
			}
		}
	} else {
		alert('Unable to locate ' + radioName);
	}
}

// --------------------------------------------------------
// disables a radio group
// --------------------------------------------------------
function disableRadio(radioName) {

	var radioGroup = document.getElementsByName(radioName);
	
	if (radioGroup) {
		for (i=0; i < radioGroup.length; i++) {
			radioGroup[i].disabled = true;
		}
	} else {
		alert('Unable to locate ' + radioName);
	}
}

// --------------------------------------------------------
// enables a radio group
// --------------------------------------------------------
function enableRadio(radioName) {

	var radioGroup = document.getElementsByName(radioName);
	
	if (radioGroup) {
		for (i=0; i < radioGroup.length; i++) {
			radioGroup[i].disabled = false;
		}
	} else {
		alert('Unable to locate ' + radioName);
	}
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function toProperCase(s)
{
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
	var i;
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}

function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}

function validateDollar( obj, bAlertFocus )
{
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}

	if (obj) {
		var afterDecimal = 0;
		var bHitDecimal = false;
		
		var temp_value = obj.value;
		
		if (temp_value == "")
		{
		  obj.value = "0.00";
		  return true;
		}

		var Chars = "0123456789.,$";
		for (var i = 0; i < temp_value.length; i++)
		{
			if (bHitDecimal) afterDecimal++;
			if (temp_value.charAt(i) == '.') bHitDecimal = true;

			if (afterDecimal>2) {
		        if (bAlertFocus) {
			        alert("Only 2 decimal values are allowed in this field.");
			        obj.focus();
				    obj.select();
				}
				return false;
			}

		    if (Chars.indexOf(temp_value.charAt(i)) == -1)
		    {
		        if (bAlertFocus) {
			        alert("Invalid Character(s)\n\nOnly numbers (0-9), a dollar sign, a comma, and a period are allowed in this field.");
			        obj.focus();
				    obj.select();
				}
		        return false;
		    }
		}
		
		if (bHitDecimal && afterDecimal<2) {
			for (var i = afterDecimal; i<2; i++) {
				obj.value += '0';
			}
		} else if (!bHitDecimal) {
			obj.value += '.00';
		}	
	} else {
		alert('Could not locate '+obj);
	}
	
	return true;
}
// based on 3 fields called field_nm_hh,field_nm_mm,field_nm_ss 
function validTime(base_fld) {
	var hh = $(base_fld + '_hh').value;
	var mm = $(base_fld + '_mm').value;
	var ss = $(base_fld + '_ss').value;

	return isNum(hh) && isNum(mm) && isNum(ss) && 
		parseInt(hh)>=0 && parseInt(hh)<24 &&
		parseInt(mm)>=0 && parseInt(mm)<60 &&
		parseInt(ss)>=0 && parseInt(ss)<60;		
}

// returns true if the key pressed was a number
// usage: onkeypress="return isNumberKey(event);"
function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode
	
	var allowed_special_keys = new Array();
	//allowed_special_keys.push(46) // delete key
	
	if (String.fromCharCode(charCode) != ".") {
		allowed_special_keys.push(46) // delete key
	}
	if (String.fromCharCode(charCode) != "'") {
		allowed_special_keys.push(39) // right arrow
	}
	if (String.fromCharCode(charCode) != "%") {
		allowed_special_keys.push(37) // left arrow
	}
	
	if (charCode > 31 && (charCode < 48 || charCode > 57) && !allowed_special_keys.inArray(charCode)) return false;
	//if (String.fromCharCode(charCode) = ".") return false;
	
	return true;
}

// returns true if the key pressed was an alphanumeric value
// usage: onkeypress="return isAlphaNumberKey(event);"
function isAlphaNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode
	var allowed_special_keys = new Array();
	
	allowed_special_keys.push(8)  // backspace
	allowed_special_keys.push(9)  // tab
	if (String.fromCharCode(charCode) != ".") {
		allowed_special_keys.push(46) // delete key
	}
	allowed_special_keys.push(39) // right arrow
	if (String.fromCharCode(charCode) != "%") {
		allowed_special_keys.push(37) // left arrow
	}
	if (!isAlphanum(String.fromCharCode(charCode)) && !allowed_special_keys.inArray(charCode)) return false;
	return true;
}

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

function isArray(obj) {
   return obj.constructor == Array;
}

function addEvent(elm, evType, fn, useCapture) {
	elm = $(elm);
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function insertAfter(newElement, targetElement) 
{ 
	newElement = $(newElement);
	targetElement = $(targetElement);

    // Target is what you want it to go after. 
    // Look for this elements parent. 
    var parent = targetElement.parentNode; 

    // If the parents lastchild is the target 
    // element... 
    if(parent.lastchild == targetElement) { 
        // Add the newElement after the target 
        // element. 
        parent.appendChild(newElement); 
    } else { 
        // Else the target has siblings, insert 
        // the new element between the target and 
        // it's next sibling. 
        parent.insertBefore(newElement, targetElement.nextSibling); 
    } 
} 

function scrollToElement(obj, pxPaddingX, pxPaddingY) {

	obj = $(obj);

	var posX = 0;
	var posY = 0;
              
	while (obj != null) {
		posX += obj.offsetLeft;
		posY += obj.offsetTop;
		obj = obj.offsetParent;
	}
	posX += (pxPaddingX ? pxPaddingX : 0);
	posY += (pxPaddingY ? pxPaddingY : 0);
		
	window.scrollTo(posX,posY);
}

function getScrollPosition() {
	var scrollTop = document.body.scrollTop;

	if (scrollTop == 0) {

	    if (window.pageYOffset) {
	        scrollTop = window.pageYOffset;
		} else {
	        scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
}

function switchColorAndCursor(box, direction) {

	if(direction=='in') {
		$(box).style.cursor = "pointer";
		changeOpac(60,box);
	} else {
		$(box).style.cursor = "default";
		changeOpac(100,box);
	}
}

function switchCursor(obj, direction) {
			
	if(direction=='in') {
		$(obj).style.cursor = "pointer";
	} else {
		$(obj).style.cursor = "default";
	}
}

//change the opacity for different browsers
// original from: http://www.brainerror.net/scripts_js_blendtrans.php
function changeOpac(opacity,id) {
	var group = $(id);
	
	if (group) {

		var object = group.style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";

		var elem = group.childNodes;

		for(var i = 0; i < elem.length; i++) {
			if (elem[i].id) {
				changeOpac(opacity,elem[i].id); // recursively set the opacity on the children
			}
		}		
	}
}

// JavaScript Document
function resizeimage(imagename, maxwidth, maxheight) {
	oldimageheight=$(imagename).height;
	oldimagewidth=$(imagename).width;
	
	for (i = $(imagename).height; i >= maxheight; i--){
		$(imagename).height=i;
		$(imagename).width=(i/oldimageheight)*oldimagewidth
	}
	oldimageheight=$(imagename).height;
	oldimagewidth=$(imagename).width;

	for (i=$(imagename).width; i >= maxwidth ; i--){
		$(imagename).width=i;
		$(imagename).height=(i/oldimagewidth)*oldimageheight;
	}

} 
