function browsertype() {
	if (navigator.userAgent.indexOf('MSIE') > -1) {
		return "ie";
	}
	if (navigator.userAgent.indexOf('Opera') > -1) {
		return "opera";
	}
	return "mozilla";
}

switch (browsertype()) {
	case "ie":
		var tr_showproperty = 'block';
		var tbody_showproperty = 'block';
		break;
	default:
		var tr_showproperty = 'table-row';
		var tbody_showproperty = 'table-row-group';
}

// Hide an element
function hide(element) {
	element.style.visibility = 'hidden';
	element.style.display = 'none';
}

// Unhide/show an element
function show(element, type) {
	element.style.visibility = 'visible';
	element.style.display = type;
}
function ValidateEmail(str) {
	var expr = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (expr.test(str)) {
   		return true;
	} else {
		return false;
	}
}

// str_replace() - just like the PHP version. Used mainly in the convert_funky_letters() function, which is also performed serverside.
function str_replace(match, replacement, string) {
	var result = '';
	rexp = new RegExp(match, 'gi');
	result = string.replace(rexp, replacement);
	return result;
}

function convert_funky_letters(form) {
	for(i = 0; i < form.length;i++){
		el=form[i];
		if ((el.type == 'text') || (el.type == 'textarea')) {
			el.value = str_replace("–", '-', str_replace('”', '"', str_replace("…",'...', str_replace("’", "'", str_replace("™", '(TM)', str_replace("©", '(C)', str_replace("®", '(R)', el.value)))))));
		}
	}
	return false;
}

function validate_add_lemseabonnement() {
	if (document.bliabonnent.navn.value == '') {
		window.alert('Du må skrive navnet ditt.');
		document.bliabonnent.navn.focus();
		return false;
	}
	if (document.bliabonnent.email.value == '') {
		window.alert('Du må skrive Email-adressen din.');
		document.bliabonnent.email.focus();
		return false;
	}
	if (!ValidateEmail(document.bliabonnent.email.value)) {
		window.alert('Email-adressen du oppgav er ugyldig.');
		document.bliabonnent.email.focus();
		return false;
	}
	return true;
}

function validate_remove_lemseabonnement() {
	if (document.slettabonnent.email.value == '') {
		window.alert('Du må skrive Email-adressen din.');
		document.slettabonnent.email.focus();
		return false;
	}
	if (!ValidateEmail(document.slettabonnent.email.value)) {
		window.alert('Email-adressen du oppgav er ugyldig.');
		document.slettabonnent.email.focus();
		return false;
	}
	return true;
}

function showhide(element) {
	var current_vis = document.getElementById(element).style.visibility;
	if ((current_vis == 'visible') || (!current_vis)) {
		setCookie('SDWEB_' + str_replace(' ', '_', element),'hidden');
		var new_vis = 'hidden';
	} else {
		setCookie('SDWEB_' + str_replace(' ', '_', element),'visible');
		var new_vis = 'visible';
	}
	if (new_vis == 'visible') {
		show(document.getElementById(element), tbody_showproperty);
	} else {
		hide(document.getElementById(element));
	}
}

//////////////////////
// Cookie-functions //
//////////////////////

/**
* Sets a Cookie with the given name and value.
*
* name       Name of the cookie
* value      Value of the cookie
* [expires]  Expiration date of the cookie (default: end of current session)
* [path]     Path where the cookie is valid (default: path of calling document)
* [domain]   Domain where the cookie is valid
*              (default: domain of calling document)
* [secure]   Boolean value indicating if the cookie transmission requires a
*              secure transmission
*/
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

/**
* Gets the value of the specified cookie.
*
* name  Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
*   or null if cookie does not exist.
*/
function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
* Deletes the specified cookie.
*
* name      name of the cookie
* [path]    path of the cookie (must be same as path used to create cookie)
* [domain]  domain of the cookie (must be same as domain used to create cookie)
*/
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
