function insertAfter(parent, node, referenceNode){
	parent.insertBefore(node, referenceNode.nextSibling);
}

function redirectParent(url){
	window.parent.document.location.href = url;
	return false;
}

function redirect(url){
	document.location.href = url;
	return false;
}

function confirmDelete(url){
	if (confirm('Czy napewno chcesz usunąć?')){
		redirect(url);
	}
}

function disableSelection(element) {
    element.onselectstart = function(){
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}

function applyInterface(){
	var elements = getElementsByClassName("interface");
	var i;
	for (i = 0; i < elements.length; i++){
		disableSelection(elements[i]);
	}
}

function addLoad(f){
	var old = window.onload;
	window.onload = function(){
		if (old)
			old();
		f();
	}
}

function getInternetExplorerVersion(){
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer'){
	var ua = navigator.userAgent;
	var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
	}
	return rv;
}


var domReady = function(){

}

function addDomReady(f){
	var old = domReady;
	domReady = function(){
		if (old)
			old();
		f();
	}
}

function checkIe(){
	var ver = getInternetExplorerVersion();
	if ((ver != -1) && (ver < 8.0)){
		alert('Na ten moment prosimy nie korzystać z przeglądarki Internet Explorer.\n' + 
			  'Zaleca się korzystanie z przeglądarki Mozilla Firefox lub Opera.');
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showDivElement(elementId){
	var element = document.getElementById(elementId);
	if( element ){
		if( element.style.display == 'block' ){
			element.style.display = 'none';
		}else{
			element.style.display = 'block';
		}
	}
}

function hideDivElement(elementId){
	var element = document.getElementById(elementId);
	if( element ){
		element.style.display = 'none';
	}
}

function showHideDivElement(elementId){
	var element = document.getElementById(elementId);
	if( element ){
		if (element.style.display != 'none'){
			element.style.display = 'none';
		}else{
			element.style.display = 'block';
		}
	}
}

function confirmBeforeAction(msg, href){
	if (confirm(msg)){
		document.location.href = href;
	}
}

function confirmBeforeFormSubmit(msg){
	return (confirm(msg));
}

function topBarPasswordTrick(o){
	o.style.display = 'none';
	o.style.position = 'absolute';
	document.getElementById('accountPassword').style.position = '';
	document.getElementById('accountPassword').style.display = '';
	document.getElementById('accountPassword').focus();
}

function topBarUsernameTrick(o){
	o.className = '';
	o.value = '';
}

addLoad(applyInterface);

function fixes(){
	$$("*:last-child").addClass("last-child");
	$$("*:first-child").addClass("first-child");
}

function applyFocus(){
	$$(".focus").each(function(el){
		el.focus();
	});
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function updateTinyFields(){
	if (!(typeof(tinyMCE) == "undefined")){ //jesli uzywamy TINY
		tinyMCE.triggerSave();//(true,true); //zrób magie, aby przed wysłaniem formatki przekazać dane TINY do odp. pól.
	}
}

function editorLaunch(moduleHandle, type){
	var newwindow = window.open('?component=editor&editorPage=applet&editorModuleHandle=' + moduleHandle + '&editorId=' + type, 'popup', 'height='+(screen.height-10)+', width='+(screen.width-10));
	newwindow.moveTo(0,0);
	if (window.focus)
		newwindow.focus();
}

function editorLoadLaunch(taskHandle, loadTicket){
	var newwindow = window.open('?component=editor&editorPage=applet&editorTaskHandle=' + taskHandle + '&editorLoadTicket=' + loadTicket + '&editorId=Test', 'popup', 'height='+(screen.height-10)+', width='+(screen.width-10));
	newwindow.moveTo(0,0);
	if (window.focus)
		newwindow.focus();
}

function editorSwitchDo(type, filename, loadTicket){
/*	alert('Type: ' + type + ' / Filename: ' + filename); */
	var cnt = document.getElementById('appletContainer');
	var sidParam = document.getElementById('sidParam').value;
	var moduleHandleParam = document.getElementById('moduleHandleParam').value;
	var lafParam = document.getElementById('lafParam').value;
	var fontParam = document.getElementById('fontParam').value;
	var loadTicketParam = document.getElementById('loadTicketParam').value;
	var code = 'pl.gint.editor.' + type.toLowerCase() + 'editor.' + type + 'EditorForm';
	cnt.innerHTML = 
	'	<applet id = "applet" archive = "html/editor/GintTaskEditor.jar, html/editor/GTEConfig.jar, html/editor/substance.jar, html/editor/chttp.jar, html/editor/cl.jar, html/editor/cladapters.jar, html/editor/clapi.jar, html/editor/cc.jar" code = "' + code + '" style = "width: 100%">' +
	'		<param id = "sidParam" name = "sid" value = "' + sidParam + '" />' +
	'		<param id = "moduleHandleParam" name = "moduleHandle" value = "' + moduleHandleParam + '" />' +
	'		<param id = "loadTicketParam" name = "loadTicket" value = "' + loadTicketParam + '" />' +
	'		<param id = "lafParam" name = "laf" value = "' + lafParam + '" />' +
	'		<param id = "fontParam" name = "font" value = "' + fontParam + '" />' +
	'		<param id = "filenameParam" name = "filename" value = "' + filename + '" />' +
	'		<param name = "mayscript" value = "true" />' +
	'       <param name = "java_arguments" value="-Xmx128m" />'+
	'	</applet>';
	//window.location.reload(); //?
}

function editorSwitch(type, filename){
	setTimeout("editorSwitchDo('" + type + "', '" + filename + "')", 100);
}


addLoad(fixes);
addLoad(applyFocus);

