var N_BASEZINDEX = 10000;

function f_dialogOpen (s_url, s_title, s_features) {
	if (!window.a_windows)
		window.a_windows = [];

	// parse parameters
	var a_features = [];
	var a_features = s_features.split(',');
	for (var i = 0; i < a_features.length; i++)
	// create element for window
	var n_nesting = a_windows.length;
	var e_window = document.createElement("div");
	e_window.style.position = 'absolute';
	var n_width  = a_features[0] ? parseInt(a_features[0]) : 300;
	var n_height = a_features[1] ? parseInt(a_features[1]) : 200;
	var a_docSize = f_documentSize ();
	e_window.style.left = (a_features.left ? parseInt(a_features.left) : ((a_docSize[0] - n_width)  / 2) + a_docSize[2])-10 + 'px';
	e_window.style.top  = (a_features.top  ? parseInt(a_features.top)  : ((a_docSize[1] - n_height) / 2) + a_docSize[3]) + 'px';
	e_window.style.zIndex = N_BASEZINDEX + a_windows.length * 2 + 2;

      var flashes = [];
    flashes = document.getElementsByTagName("object");
    for (var f=0; f < flashes.length; f++){
        flashes[f].style.visibility = "hidden";
    }

    var select = [];
    flashes = document.getElementsByTagName("select");
    for (var f=0; f < flashes.length; f++){
        flashes[f].style.visibility = "hidden";
    }

    e_window.innerHTML = '<table border="0" class="' + (a_features.css ? a_features.css : 'dialogwindow') + '" style="z-index:10;"><tr><td><iframe frameBorder="0" name="'+s_title+'" style = "border:0 solid black" scrolling="' + (a_features.scrollbars ? a_features.scrollbars : "no")  + '" width="' + n_width + '" height="' + n_height + (s_url!=''?'" src="' + s_url + '"':'')+'> </iframe></td></tr></table>';
//    alert(e_window.innerHTML);
	document.body.appendChild(e_window);
    var txt = document.createTextNode(" ");
    document.body.appendChild(txt);

    a_windows[n_nesting] = e_window;

    // put the screen
	f_putScreen(true);
}
function f_putScreen (b_show) {

	if (b_show == null && !window.b_screenOn)
		return;

	if (!b_show) {
		//window.b_screenOn = false;
		if (e_screen) {
            e_screen.style.display = 'none';

            // attach event
            if (document.removeEventListener) {
                document.removeEventListener('mousemove', f_dragProgress, false);
                window.removeEventListener('resize', f_putScreen, false);
                window.removeEventListener('scroll', f_putScreen, false);
            }
            if (window.detachEvent) {
                document.detachEvent('onmousemove', f_dragProgress);
                window.detachEvent('onresize', f_putScreen);
                //window.detachEvent('onscroll', f_putScreen);
            }
            else {
                document.onmousemove = undefined;
                window.onresize = undefined;
                //window.onscroll = undefined;
            }
            document.body.removeChild(e_screen);
            window.e_screen = null;
        }
        return;
	}

	// create the layer if doesn't exist
	if (window.e_screen == null) {
        window.e_screen = document.createElement("div");
		e_screen.innerHTML = "&nbsp;";
		document.body.appendChild(e_screen);

		e_screen.style.position = 'absolute';
		e_screen.id = 'eScreen';

		// attach event
		if (document.addEventListener) {
			document.addEventListener('mousemove', f_dragProgress, false);
			window.addEventListener('resize', f_putScreen, false);
			window.addEventListener('scroll', f_putScreen, false);
		}
		if (window.attachEvent) {
			document.attachEvent('onmousemove', f_dragProgress);
			window.attachEvent('onresize', f_putScreen);
			//window.attachEvent('onscroll', f_putScreen);
		}
		else {
			document.onmousemove = f_dragProgress;
			window.onresize = f_putScreen;
			//window.onscroll = f_putScreen;
		}
	}

	// set properties
	var a_docSize = f_documentSize();
	e_screen.style.left = a_docSize[2] + 'px';
	e_screen.style.top = a_docSize[3] + 'px';
	e_screen.style.width = '100%';//a_docSize[0] + 'px';
	e_screen.style.height = a_docSize[1] + 'px';
	e_screen.style.zIndex = N_BASEZINDEX + a_windows.length * 2 - 1;
	e_screen.style.display = 'block';
}
function f_documentSize () {

var n_scrollX = 0,
	n_scrollY = 0;

	if (typeof(window.pageYOffset) == 'number') {
		n_scrollX = window.pageXOffset;
		n_scrollY = window.pageYOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop )) {
		n_scrollX = document.body.scrollLeft;
		n_scrollY = document.body.scrollTop;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		n_scrollX = document.documentElement.scrollLeft;
		n_scrollY = document.documentElement.scrollTop;
	}

	if (typeof(window.innerWidth) == 'number')
		return [window.innerWidth, window.innerHeight, n_scrollX, n_scrollY];
	if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		return [document.documentElement.clientWidth, document.documentElement.clientHeight, n_scrollX, n_scrollY];
	if (document.body && (document.body.clientWidth || document.body.clientHeight))
		return [document.body.clientWidth, document.body.clientHeight, n_scrollX, n_scrollY];
	return [0, 0];
}
function f_dialogClose () {

	var n_nesting = a_windows.length - 1;
	// destroy element
	if (a_windows[n_nesting].removeNode)
		a_windows[n_nesting].removeNode(true);
	else if (document.body.removeChild)
		document.body.removeChild(a_windows[n_nesting]);
	a_windows[n_nesting] = null;
	a_windows.length = n_nesting;

    var flashes = [];
    flashes = document.getElementsByTagName("object");
    for (var f=0; f < flashes.length; f++){
        flashes[f].style.visibility = "visible";
    }

    var select = [];
    flashes = document.getElementsByTagName("select");
    for (var f=0; f < flashes.length; f++){
        flashes[f].style.visibility = "visible";
    }
    // move the screen

    f_putScreen(n_nesting);

}
function f_dragProgress (e_event) {
	if (!e_event && window.event) e_event = window.event;
	if (!e_event || window.e_draggedWindow == null) return;

	var n_newMouseX = e_event.clientX;
	var n_newMouseY = e_event.clientY;

	window.e_draggedWindow.style.left = (parseInt(window.e_draggedWindow.style.left) - window.n_mouseX + n_newMouseX) + 'px';
	window.e_draggedWindow.style.top  = (parseInt(window.e_draggedWindow.style.top)  - window.n_mouseY + n_newMouseY) + 'px';

	window.n_mouseX = n_newMouseX;
	window.n_mouseY = n_newMouseY;
}