var toolbarMenus = new Array;
var currentMenu = '';
var currentLeftHandNav = '';


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


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


/*
Highlight the specified left hand nav item
*/
function selectLeftNav(iNavItem) {
	if (iNavItem.className.indexOf('Current') == -1) { 
		iNavItem.className = 'leftHandNavActive';
		findObj(iNavItem.id + '_left').src = '/images/left_navs/left_border_active.gif';
		findObj(iNavItem.id + '_right').src = '/images/left_navs/right_border_active.gif';
	}
}


/*
Un-Highlight the specified left hand nav item
*/
function deSelectLeftNav(iNavItem) {
	if (iNavItem.className.indexOf('Current') == -1) { 
		iNavItem.className = 'leftHandNav';
		findObj(iNavItem.id + '_left').src = '/images/left_navs/left_border.gif';
		findObj(iNavItem.id + '_right').src = '/images/left_navs/right_border.gif';
	}
}


/*
Set the current left hand nav item
*/
function setCurrentLeftNav(iNavItem) {
	if (currentLeftHandNav.id) {
		currentLeftHandNav.className = 'leftHandNav';
		findObj(currentLeftHandNav.id + '_left').src = '/images/left_navs/left_border.gif';
		findObj(currentLeftHandNav.id + '_right').src = '/images/left_navs/right_border.gif';
	}
	
	currentLeftHandNav = iNavItem;
	
	if (iNavItem.className.indexOf('Current') == -1) { 
		iNavItem.className = 'leftHandNavCurrent';
		findObj(iNavItem.id + '_left').src = '/images/left_navs/left_border_active.gif';
		findObj(iNavItem.id + '_right').src = '/images/left_navs/right_border_active.gif';
	}
}


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


