// this variable will hold the window obect
// we only allow one pop-up at a time
var popUpWnd = null;
var modalWnd = null;


/*/
 *  PURPOSE:
 * 		To create and center a pop-up window.
 * 
 *  COMMENTS:
 * 		It will replace to old pop-up if called
 *  	without calling DestroyPopUpWnd() first..
/*/

function CreatePopUpWnd (url, width, height, resize, name)
{
	var doCenter = false;

	if(!name)
	{
		name = "ATT_POPUP";
	}
	
	if((popUpWnd == null) || popUpWnd.closed)
	{
		attribs = "";

		/*/ there's no popUpWnd displayed /*/

		// assemble some params
		if(resize) size = "yes"; else size = "no";

		/*/
		 *  We want to center the pop-up; however, to do this we need to know the
		 *  screen size.  The screen object is only available in JavaScript 1.2 and
		 *  later (w/o Java and/or CGI helping), so we must check for the existance
		 *  of it in the window object to determine if we can get the screen size.
		 * 
		 *  It is safe to assume the window object exists because it was implemented
		 *  in the very first version of JavaScript (that's 1.0).
		/*/
		for(var item in window)
			{ if(item == "screen") { doCenter = true; break; } }

		if(doCenter)
		{	/*/ center the window /*/

			// if the screen is smaller than the window, override the resize setting
			if(screen.width <= width || screen.height <= height) size = "yes";

			WndTop  = (screen.height - height) / 2;
			WndLeft = (screen.width  - width)  / 2;

			// collect the attributes
			attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
			"status=no,toolbar=no,directories=no,menubar=no,location=no,top=" + WndTop + ",left=" + WndLeft;
		}
		else
		{
			/*/
			 *  There is still one last thing we can do for JavaScrpt 1.1
			 *  users in Netscape.  Using the AWT in Java we can pull the
			 *  information we need, provided it is enabled.
			/*/
			if(navigator.appName=="Netscape" && navigator.javaEnabled())
			{	/*/ center the window /*/

				var toolkit = java.awt.Toolkit.getDefaultToolkit();
				var screen_size = toolkit.getScreenSize();

				// if the screen is smaller than the window, override the resize setting
				if(screen_size.width <= width || screen_size.height <= height) size = "yes";

				WndTop  = (screen_size.height - height) / 2;
				WndLeft = (screen_size.width  - width)  / 2;

				// collect the attributes
				attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
				"status=no,toolbar=no,directories=no,menubar=no,location=no,top=" + WndTop + ",left=" + WndLeft;
			}
			else
			{	/*/ use the default window position /*/

				// override the resize setting
				size = "yes";

				// collect the attributes
				attribs = "width=" + width + ",height=" + height + ",resizable=" + size + ",scrollbars=" + size + "," + 
				"status=no,toolbar=no,directories=no,menubar=no,location=no";
			}
		}

		// create the window
		popUpWnd = open(url, name, attribs);
	}
	else
	{
		// destory the current window
		DestroyPopUpWnd();
		// recurse, just once, to display the new window
		CreatePopUpWnd(url, width, height, resize, name);
	}
}

/*/
 *  PURPOSE:
 * 		To destroy the pop-up window.
 * 
 *  COMMENTS:
 * 		This is available if wish to destroy
 *  	the pop-up window manually.
/*/

function DestroyPopUpWnd ()
{
	// close the current window
	if(popUpWnd != null)
	{
		popUpWnd.close();
		popUpWnd = null;
	}
}

function GetCheckBoxValue(checkBoxId)
{
	var checkBox; 
	checkBox = document.getElementById(checkBoxId);
	if(null != checkBox)
	{
		return checkBox.checked;
	}
	else
	{
		return false;
	}
}

function GetRadioButtonListSelectedValue(radioButtonListId)
{
	for(i=0;;i++)
	{
		if(document.getElementById(radioButtonListId + '_' + i))
		{
			if(document.getElementById(radioButtonListId + '_' + i).checked)
			{
				return document.getElementById(radioButtonListId + '_' + i).value;
			}
		}
		else
		{
			return null;
		}
	}
}

function SetControlVisibility(controlId, isVisible)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return;

	if(isVisible)
	{
		if(controlId == 'TDLeft')
		{
			// ctl.style.display='auto'; 
		}
		else
		{
			ctl.style.display='block'; 
		}
			
		ctl.style.visibility='visible';
	}
	else
	{
		ctl.style.display='none';
		ctl.style.visibility='hidden';
	}
}

function SetControlState(controlId, isEnabled)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return;

	ctl.disabled = !isEnabled;
}

function GetControlValue(controlId)
{
	var ctl = document.getElementById(controlId);
	if(!ctl)return null;
	
	return ctl.value;
}


function igtbl_getNumberOfSelectedRows(gridName)
{
	var selCount = 0;
	var oGrid = igtbl_getGridById(gridName);
	if(null == oGrid)return;

	for(i=0;i<oGrid.Rows.length;i++)
	{
		if(oGrid.Rows.getRow(i).getSelected())selCount++;
	}
	return selCount;
}

function IgnoreEvents(e)
{
	return false;
}

function ShowModalDialogBox(url, title, width, height, scroll, resize)
{
	var attributes;
	if (window.showModalDialog)
	{
		attributes = 'center:yes;status:no;dialogHide:true;help:no;';
		if(scroll)
		{
			attributes+='scroll:yes;';
		}
		else
		{
			attributes+='scroll:no;';
		}

		if(resize)
		{
			attributes+='resizable:yes;';
		}
		else
		{
			attributes+='resizable:no;';
		}
		
		attributes+= 'dialogWidth:' + width + 'px;';
		attributes+= 'dialogHeight:' + height + 'px;';
		window.showModalDialog(url,title,attributes);
		return window.returnValue;
	}
	else
	{
		window.top.captureEvents(Event.CLICK|Event.FOCUS);
		window.top.onclick=IgnoreEvents;
		window.top.onfocus=HandleFocus; 
		attributes = 'dependent=yes,'
		attributes+= 'width=' + width + 'px,';
		attributes+= 'height=' + height + 'px,';
		modalWnd = window.open(url,title,attributes);
		modalWnd.focus();
	}
}

function HandleFocus()
{
	if (modalWnd)
	{
		if (!modalWnd.closed)
		{
			modalWnd.focus();
		}
		else
		{
			window.top.releaseEvents (Event.CLICK|Event.FOCUS);
			window.top.onclick = '';
		}
	}
return false;
}

function ReloadFrame(frameName)
{
	var targetFrame = top.frames[frameName];
	if(null != targetFrame)targetFrame.location.reload(true);
}

function PrintFrame(frameName)
{
	var targetFrame = top.frames[frameName];
/*	if(window.print)
	{
//		alert("window.print");
		window.print();
	}
*/
	if(null != targetFrame)
	{
//		alert("targetFrame.Print");
		targetFrame.focus();
		targetFrame.print();
	}
}

function PrintIFrame(frameName)
{
	var targetFrame = window.frames[frameName];
	if(null != targetFrame)
	{
//		alert("targetFrame.Print");
		targetFrame.focus();
		targetFrame.print();
	}
}

function DefaultButtonHandler(ctlId)
{
	var btn = document.getElementById(ctlId);
	if(!btn)return;
	if(!btn.click)return;
	
	if(window.event != undefined)
	{	
		// process only the Enter key
		if (event.keyCode == 13)
		{
				// cancel the default submit
				event.returnValue=false;
				event.cancel = true;
				// submit the form by programmatically clicking the specified button
				btn.click();
		}
	}
}

