var N_BASEZINDEX = 100;
var RE_PARAM = /^\s*(\w+)\s*\=\s*(.*)\s*$/;
window.b_w_screenOn = false;

// this function makes the document numb to the mouse events by placing the transparent layer over it
function f_w_putScreen (b_w_show) {
	//alert('f_w_putScreen: '+b_w_show);
	if (b_w_show == null && !window.b_w_screenOn){		
		toggleParentControls(true);	
		return;
	}

	if (b_w_show == false) {
		window.b_w_screenOn = false;
		if(window.e_w_screen) window.e_w_screen.style.display = 'none';
			
		// remove event
		if (document.removeEventListener) {
			document.removeEventListener('mousemove', f_w_dragProgress, false);
			window.removeEventListener('resize', f_w_putScreen, false);
			window.removeEventListener('scroll', f_w_putScreen, false);
		}
		else if (window.detachEvent) {
			document.detachEvent('onmousemove', f_w_dragProgress);
			window.detachEvent('onresize', f_w_putScreen);
			window.detachEvent('onscroll', f_w_putScreen);
		}
		else {
			document.onmousemove = function(){};
			window.onresize = function(){};
			window.onscroll = function(){};
		}			
		window.e_w_screen = null;
		toggleParentControls(true);		
		return;
	}

	// create the layer if doesn't exist
	if (window.e_w_screen == null) {
		window.e_w_screen = document.createElement("div");
		window.e_w_screen.innerHTML = "&nbsp;";
		window.e_w_screen.style.position = 'absolute';
		window.e_w_screen.id = 'windowModalScreen';
		document.body.appendChild(window.e_w_screen);
		
		// attach event
		if (document.addEventListener) {
			document.addEventListener('mousemove', f_w_dragProgress, false);
			window.addEventListener('resize', f_w_putScreen, false);
			window.addEventListener('scroll', f_w_putScreen, false);
		}
		if (window.attachEvent) {
			document.attachEvent('onmousemove', f_w_dragProgress);
			window.attachEvent('onresize', f_w_putScreen);
			window.attachEvent('onscroll', f_w_putScreen);
		}
		else {
			document.onmousemove = f_w_dragProgress;
			window.onresize = f_w_putScreen;
			window.onscroll = f_w_putScreen;
		}	
	}   

	// set properties
	var a_w_docSize = f_w_documentSize();
	window.e_w_screen.style.left = a_w_docSize[2] + 'px';
	window.e_w_screen.style.top = a_w_docSize[3] + 'px';
	window.e_w_screen.style.width = a_w_docSize[0] + 'px';
	window.e_w_screen.style.height = a_w_docSize[1] + 'px';
	window.e_w_screen.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 - 1;
	window.e_w_screen.style.display = 'block';	
	window.b_w_screenOn = true;
	
	toggleParentControls(false);	
}

// returns the size of the document
function f_w_documentSize () {

var n_w_scrollX = 0,
	n_w_scrollY = 0;

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

	if (typeof(window.innerWidth) == 'number')
		return [window.innerWidth, window.innerHeight, n_w_scrollX, n_w_scrollY];
	if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		return [document.documentElement.clientWidth, document.documentElement.clientHeight, n_w_scrollX, n_w_scrollY];
	if (document.body && (document.body.clientWidth || document.body.clientHeight))
		return [document.body.clientWidth, document.body.clientHeight, n_w_scrollX, n_w_scrollY];
	return [0, 0];
}

