/*
Show/hide a layer
*/
function toggleLayer(iLayer, iState) {
	hideLayer = false;
	
	if (document.getElementById) {
        if (!iLayer.id) {
			var obj = document.getElementById(iLayer);
			obj.style.visibility = iState ? "visible" : "hidden";
        } else {
        	iLayer.style.visibility = iState ? "visible": "hidden";
        }        
		
	} else if (document.layers) {
		if (!iLayer.id) {
       		document.layers[iLayer].visibility = iState ? "show" : "hide";
		} else {
			iLayer.visibility = iState ? "show" : "hide";
		}
    
	} else if (document.all) {
		if (!iLayer.id) {
        	document.all[iLayer].style.visibility = iState ? "visible" : "hidden";
		} else {
			iLayer.style.visibility = iState ? "visible" : "hidden";
		}
    }
    
	if (iState == 1) { setTimeout('hideLayer = true;', 25); }
}


/*
Center a layer's position on the current screen
*/
function centerLayerOnScreen(obj, iMethod) {
	// The layer is going to be centered within the current screen
	if (iMethod == 'relative') {
		if (document.body.scrollHeight > document.body.offsetHeight) {
			var maxWidth = document.body.scrollWidth;
			var maxHeight = document.body.scrollHeight;
		} else {
			var maxWidth = document.body.offsetWidth;
			var maxHeight = document.body.offsetHeight;
		}
		
	// The layer is going to be centered within a maximized window
	} else {
 		var maxWidth = screen.width;
 		var maxHeight = screen.height;
	}
	
	if (!obj.id) { obj = findObj(obj); }
	if (!obj) { return false; }
	 	
	// Adjust max vars based on scroll position
	//if (document.body.scrollTop) { maxHeight = maxHeight + document.body.scrollTop; }
	//if (document.body.scrollLeft) { maxWidth = maxWidth + document.body.scrollLeft; }	
	
	obj.style.left = ((maxWidth / 2) - (obj.offsetWidth / 2)) + 'px';
	obj.style.top = ((maxHeight / 2) - (obj.offsetHeight / 2)) + 'px';	
}


function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
    
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    	return false;
    } else {
		return true;
    }
}


/*
Replaces text with by in string
*/
function replace(string, text, by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


/*
Find an object on the page
*/
function findObj(objName) {
	var theObj = document.getElementById(objName);
	
	if (!theObj) {
		//theObj = document.all[objName];
	}
	
	return theObj;
}


/*
Find an object that is contained within an object
*/
function findContainedObj(objName, objContainer) {
	if (!objContainer.id) { objContainer = findObj(objContainer); }
	if (!objContainer) { return false; }
	
	var theObj = objContainer.elements[objName];
	
	if ((!theObj) && (objContainer.all)) {
		theObj = objContainer.all[objName];
	}
	
	// If no object found, search through layers
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("div");
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
	
	// If we still haven't found the object, look for table cells
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("td");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
		
	// If we still haven't found the object, look for spans
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("span");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}	

	// If we still haven't found the object, look for tables
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("table");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
	
	return theObj;	
}
