var userAgent = navigator.userAgent.toLowerCase();
var is_opera = (userAgent.indexOf('opera') != -1);
var is_saf = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv = (userAgent.indexOf('webtv') != -1);
var is_ie = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4 = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz = (navigator.product == 'Gecko');
var is_kon = (userAgent.indexOf('konqueror') != -1);
var is_ns = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4 = ((is_ns) && (parseInt(navigator.appVersion) == 4));

// Fetches DOM object by ID
function fetch_object(idname) {
	if (document.getElementById) {
		return document.getElementById(idname);
	} else if (document.all) {
		return document.all[idname];
	} else if (document.layers) {
		return document.layers[idname];
	}
	return false;
}

// Forwards user to url
function goTo(url) {
	window.location.href = url;
}

// Set url as homepage if possible, otherwise output error
function setHome(url, errorTxt) {
	 if(window.ActiveXObject && document.getElementById){
		 document.getElementById('homepage').setHomePage(url);
	} else {
		alert(errorTxt);
	}
}

// Attempts to add website to favorites
function addToFavorites(errorTxt) {
	if (is_ie) {
		window.external.AddFavorite(location.href, document.title);
	} else {
		alert(errorTxt);
	}
}

// Adds text to a document element
function addTxt(theTxt, theField) {
	obj = eval("document."+theField);
	if (obj) {
		obj.value += " "+theTxt;
	} else {
		fetch_object(theField).value += " "+theTxt;
	}
}

// Adds text to a document element and adds a newline if current value is empty
function addTxtNewline(theTxt, theField) {
	obj = eval("document."+theField);
	elm = (obj) ? obj : fetch_object(theField);
	elm.value = elm.value.replace( /^\s+/g,'').replace(/\s+$/g,'')
	if (elm.value.length == 0) {
		elm.value = theTxt;
	} else {
		elm.value += '\n'+theTxt;
	}
}

// Closes window after a certain time (in microseconds)
function windowClose(timedelay) {
	setTimeout("window.close();",timedelay);
}

// Refreshes opener window and closes current window
function refreshParentAndClose(timedelay) {
	window.opener.location.href = window.opener.location.href;
	setTimeout("window.close();",timedelay);
}

// Submits form which opened
function updateOpenerForm(theForm) {
	theForm.performaction.value='false';
	theForm.submit()
	return null;
}

// Asks user if he is sure to delete this, submits form
function sureToDelete(text) {
	return confirm(text);
}

// Asks for text and when affirmative goes to certain url
function confirmToUrl(text, url) {
	if (confirm(text)) {
		window.location.href = url;
	}
}

// Sets the image src
function changeInner(elm, url) {
	var block = fetch_object(elm);
	if (url != "" && url != "undefined" && url != null) {
		block.innerHTML = "<img src=\""+url+"\" />";
	} else {
		block.innerHTML = "";
	}
}

// Changes the element class
function switchClass(elm, tclass) {
	cclass = elm.className;
	setClass(elm, tclass);
	if (cclass) {
		elm.onmouseout = function() {setClass(elm,cclass);}
	}
}
function setClass(elm, tclass) { elm.className = tclass; }

// Changes the image source
function switchSrc(elm, tsrc) {
	csrc = elm.src;
	setSrc(elm, tsrc);
	if (csrc) {
		elm.onmouseout = function() {setSrc(elm,csrc);}
	}
}
function setSrc(elm, tsrc) { elm.src = tsrc; }

// Submit form from somewhere other than submit
function submitForm(frm) {
	document.forms[frm].submit();
}

// Show a preview of the current form
function previewForm(frm) {
	frm.action = 'preview';
	frm.submit();
}

// Shows a popup window
function popup(url, wd, hd) {
	window.open(url, "remote", "width="+wd+",height="+hd+","+
			"toolbar=no,status=no,resizable=yes,scrollbars=yes,menubar=no,"+
			"top="+((screen.availHeight/2)-(hd/2))+","+ 
			"left="+((screen.availWidth/2)-(wd/2)));
	return void(0);
}

// Shows a popup in the corner
function popupCorner(url, wd, hd) {
	window.open( url , "remote", "width="+wd+",height="+hd+","+
			"toolbar=no,status=no,resizable=yes,scrollbars=yes,menubar=no,top=50,left=50");
	return void(0);
}

// Display or hides an element
function toggleDisplay(me, displayStyle) {
	elm = fetch_object(me);
	current = (elm.style.display == 'block') ? 'none' : 'block';
	elm.style.display = current;
}

// Disables form submit button and tries to load 'form submitting' box if possible
function formProcessing(butt, msg) {
	butt.value = msg;
	butt.form.click();
	butt.form.submit();
	butt.disabled = 1;
	var ifp = fetch_object("processing");
	if (ifp) {
		scroll(0,0);
		ifp.className = "processing-on";
		centerElm("processing");
	}
}

// Output error text around input field
function error(elm, msg) {
	err = fetch_object(elm+"_error");
	txt = fetch_object(elm+"_errortxt");
	if (err) {
		err.className = "error-input";
		txt.className = "error-input-txt";
		txt.innerHTML = msg;
	} else {
		alert(msg+" ("+elm+")");
	}
}

// Centers element
function centerElm(elm) {
	if (typeof(window.innerWidth)=='number' ) {
		scrWidth = window.innerWidth;
		scrHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		scrWidth = document.documentElement.clientWidth;
		scrHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		scrWidth = document.body.clientWidth;
		scrHeight = document.body.clientHeight;
	}
	if (scrWidth || scrHeight) {
		var iTop = (scrHeight-fetch_object(elm).offsetHeight)/2;
	    var iLeft = (scrWidth-fetch_object(elm).offsetWidth)/2;
	    fetch_object(elm).style.left=iLeft + document.body.scrollLeft;
	    fetch_object(elm).style.top=iTop + document.body.scrollTop;
	}
}