var isDialogDisplayed = false;

var isIE=(document.all != null)&&(document.getElementById != null);
var isDOM=document.getElementById&&!document.all;
var isNS4=document.layers?true:false;
var isIE6withDocType=document.documentElement && document.documentElement.clientHeight;

var isDialogDisplayed = false;
var dialogHeight = 150;
var dialogWidth = 305;
var theDialogDiv;
var oldDialogInnerHTML;

if (isNS4)
{
  alert("Netscape Navigator 4 is not supported. Please use Netscape 7+, IE5.5+, or Mozilla 1.0+.");
}

function helpChangeState(state)
{
  document.toggleHelpVisibilityForm.wlpHelp.value = state;
  if(isIE) showWaitState();
  document.toggleHelpVisibilityForm.submit();
}

function toggleDialogBoxForDiv(theId)
{
    toggleDialogBox(theId, dialogHeight , dialogWidth);
}

function toggleDialogBox(theId,height,width,title)
{
	theDialogDiv = document.getElementById(theId);
	dialogHeight = height;
	dialogWidth = width;
   
	isDialogDisplayed = !isDialogDisplayed;
	dialogBkgdDiv = document.getElementById("dialogBackgroundDiv");

	//title = htmlChars(title);
	
	if (isDialogDisplayed) 
	{
	    addDialogTitle(title, theDialogDiv);
		setDialogSizeAndPos();
	} else
	{
		theDialogDiv.innerHTML = oldDialogInnerHTML;
	}
	toggleDialogBkgd();
	var viz = isDialogDisplayed ? "visible" : "hidden";
	theDialogDiv.style.visibility = viz;
	if (isDialogDisplayed)
	{
		var keepGoing = true;
		var divElements = theDialogDiv.getElementsByTagName('INPUT');
		for (i=0; i<divElements.length && keepGoing; i++)
		{
			if (divElements[i].type=="text")
			{
				divElements[i].focus();
				divElements[i].select();
				keepGoing = false;
			}
		}
		
		// if there is no text type, let us select the select type....
		if(keepGoing == true)
		{
            divElements = theDialogDiv.getElementsByTagName('SELECT');
            for (i=0; i<divElements.length; i++)
            {
                divElements[i].focus();
                break;
			}
		}		
	}
}

function addDialogTitle(title , theDialogDiv)
{
	oldDialogInnerHTML = theDialogDiv.innerHTML;
    /* ********************************************** */
    /* NOT USED TITLE BAR
	title = title ? title : "&nbsp;";
	var titleDiv = "<div class='table-label-bottom' ";
	titleDiv += "style='vertical-align:top;text-align:left;padding:6px;'>\n";
	titleDiv += "<span class='text_bold_12px'>" + title + "</span>\n";
	titleDiv += "</div>\n";
	titleDiv += "<div style='padding:6px;'>\n";
	theDialogDiv.innerHTML = titleDiv + oldDialogInnerHTML + "\n</div>\n";
    */
    /* ********************************************** */
    theDialogDiv.innerHTML = oldDialogInnerHTML;
}

function toggleDialogBkgd()
{
    var keepGoing = true;
	var dbdViz = isDialogDisplayed ? "visible" : "hidden";
	var hfdViz = isDialogDisplayed ? "none" : "block";

	var divElements = document.getElementsByTagName('SELECT');
	for (i=0; i<divElements.length; i++)
	{
		divElements[i].style.display = hfdViz;
	}

	if (isDialogDisplayed) 
	{
		divElements = theDialogDiv.getElementsByTagName('SELECT');
		for (i=0; i<divElements.length; i++)
		{
			divElements[i].style.display = "block";
		}
	}
	
	dialogBkgdDiv.style.visibility = dbdViz;
}



function setDialogSizeAndPos()
{
    dialogBkgdDiv = document.getElementById("dialogBackgroundDiv");
    
	if (isDialogDisplayed)
	{
		var base;
		
		if (isIE6withDocType)
		{
			base = document.documentElement;
		} else
		{
			base = document.body;
		}
		
		var ch = base.clientHeight;
		var sh = base.scrollHeight;
		var cw = base.clientWidth;
		var sw = base.scrollWidth;

		var theTop;
		var theLeft;
		
		if ( (ch - dialogHeight) < 0 )
		{
			theTop = base.scrollTop;
		} else {
			theTop = parseInt((ch - dialogHeight)/2) + base.scrollTop;				
		}

		if ( (cw - dialogWidth) < 0 )
		{
			theLeft = base.scrollLeft;
		} else {
			theLeft = parseInt((cw - dialogWidth)/2) + base.scrollLeft;				
		}
				
		if (isDOM)
		{
			// Appears to be Mozilla
			dialogBkgdDiv.style.height = ((ch > sh)? ch : sh) + "px";
			dialogBkgdDiv.style.width = ((cw > sw)? cw : sw) + "px";
			theDialogDiv.style.top = theTop + "px";
			theDialogDiv.style.left = theLeft + "px";
			theDialogDiv.style.height = dialogHeight + "px";
			theDialogDiv.style.width = dialogWidth + "px";
		} else
		{
			dialogBkgdDiv.style.height = (ch > sh)? ch : sh;
			dialogBkgdDiv.style.width = (cw > sw)? cw : sw;
			theDialogDiv.style.top = theTop;
			theDialogDiv.style.left = theLeft;
			theDialogDiv.style.height = dialogHeight;
			theDialogDiv.style.width = dialogWidth;
		}
	}
}

function showWaitState()
{
    var waitDiv = document.getElementById("waitDiv");
	if (waitDiv != null)
	{
		waitDiv.style.visibility = "visible";
	}
}
onresize=setDialogSizeAndPos;
onscroll=setDialogSizeAndPos;

/* *********** SUBMIT AND UTIL FUNCTION ********** */

function submitLogin() 
{
        if(isIE) showWaitState();
        //document.loginForm.submit();
        document.getElementById(getNetuiTagName("loginForm")).submit()
}

function submitCancelCreateResource() {
        toggleDialogBoxForDiv("dialogDiv");
}

var liberaLibroWindow;

function launchLiberaLibro()
{
 if (!liberaLibroWindow || liberaLibroWindow.closed)
        liberaLibroWindow = openNewWindow('/portalWeb/liberalibro/launchLiberaLibro.jsp','liberaLibro', 790, 570, true, true, false);
    liberaLibroWindow.focus();
}

