//////////////////////////////////////////////////////////////////////////
// Global for brower version branching.
//////////////////////////////////////////////////////////////////////////
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
var is_Mac	= (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
var is_IEmac= ((document.all)&&(is_Mac)) ? true : false;
    
//////////////////////////////////////////////////////////////////////////
// Body onload utility (supports multiple onload functions)
//////////////////////////////////////////////////////////////////////////
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
	if (is_IEmac && is_ie4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
	{
		if  (! window.onload)
			window.onload = SafeOnload;
		else
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	}
}

function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
	{
	  if( typeof(gSafeOnload[i]) == "function" )
	  	gSafeOnload[i]();
	  else
	  if( typeof(gSafeOnload[i]) == "string" )
			eval(gSafeOnload[i]);
	}
}

//////////////////////////////////////////////////////////////////////////

var gSafeOnUnload = new Array();
function SafeAddOnUnload(f)
{
	if (is_IEmac && is_ie4)  // IE 4.5 blows out on testing window.onload
	{
		window.onunload = SafeOnUnload;
		gSafeOnUnload[gSafeOnUnload.length] = f;
	}
	else 
	{
		if  (! window.onload)
			window.onunload = SafeOnUnload;
		else
		if (window.onunload != SafeOnUnload)
		{
			gSafeOnUnload[0] = window.onunload;
			window.onunload  = SafeOnUnload;
		}		
		gSafeOnUnload[gSafeOnUnload.length] = f;
	}
}

function SafeOnUnload()
{
	for (var i=0;i<gSafeOnUnload.length;i++)
	{
	  if( typeof(gSafeOnUnload[i]) == "function" )
	  	gSafeOnUnload[i]();
	  else
	  if( typeof(gSafeOnUnload[i]) == "string" )
			eval(gSafeOnUnload[i]);
	}
}

//////////////////////////////////////////////////////////////////////////

function DLGOpen(url, p, width, height, wName)
{ 
	dt = new Date(); wName = dt.getTime()
	openWindow(url + "?" + p, wName, width, height, "no", "no", "no", "no", "no", "no")
}

//////////////////////////////////////////////////////////////////////////

var gCallOPF = new Array();
function DLGExOpen(ctrl, url, p, width, height)
{ 
	if( ctrl.processed == undefined )
	{
		var wName = ctrl.id;
		gCallOPF[gCallOPF.length] =  wName + ';' + ctrl.id;
		openWindow(url + "?" + p, wName , width, height,"no", "no", "no", "no", "yes", "no")
	}
	else
		alert( hrefMessage );
	
	return false; // Open href
}

function DLGExOpen2(ctrl, url, p, width, height, resizeable, scrollbars)
{ 
	if( ctrl.processed == undefined )
	{
		var wName = ctrl.id;
		gCallOPF[gCallOPF.length] =  wName + ';' + ctrl.id;
		openWindow(url + "?" + p, wName , width, height, resizeable, "no", "no", "no", "yes", scrollbars)
	}
	else
		alert( hrefMessage );
	
	return false; // Open href
}

function DLGExClose() 
{
	if( opener )
	{
		for (var i=0;i<opener.gCallOPF.length;i++) 
		{
			var v = opener.gCallOPF[i].split(';');
			if( v[0] == window.name ) 
			{
				var ctrl = opener.document.getElementById(v[1]);
				if( ctrl != null )
				{
					if( opener.hrefMessage != "" ) ctrl.processed = true;
				
					if( opener.hideLineItem )
						ctrl.parentNode.parentNode.style.display = 'none';
					else if (opener.isInsideTable)
					{
					  ctrl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.background = opener.markColor;
					}
					else
						ctrl.parentNode.parentNode.style.background = opener.markColor;
				}
				break;
			}
		}
	}
	window.close();
}

function DLGCloseAndRefresh()
{
	if( opener ) opener.document.location.reload(0);
	window.close();
}

//////////////////////////////////////////////////////////////////////////
// Dialog / Popup Window
//////////////////////////////////////////////////////////////////////////

