/* common/js/common.js */

/**
 * common Object 
 */
var common = new Object();

common.resizeInnerWindowTo = function(width, height){
	if (window.innerWidth == null){ // IE
		window.resizeToWidth = width;
		window.resizeToHeight = height;
		setTimeout('common.windowResizeBy()',1000)
	}else{ // Firefox etc.
		window.parent.resizeBy(width - window.innerWidth, height - window.innerHeight);
	}
}
common.windowResizeBy = function(){
	try{
		if(window.dialogWidth){ // IE Modal Dialog
			window.dialogWidth = window.resizeToWidth + "px";
			window.dialogHeight = window.resizeToHeight + "px";
		}
		else window.resizeBy(resizeToWidth - document.body.clientWidth, resizeToHeight - document.body.clientHeight);
	}catch(e){
		//alert("resizeToWidth=" + resizeToWidth)
		window.resizeTo(resizeToWidth + 10, resizeToHeight + 90);
	}
}

common.resizeToContent = function(){
	var scrollWidth = document.getElementById("content").scrollWidth
	var scrollHeight = document.getElementById("content").scrollHeight + document.getElementById("foot").scrollHeight;
	var resizeToWidth = 0;
	var resizeToHeight = 0;
	
	if(scrollWidth > document.body.clientWidth){
		if(scrollWidth <= screen.availWidth) resizeToWidth = scrollWidth - document.body.clientWidth + 5;
		else resizeToWidth = screen.availHeight - document.body.clientWidth;
	}
	
	if(scrollHeight > document.body.clientHeight){
		if(scrollHeight <= screen.availHeight - 28) resizeToHeight = scrollHeight - document.body.clientHeight + 5;
		else resizeToHeight = screen.availHeight - document.body.clientHeight - 28;
	}
	
	if(window.dialogWidth){ // IE Modal Dialog
		window.dialogWidth = scrollWidth + 10 + "px";
		window.dialogHeight = scrollHeight + 30 + "px";
	}
	else window.resizeBy(resizeToWidth, resizeToHeight);
	
	// Move window up if bottom is below screen bottom
	if(window.screenY){//Firefox
		if(document.body.clientHeight + window.screenY + 28 > screen.availHeight)
			window.moveBy(0, 0);
	}
	else if(window.screenTop){//IE
		if(document.body.clientHeight + window.screenTop > screen.availHeight){
			var deltaY = document.body.clientHeight + window.screenTop - screen.availHeight;
			if(deltaY > window.screenTop + 22) deltaY = window.screenTop + 22;
			window.moveBy(0, - deltaY);
		}
	}	
}

common.initFocus = function(form){
	if(form == null) form = document.forms[0];
	try{
		if(form != null && form.focusElement != null) {
			var focusElement = document.getElementById(form.focusElement.value);
			if(focusElement){
				focusElement.focus();
				if(focusElement.select)focusElement.select();
			}
		}
	}catch(e){}
	
	for(var i = 0; i < form.elements.length; i++){
		form.elements[i].onfocus = function(evt){
			evt = (evt) ? evt : ((window.event) ? window.event : "")
			var target = evt.target || evt.srcElement;
			form.focusElement.value = target.id;
		}
	}
}


/** 
 * Framework for opening windows and modal dialogs 
 * plus evaluating returnValue on the opening page when closing the window. 
 */

/**
 * Opening a modal dialog if possible or else opening a plain window.
 * Evaluating the returnValue.
 */
common.openModalDialog = function(url, title, width, height) {
	if(window.showModalDialog && (window["useModalDialog"] == null || useModalDialog)) {
		common.title = title;
		if(document.all) {
			width += 6;
			height += 25;
		}		
		var winSettings = "center:yes;status:no;resizable:yes;help:no;dialogWidth:" + width + "px;dialogHeight:" + height + "px";
		var returnVal = window.showModalDialog(url, common, winSettings);
		eval(returnVal);
	}
	else {
		common.openWindow(url, title, width, height);
	}
}
/**
 * Opening a plain window.
 */
common.openWindow = function(url, title, width, height, bigWidth, bigHeight) {
	
	// Firefox 7 does not allow resizeBy or resizeTo, so we have to open a big window!.
	if(window.innerWidth && bigWidth && bigHeight){
		width = bigWidth;
		height = bigHeight;
	}
	
	var win = window.open(url, title, "toolbar=no,location=no,scrollbars=auto,resizable=yes,width="+width+",height="+height+"");
	win.focus();
	win.moveTo((screen.availWidth - 8 - width)/2, (screen.availHeight - 30 - height)/2);
	win.focus();
	return win;
}

/**
 * Closing a window or modal dialog with a value to be evaluated on the opener.
 * Excample: common.closeWindow("reload()");
 */
common.closeWindow = function(value){
	common.returnValue(value);
	window.close();
}

/**
 * Setting the returnValue on a modal dialog or evaluates the value on the opener of a plain window.
 */
common.returnValue = function(value){
	if (opener) opener.eval(value);
	else returnValue = value;
}

/**
 * Setting the title of the window as set in common.title of the opener.
 */
if(window.dialogArguments) window.document.title = window.dialogArguments.title;

/**
 * Boolean value.
 * true, if disableInput has been called last.
 * false default or if enableInput has been called last.
 */ 
common.isInputDisabled = false;
 
/**
 * Disables all input and covers the page with an activity indicator.
 */
common.disableInput = function(){
	common.isInputDisabled = true;
	document.body.onkeypress = function(event){ return false; }
	document.body.onkeydown = function(event){ return false; }
	document.body.onkeyup =  function(event){ return false; }
	if(document.getElementById("coverAll"))document.getElementById("coverAll").style.display = "block";
}

/**
 * Enables all input and remove the cover of the page with the activity indicator.
 */
common.enableInput = function(){
	common.isInputDisabled = false;
	document.body.onkeypress = "";
	document.body.onkeydown = "";
	document.body.onkeyup = "";
	if(document.getElementById("coverAll"))document.getElementById("coverAll").style.display = "none";
}


