//--------------------------------------------------------------------------------------
//	Modal Dialog Code
//--------------------------------------------------------------------------------------
var appVersion = parseInt(navigator.appVersion,10);
var Nav4 = ((navigator.appName == "Netscape") && (appVersion >= 4));
var dialogWin = new Object();

//--------------------------------------------------------------------------------------
function UserHitCancel()
{
	window.close();	
}

//--------------------------------------------------------------------------------------
function returndata(szDataString)
{
	if ( Nav4 )
	{
		// Netscape
		if ( top.opener && !top.opener.closed )
		{
			top.opener.dialogWin.returnedValue = szDataString;
			top.opener.dialogWin.returnFunc( szDataString );
		}
		else
		{
			alert("You have closed the main window.\n\nNo action will be taken on the choices in this dialog box.");
		}
		
		top.window.close();
	}
	else
	{
		// Internet Explorer
		// This modal dialog is in a frameset so use "top." instead of "window.".
		top.returnValue = szDataString;
		top.close();
	}
	
	return false;
}

//--------------------------------------------------------------------------------------
// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc -- reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
//--------------------------------------------------------------------------------------
function openDialog(url, width, height, returnFunc, args)
{
	if ( !dialogWin.win || (dialogWin.win && dialogWin.win.closed) )
	{
		// Initialize properties of the modal dialog object.
		dialogWin.returnFunc	= returnFunc
		dialogWin.returnedValue = ""
		dialogWin.args			= args
		dialogWin.url			= url
		dialogWin.width			= width
		dialogWin.height		= height
		
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dialogWin.name = (new Date()).getSeconds().toString()
		
		// Assemble window attributes and try to center the dialog.
		if ( 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 + ",resizable=no,scrollbars=yes,width=" +
			           dialogWin.width + ",height=" + dialogWin.height		
		}
		
		// Generate the dialog and make sure it has focus.		
		dialogWin.win = window.open( dialogWin.url, dialogWin.name, attr );
	}
	
    dialogWin.win.focus()
}

//--------------------------------------------------------------------------------------
//- Open a new window to enter the exact addr and mask for a specific client region
//--------------------------------------------------------------------------------------
function doModal(Url, Width, Height )
{
	if ( typeof( handleModalValues ) != 'undefined' )
	{
		if ( Nav4 )
		{
			openDialog( Url, Width, Height, handleModalValues );
		}
		else
		{
			var szValues = window.showModalDialog( Url, "", "dialogWidth:" + Width + "px; dialogHeight:" + Height +"px; center: yes;" );
			handleModalValues(szValues);
		}
	}
	else
	{
		alert( "Page does not implement \n\n     handleModalValues(szValues); \n\nThe dialog would not be able to communicate back without this.");
    }		
}

//--------------------------------------------------------------------------------------
//- Event handler to inhibit Navigator form element and IE link activity when dialog
//- window is active.
//--------------------------------------------------------------------------------------
function deadend()
{
	if ( dialogWin.win && !dialogWin.win.closed )
	{
		dialogWin.win.focus()
		return false
	}
}

//--------------------------------------------------------------------------------------
// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
//--------------------------------------------------------------------------------------
function blockEvents()
{
	if ( Nav4 )
	{
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = deadend	
	}
	else
	{
	    disableForms();
	}
	
	window.onfocus = checkModal
}

//--------------------------------------------------------------------------------------
// As dialog closes, restore the main window's original
// event mechanisms.
//--------------------------------------------------------------------------------------
function unblockEvents()
{
	if ( Nav4 )
	{
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
		window.onclick = null
		window.onfocus = null	
	}
	else
	{
	    enableForms()
	}
}

//--------------------------------------------------------------------------------------
// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.
//--------------------------------------------------------------------------------------
function checkModal()
{
    setTimeout( "finishChecking()", 50 );
    return true
}

//--------------------------------------------------------------------------------------
function finishChecking()
{
	if ( dialogWin.win && !dialogWin.win.closed )
	{
		dialogWin.win.focus()
	}
}

//--------------------------------------------------------------------------------------
// Since links in Internet Explorer 4 can't be disabled, preserve IE link onclick 
// event handlers while they're "disabled." Restore when reenabling the main window.
//--------------------------------------------------------------------------------------
var IELinkClicks

//--------------------------------------------------------------------------------------
// Disable form elements and links in all frames for IE.
//--------------------------------------------------------------------------------------
function disableForms()
{
   IELinkClicks = new Array()
   for (var h = 0; h < frames.length; h++)
   {
      for (var i = 0; i < frames[h].document.forms.length; i++)
      {
         for (var j = 0; j < frames[h].document.forms[i].elements.length; j++)
         {
            frames[h].document.forms[i].elements[j].disabled = true
         }
      }
      
      IELinkClicks[h] = new Array()
      for (i = 0; i < frames[h].document.links.length; i++)
      {
         IELinkClicks[h][i] = frames[h].document.links[i].onclick
         frames[h].document.links[i].onclick = deadend
      }
   }
}

//--------------------------------------------------------------------------------------
// Restore IE form elements and links to normal behavior.
//--------------------------------------------------------------------------------------
function enableForms()
{
   for (var h = 0; h < frames.length; h++)
   {
      for (var i = 0; i < frames[h].document.forms.length; i++)
      {
         for (var j = 0; j < frames[h].document.forms[i].elements.length; j++)
         {
            frames[h].document.forms[i].elements[j].disabled = false
         }
      }
      
      for (i = 0; i < frames[h].document.links.length; i++)
      {
         frames[h].document.links[i].onclick = IELinkClicks[h][i]
      }
   }
}


