// Create a popup-window
function popup(width, height, title, url, content){
	popup_window = window.open(url, 'popup_window', 'width='+width+',height='+height+',scrollbars=yes');
	if (url == '' && content != ''){
		var tmp = popup_window.document;
		tmp.write('<html>');
		tmp.write('<head>');
		tmp.write('<title>'+title+'</title>');
		tmp.write('<link rel="stylesheet" type="text/css" href="/frontend/css/form.css" />');
		tmp.write('<link rel="stylesheet" type="text/css" href="/frontend/css/table.css" />');
		tmp.write('<link rel="stylesheet" type="text/css" href="/frontend/css/font.css" />');
		tmp.write('<link rel="stylesheet" type="text/css" href="/frontend/css/custom.css" />');
		tmp.write('</head>');
		
		tmp.write('<body>');
		tmp.write(''+urldecode(unescape(content))+'');
		tmp.write('<br />');
		tmp.write('<a href="javascript:self.close()">Luk vinduet</a>');
		tmp.write('</body>');
		
		tmp.write('</html>');
		tmp.close();
	}
}

function urldecode(str){
    var histogram = {}, ret = str.toString(), unicodeStr = '', hexEscStr = '';
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
	histogram['<']	 = '%3C';
	histogram['>']	 = '%3E';
	histogram['/']	 = '%2F';
	
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u00DC'] = '%DC';
    histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4';
    histogram['\u00E4'] = '%E4';
    histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6';
    histogram['\u00DF'] = '%DF'; 
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
 
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    ret = ret.replace(/%20/ig, ' ');
	
	return ret;
}

// Get elements with a certain class-name
function getElementsByClassName(strClassName, obj){
	var ar = arguments[2] || new Array();
	var re = new RegExp("\\b" + strClassName + "\\b", "g");
	
	if (re.test(obj.className)){
		ar.push(obj);
	}
	for ( var i = 0; i < obj.childNodes.length; i++ ){
		getElementsByClassName(strClassName, obj.childNodes[i], ar);
	}
	
	return ar;
}

// Show/hide an element
function display(elm, mode, show, hide){
	if (show == '' || show == undefined){
		show = 'block';
	}
	if (hide == '' || hide == undefined){
		hide = 'none';
	}
	
	elm = $(elm);
	
	switch (mode){
		case 'show':
			elm.setStyle('display', show);
		break;
		case 'hide':
			elm.setStyle('display', hide);
		break;
		default:
			if (elm.getStyle('display') == show){
				elm.setStyle('display', hide);
			}
			else{
				elm.setStyle('display', show);
			}
		break;
	}
}

// Show/hide all elements with a certain class-name
function display_all(class_name, mode, show, hide){
	if (show == '' || show == undefined){
		show = 'block';
	}
	if (hide == '' || hide == undefined){
		hide = 'none';
	}
	
	var elements = getElementsByClassName(class_name, document.body);
	
	for (i = 0; i < elements.length; i++){
		switch (mode){
			case 'show':
				elements[i].setStyle('display', show);
			break;
			case 'hide':
				elements[i].setStyle('display', hide);
			break;
			default:
				if (elements[i].getStyle('display') == show){
					elements[i].setStyle('display', hide);
				}
				else{
					elements[i].setStyle('display', show);
				}
			break;
		}
	}
}

// Check all elements with a certain class-name
function check_all(class_name){
	var elements = getElementsByClassName(class_name, document.body);
	
	for (i = 0; i < elements.length; i++){
		elements[i].checked = true;
	}
}

// Un-check all elements with at certain class-name
function uncheck_all(class_name){
	var elements = getElementsByClassName(class_name, document.body);
	
	for (i = 0; i < elements.length; i++){
		elements[i].checked = false ;
	}
}
