/* ----------------------------------------------------------------------------------------------
	Begin Global Scripts

	These scripts are for the Link home page and all other javascript files.

	The home page consists of the home document and hidden Div tags that compose the
	tab pages.  The Divs are hidden and shown as appropriate.
   ---------------------------------------------------------------------------------------------- */

// -- BEGIN GLOBAL CONSTANTS --

	TAB  = "Tab";
	DIV  = "Div";
	HOME = "Link";
	TAB_COLLECTION_DIV  = "TabCollection";
	SELECT_PIPELINE_DIV = "SelectPipelineList";
	PIPELINE_DD_DIV     = "PipelineTabDropdown";
	CHANGE_PASSWORD_DIV = "ChangePassword";
	TABPAGE_SRC 	    = "%1Home.asp?pipe=%2";
	MSG_DISPLAY_DIV     = "MessageDiv";
	PIPE_ID_ATTR        = "_pipe";

	MSG_TXT_DISPLAY_INDEX  = new Number(0);	// index for Message Contents IFRAME
	SEL_BUS_UNIT_INDEX     = new Number(1);	// index for Select Bus Unit Drop-down IFRAME
	TABPAGE_DD_INDEX       = new Number(2);	// index for Tabpage Dropdown IFRAME
	TABPAGE_START_INDEX    = new Number(3);	// Starting index for 1st tab page IFRAME.  Skip Drop-down menus

// -- END GLOBAL CONSTANTS --

// -- BEGIN IVARS --

	_pipelineId    = HOME;
	_authString = "";
// -- END IVARS --

// Loop through all pipeline tab labels until we match an X coordinate.  This is yet another
// N.S. hack.  I hate N.S.
function getPipeIdForTabPageDD() {
	var xCoord = document.getElementById(PIPELINE_DD_DIV).offsetLeft;
	var objs   = document.getElementsByTagName("LABEL");
	for( var idx=0; idx<objs.length; idx++ ) {
		if( objs[idx].getAttribute(PIPE_ID_ATTR) != null ) {
			if ( objs[idx].offsetLeft == xCoord ) {
				return objs[idx].getAttribute(PIPE_ID_ATTR);
			}
		}
	}
}

function userAuthenticated() {
	var changePasswordDiv;
	if ( document.frames != null ) {	// I.E.
		changePasswordDiv = document.frames[TABPAGE_START_INDEX].document.getElementById(CHANGE_PASSWORD_DIV);
	}
	else {
	// No op for Netscape
//		changePasswordDiv = document.window.frames[TABPAGE_START_INDEX].document.getElementById(CHANGE_PASSWORD_DIV);
	}
	if( changePasswordDiv != null )
		changePasswordDiv.parentElement.style.display="block";
}

function getAuthentication() {
	return _authString;
}

// Moused over Drop down Menu of Pipelines.  Show List
function mouseOverSelectPipeline() {
	document.getElementById(SELECT_PIPELINE_DIV ).style.display="block";
	
	return;
}
// Moused off Drop down Menu of Pipelines.  Hide List
function mouseOutSelectPipeline() {
//	if( document.all == null) {  //netscape
//		setTimeout( "delaymouseOutPipelineTab(SELECT_PIPELINE_DIV)", 2000 );
//		return;
//	}
	document.getElementById(SELECT_PIPELINE_DIV ).style.display="none";
	return;
}
function delaymouseOutPipelineTab(DIV) {
//alert("delay");
	document.getElementById(DIV ).style.display="none";
}
// Relocate Pipeline tab drop-down
function movePipelineDropdown(event) {
	var eventSrc = getEventSource(event);

	if( eventSrc != null) {
		var obj = document.getElementById(PIPELINE_DD_DIV );
		obj.style.left = new Number(eventSrc.offsetLeft);
	}
	return;
}

// Moused over Drop down Menu on pipeline tab.  Show List
function mouseOverPipelineTab(event) {
	document.getElementById(PIPELINE_DD_DIV ).style.display="block";
	return;
}
// Moused off Drop down Menu on pipeline tab.  Show List
function mouseOutPipelineTab(event) {
//	if( document.all == null) {  //netscape
//			setTimeout( "delaymouseOutPipelineTab(PIPELINE_DD_DIV)", 2000 );
//		return;
//	}

	document.getElementById(PIPELINE_DD_DIV ).style.display="none";
	return false;
}


// Helper method to get source object for event
function getEventSource(event) {
	if( event == null )
		return null;

	var eventSrc = event.srcElement;
	if( eventSrc == null ) {	//if N.S.
		eventSrc = event.target;
		if( eventSrc.nodeType == 3 ) {
			eventSrc = eventSrc.parentNode;
		}
	}

	return eventSrc;
}


