﻿var outerYBand = 0; // title+border
var outerXBand = 0; // 2*border

MIN_WIDTH = 800;
MAX_WIDTH = 800;
MIN_HEIGHT = 600;
MAX_HEIGHT = 600;

var focusopener = false;

function SetAsDialog()
{            
	AddEventHandling(window, 'unload', windows_page_unload);
	AddEventHandling(window, 'focus', windows_page_focus);
	AddEventHandling(window, 'resize', windows_page_focus);
	AddEventHandling(window, 'click', windows_page_focus);
	AddEventHandling(window, 'dblclick', windows_page_focus);	
}

        
var bUseAvailLeft =  (typeof(screen.availLeft) != 'undefined');

var BASE_LEFT = (bUseAvailLeft ? screen.availLeft : 0);
var BASE_TOP = (typeof(screen.availTop) != 'undefined' ? screen.availTop : 0);
	
function CenterX(winwidth) {
	return (screen.availWidth - outerXBand - BASE_LEFT - winwidth) >> 1;
}                                                        

function CenterY(winheight) {
	return (screen.availHeight - BASE_TOP - outerYBand - winheight) >> 1;
}

function FitWindowToAvailableScreen( objWindow, iMaxWidth, iMaxHeight, iMinWidth, iMinHeight){
	
	if (typeof(iMaxWidth) == 'undefined') var iMaxWidth = MAX_WIDTH;
	if (typeof(iMinWidth) == 'undefined') var iMinWidth = iMaxWidth;

	if (typeof(iMaxHeight) == 'undefined') var iMaxHeight = MAX_HEIGHT;
	if (typeof(iMinHeight) == 'undefined') var iMinHeight = iMaxHeight;

	var Y = screen.availHeight;
	if (Y > iMaxHeight && iMaxHeight > 0) Y = iMaxHeight;
	var TOP = CenterY(Y);

	var bMinWidth = false;
	
  	var X = screen.availWidth;
	if (X > iMaxWidth){
		X = iMaxWidth;
		var LEFT = CenterX(X);
	}
	else if (X < iMinWidth){
		X = iMinWidth;
		var LEFT = BASE_LEFT;
		bMinWidth = true;
	}
	else {	
		TOP = BASE_TOP;
		var LEFT = BASE_LEFT;
	}
	
	objWindow.moveTo(LEFT,TOP);
	objWindow.resizeTo(X, Y);

	if (! bUseAvailLeft && ( LEFT == 0 ) && (typeof(objWindow.screenLeft) != 'undefined' ) ){ //trying to ignore border of window
		LEFT -= objWindow.screenLeft;
		if (! bMinWidth) X += (2*objWindow.screenLeft);
		objWindow.moveTo(LEFT,TOP);
		objWindow.resizeTo(X, Y);
	}
}

if (! window.localdialogs){
	var windowarray = WindowArrayWindow();
	if (windowarray == null) windowarray = new Array();
}
else
	var windowarray = new Array();

var currentdialog = null;

function WindowArrayWindow(){
	var cw = window;
	var lastwindowarray = null;
	do {		
		if (cw.windowarray) lastwindowarray = cw.windowarray;
		if (cw == cw.parent) break; else cw = cw.parent;
	}
	while (cw);
	return lastwindowarray;
}

function opennewwindow(URL, windowName, windowFeatures,remember,outW,outH,_left,_top){	
	if (typeof(_left) == 'undefined'){
		var p = windowFeatures.match(/left=(-|\+)*[0-9]*/i);	
		if (p){
			_left = parseInt(p[0].substr(5));
		}
	}
	if (typeof(_top) == 'undefined'){
		var p = windowFeatures.match(/top=(-|\+)*[0-9]*/i);	
		if (p){
			_top = parseInt(p[0].substr(4));
			if (_top < 0) _top = 0;
		}
	}
	if (typeof(outW) == 'undefined'){
		var p = windowFeatures.match(/outerwidth=[0-9]*/i);	
		if (p){
			outW = parseInt(p[0].substr(6))+outerXBand;
		}
	}
	if (typeof(outH) == 'undefined'){
		var p = windowFeatures.match(/outerheight=[0-9]*/i);
		if (p){
			outH = parseInt(p[0].substr(7))+outerYBand;
		}
	}
	
	var p = windowFeatures.match(/status=[yes|no|1|0]*/i);
	if (p){
			if (windowFeatures != '') windowFeatures += ','
			windowFeatures += 'status=no';
		}

	var tmpwin;
	if (URL==null)//if URL==null, messageboxII.show throws HttpException "File doesn't exist" which can be catched in Global.asax
		tmpwin = window.open('',windowName,windowFeatures);
	else
		tmpwin = window.open(URL,windowName,windowFeatures);
	tmpwin.focus();
	tmpwin.preserve = false;
	if (typeof(remember) == 'undefined' || remember){
		var wi = windowindex(windowName);
		if (  wi > -1) {
			windowarray[wi] = tmpwin;		
		}
		else {		
			var i = windowarray.length;
			windowarray[i] = tmpwin;
		}
		currentdialog = tmpwin;		
	}
	
	if (typeof(outH) != 'undefined' && typeof(outW) != 'undefined') tmpwin.resizeTo(outW,outH);
	if (typeof(_left) != 'undefined' && typeof(_top) != 'undefined') tmpwin.moveTo(_left,_top);
	return tmpwin;
}

