/*
Prevent JS error from appearing on the client browser
*/
function stopErrors() {
	return true;
}
//window.onerror = stopErrors;


/*
Trim a string from leading and trailing spaces
*/
function Trim(iString) {
  iString = iString.replace( /^\s+/g, "" );
  return iString.replace( /\s+$/g, "" );
}


/*
Pre-load image functions
*/
function preloadBaseImages() {
	var imageArray = new Array('loading.gif');
							   
    preloadImages(imageArray, '/images/');	
}


/*
Pre-load the image array
*/
function preloadImages(imageArray, dirPrefix) {
	for(i = 0; i < imageArray.length; i++) {
		var imageName = new Image();
        imageName.src = dirPrefix + imageArray[i];
	}
}


/*
Set a cookie for the specified length
*/
function setCookie(name, value, ex) {
	var prefix = window.location.hostname;
	var expDate  = new Date();
  	expDate.setMinutes(expDate.getMinutes() + ex * 60);
  	document.cookie = name +"=" + value + "; expires=" + expDate.toGMTString() + "; path=/; domain=" + prefix;
}


/*
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) {
	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;	
}


/*
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 the X position of the specified object
*/
function findPosX(obj) {
	if (!obj.id) { obj = findObj(obj); }
	if (!obj) { return false; }
	
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	
	return curleft;
}


/*
Find the Y position of the specified object
*/
function findPosY(obj) {
	if (!obj.id) { obj = findObj(obj); }
	if (!obj) { return false; }
	
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	
	return curtop;
}


/*
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); }
}


/*
Position a layer based on the coords of the mouse
*/
function moveLayerToMouse(obj, e) {
	var tempX = 0;
	var tempY = 0;
	var offset = 5;

	if (!obj.id) { obj = findObj(obj); }
	if (!obj) { return false; }

	if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {
    	tempX = e.pageX;
    	tempY = e.pageY;
  	}

	if (tempX < 0) { tempX = 0; }
 	if (tempY < 0) { tempY = 0; }
 	
	// Determine the max width and height that we have to work with
 	if (self.innerWidth) {
		var maxWidth = self.innerWidth;
		var maxHeight = self.innerHeight;
	
 	} else if (document.documentElement && document.documentElement.clientWidth) {
		var maxWidth = document.documentElement.clientWidth;
		var maxHeight = document.documentElement.clientHeight;
	
 	} else if (document.body) {
		var maxWidth = document.body.clientWidth;
		var maxHeight = document.body.clientHeight;		
	}

	// 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; }	
	
	if (tempX + obj.offsetWidth + 30 > maxWidth) {
		obj.style.left = (tempX - obj.offsetWidth) + 'px';
	} else {
		obj.style.left = (tempX + offset) + 'px';
	}
	
	if (tempY + obj.offsetHeight + 30 > maxHeight) {
		obj.style.top = (tempY - obj.offsetHeight) + 'px';
	} else {
		obj.style.top = (tempY + offset) + 'px';
	}
}


/*
"width=480,height=350,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0"
*/
function launchWin(urlPath, winName, params) {	
	var paramArray = params.split(",");
	var windowWidth = Trim(paramArray[0].split("=")[1]);
	var windowHeight = Trim(paramArray[1].split("=")[1]);

	var windowLeft = (self.screen.width-windowWidth) / 2;
	var windowTop = (self.screen.height-windowHeight) / 2;
	
	params = params + ',top=' + windowTop + ',left=' + windowLeft;
	//alert(params);
	
	var winName = window.open(urlPath,winName,params);
	
	//winName.moveTo((self.screen.width-windowWidth) / 2, (self.screen.height-windowHeight) / 2);
	winName.focus();

	/*
	var paramArray = params.split(",");
	var windowWidth = Trim(paramArray[0].split("=")[1]);
	var windowHeight = Trim(paramArray[1].split("=")[1]);	
	
	winDialog = window.showModalDialog(urlPath, winName,"dialogHeight:" + windowHeight + "px;dialogWidth=" + windowWidth + "px;status:0;help:0;center:1");
	*/
}


/*
Open a window maximized
*/
function openWinMax(aURL, aWinName, params) {
   var wOpen;
   var sOptions;

   sOptions = params;
   sOptions = sOptions + ',width=' + (screen.availWidth - 10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight - 122).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';

   wOpen = window.open('', aWinName, sOptions);
   wOpen.location = aURL;
   wOpen.focus();
   wOpen.moveTo(0, 0);
   wOpen.resizeTo(screen.availWidth, screen.availHeight);
   return wOpen;
}


/*
Reload the current window after a set time
*/
function reloadWindowTimeout() {
	setTimeout('window.location.reload();', 250);
}


/*
Redirect the current window after a set time
*/
function redirectWindowTimeout(iLocation) {
	setTimeout('window.location.href=\'' + iLocation + '\'', 250);
}