// Return an IFRAME named
function getIFrameNamed(aName) {
	var iframes = document.getElementsByTagName("IFRAME");

	for( var idx = 0; idx < iframes.length; idx++ ) {
		if( iframes[idx].id == aName ) {
			if ( document.frames != null ) {	// I.E.
				return document.frames(idx)
			}
			else {	// N.S.
				return this.window.frames[idx]
			}
		}
	}
}

// Helper method to hide last tab page
function hideLastTabPage() {
	document.getElementById( _pipelineId + DIV ).style.display="none";
	document.getElementById( _pipelineId + TAB ).className="navglobaloff";
}

// Helper to show new tab page
function showNewTabPage(pipe) {
	_pipelineId=pipe;

     var legal = getLegalInfo(pipe) ;  
 
     // change the title
    if (legal.length > 0) {
         document.title = legal + " - Link Informational Postings ";
         }
   else {
        document.title = "Link Informational Postings";
        }
	var div=document.getElementById( _pipelineId + DIV );
	if( div == null ) {
		alert( "UNABLE TO FIND TABPAGE " + _pipelineId + DIV );
		return;
	}

	if( div.src.toString() == "" ) {
		var theUrl = TABPAGE_SRC.replace("%1", _pipelineId );
		theUrl = theUrl.replace("%2", _pipelineId.toUpperCase() );
		div.src = theUrl;
//		div.
	}
	div.style.display="block";

	document.getElementById( _pipelineId + TAB ).className="navglobalon";

	handleResize();

	// Notify IFrame it is about to be "shown"
	try {
		getIFrameNamed(_pipelineId + DIV).refreshDisplay();
	}
	catch(ex){}

}
function getLegalInfo(pipe) {
    legalArrayCode= pipeLegalCode.split("|");
    legalArrayName= pipeLegalName.split("|");

 	for(var i = 0; i<legalArrayCode.length-1; i++) {
	
			var found = false;
			var n = 0;

		while (!found && n < legalArrayCode.length) {
			if (legalArrayCode[n] == (pipe)) {
					found = true;
					var hName = legalArrayName[n];
				}
				
				n++;
	    }
	}    
   if (found) {
        return  hName;
        }else{
         return "";
      }

}

function getPipeAbbrviation(pipe) {
    legalArrayCode= pipeLegalCode.split("|");
    abbrArray= pipeLineAbbr.split("|");

	for(var i = 0; i<legalArrayCode.length-1; i++) {

			var found = false;
			var n = 0;

		while (!found && n < legalArrayCode.length) {
			if (legalArrayCode[n] == (pipe)) {
				found = true;
				var hName = abbrArray[n];
			}

			n++;
	    }
	}    
	if (found) {
        return  hName;
    }else{
         return "";
    }

}

// Open up a new Pipeline if not already open
function openPipe(event, aPipe, aLabel) {
	var eventSrc = getEventSource(event);
	var pipe     = aPipe;
	var tabLabel = aLabel;

	if(pipe != "Security")
		tabLabel = getPipeAbbrviation(pipe);
		
	document.getElementById	(SELECT_PIPELINE_DIV).style.display="none";

	if( pipe != null ) {
		// Create new "tab" on Tab bar across top of page
		if( document.getElementById(pipe+"Tab") == null ) {
			var html = document.getElementById("TabCollection").innerHTML;

			html+="<Label class='navglobaloff' _pipe=" + pipe;
			html+=" id=" + pipe + "Tab ";
			html+="onClick='tabClicked(event);' onFocus='if(this.blur)this.blur();' onmouseover='movePipelineDropdown(event);mouseOverPipelineTab(event);' onmouseout='mouseOutPipelineTab(event);'>"
			html+= tabLabel + "</Label>";

			document.getElementById(TAB_COLLECTION_DIV).innerHTML=html;
		}
		else {
			document.getElementById(pipe+TAB).style.display="";
			var html = document.getElementById("TabCollection").innerHTML;
			document.getElementById(TAB_COLLECTION_DIV).innerHTML=html;
		}

		//Remember this pipeline in a cookie
		addPipePref(pipe);

		// Hide whatever tab page we are on and show the new one.
		hideLastTabPage();
		showNewTabPage(pipe);
	}
	else {
		alert( "Cannot open Pipeline tabpage because no pipe name was specified" );
	}
	// Override I.E. event handling
	return false;
}
// This removes the tabpage from the top menu,
// hides the current page and resets it back to EmptyFrame.htm
function closePipe() {
	if( _pipelineId == HOME ) {
		alert("You cannot close the Link Home Page" );
		return;
	}

	var res = window.confirm("OK to Close Business Unit?  Any unsaved work will be lost." )
	if( ! res )
		return;

	var div=document.getElementById( _pipelineId + DIV );
	if( div == null ) {
		alert( "UNABLE TO FIND TABPAGE " + _pipelineId + DIV );
		return;
	}

	//Remove this pipeline from cookie
	removePipePref(_pipelineId.toUpperCase());


	hideLastTabPage();

	div.src = "";

	// Remove from Tabs across top
	if( document.all )
		document.getElementById(_pipelineId +"Tab").outerHTML = "";
	else
		document.getElementById(_pipelineId +"Tab").style.display = "none";


	showNewTabPage(HOME);

	return false;
}