function f_w_dialogOpen (s_url, s_title, s_features) {
	if (!window.a_w_windows)
		window.a_w_windows = [];
		
	// parse parameters
	var a_w_featuresStrings = s_features.split(',');
	var a_w_features = [];
	for (var i = 0; i < a_w_featuresStrings.length; i++)
		if (a_w_featuresStrings[i].match(RE_PARAM))
			a_w_features[String(RegExp.$1).toLowerCase()] = RegExp.$2;
	
	// create element for window
	var n_w_nesting = a_w_windows.length;

	var e_w_window = document.createElement("div");
	e_w_window.style.position = 'absolute';
	var n_w_width  = a_w_features.width  ? parseInt(a_w_features.width)  : 300;
	var n_w_height = a_w_features.height ? parseInt(a_w_features.height) : 200;
	var a_w_docSize = f_w_documentSize ();
	e_w_window.style.left = (a_w_features.left ? parseInt(a_w_features.left) : ((a_w_docSize[0] - n_w_width)  / 2) + a_w_docSize[2]) + 'px';
	e_w_window.style.top  = (a_w_features.top  ? parseInt(a_w_features.top)  : ((a_w_docSize[1] - n_w_height) / 2) + a_w_docSize[3]) + 'px';
	e_w_window.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 + 2;
	
	// fix for SELECT boxes displayed over DIVs (window/screen)
	var n_w_dialogWindowBG_zIndex = parseInt(e_w_window.style.zIndex) - 2;	
	var n_w_dialogWindowBG_left = e_w_window.style.left;
	var n_w_dialogWindowBG_top =  e_w_window.style.top;
	
	// fix for spinner
	var n_w_spinner_left, n_w_spinner_top, n_w_spinner_zIndex;
	n_w_spinner_left = (n_w_width/2) - 16; 		
	n_w_spinner_top =  (n_w_height/2); 	
	n_w_spinner_zIndex = parseInt(e_w_window.style.zIndex) + 2;
		
	// 
	e_w_window.innerHTML = 
		'<iframe name="dialogWindowBG" id="dialogWindowBG" style="z-index: ' + n_w_dialogWindowBG_zIndex + 
		'; left: ' + n_w_dialogWindowBG_left + 'px; top: ' + n_w_dialogWindowBG_top + 'px;" height = "' + n_w_height + 
		'" width = "' + n_w_width + '"></iframe>' +
		'<table border="1" class="' + (a_w_features.css ? a_w_features.css : 'dialogWindow') +
		'"><tr><th onmousedown="f_w_dragStart(' + n_w_nesting + ', event)" onmouseup="f_w_dragEnd()" ' + 
		'onmousemove="f_w_dragProgress(event)" onselectstart="return false"><span style="float: left">' +
		(s_title ? s_title : 'Dialog Window') + '</span><img src="' + windowsCloseIcon +
		'" onclick="top.f_w_dialogClose();f_putScreen(false);" onmousedown="return false;" ' +
		' style="float: right" title="Close"></th></tr><tr><td><iframe width="' + n_w_width +
		'" height="' + n_w_height + '" src="' + s_url + '"></iframe></td></tr></table>';
		
	document.body.appendChild(e_w_window);
	a_w_windows[n_w_nesting] = e_w_window;
	
	// put the screen
	f_w_putScreen(true);
}

function f_w_dialogPost (o_form, s_title, s_features) {
	if (!window.a_w_windows)
		window.a_w_windows = [];
		
	// parse parameters
	var a_w_featuresStrings = s_features.split(',');
	var a_w_features = [];
	for (var i = 0; i < a_w_featuresStrings.length; i++)
		if (a_w_featuresStrings[i].match(RE_PARAM))
			a_w_features[String(RegExp.$1).toLowerCase()] = RegExp.$2;
	
	// create element for window
	var n_w_nesting = a_w_windows.length;

	var e_w_window = document.createElement("div");
	e_w_window.style.position = 'absolute';
	var n_w_width  = a_w_features.width  ? parseInt(a_w_features.width)  : 300;
	var n_w_height = a_w_features.height ? parseInt(a_w_features.height) : 200;
	var a_w_docSize = f_w_documentSize ();
	e_w_window.style.left = (a_w_features.left ? parseInt(a_w_features.left) : ((a_w_docSize[0] - n_w_width)  / 2) + a_w_docSize[2]) + 'px';
	e_w_window.style.top  = (a_w_features.top  ? parseInt(a_w_features.top)  : ((a_w_docSize[1] - n_w_height) / 2) + a_w_docSize[3]) + 'px';
	e_w_window.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 + 2;
	
	// fix for SELECT boxes displayed over DIVs (window/screen)
	var n_w_dialogWindowBG_zIndex = parseInt(e_w_window.style.zIndex) - 2;	
	var n_w_dialogWindowBG_left = e_w_window.style.left;
	var n_w_dialogWindowBG_top =  e_w_window.style.top;	
	
	// fix for spinner
	var n_w_spinner_left, n_w_spinner_top, n_w_spinner_zIndex;
	n_w_spinner_left = (n_w_width/2) - 16; 		
	n_w_spinner_top =  (n_w_height/2); 	
	n_w_spinner_zIndex = parseInt(e_w_window.style.zIndex) + 2;
	
	e_w_window.innerHTML = 
		'<iframe name="dialogWindowBG" id="dialogWindowBG" style="z-index: ' + n_w_dialogWindowBG_zIndex + 
		'; left: ' + n_w_dialogWindowBG_left + 'px; top: ' + n_w_dialogWindowBG_top + 'px;" height = "' + n_w_height + 
		'" width = "' + n_w_width + '"></iframe>' +
		'<table border="1" class="' + (a_w_features.css ? a_w_features.css : 'dialogWindow') +
		'"><tr><th onmousedown="f_w_dragStart(' + n_w_nesting + ', event)" onmouseup="f_w_dragEnd()" ' + 
		'onmousemove="f_w_dragProgress(event)" onselectstart="return false"><span style="float: left">' +
		(s_title ? s_title : 'Dialog Window') + '</span><img src="' + windowsCloseIcon +
		'" onclick="top.f_w_dialogClose();" onmousedown="return false;" ' +
		' style="float: right" title="Close"></th></tr><tr><td><iframe id="formTarget" name="formTarget" width="' + n_w_width +
		'" height="' + n_w_height + '"></iframe></td></tr></table>';
		
	document.body.appendChild(e_w_window);
	a_w_windows[n_w_nesting] = e_w_window;
	
	o_form.target = "formTarget"; 	
	o_form.submit();
	
	// put the screen
	f_w_putScreen(true);
}

