function windowOpen(Url, Width, Height, Name, Php) {
	if (navigator.userAgent.indexOf('Opera') >= 0) {
		var ow = document.body.clientWidth, oh = document.body.clientHeight;
	} else {
		var ow = screen.width, oh = screen.height;
	}
	Width = Width || 640;
	Height = Height || 480;
	var left = (ow - Width)/2;
	var top = (oh - Height)/2;

	var feats = new Array("width=" + Width, "height=" + Height, "left=" + left, "top=" + top, "scrollbars=1", "status=1", "resizable=1");

	if (!Name) Name = Url.replace(/([\W]+)/g, '');
	if (!Php) Php = 'window.php';
	if (Url == '') {
		Url = 'about:blank';
	} else {
		var base = document.getElementsByTagName('base');
		Url = base[0].href+Php+Url;
	}

	window.open(Url,Name,feats.join(',')).focus();
}

function load() {
	if (window.name) {
		focusForm();
		addEvent(document.body, 'keyup', formKeyup); // IE & Opera
		//addEvent(window, 'keyup', formKeyup); // Gecko & Opera
	} else {
		addEvent(document.body, 'keyup', keyup); // IE & Opera
		if (!window.opera) addEvent(window, 'keyup', keyup); // Gecko & Opera
	}
	if (window.controller) InitControls();
	if (window.ToolTip) {
		ToolTip.Init();
		addEvent(document.body, 'keydown', keydown); // IE & Opera
		addEvent(window, 'keydown', keydown); // Gecko & Opera
	}
}

function focusForm() {
	if (document.forms[0]) {
		var item = document.forms[0].elements[0];
		if (item.type != 'hidden' && !item.disabled && item.focus) item.focus();
	}
}

function keyup(e) {
	if (e.keyCode == 123 && e.ctrlKey) {
		login(); // Ctrl+F12
	} else if (e.keyCode == 113 && e.ctrlKey) { // Ctrl+F2
        link = document.getElementById('metaProto');
		if (link && link.name == 'proto') {
			if (window.location.href.indexOf('/proto') != -1) window.location.href = window.location.href.replace('/proto', '');
			else window.location.href = link.content;
		}
	}
	if (window.ToolTip && e.keyCode == 17) ToolTip.CtrlPressed = false;
}

function formKeyup(e) {
	if (e.keyCode == 27) formClose(); // ESC
}

function formClose() {
	if (!document.forms[0]) return;
	form = document.forms[0];
	form.modified = false;
	for (i=0; i<form.elements.length; i++) {
		if (form.elements[i].value != form.elements[i].defaultValue) {
			form.modified = true;
			break;
		}
	}
	if (!form.modified) {
		window.close();
	} else {
		if (window.confirm('Data has been modified. Submit data?')) form.submit();
		else window.close();
	}
}

function keydown(e) {
	ToolTip.CtrlPressed = e.keyCode == 17; // Ctrl
}

function login() {
	windowOpen('?page=login', 480, 320, 'login');
}

function addEvent(node, evtType, func) {
	if (node.addEventListener) {
		node.addEventListener(evtType, func, false);
		return true;
	} else if (node.attachEvent ) {
		return node.attachEvent("on" + evtType, func);
	} else {
		return false;
	}
}

function switchLang(langFrom, langTo) {
	var href = window.location.href;

	// switch one to another
	re = new RegExp('/'+langFrom+'/'); // only one replace
	href = window.location.href.replace(re, '/'+langTo+'/');

	// no replacement occured
	if (href == window.location.href) {
		// switch from default language
		var base = document.getElementsByTagName('base');
		href = window.location.href.replace(base[0].href, base[0].href+langTo+'/');
	}

	if (href == window.location.href) return true;

	window.location.href = href;
	return false;
}

addEvent(window, 'load', load);
