﻿// JScript File
function PopupWin(url, windowName, height, width, scrollbars)
{
	windowName = windowName + Math.round(100000*Math.random())

	window.open(url,windowName,'height=' + height + ',width=' + width + ',status=no,resizable=yes,scrollbars=' + scrollbars + ',toolbar=no,location=yes,menubar=no');
}

/// <summary>
/// Open a modal dialog
/// </summary>
function OpenDialog(url, windowName, width, height)
{
	if(window.showModalDialog)	// IE only
	{
		// Pass postback function in as modal dialog argument
		var args=new Object();
		args.window = window;
		args.doPostBack = doPostBack;  // function defined on calling page
		var sFeatures = 'dialogHeight:' + height + 'px;dialogWidth:' + width +
		                'px;resizeable:yes;scroll:no';
		window.showModalDialog(url,args,sFeatures);
	}
	else	// Firefox uses modal flag which just means stay on top
	{
		// Center dialog
		var x = (window.screen.availWidth - width)/2;
		var y = (window.screen.availHeight - height)/2;
		var win = window.open(url,windowName,
		    'height=' + height + ',width=' + width + ',top=' + y + ',left=' + x +
		    ',status=no,resizable=yes,scrollbars=no,toolbar=no,location=no,menubar=no,modal=yes');
	}
}

document.onmousemove = captureMousePosition;

////// GridView Actions to make it function like a listbox //////

//// Member variables ////
// gridWithFocus is the server side id of the grid. It is not the client side ID
// because the callback event requires the grid name rather than the grid ID and
// that needs to be set manually.
// ctrlPrePend is the part of the name and ID that asp.net prepends to the server
// side id of the control to ensure that it is unique. It is used in the functions
// to make sure that the correct controls are being called.
var gridWithFocus;
var ctrlPrePend;

// SetFocus sets the member variable gridWithFocus to the current gridview that 
// needs to appear to have focus.
// This takes the server side ID of the grid as an argument.
function SetFocus(gridID)
{
	gridWithFocus = gridID;
}

// SetPrePend is called when the control is loaded and it sets the member variable 
// ctrlPrePend to the string that asp.net prepends to the controls to ensure
// its uniqueness.
function SetPrepend(prePend)
{
	ctrlPrePend = prePend;
}

// This is an override that will keep existing calls to this function from breaking.
function KeyDownHandler(evt)
{
    return KeyDownHandler(evt, 'cbpTabSheet');
}

// KeyDownHandler handles the keystroke events on the page. It is called in the
// onload event of the body tag. If the keys pressed are either the up arrow key
// or the down arrow key, then it invokes the callback method for the row before
// or after the currently selected row depending on which key was pressed.
// JohnM: Added variable for panelID so that the callback panel will not have to be
// JohnM: named 'cbpTabSheet' every time.
function KeyDownHandler(evt, panelID)
{
	// Sets the variables that will be used depending on whether the methods 
	// require the name of the control or the ID of the control.
	var ctrlPrePendID = '';
	var ctrlPrePendName = '';
	if (ctrlPrePend != '')
	{
		ctrlPrePendID = ctrlPrePend + '_';
		ctrlPrePendName = ctrlPrePend + '$';
	}
	
	// Get the controls on the page that contain the information needed.
	var grid = document.getElementById(ctrlPrePendID + gridWithFocus);
	if (grid == null)
		return true;
	
	var next = document.getElementById(ctrlPrePendID + 'nextRow');
	var prev = document.getElementById(ctrlPrePendID + 'prevRow');
	var charCode = (evt.which) ? evt.which : event.keyCode
	
	// If the key pressed is not the up or down arrow then return true which lets the
	// event bubble up. If it is the up or down arrow then handle the event.
	if ((charCode != 38) && (charCode != 40))
	{
		return true;
	}
	else
	{
		// Only handle the event if the grid that is supposed to be handled is currently
		// being handled has the focus.
		if (gridWithFocus != '')
		{
			if (charCode == 38) // Up arrow
			{
				RadCallbackNamespace.AsyncRequest(ctrlPrePendName + gridWithFocus, 'Select$'+ prev.value, ctrlPrePendID + panelID)
			}
			
			if (charCode == 40) // Down arrow
			{
				RadCallbackNamespace.AsyncRequest(ctrlPrePendName + gridWithFocus, 'Select$'+ next.value, ctrlPrePendID + panelID)
			}
			
			return false;
		}
		else
		{
			return true;
		}
	}
}


// Sets the scroll position for the specified control
function SetScrollPosition(divID, pos)
{
	var div = document.getElementById(divID);
	div.scrollTop = pos;
}

