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));

// 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) {
	$(theField).set("value", $(theField).get("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 : $(theField);
	elm.value = elm.value.replace( /^\s+/g,'').replace(/\s+$/g,'')*/
	if ($(theField).get("value").length == 0) {
		$(theField).set("value", theTxt);
	} else {
		$(theField).set("value", $(theField).get("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 url
function confirmToUrl(text, url) {
	if (confirm(text)) {
		window.location.href = url;
	}
}

// Sets the image src
function changeInner(elm, url) {
	var block = $(elm);
	if (url != "" && url != "undefined" && url != null) {
		block.innerHTML = "<img src=\""+url+"\" />";
	} else {
		block.innerHTML = "";
	}
}

// Changes the element class
function setClass(elm, tclass) { elm.className = tclass; }
function switchClass(elm, tclass) {
	cclass = elm.className;
	setClass(elm, tclass);
	if (cclass) {
		elm.onmouseout = function() { setClass(elm,cclass); }
	}
}

// Changes the image source
function setSrc(elm, tsrc) { elm.src = tsrc; }
function switchSrc(elm, tsrc) {
	csrc = elm.src;
	setSrc(elm, tsrc);
	if (csrc) {
		elm.onmouseout = function() { setSrc(elm,csrc); }
	}
}

// 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 = $(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 = $("processing");
	if (ifp) {
		scroll(0,0);
		ifp.className = "processing-on";
		centerElm("processing");
	}
}

// Output error text around input field
function error(elm, msg) {
	err = $(elm+"_error");
	txt = $(elm+"_errortxt");
	if (err) {
		err.className = "error-input";
		txt.className = "error-input-txt";
		txt.innerHTML = msg;
	} else {
		alert(msg+" ("+elm+")");
	}
}

// Hides an element by it's ID
function hide(elmID) {
	$(elmID).style.display = 'none';
}

// 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-$(elm).offsetHeight)/2;
	    var iLeft = (scrWidth-$(elm).offsetWidth)/2;
	    $(elm).style.left=iLeft + document.body.scrollLeft;
	    $(elm).style.top=iTop + document.body.scrollTop;
	}
}