function windowindex(windowname){
	for (var i = 0; i < windowarray.length; i++){
		if (! windowarray[i].closed ) {
		 if (windowarray[i].name == windowname) return i;
		}
	}
	return -1;
}

function windows_page_unload(eventObject)
{
	if (windowarray)
	{
		for (var x=0;x < windowarray.length;x++){
			if (windowarray[x]){
				if (! windowarray[x].closed){
					var bPreserve = (typeof(windowarray[x].preserve) != 'undefined');
					if (bPreserve){
						bPreserve = windowarray[x].preserve;
					}
					if (! bPreserve){
						windowarray[x].close();
					}
				}
			}
		}
	}
	
	if (focusopener) FocusOpener();
	
	TestCloseWindow(eventObject);
}


function TestCloseWindow(eventObject)
{
	if (! eventObject) var e = window.event; else var e = ValidateEventObjectForMSIEStyle(eventObject);
	if (e)
	{
		if (navigator.appVersion.search(/(MSIE 6.0|MSIE 5.0|MSIE 4.0)/gi) > -1)
		{
			if (typeof(e.clientX) != 'undefined')
			{
				if(e.clientX < -screen.width && e.clientY < -screen.height)
				{
					if (typeof(OnClose) == 'function') OnClose(e);
				}
			}
		}
		else// if (navigator.appVersion.search(/(MSIE 7.0)/gi) > -1)
		{
			if (typeof(e.clientY) != 'undefined')
			{
				if (e.clientY < 0)// && (e.clientX > (document.documentElement.clientWidth - 5) || e.clientX < 15))
				{
					if (typeof(OnClose) == 'function') OnClose(e);
				}
			}
		}
	}
}


var currentdialogfocus = true;

function windows_page_focus(e){
	if (e) var event = e;	

	if (navigator.appVersion.search(/(MSIE 5.5|MSIE 6.0)/gi) != -1){
		if (currentdialogfocus && currentdialog){		
			if (! currentdialog.closed){
				try{
		currentdialog.focus();
				}
				catch(e){};
				if (window.event){
					window.event.returnValue = false;
				}				
			}
		}
		else
		if (currentdialogfocus){
			for (var x=0;x < frames.length;x++) if (frames[x].page_focus) frames[x].page_focus();
		}		
	}
}

function ShowDlg(OldWindow,DlgName,FileName,parameters) {	
  exist = false;
  var r = null;
  if (OldWindow)
  	if (! OldWindow.closed) {
		exist = true;		
		OldWindow.document.location.href = FileName;
		r = OldWindow;
	}
  if ( ! exist) {    
  	r = opennewwindow(FileName,DlgName,parameters);
  }
  if (r) r.focus();
  return r;
}

function RunDlg(OldWindow,DlgName,FileName, dWidth, dHeight) {	
   	var X = CenterX(dWidth);
  	var Y = CenterY(dHeight);	
		openparams = 'resizable=yes,status=no,center=yes,help=no,minimize=no,maximize=no,border=thin,statusbar=no,width=' + dWidth + ',height=' + dHeight + ',left='+X+',top='+Y;	
  	return ShowDlg(OldWindow,DlgName,FileName,openparams);
}

function RunWindowAsDlg(OldWindow,DlgName,FileName, dWidth, dHeight) {	
	var X = CenterX(dWidth);
	var Y = CenterY(dHeight);	
  	openparams = 'resizable=yes,status=yes,sccenter=yes,help=no,minimize=no,maximize=no,border=thin,statusbar=yes,scrollbars=yes,width=' + dWidth + ',height=' + dHeight + ',left='+X+',top='+Y;
  	return ShowDlg(OldWindow,DlgName,FileName,openparams);
}

function ShowModal(FileName,dWidth,dHeight) {
	var X = CenterX(dWidth);
	var Y = CenterY(dHeight);	
	return window.showModalDialog('Dialog.asp?' + FileName, '', 'status:no;center:yes;help:no;minimize:yes;maximize:yes;border:thick;statusbar:no;dialogWidth:' + dWidth + ';dialogHeight:' + dHeight + 'px,left='+X+',top='+Y);
}

function CloseDlg(sMessage) {
	var doClose;
	if (sMessage) {
		doClose = confirm(sMessage);
	} else {
		doClose = true;
	}
	if (doClose) { return top.close() }
}

function CloseWindow(windowp){
	if (windowp)
	if (typeof(windowp.closed) != 'undefined')
	if (! windowp.closed)	
	if (windowp.top)
	if (! windowp.top.closed)
		windowp.top.close();		
}

function FocusOpener(){
	if (window.top.opener)
	if (! window.top.opener.closed) {
		window.top.opener.top.focus();			
	}
}

function SetTitle(strTitle){	
	if (typeof(strTitle) == 'undefined'){
		if (window.document)
			var strTitle = window.document.title;
		else
			return;
	}
	
	if (window.top.document)
	if (window.top.document.title = strTitle){
	}
}