/*
Center a layer's position on the current screen
*/
function centerLayerOnScreen(obj, iMethod) {
	if (!obj.id) { obj = findObj(obj); }
	if (!obj) { return false; }

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


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


/*
Pop-up a menu item in the specified direction
*/
function popupMenu(iMenu, iParentItem, iDirection) {
	activateToolbarMenu(iMenu);	
	
	// Parent menu item
	if (iMenu.indexOf('_submenu') == -1) {
		hideToolbarMenus();
		
		var tmpMenu = findObj(iMenu);
		var tmpParent = findObj(iParentItem);
		var tmpX = findPosX(tmpParent);
		var tmpY = findPosY(tmpParent);
		
		if (iDirection == 'right') {
			tmpMenu.style.left = tmpX + 'px';
			tmpMenu.style.top = (tmpY + tmpParent.offsetHeight) + 'px';
		
		} else if (iDirection == 'left') {
			tmpMenu.style.left = ((tmpX + tmpParent.offsetWidth) - tmpMenu.offsetWidth) + 'px';
			tmpMenu.style.top = (tmpY + tmpParent.offsetHeight) + 'px';
		}
		
		toggleLayer(tmpMenu, 1);

	} else {
		//hideToolbarMenus();
		hideSubMenus();
		
		var tmpMenu = findObj(iMenu);
		var tmpParent = findObj(iParentItem);
		var tmpX = findPosX(tmpParent);
		var tmpY = findPosY(tmpParent);

		if (iDirection == 'right') {
			tmpMenu.style.left = (tmpX + tmpParent.offsetWidth) + 'px';
			tmpMenu.style.top = tmpY + 'px';
		
		} else if (iDirection == 'left') {
			tmpMenu.style.left = ((tmpX - tmpMenu.offsetWidth) + 2) + 'px';
			tmpMenu.style.top = tmpY + 'px';
		}
		
		toggleLayer(tmpMenu, 1);
	}
	
	
	toolbarMenus[toolbarMenus.length] = iMenu;	
}


/*
Make sure the current menu item is in the currentMenu var (to prevent hiding)
*/
function activateToolbarMenu(iMenuItem) {
	if (currentMenu.indexOf(iMenuItem + ',') == -1) {
		currentMenu += iMenuItem + ',';
	}
}


/*
Automatically hide any open toolbar menus after 300ms
*/
function autoHideToolbar(iMenuItem) {
	currentMenu = replace(currentMenu, iMenuItem + ',', ''); 
	
	setTimeout('hideToolbarMenus();', 350);
}


/*
Loop through the menu array and hide any open toolbar menus
*/
function hideToolbarMenus() {
	for (var i in toolbarMenus) {
		if ((toolbarMenus[i]) && (currentMenu.indexOf(toolbarMenus[i] + ',') == -1)) {
			toggleLayer(toolbarMenus[i], 0);
			delete toolbarMenus[i];
		}
	}
}


/*
Loop through the menu array and hide any open toolbar sub-menus
*/
function hideSubMenus() {
	for (var i in toolbarMenus) {
		if ((toolbarMenus[i]) && (currentMenu.indexOf(toolbarMenus[i] + ',') == -1) && (toolbarMenus[i].indexOf('_submenu') > -1)) {
			toggleLayer(toolbarMenus[i], 0);
			delete toolbarMenus[i];
		}
	}	
}


/*
SuckerTree Vertical Menu 1.1 (Nov 8th, 06)
By Dynamic Drive: http://www.dynamicdrive.com/style/
*/
function buildsubmenus() {
	var tmpObj = document.getElementById('suckertree1');
	if (!tmpObj) { return false; }
	
	var ultags = document.getElementById('suckertree1').getElementsByTagName("ul");
    for (var t = 0; t < ultags.length; t++) {
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfolderstyle";
    	
		if (ultags[t].parentNode.parentNode.id=='suckertree1') { // if this is a first level submenu
			ultags[t].style.left = ultags[t].parentNode.offsetWidth + "px"; // dynamically position first level submenus to be width of main menu item
		
		} else { // else if this is a sub level submenu (ul)
		  ultags[t].style.left = ultags[t-1].getElementsByTagName("a")[0].offsetWidth + "px"; // position menu to the right of menu item that activated it
		}
    
		ultags[t].parentNode.onmouseover = function() {
    		this.getElementsByTagName("ul")[0].style.display="block";
    	}
    	
    	ultags[t].parentNode.onmouseout = function() {
    		this.getElementsByTagName("ul")[0].style.display="none";
    	}
    }
	
    for (var t = ultags.length - 1; t > -1; t--) { // loop through all sub menus again, and use "display:none" to hide menus (to prevent possible page scrollbars
		ultags[t].style.visibility = "visible";
		ultags[t].style.display = "none";
	}
}

if (window.addEventListener) {
	window.addEventListener("load", buildsubmenus, false)

} else if (window.attachEvent) {
	window.attachEvent("onload", buildsubmenus)
}


/*

*/
function showPLANMenu(iID, iImage, iModifier, iY) {
	var tmpParent = findObj(iImage);
	var tmpX = findPosX(tmpParent)
	var tmpY = findPosY(tmpParent);
	
	findObj(iID).style.left = (tmpX + iModifier) + 'px';
	findObj(iID).style.top = ((tmpParent.offsetHeight + tmpY) + iY) + 'px';

	toggleLayer(iID, 1);
}


/*

*/
function hidePLANMenu(iID) {
	if (currentMenu != iID) { 
		toggleLayer(iID, 0);
	}
}


/*
Make sure the current menu item is in the currentMenu var (to prevent hiding)
*/
function activateToolbarMenu(iMenuItem) {
	if ((currentMenu != '') && (currentMenu != iMenuItem.id)) {
		findObj(currentMenu).src = '/images/toolbar/' + currentMenu + '_btn.gif';
	}

	currentMenu = iMenuItem.id;	
	findObj(iMenuItem.id).src = '/images/toolbar/' + iMenuItem.id + '_btn_active.gif';	
}


/*
Deactivate any active toolbar menus
*/
function deactivateToolbar() {
	document.menuTimeout = setTimeout('findObj(\'' + currentMenu + '\').src = \'/images/toolbar/' + currentMenu + '_btn.gif\';', 360);
}



/*
Login the user
*/
function loginUser() {
	/*
	var tmpFrm = findObj('login_frm');
	
	doFormSubmit('Logging In...', tmpFrm);
	clearFormErrors(tmpFrm);
	
	performAjax(tmpFrm, '/index.php?action=XML_LOGIN', '', '');
	*/
	
	doFormSubmit('Logging In...', findObj('login_frm'));
	submitAJAX('/index.php?action=LOGIN_USER', jQuery('#login_frm').serialize());
}


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