function f_w_dialogConfirm (o_form, s_title, s_features, s_message) {
	if (!window.a_w_windows)
		window.a_w_windows = [];
		
	// parse parameters
	var a_w_featuresStrings = s_features.split(',');
	var a_w_features = [];
	for (var i = 0; i < a_w_featuresStrings.length; i++)
		if (a_w_featuresStrings[i].match(RE_PARAM))
			a_w_features[String(RegExp.$1).toLowerCase()] = RegExp.$2;
	
	// create element for window
	var n_w_nesting = a_w_windows.length;

	var e_w_window = document.createElement("div");
	e_w_window.style.position = 'absolute';
	var n_w_width  = a_w_features.width  ? parseInt(a_w_features.width)  : 300;
	var n_w_height = a_w_features.height ? parseInt(a_w_features.height) : 200;
	var a_w_docSize = f_w_documentSize ();
	e_w_window.style.left = (a_w_features.left ? parseInt(a_w_features.left) : ((a_w_docSize[0] - n_w_width)  / 2) + a_w_docSize[2]) + 'px';
	e_w_window.style.top  = (a_w_features.top  ? parseInt(a_w_features.top)  : ((a_w_docSize[1] - n_w_height) / 2) + a_w_docSize[3]) + 'px';
	e_w_window.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 + 2;
	
	// fix for SELECT boxes displayed over DIVs (window/screen)
	var n_w_dialogWindowBG_zIndex = parseInt(e_w_window.style.zIndex) - 2;	
	var n_w_dialogWindowBG_left = e_w_window.style.left;
	var n_w_dialogWindowBG_top =  e_w_window.style.top;
	
	// fix for spinner
	var n_w_spinner_left, n_w_spinner_top, n_w_spinner_zIndex;
	n_w_spinner_left = (n_w_width/2) - 16; 		
	n_w_spinner_top =  (n_w_height/2); 	
	n_w_spinner_zIndex = parseInt(e_w_window.style.zIndex) + 2;
		
	// 
	e_w_window.innerHTML = 
		'<iframe name="dialogWindowBG" id="dialogWindowBG" style="z-index: ' + n_w_dialogWindowBG_zIndex + 
		'; left: ' + n_w_dialogWindowBG_left + 'px; top: ' + n_w_dialogWindowBG_top + 
		'px;" width="' + n_w_width + '" height="' + n_w_height + '"></iframe>' +
		'<table border="1" class="' + (a_w_features.css ? a_w_features.css : 'dialogWindow') +
		'"><tr><th onmousedown="f_w_dragStart(' + n_w_nesting + ', event)" onmouseup="f_w_dragEnd()" ' + 
		'onmousemove="f_w_dragProgress(event)" onselectstart="return false"><span style="float: left">' +
		(s_title ? s_title : 'Dialog Window') + '</span><img src="' + windowsCloseIcon +
		'" onclick="top.f_w_dialogClose();" onmousedown="return false;" ' +
		' style="float: right" title="Close"></th></tr><tr><td align="center"><table border="0" height="' + n_w_height + 
		'" width="' + n_w_width + '" cellspacing="5" cellpadding="5" align="center"><tr><td colspan="5">&nbsp;</td></tr><tr><td>&nbsp;</td><td><img src="/centurion/images/icons/large/confirm32.gif"></td><td>&nbsp;</td><td>' + s_message +
		'</td><td>&nbsp;</td></tr><tr><td colspan="5">&nbsp;</td></tr><tr><td colspan="5" align="center"><input type="button" class="button" value="OK" onclick="top.document.'+o_form.name+'.submit();top.f_w_dialogClose();">' +
		'&nbsp;<input type="button" class="button" value="Cancel" onclick="top.f_w_dialogClose();"></td></tr><tr><td colspan="5">&nbsp;</td></tr></table></td></tr></table>';
		
	document.body.appendChild(e_w_window);
	a_w_windows[n_w_nesting] = e_w_window;
	o_form.target = "_self";
	
	// put the screen
	f_w_putScreen(true);
	enableSelectedCheckboxes(o_form);
}
function f_w_dialogErrors (s_title, s_features, s_message) {
	if (!window.a_w_windows)
		window.a_w_windows = [];
		
	// parse parameters
	var a_w_featuresStrings = s_features.split(',');
	var a_w_features = [];
	for (var i = 0; i < a_w_featuresStrings.length; i++)
		if (a_w_featuresStrings[i].match(RE_PARAM))
			a_w_features[String(RegExp.$1).toLowerCase()] = RegExp.$2;
	
	// create element for window
	var n_w_nesting = a_w_windows.length;

	var e_w_window = document.createElement("div");
	e_w_window.style.position = 'absolute';
	var n_w_width  = a_w_features.width  ? parseInt(a_w_features.width)  : 300;
	var n_w_height = a_w_features.height ? parseInt(a_w_features.height) : 200;
	var a_w_docSize = f_w_documentSize ();
	e_w_window.style.left = (a_w_features.left ? parseInt(a_w_features.left) : ((a_w_docSize[0] - n_w_width)  / 2) + a_w_docSize[2]) + 'px';
	e_w_window.style.top  = (a_w_features.top  ? parseInt(a_w_features.top)  : ((a_w_docSize[1] - n_w_height) / 2) + a_w_docSize[3]) + 'px';
	e_w_window.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 + 2;
	
	// fix for SELECT boxes displayed over DIVs (window/screen)
	var n_w_dialogWindowBG_zIndex = parseInt(e_w_window.style.zIndex) - 2;	
	var n_w_dialogWindowBG_left = e_w_window.style.left;
	var n_w_dialogWindowBG_top =  e_w_window.style.top;
		
	// 
	e_w_window.innerHTML = 
		'<iframe name="dialogWindowBG" id="dialogWindowBG" style="z-index: ' + n_w_dialogWindowBG_zIndex + 
		'; left: ' + n_w_dialogWindowBG_left + 'px; top: ' + n_w_dialogWindowBG_top + 
		'px;" width="' + n_w_width + '" height="' + n_w_height + '"></iframe>' +
		'<table border="1" class="' + (a_w_features.css ? a_w_features.css : 'dialogWindow') +
		'"><tr><th onmousedown="f_w_dragStart(' + n_w_nesting + ', event)" onmouseup="f_w_dragEnd()" ' + 
		'onmousemove="f_w_dragProgress(event)" onselectstart="return false"><span style="float: left">' +
		(s_title ? s_title : 'Dialog Window') + '</span><img src="' + windowsCloseIcon +
		'" onclick="top.f_w_dialogClose();" onmousedown="return false;" ' +
		' style="float: right" title="Close"></th></tr><tr><td align="center"><table border="0" height="' + n_w_height + 
		'" width="' + n_w_width + '" cellspacing="5" cellpadding="5"><tr><td colspan="5">&nbsp;</td></tr>' +
		'<tr><td width="15"></td><td valign="top"><img src="/centurion/images/icons/large/error32.gif"></td><td>&nbsp;</td><td align="left" width="260">Please see the message list below:</td><td width="15"></td></tr><tr><td colspan="5">&nbsp;</td></tr><tr><td width="15"></td><td valign="top" colspan="3"><div class="messageList">' + s_message +
		'</div></td><td width="15"></td></tr><tr><td colspan="5">&nbsp;</td></tr><tr><td width="15"></td><td colspan="3" align="right"><input type="button" class="button" value="OK" onclick="top.f_w_dialogClose();"></td><td width="15"></td></tr><tr><td colspan="5">&nbsp;</td></tr></table></td></tr></table>';
				
	document.body.appendChild(e_w_window);
	a_w_windows[n_w_nesting] = e_w_window;
	
	// put the screen
	f_w_putScreen(true);	
}

