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


/*
Strip out HTML from the specified string
*/
function stripHTML(iString) {
	var newString = iString.replace(/(<([^>]+)>)/ig,"");
	
	// Replace carriage returns and line feeds
	newString = escape(newString);
	newString = newString.replace("%0D%0A"," ");
	newString = newString.replace("%0A"," ");
	newString = newString.replace("%0D"," ");
	newString = unescape(newString);
	
	// Trim string
	newString = Trim(newString);
	
	return newString;
}


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


/*
Make sure the specified string is numeric
*/
function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	for (i = 0; i < strString.length && blnResult == true; i++) {
    	strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
      	}
	}
	
	return blnResult;
}


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


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


/*

*/
function popup(mylink, windowname) {
	var href;
	
	if (!window.focus) {
		return true;
	}

	if (typeof(mylink) == 'string') {
		href = mylink;
	} else {
		href = mylink.href;
	}

	window.open(href, windowname, 'width=620,height=400,scrollbars=yes,statusbar=no,resizable=no');
	return false;
}


/*

*/
function tallpopup(mylink, windowname) {
	var href;
	
	if (! window.focus) {
		return true;
	}
	
	if (typeof(mylink) == 'string') {
		href=mylink;
	} else {
		href=mylink.href;
	}
	   
	window.open(href, windowname, 'width=620,height=600,scrollbars=yes,statusbar=no,resizable=no');
	return false;
}


/*
Toggle Personal links display
*/
function toggleLinks(elem, imgElem) {
	display = document.getElementById(elem).style.display;
	
	// image manipulation for folders
	if (imgElem != "") {
		imgDisplay = document.getElementById(imgElem).src;
		if (document.getElementById(elem).style.display == "none") {
			document.getElementById(imgElem).src = "/images/minus_box.gif";
		} else {
			document.getElementById(imgElem).src = "/images/plus_box.gif";
		}
	}
			
	if ((elem) && (display == "none")) {
		document.getElementById(elem).style.display="";
	} else {
		document.getElementById(elem).style.display="none";
	}	
}


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


function preloadImages(imageArray, dirPrefix) {
	for(i = 0; i < imageArray.length; i++) {
		var imageName = new Image();
        imageName.src = dirPrefix + imageArray[i];
	}
}


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


/*
Save the current portal content
*/
function SaveContent() {
	submit_form();
	
	var tmpForm = findObj('portal_content_form');	
	tmpForm.submit();
}