var gDialogs = new Array();
//var dialogWin  = new Object()
function openWindow(url, windowName, width, height, resizable, location, menubar, toolbar, status, scrollbars) 
{
	var dialogWin  = new Object()

	for (var i=0;i < gDialogs.length;i++) 
		if( gDialogs[i].name == windowName ) { dialogWin = gDialogs[i]; break; }
	
	 if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) 
	 {
		  // Initialize properties of the modal dialog object.
		  dialogWin.returnedValue = ""
		
		  //dialogWin.args = args
		  dialogWin.url		= url
		  dialogWin.width	= width
		  dialogWin.height	= height
		
		  features = "resizable=" + resizable + ",location=" + location + ",menubar=" + menubar + ",toolbar=" + toolbar + ",status=" + status + ",scrollbars=" + scrollbars;
		
		  // Keep name unique so Navigator doesn't overwrite an existing dialog.
		  if( windowName == "" )
			dialogWin.name = (new Date()).getSeconds().toString()
		  else
		    dialogWin.name = windowName;
		
		  // Assemble window attributes and try to center the dialog.
		  if (is_nav4) 
		  {
			// Center on the main window.
			dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width) / 2)
			dialogWin.top = window.screenY +  ((window.outerHeight - dialogWin.height) / 2)
			var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",width=" +	dialogWin.width + ",height=" + dialogWin.height + "," + features
		} 
		else 
	  	{
			// The best we can do is center in screen.
	   		dialogWin.left = (screen.width - dialogWin.width) / 2
	   		dialogWin.top = (screen.height - dialogWin.height) / 2
	   		var attr = "left=" + dialogWin.left + ",top=" +	dialogWin.top + ",width=" + dialogWin.width + ",height=" + dialogWin.height + "," + features
	  	}
	
	  	// Generate the dialog and make sure it has focus.
	  	dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr)
	  	dialogWin.win.focus()
	  	gDialogs[gDialogs.length] = dialogWin;
	} 
 	else 
 		dialogWin.win.focus()
}

//////////////////////////////////////////////////////////////////////////
// Print
//////////////////////////////////////////////////////////////////////////
function printFrame(frame, onfinish) {
  if ( is_nav ) {
     if ( !frame ) window.print(); else frame.print();
     return;
  }
  if ( ! frame ) frame = window;
  if ( frame.document.readyState != "complete" )
  {   
      if ( !confirm("O documento a ser impresso ainda não terminou de ser carregado! Proceder com a impressão?") )
      {
         if ( onfinish ) onfinish();
         return;
      }
  }
  if ( is_ie5up ) {
    /* focus handling for this scope is IE5Beta workaround, should be gone with IE5 RTM. */
    var focused = document.activeElement; 
    frame.focus();
    frame.self.print();
    if ( onfinish ) onfinish();
    if ( focused && !focused.disabled ) focused.focus();
    return;
  }
  // fake print() for IE4.x
  if ( is_ie4 ) 
  {
	window.print = printFrame;

	var eventScope = printGetEventScope(frame);
	var focused = document.activeElement;

	window.printHelper = function() {
		execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
		printFireEvent(frame, eventScope, "onafterprint");
		printWB.outerHTML = "";
		if ( onfinish ) onfinish();
		if ( focused && !focused.disabled ) focused.focus();
		window.printHelper = null;
	}
	document.body.insertAdjacentHTML("beforeEnd",
		"<object id=\"printWB\" width=0 height=0 \
		classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");

	printFireEvent(frame, eventScope, "onbeforeprint");
	frame.focus();
	window.printHelper = printHelper;
	setTimeout("window.printHelper()", 0);
  }
}

// Print helpers
function printFireEvent(frame, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": frame.execScript(handler); break;
    case "function": handler();
  }
}

function printGetEventScope(frame) {
  var frameset = frame.document.all.tags("FRAMESET");
  if ( frameset.length ) return frameset[0];
  return frame.document.body;
}

//////////////////////////////////////////////////////////////////////////
// MPLS :: Load
//////////////////////////////////////////////////////////////////////////
function checkFrames() { }

function waitAndRedirect(seconds, url)
{
	var r = "redirectUrl('" + url + "')";
	setTimeout(r, seconds*1000);
}
function redirectUrl(url)
{
	document.location.replace(url);
}
function loadFrameContent(frame, url)
{
	document.frames[frame].document.location.replace(url);
}
function loadIFrameContent(frame, url)
{
	var frameObj = document.getElementById(frame);
	frameObj.src = url
}