function f_w_dialogClose () {

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

	// remove the screen
	f_w_putScreen(n_w_nesting ? true : false);
}


// drag'n'drop functions
function f_w_dragStart (s_name, e_w_event) {
	if (!e_w_event && window.event) e_w_event = window.event;

	// save mouse coordinates
	window.n_w_mouseX = e_w_event.clientX;
	window.n_w_mouseY = e_w_event.clientY;
	window.e_w_draggedWindow = window.a_w_windows[s_name];
	return false;
}
function f_w_dragProgress (e_w_event) {
	if (!e_w_event && window.event) e_w_event = window.event;
	if (!e_w_event || window.e_w_draggedWindow == null) return;

	var n_w_newMouseX = e_w_event.clientX;
	var n_w_newMouseY = e_w_event.clientY;
	
	window.e_w_draggedWindow.style.left = (parseInt(window.e_w_draggedWindow.style.left) - window.n_w_mouseX + n_w_newMouseX) + 'px';
	window.e_w_draggedWindow.style.top  = (parseInt(window.e_w_draggedWindow.style.top)  - window.n_w_mouseY + n_w_newMouseY) + 'px';
	
	window.n_w_mouseX = n_w_newMouseX;
	window.n_w_mouseY = n_w_newMouseY;
}
function f_w_dragEnd () {
	window.e_w_draggedWindow = null;
}
function toggleParentControls(b_w_enable){
	var forms, f, i, count;
	forms = window.document.forms;
	count = forms.length;
	if(forms){
		for (i=0;i<count;i++){
            f = forms[i];
            toggleFormControls(f, b_w_enable); 
			toggleAnchorsAndLinks(b_w_enable); 
        }
	}
}
function toggleFormControls(form, b_w_enable){
    var e, f, i, count;
    if(form){
        f=form;
        count=f.elements.length;
        for (i=0;i<count;i++){
            e = f.elements[i];	
			e.disabled = !b_w_enable;				
        }
    }
}
function toggleAnchorsAndLinks(b_w_enable){
	var a, i, count, anchors;
	anchors = document.anchors;
	count = anchors.length;
	for (i=0;i<count;i++){
		a = anchors[i];		
		if(!b_w_enable) a.onfocus = function(){window.e_w_screen.focus()};
		else a.onfocus = function(){};
	}
	anchors = document.links;
    count = anchors.length;
   	for (i=0;i<count;i++){
		a = anchors[i];		
		if(!b_w_enable) a.onfocus = function(){window.e_w_screen.focus()};
		else a.onfocus = function(){};
	}   
}
function enableSelectedCheckboxes(form){
    var e, f, i, count;
    if(form){
        f=form;
        count=f.elements.length;
        for (i=0;i<count;i++){
            e = f.elements[i];	
			if (e.name != TOGGLE_ALL && e.type==CHECKBOX && e.disabled && e.checked)
				e.disabled = false;
        }
    }
}
/*
function f_w_showModalSpinner(){
	if (!window.a_w_windows)
		window.a_w_windows = [];
	var a_w_docSize = f_w_documentSize ();
	var e_w_spinner = document.createElement("div");
	e_w_spinner.style.position = 'absolute';
	var n_w_width  = 172;
	var n_w_height = 100;
	e_w_spinner.style.left = (((a_w_docSize[0] - n_w_width)  / 2) + a_w_docSize[2]) + 'px';
	e_w_spinner.style.top  = (((a_w_docSize[1] - n_w_height) / 2) + a_w_docSize[3]) + 'px';
	e_w_spinner.style.zIndex = N_BASEZINDEX + a_w_windows.length * 2 + 2;
	
	e_w_spinner.innerHTML = '<img src="' + windowsSpinnerModal + '" border="0">';
	e_w_spinner.id = 'spinner';
	var n_w_nesting = a_w_windows.length;
	document.body.appendChild(e_w_spinner);
	a_w_windows[n_w_nesting] = e_w_spinner;
	// put the screen
	f_w_putScreen(true);	
}*/