// Sets the size of the control relative to the window size
function doSize()
{
    var treeDiv;
    var gridContainerDiv;
    var containerDiv;
    treeDiv = document.getElementById("treeDiv");
    gridContainerDiv = document.getElementById("gridContainerDiv");
    containerDiv = document.getElementById("containerDiv");

	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 ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

    myWidth = myWidth - treeDiv.offsetWidth - 35;
    if(myWidth < 700)
    {
        gridContainerDiv.style.width = 700;
    }
    else
    {
        gridContainerDiv.style.width = '100%';
    }

    if(containerDiv)
    {
        containerDiv.style.width = myWidth;
        containerDiv.style.height = 445;
    }
}


//displays the Reports menu
var DialogBox = null;

function findPos(obj) 
{
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}

	return [curleft,curtop];
}

function showReportsMenu(refObj)
{
	if(DialogBox == null)
		DialogBox = document.all("DialogBoxWrapper");
	DialogBox.style.display = "block";

	var coors = findPos(refObj);
	
	// This takes into consideration the 2 pixels for the border.
	var dialogWidth = DialogBox.offsetWidth + 2;
	
	// This takes into account the 2 pixels that are clear on the
	// the outside edge of the button.
	var buttonWidth = refObj.offsetWidth - 2;
	
	var btnCenter = buttonWidth / 2;
	var dialogCenter = dialogWidth / 2;
	var dialogOffset = dialogCenter - btnCenter;
	
	var left = coors[0] - dialogOffset;
	var top = coors[1] - DialogBox.clientHeight - 2;
		
	DialogBox.style.left = left 
	DialogBox.style.top = top;
	timeoutDialogBox(true);
}

function hideDialogBox()
{
	if(DialogBox)
		DialogBox.style.display = "none";
}

xMousePos = 0;
yMousePos = 0;
xMousePosMax = 0;
yMousePosMax = 0;

function captureMousePosition(e)
{
	if(document.layers)
	{
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth + window.pageXOffset;
		yMousePosMax = window.innerHeight + window.pageYOffset;
	}
	else if(document.all)
	{
	    if(document.body)
	    {
		    xMousePos = window.event.clientX + document.documentElement.scrollLeft;
		    yMousePos = window.event.clientY + document.documentElement.scrollTop;
		    xMousePosMax = document.body.clientWidth + document.body.scrollLeft;
		    yMousePosMax = document.body.clientHeight + document.body.scrollTop;
		}
	}
	else if(document.getElementById)
	{
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth + window.pageXOffset;
		yMousePosMax = window.innerHeight + window.pageYOffset;
	}
}

function timeoutDialogBox(isFirst)
{
	var boxX = parseFloat(DialogBox.offsetLeft);
	var boxY = parseFloat(DialogBox.offsetTop);
	var boxW = parseFloat(DialogBox.offsetWidth);
	var boxH = parseFloat(DialogBox.offsetHeight);
	
	if(isFirst || 
	   ((xMousePos >= boxX) && 
	    (xMousePos <= (boxX+boxW)) && 
	    (yMousePos >= boxY) && 
	    (yMousePos <= (boxY+boxH))))
		setTimeout("timeoutDialogBox(false)", 2000);
	else
		hideDialogBox();
}


// update the calling windows
function refreshParents(refresher)
{
    refreshParentsWithArgs(refresher, '');
}
// update the calling windows
function refreshParentsWithArgs(refresher, args)
{
    if(window.opener)
        window.opener.__doPostBack(refresher,args);
    if(window.opener.window.opener)
        window.opener.window.opener.location.href = window.opener.window.opener.location.href;
}

function refreshParent(refresher)
{
	refreshParentWithArgs(refresher, '');
}

function refreshParentWithArgs(refresher, args)
{
    if(window.opener)
        window.opener.__doPostBack(refresher,args);
}

function CheckForChanges()
{
	if (document.form1.hdnIsChanged.value == "1")
	{
		var msg = "The information on this page appears to have changed.\n\n";
		msg += "Click OK to lose changes and continue. \n";
		msg += "Click Cancel to continue working on this page.";
		
		var agree=confirm(msg);

		if (agree)
			return true ;
		else
			return false ;
	}
	else
	{
		return true;
	}
}

function MarkPageAsChanged()
{
	document.form1.hdnIsChanged.value = "1"
}

function WaitForChange(control)
{
	document.form1.hdnCurrentTextValue.value = control.value;
}

function StopWaitingForChange(control)
{
	document.form1.hdnCurrentTextValue.value = "";
}

function CheckForTextChange(control)
{
	if (control.value != document.form1.hdnCurrentTextValue.value)
		document.form1.hdnIsChanged.value = "1";
}