//////////////////////////////////////////////////////////////////////////
// MPLS :: Focus
//////////////////////////////////////////////////////////////////////////
function FirstFocus_FindFirst()
{
	for( var j = 0; j < document.forms.length; j++ ) {
		var theForm = document.forms[j];
		for (var i = 0; i < theForm.length; i++)
		{
			if (	theForm.elements[i].type == "text"			|| 
	   				theForm.elements[i].type == "password"		|| 
	   				theForm.elements[i].type == "button"		|| 
	   				theForm.elements[i].type == "submit"		|| 
	   				theForm.elements[i].type == "select-one"	|| 
					theForm.elements[i].type == "select-multiple")
			{
				if( theForm.elements[i].disabled  == undefined  || theForm.elements[i].disabled  == false )
				{
					if( theForm.elements[i].AutoFocus == undefined || theForm.elements[i].AutoFocus == true )
					{
						try
						{
							theForm.elements[i].focus();
						}
						catch (err)
						{
						}
						break;
					}
				}
			}
		}
	}
}
function FirstFocus_Focus(id, name)  {
	var targetControl = FirstFocus_FindControl( id, name );
	if ( targetControl != null && targetControl.focus ) {
		try
		{
			targetControl.focus();
		}
		catch(err)
		{
		}
	}
}
function FirstFocus_FindControl( id, name ) {
	if ( typeof(document.getElementById) != "undefined" ) {
		var focusControl = document.getElementById(id);
		if ( focusControl != null ) {
			return focusControl;
		}
	}
	for( var i = 0; i < document.forms.length; i++ ) {
		var theForm = document.forms[i];
		var focusControl = theForm[name];
		if ( focusControl != null ) {
			return focusControl;
		}
	}
	return null;
}

//////////////////////////////////////////////////////////////////////////
// Box/Screen functions
//////////////////////////////////////////////////////////////////////////

function pageShowHourglass()
{
  document.body.style.cursor = 'wait';
}

function getTop(obj)
{
   return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getTop(obj.offsetParent) );
}
function centerPanel(panel, refArea)
{
	if( panel && refArea )
	{
		panel.style.left = (refArea.offsetWidth - panel.offsetWidth) / 2
		panel.style.top  = getTop(refArea);
	}
}
function hidePanel(panel)
{
	if( panel ) panel.style.display="none";
}
function showPanel(panel)
{
	if( panel ) panel.style.display="block";
}
function hideShowPanel(panel)
{
	if( panel )
	{
		if( panel.style.display=="block" ) 
			panel.style.display = "none";
		else
			panel.style.display = "block";
	}
}
function hideShowPanelById(id)
{
	obj = document.getElementById(id);
	hideShowPanel(obj);
}
function ToogleGR(id)
{
	obj = document.getElementById("td"+id);
	lit = document.getElementById("sp"+id);
	if( obj )
	{
		if( obj.style.display == 'none' )
		{
			obj.style.display = ''; 
			lit.innerText = "-";
		}
		else 
		{
			obj.style.display = 'none';
			lit.innerText= "+";
		}
	}
}

function resizeAndCenter(width, height)
{
	window.resizeTo(width, height);

	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else 
	if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	// Assemble window attributes and try to center the dialog.
	if (is_nav4) 
	{
		// Center on the main window.
		var left = window.screenX + ((window.outerWidth - width) / 2)
		var top = window.screenY  + ((window.outerHeight - height) / 2)
		window.moveTo(left, top)
	} 
	else 
	{
		// The best we can do is center in screen.
	   	var left = (screen.width  - width) / 2
	   	var  top = (screen.height - height) / 2
		window.moveTo(left, top)
	}

}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

// setCookie function
// sets the menu cookie
function setCookie(cookieName, value, expires) {
	if (expires != null)
		cookieInfo = cookieName + "=" + escape(value) + ";expires=" + expires + ";path=/";
	else
		cookieInfo = cookieName + "=" + escape(value) + ";path=/";
		
	document.cookie = cookieInfo;  
	return document.cookie;
}

// getExpirationDate function
// gets the menu cookie from the browser
function getExpirationDate(days){
	today = new Date();
	today.setTime(Date.parse(today) + (days * 60 * 60 * 24 * 100));
	return  today.toUTCString();
}


//////////////////////////////////////////////////////////////////////////

//Insere cookie indicando timezone
setCookie("tzOffset", (new Date).getTimezoneOffset(), null);
