/* Common Javascript used throughout system */

userAgent = window.navigator.userAgent; 
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
CSIsW3CDOM = ((document.getElementById) && !(IsIE()&&browserVers<6)) ? true : false;

function IsIE() { return userAgent.indexOf("MSIE") > 0;}

function IsWin() { return (typeof(navigator.platform) != "undefined"  &&  navigator.platform.substr(0,3)=="Win");}

function CSIEStyl(s) { return document.all.tags("div")[s].style; }

function CSNSStyl(s) { if (CSIsW3CDOM) return document.getElementById(s).style; else return findElement(s,0);  }

mustInitImg = true;

function initImgID() {
	di = document.images; 
	if (mustInitImg && di) { 
		for (var i=0; i<di.length; i++) { 
			if (!di[i].id) di[i].id=di[i].name; 
		} 
	mustInitImg = false;
	}
}

function findElement(n,ly) {
	d = document;
	if (browserVers < 4) return d[n];
	if ((browserVers >= 6) && (d.getElementById)) {initImgID; return(d.getElementById(n))}; 
	var cd = ly ? ly.document : d;
	var elem = cd[n];
	if (!elem) {
		for (var i=0;i<cd.layers.length;i++) {
			elem = findElement(n,cd.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}

function changeImages() {
	d = document;
	if (d.images) {
		var img;
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			img = null;
			if (d.layers) {img = findElement(changeImages.arguments[i],0);}
			else {img = d.images[changeImages.arguments[i]];}
			if (img) {img.src = changeImages.arguments[i+1];}
		}
	}
}

function trim(field) {
	//trims leading and trailing spaces
	var i=0,j=field.length;
	while(i<j && field.charAt(i)==' ') i++;
	while(j>i && field.charAt(j-1)==' ') j--;
	return field.substring(i,j);
	}

// CROSS-BROWSER ROUTINES TO DEAL WITH KEYBOARD INPUT
// In IE on Windows, uses default Tab action and converts Enter to Tab except in Textareas and Submits
// On other browsers/platforms, traps Tab and Enter, and uses nextFocus routine to advance
// the focus to the next focusable field in tabindex order.
// Function firstFocus sets focus to focusable field with lowest positive tabindex.

function keyCode(pEvent) {

// cross-browser keycode for keyboard event

   if (IsIE()) return event.keyCode;
   var aKeyCode = pEvent.keyCode;
   if (aKeyCode == 0) aKeyCode = pEvent.which;	//Mozilla
   return aKeyCode;
}
	
function keyTarget(pEvent) {

// cross-browser target of keyboard event

   return IsIE() ? event.srcElement : pEvent.target;
}

function tabIndex(pElement) {

// Return signed tabIndex (Safari forces tabIndex to be positive)

	return (pElement.tabIndex < 16384) ? pElement.tabIndex : pElement.tabIndex - 32768;
}

function IsFocusable(pElement) {

// Returns true if pElement is a form element that can be given the focus and is not disabled or readonly

   if (pElement.disabled || pElement.readOnly) return false;
   var aType = pElement.type;
   return (aType == "text" ||  aType == "textarea" || aType == "password" 
      || aType == "submit" ||  aType.substr(0,6) == "select");
}

function trapEnter(pEvent) {

// onkeydown="return trapEnter(event);" convert enter to tab in IE Windows; call nextFocus on tab,enter in IE Mac

   return IEtrapEnter();
}

function IEtrapEnter() {

// onkeydown="return IEtrapEnter();" convert enter to tab in IE Windows; call nextFocus on tab,enter in IE Mac

   if (IsIE()) {
      var aTarget = event.srcElement;
      if (IsWin()) {
         if (event.keyCode == 13  &&  aTarget.type != "textarea"  &&  aTarget.type != "submit") event.keyCode = 9;
         return true;
      } else {
         if ( event.keyCode == 9  ||  (event.keyCode == 13  &&  aTarget.type != "textarea"  &&  aTarget.type != "submit") ) {
            nextFocus(aTarget);
            return false;
         }
      }
   }
   return true;
}

function trapTabEnter(pEvent) {

// onkeydown="return trapTabEnter(pEvent);" convert enter to tab in IE Windows; call nextFocus on tab,enter for other browsers; 
// if neither tab or enter, return keycode (treated as true)

   var aKeyCode = keyCode(pEvent);
   var aTarget = keyTarget(pEvent);
   if (aKeyCode == 9  ||  (aKeyCode == 13  &&  aTarget.type != "textarea"  &&  aTarget.type != "submit") ) {
      if (IsIE()  &&  IsWin()) {
         event.keyCode = 9; 
         return true;
      }
      nextFocus(aTarget);
      return false;
   }
   return aKeyCode;
}

// Checks for valid data entry

function trapInvalid(pEvent,pValidChars) {

// cross-browser; traps tab/enter moving focus, traps and suppresses invalid char

   var aResult = trapTabEnter(pEvent);
   switch (aResult) {
      case true:
         return aResult;
      case false:
         return aResult;
      default:
         return (pValidChars.indexOf(String.fromCharCode(aResult)) >= 0);
   }
}

function trapInvalidCurrency(pEvent) {

   return trapInvalid(pEvent,"0123456789,-");
}

function trapInvalidDigit(pEvent) {

   return trapInvalid(pEvent,"0123456789");
}

function trapInvalidInteger(pEvent) {

   return trapInvalid(pEvent,"0123456789,");
}

function trapInvalidDecimal(pEvent) {

   return trapInvalid(pEvent,"0123456789.,-");
}

function trapInvalidUserChar(pEvent) {

   return trapInvalid(pEvent,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-");
}

function trapInvalidPWChar(pEvent) {

   return trapInvalid(pEvent,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-#&?!@$%");
}

function firstFocus() {

// Move focus to focusable field of form with smallest positive tabIndex

   var form0 = document.frmEdit;
   if (!form0) {
//      alert("No form called frmEdit on page");
      return;
   }

   var numElements = form0.elements.length;
   if (numElements == 0) return;

   var elTabIndexSmallest;
   var tabIndexSmallest=99999;
   
   for (var i = 0; i<numElements; i++) {

      var elCur = form0.elements[i];

      if (IsFocusable(elCur)) {
         var tabIndexCur = tabIndex(elCur);
         if (tabIndexCur > 0  &&  tabIndexCur < tabIndexSmallest) {
            elTabIndexSmallest = elCur;
            tabIndexSmallest = tabIndexCur;
            //alert("Smallest tabindex is " + tabIndexCur + " for " + elCur.name);
         }
      }
   }
   if(tabIndexSmallest<99999) {
      elTabIndexSmallest.focus();
      elTabIndexSmallest.select();
   }
}

function nextFocus(pElement) {

// Move focus to later focusable field of form with tabIndex same as this;
// or to first tabindex next larger than this, or to field with smallest positive tabIndex

   var form0 = document.frmEdit;
   if (!form0) {
      alert("No form called frmEdit on page");
      return;
   }
   var numElements = form0.elements.length;
   if (numElements == 0) return;

   var elTabIndexSmallest,elTabIndexNextLarger;
   var tabIndexSmallest=99999,tabIndexNextLarger=99999;
   
   var tabIndexBase = tabIndex(pElement);
   var aBaseElFound = false;
   var elNext=pElement;
   
   for (var i=0; i<numElements; i++) {

      var elCur = form0.elements[i];

      if (elCur===pElement) {
         aBaseElFound = true;
         for (var j=0; j<numElements; j++) {
            elNext = form0.elements[(i+j+1)%numElements];
            if (IsFocusable(elNext)) break;
         }
         continue;
      }
      
      if (IsFocusable(elCur)) {
         var tabIndexCur = tabIndex(elCur);
         if ( (tabIndexCur > tabIndexBase || (aBaseElFound  &&  tabIndexCur == tabIndexBase) )    &&  tabIndexCur < tabIndexNextLarger) {
            elTabIndexNextLarger = elCur;
            tabIndexNextLarger = tabIndexCur;
         }
         if (tabIndexCur > 0  &&  tabIndexCur < tabIndexSmallest) {
            elTabIndexSmallest = elCur;
            tabIndexSmallest = tabIndexCur;
         }
      }
   }
   elTarget = elNext;
   if (tabIndexBase > 0) {
      if (tabIndexNextLarger<99999) elTarget = elTabIndexNextLarger;
      else if (tabIndexSmallest<99999) elTarget = elTabIndexSmallest;
   }
   elTarget.focus();
   elTarget.select();
}
