function create_request() {
    var request = undefined;
    try {
	request = new XMLHttpRequest();
    } catch (trymicrosoft) {
	try {
	    request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
	    try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
		request = false;
	    }
	}
    }
    return request;
}

function ajax_post(url_str, post_str, success_fnc, error_fnc, async) {
    var request = create_request();
    if((async == undefined) || (async == '')) var async = true;
    if((post_str == undefined) || (post_str == '')) var post_str = true;
    
    request.onreadystatechange = function() {
	if(request.readyState == 4) {
	    if(request.status == 200) {
		if((success_fnc != undefined) && (success_fnc != '')) eval(success_fnc+'(request);');
		else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)');
	    }
	}
    }
    request.open('POST', url_str, true);
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    request.send(post_str);
}

function ajax_get(url_str, success_fnc, error_fnc, async) {
    var request = create_request();
    if((async == undefined) || (async == '')) var async = true;
    
    request.onreadystatechange = function() {
	if(request.readyState == 4) {
	    if(request.status == 200) {
		if((success_fnc != undefined) && (success_fnc != '')) eval(success_fnc+'(request);');
		else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)');
	    } else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)');
	}
    }
    request.open('GET', decodeURI(url_str), async);
    request.send(null);
    return true;
}

function clear_element(parent_element) {
    if(parent_element != undefined) {
	var children = parent_element.childNodes;
	while(children.length > 0) {
	    parent_element.removeChild(children[0]);
	    children = parent_element.childNodes;
	}
    }
    return true;
}

function popup(url,width,height,scrolls) {
    var popup_div = document.getElementById('extradiv');
    var _url = url || undefined;
    var _width = width || 350;
    var _height = height || 200;
    var _scrolls = scrolls || false;
    var _height = _height + 20; // To make room for the close button and border
    var _width = _width; // To make room for the border

    var window_body = document.body||document.getElementsByTagName("body")[0];
    var window_width = window_body.clientWidth || window_body.innerWidth;
    var window_height = window_body.clientHeight || window_body.innerHeight;
    
    popup_div.style.border = '1px solid #060';
    popup_div.style.backgroundColor = '#efe';
    popup_div.style.width = _width+'px';
    popup_div.style.height = _height+'px';
    popup_div.style.margin = 'auto';
    popup_div.style.top = ((window_height-_height)/2)+'px';
    popup_div.style.left = ((window_width-_width)/2)+'px';
    
    if(scrolls != undefined) popup_div.style.overflow = 'auto';
    else popup_div.style.overflow = 'hidden';
    
    var loader_div = document.createElement('div');
    loader_div.innerHTML = '<img src="/img/loading.gif" alt="loader" />';
    loader_div.style.width = '34px';
    loader_div.style.height = '34px';
    loader_div.style.position = 'absolute';
    loader_div.style.top = ((_height-32)/2)+'px';
    loader_div.style.left = ((_width-32)/2)+'px';
    
    popup_div.appendChild(loader_div);
    popup_div.style.visibility = 'visible';
    popup_div.style.display = 'block';
    
    ajax_get(url,'popup_loader_success','popup_loader_error',true);
}

function popup_loader_success(request) {
    var popup_div = document.getElementById('extradiv');
    var content_div = document.createElement('div');
    content_width = popup_div.style.width.substring(0,popup_div.style.width.length-2);
    content_height = popup_div.style.height.substring(0,popup_div.style.height.length-2);
    
    //content_div.style.width = (content_width-2)+'px';
    //content_div.style.height = (content_height-2)+'px';
    
    var closer = '<div style="position:absolute;top:0px;left:'+(content_width-18)+'px;cursor:pointer;" onclick="javascript:document.getElementById(\'extradiv\').style.visibility=\'hidden\';document.getElementById(\'extradiv\').style.display=\'none\';"><img src="/img/delete.png" /></div>';

    //var closer = '<div style="border:1px solid black;float:right;cursor:pointer;" onclick="javascript:document.getElementById(\'extradiv\').style.visibility=\'hidden\';document.getElementById(\'extradiv\').style.display=\'none\';"><img src="/img/delete.png" /></div>';

    var the_responseText = request.responseText;

    reg_ex = /<script.*>((\s|.*)*)<\/script>/i
    if(reg_ex.test(the_responseText)) {
	my_array = reg_ex.exec(request.responseText);
	the_responseText = request.responseText.replace(reg_ex, ' ');

	content_div.innerHTML = closer + the_responseText;
	clear_element(popup_div);
	popup_div.appendChild(content_div);

	eval(my_array[1]);
    } else {
	content_div.innerHTML = closer + the_responseText;
	clear_element(popup_div);
	popup_div.appendChild(content_div);
    }


    //    content_div.innerHTML = closer + request.responseText;

}

function popup_loader_error(request) {
    var popup_div = document.getElementById('extradiv');
    var content_div = document.createElement('div');
    
    content_width = popup_div.style.width.substring(0,popup_div.style.width.length-2);
    content_height = popup_div.style.height.substring(0,popup_div.style.height.length-2);
    
    content_div.style.width = content_width+'px';
    content_div.style.height = content_height+'px';
    
    var closer = '<div style="position:absolute;top:0px;left:'+(content_width-18)+'px;cursor:pointer;" onclick="javascript:document.getElementById(\'extradiv\').style.visibility=\'hidden\';document.getElementById(\'extradiv\').style.display=\'none\';"><img src="/img/delete.png" /></div>';

    //    var closer = '<div style="float:left;width:'+(content_width-18)+'px;">&nbsp;</div><div style="cursor:pointer;" onclick="javascript:document.getElementById(\'extradiv\').style.visibility=\'hidden\';document.getElementById(\'extradiv\').style.display=\'none\';"><img src="/admin/img/delete.png" /></div>';
    
    content_div.innerHTML = closer+"Der opstod en fejl!";
    clear_element(popup_div);
    popup_div.appendChild(content_div);
}

//function errorHandler() {
//    alert('bla');
//    return true;
//}

//document.onerror = errorHandler;