// Tab label clicked
function tabClicked(event){
	var eventSrc = getEventSource(event);

	if( eventSrc != null && eventSrc.getAttribute("id") != ( _pipelineId + TAB ) ) {
		hideLastTabPage();
		showNewTabPage(eventSrc.getAttribute("_pipe"));
	}
}

function handleResize() {
	var newDim = new Object();
	var frames;

	if( document.body.clientWidth != null ) {	// I.E.
		newDim.width =document.body.clientWidth;
		newDim.height=document.body.clientHeight;
	}
	else if ( window.innerWidth != null ) {		// N.S.
		newDim.width =new Number(window.innerWidth) -5 ;
		newDim.height=new Number(window.innerHeight) -5;
	}
	else
		alert( "handleResize() - Doesn't know what to do" );

	// Resize all frames so they match screen size
	frames = document.getElementsByTagName("IFRAME");
	for( var idx=TABPAGE_START_INDEX; idx<frames.length; idx++ ) {
		frameResized( frames[idx], newDim.width, newDim.height );
	}
}

function frameResized(iframeElement, newWidth,newHeight) {
	newHeight -= iframeElement.offsetTop;
	var minW = new Number(iframeElement.getAttribute("_minWidth" ));
	var minH = new Number(iframeElement.getAttribute("_minHeight" ));

	iframeElement.style.height = ( newHeight > minH ? newHeight : minH ) + 'px';
	iframeElement.style.width  = ( newWidth  > minW ? newWidth  : minW ) + 'px';

}

function formatTime() {
	var dt  = new Date();
	var hr = new String(dt.getHours());
	var mn = new String(dt.getMinutes());
	var sc = new String(dt.getSeconds());

	hr = ( hr.length==1) ? "0" + hr : hr;
	mn = ( mn.length==1) ? "0" + mn : mn;
	sc = ( sc.length==1) ? "0" + sc : sc;

	return hr + ":" + mn + ":" + sc;
}
/***************************************************************************************
 * These methods are used to hide a show messages that are sent via the applet.  They
 * appear at the top of the screen and, as you mouse over them, show/hide an Iframe that
 * displays the actual contents of the message.
 ***************************************************************************************/
function showAlertDetail(event) {
	document.getElementsByTagName("IFRAME")[MSG_TXT_DISPLAY_INDEX].style.display="block";
	if( document.frames != null )
		document.frames[MSG_TXT_DISPLAY_INDEX].document.body.innerHTML=getEventSource(event).getAttribute("_msg");
	else
		this.window.frames[loginIdx].document.body.innerHTML=getEventSource(event).getAttribute("_msg");
}
function hideAlertDetail(event) {
	document.getElementsByTagName("IFRAME")[MSG_TXT_DISPLAY_INDEX].style.display="none";
}

// ------------------------------------------------
// This will add a new <DIV> to the list of messages.
// There is an 'X' to close, the date/time, the pipe, and the message title
// ------------------------------------------------
function showAlert(aTitle, aMsg, aPipe) {

	var obj = document.getElementById(MSG_DISPLAY_DIV);
	var newMsg = "<div><img style='position:relative;top:2px;left:2px;' src=/images/alert_close.gif onclick='clearAlert();' title='Click to clear message' style=' cursor: default; cursor: pointer;'></img> " + formatTime() +  " - " + aPipe + " - ";
	newMsg += "<a style=\"color:white;\" href='javascript:void(0);' onclick=\"openPipe(null,'" + aPipe + "','');\" onmouseover=\"showAlertDetail(event);\" onmouseout=\"hideAlertDetail(event);\" ";
	newMsg += "title='Click to go to Business Unit that generated this message' _msg='" + aMsg + "'>";
	newMsg += aTitle + "</a></div>";
	obj.innerHTML=newMsg + obj.innerHTML;

	obj.style.backgroundColor="#5F84C2";
}
function clearAlert() {
	var eventSrc = getEventSource(event);
	eventSrc.parentNode.outerHTML="";

	var src = new String( document.getElementById(MSG_DISPLAY_DIV).innerHTML );
	if( src.length==0 )
		document.getElementById(MSG_DISPLAY_DIV).style.backgroundColor="white";
}
/************ END MESSAGE DISPLAY METHODS *******************/
