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;
}

// Preloads an image
function preloadImg(filepath) {
	image1 = new Image();
	image1.src = filepath;
}

// 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) {
	elm = $(me);
	current = (elm.getStyle('display') != 'none') ? 'none' : 'block';
	elm.setStyle('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");
	}
}

// Adds messagebox message
function messagebox(txt, type) {
	var html = '<div class="'+type+'" onclick="toggleDisplay(this)">'+txt+'</div>';
	$('messagebox').innerHTML = $('messagebox').innerHTML + html;
}

// Displays an info message
function info(txt, type) {
	messagebox(txt, 'info');
}

// Displays an error message
function error(txt) {
	messagebox(txt, 'error');
}

// Displays an update message
function update(txt) {
	messagebox(txt, 'update');
}

// Output error text around input field
function errorInput(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';
}

// Resets the input value on first focus
function resetInputOnFocus(elm, color, password) {
	elm.addEvent('focus', function() {
		if (password) { 
			elm.setAttribute('type', 'password');
		}
		elm.setAttribute('value', '');
		if (elm.getStyle('font-style') == 'italic') {
			elm.setStyle('font-style', 'normal');
		}
		if (color) {
			elm.setStyle('color', color);
		}
	});
}

// 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;
	}
}

// Adds table classes to all tabbles except those with class 'empty'
function addTableClasses(cn) {
	i = 0;
	$$('table[class='+cn+'] tr').each(function(el, index) {
		if (!el.hasClass("subheader") && !el.hasClass("header")) {
			el.getElements('td:even').each(function(el, index){el.addClass('even');});
			el.getElements('td:odd').each(function(el, index){el.addClass('odd');});
			el.getElements('td:nth-child(1)').each(function(el, index){el.addClass('first');});
			el.addClass((i%2==0) ? "odd" : "even");
			i++;
		}
	});
	
}

// Strips slashes from some string, Useful when you need to strip slashes from
// PHP-generated database values.
function stripslashes(str) {
    return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':                return '\u0000';
            case '':
                return '';
            default:
                return n1;        }
    });
}
window.addEvent('domready', function() {
	addTableClasses('lst');
	addTableClasses('oview');
});
