
var ns6 = document.getElementById&&!document.all;
var vModalWindow, o, retVal;

function GetDimension(dim, isWidth) 
{ 
	if (! (dim.charAt(dim.length - 1) == "%") ) 
		return dim; 

	dim = dim.substring(0, dim.length-1); 
	if (isWidth == true) 
		dim = Math.round(screen.width * (dim/100)) + "px";
	else 
		dim = Math.round(screen.height * (dim/100)) + "px"; 

	return dim;
} 



function WebModal ( Anchor, AnchorID, Title, Url, WindowHeight, WindowWidth, Scrolling, Properties, PostbackOnUserClose, JSRunOnWindowClose ) {
	
	o = new Object;
	o.Title = Title;
	o.Properties = Properties;
	o.JSRunOnWindowClose = JSRunOnWindowClose;

	var queryStringPrefix;
	if (Url.indexOf(");")!=-1)
	{
		var myfnc = Url; // call js custom function
		Url = eval(myfnc); 
	}
	if (Url.indexOf("?")==-1)
		queryStringPrefix = "?";
	else
		queryStringPrefix = "&";
	
	var wWidth = GetDimension(WindowWidth, true);
	var wHeight = GetDimension(WindowHeight, false); 

	if (!ns6) {
	
		var returnValue = window.showModalDialog(Url + queryStringPrefix + "__LoadIFrame=True&__Scrolling=" + Scrolling +"&__PostbackOnUserClose=" + PostbackOnUserClose, o, "dialogHeight:" + wHeight + "; dialogWidth:" + wWidth + "; status:off; center:on; help:off");
		document.getElementById("__WebModalData").value = returnValue.WebModalData;
		document.getElementById("__CausePostBack").value = returnValue.CausePostBack;
		document.getElementById("__AnchorID").value = AnchorID;
		if (Anchor != null)
		{
			Anchor.OutputData = returnValue.WebModalData;
		}
		if (JSRunOnWindowClose != null && JSRunOnWindowClose != "")
			eval(JSRunOnWindowClose);
			

	
		return (returnValue.CausePostBack == "True");
	}
	else {
		o.AnchorID = AnchorID;
		o.Anchor = Anchor;
		vModalWindow = window.open(Url + queryStringPrefix + "__LoadIFrame=True&__Scrolling=" + Scrolling +"&__PostbackOnUserClose=" + PostbackOnUserClose, "", "dependent=yes, height=" + wHeight + ", width=" + wWidth);
		fCaptureEvents(window.top);
		vModalWindow.focus();
	}
	
}

function SetSelectedFile(nodeID, url)
{
	if (document.getElementById("__WebModalData").value != "")
	{
		url = url.replace("%FILEID%",document.getElementById("__WebModalData").value)
		document.getElementById(nodeID + "_fileID").value = document.getElementById("__WebModalData").value;
		document.getElementById(nodeID).setAttribute("src",url);
	}
}



function WebModalFFClose(returnValue)
{
		document.forms[0].__WebModalData.value = returnValue.WebModalData;
		document.forms[0].__CausePostBack.value = returnValue.CausePostBack;
		document.forms[0].__AnchorID.value = o.AnchorID;
		if (returnValue.CausePostBack == "True") document.forms[0].submit();
//		if (o.Anchor != null)
//		{
//			Anchor.OutputData = retVal.WebModalData;
//		}
		if (o.JSRunOnWindowClose != null && o.JSRunOnWindowClose != "")
			eval(o.JSRunOnWindowClose);

		return (returnValue.CausePostBack == "True");
}

function WireClientSideEvent(control, event, functionToAttach)
{
	var ctrl;
	if (typeof(control) != "undefined")
	{
		ctrl = control;
	}
	else
	{
		ctrl = document.getElementById(control);
	}

	if (typeof(ctrl) != "undefined")
	{
		if ( typeof( ctrl.addEventListener ) != "undefined" ) {
				ctrl.addEventListener(event, functionToAttach, false);
			} 
		else if ( typeof ( ctrl.attachEvent ) != "undefined" ) 
		{
			ctrl.attachEvent(event, functionToAttach);
		} 
		else 
		{
			ctrl[event] = functionToAttach;
		}
	}
}

function IgnoreEvents(e)
{
  return false
}

function HandleFocus()
{
	try
	{
		if (vModalWindow)
		{
			if (!vModalWindow.closed)
			{
			vModalWindow.focus()
			}
			else
			{
				fReleaseEvents(window.top);
				//window.unwatch("vDialogResultNN");
			}
		}
	}
	catch (e){
	return false
	}	  
  return false
}

function fCaptureEvents(vWindow)
{
	vWindow.captureEvents (Event.CLICK|Event.FOCUS)
	vWindow.onclick=IgnoreEvents
	vWindow.onfocus=HandleFocus 
		
	for (i=0;i<vWindow.frames.length;i++) 
	{
		var b = i
		fCaptureEvents(vWindow.frames[i]);
		i = b;
	}
}

function fReleaseEvents(vWindow)
{
	vWindow.releaseEvents (Event.CLICK|Event.FOCUS)
    vWindow.onclick = ""
      
	for (i=0;i<vWindow.frames.length;i++) 
	{
		var b = i
		fReleaseEvents(vWindow.frames[i]);
		i = b;
	}
}