function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

if( !document.getElementById ) { 
	if( document.all ) {
		document.getElementById = function( id ) {
			return document.all[ id ];
		}
	} else if ( document.layers ) {
		document.getElementById = function( id ) {
			return document.layers[ id ];
		}
	} else {
		document.getElementById = function( id ) {
			return null;
		}	
	}
}

function toggleCheck(obj,bNoClick){
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if (obj) {
		// perform the click
		if (!bNoClick) {
			obj.click();
		} else {
			obj.checked=!obj.checked;		
		}
	}
}

function isChecked(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if (!obj) {
		alert('Could not locate '+obj);
		return false;
	} else {
		return obj.checked;		
	}
}

function isVisible(obj)
{
	var el = document.getElementById(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		return (el.style.display!='none');
	}	
}
function showElement(obj) 
{
	var el = document.getElementById(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		el.style.display='';
	}
}
function hideElement(obj) 
{
	var el = document.getElementById(obj);
	if (!el) {
		alert('Could not locate '+obj);
	} else {
		el.style.display='none';
	}
}

function toggleElement(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if ( obj.style.display != "none" ) {
		obj.style.display = 'none';
	}
	else {
		obj.style.display = '';
	}
}

function disableElement(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	
	if (obj) {
		obj.disabled = true;	
	} else {
		alert('Could not locate '+obj);
	}
}

function enableElement(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	
	if (obj) {
		obj.disabled = false;	
	} else {
		alert('Could not locate '+obj);
	}
}

function toggleEnabled(obj) {
	if (typeof(obj)!='object') {
		obj = document.getElementById(obj);
	}
	if ( obj.disabled == false ) {
		obj.disabled = true;
	} else {
		obj.disabled = false;
	}
}

// returns the value of a select dropdown
function getSelectValue(selectId) {
	var elem=document.getElementById(selectId)
	var value=null;
	
	if (elem) {
		value=elem.options[elem.selectedIndex].value
	}
	
	return value;
}

function getSelectText(selectId) {
	var elem=document.getElementById(selectId)
	var value=null;
	
	if (elem) {
		value=elem.options[elem.selectedIndex].text
	}
	
	return value;
}

// 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
		}
	}
}

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;
}

function isRadioChecked(radioName) {
	return (getRadioCheckedIndex(radioName) >= 0);
}

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 '';
}

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);
	}
}

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) {
	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 = document.getElementById(base_fld + '_hh').value;
	var mm = document.getElementById(base_fld + '_mm').value;
	var ss = document.getElementById(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;		
}

