/**
	This script was created by Lasse R. Brock
	 
	The purpose is to provide generic javascript functions that can be used over and over 
	 
	The Ajax Queue System Example:
	 
	[Here we build the request]
		var function_to_call_before_sending = loading();
		var function_to_call_on_return = handleGetMap();
		ajaxQueueCancelThese('map_data'); (Only used if nessesary to cancel other requests of the same tag in the queue)
		ajaxQueueAdd('GET', url, 'map_data', function_to_call_before_sending, function_to_call_on_return, null);
	[Here we recieve the request data]
	****** Notice the use of receiveReq_ajax_queue ******
	function handleGetMap() 
	{	
		var data = receiveReq_ajax_queue.responseText;
	}
**/

/** 
	Required JS:
	php.js is required
**/

window.onload = joinFunctions(window.onload, function(){if(typeof is_numeric != 'function')
{
	alert("Husk at include php.js");
}})

/** Can be used when you want to use window.onload from several different files **/
/** What it does is to continually increase the amount of information stored in window.onload, and when we're done loading, execute it **/
/** Example: window.onload = joinFunctions(window.onload, myNewFunction ); **/
function joinFunctions(function1, function2) 
{
    return function() {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}

/** Shortcut for joinFunctions **/
function jf(function1, function2)
{
	return joinFunctions(function1, function2);
}

/** Detecting browser **/
if(navigator.userAgent.indexOf('Trident') != -1 || navigator.userAgent.indexOf('MSIE') != -1){var browser = "ie";}
else{var browser = "not ie";}
/** And browser version **/
if(navigator.appVersion.indexOf('MSIE 8.0') != -1){var browser_version = 8;}
else if(navigator.appVersion.indexOf('MSIE 7.0') != -1){var browser_version = 7;}
else if(navigator.appVersion.indexOf('MSIE 6.0') != -1){var browser_version = 6;}
else{var browser_version = 'not ie';}

var current_event = null;
var overlay_zindex_counter = 900000;
var active_overlay_ids_array = new Array();
var receiveReq_ajax_queue = getXmlHttpRequestObject();
var ajax_queue_array = new Array();
var ajax_queue_timer = "";
var ajax_queue_current_tag = undefined;
var ajax_queue_request_validity = true;

function ajaxQueue()
{	
	if(ajax_queue_array.length > 0)
	{	
		/** We run the next element from the queue **/	
		if (receiveReq_ajax_queue.readyState == 4 || receiveReq_ajax_queue.readyState == 0)
		{		
			/** Running and removing the next element **/
			var ajax_data = ajax_queue_array.shift();
						
			/** Executing the functions **/
			ajax_data[3]();
			receiveReq_ajax_queue.open(ajax_data[0], ajax_data[1], true);
			/** Setting the onreadystate function **/
			receiveReq_ajax_queue.onreadystatechange = function(){ajaxQueueObjectReturn(ajax_data[4])};
			/** If we're dealing with a post we need to set the headers **/
			if(ajax_data[0].toLowerCase() == 'post')
			{
				receiveReq_ajax_queue.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				receiveReq_ajax_queue.setRequestHeader("Content-length", ajax_data[5].length);
				receiveReq_ajax_queue.setRequestHeader("Connection", "close");
			}
			/** Submitting the request **/
			receiveReq_ajax_queue.send(ajax_data[5]);
			/** Setting the current tag so that we may invalidate it later if we need to **/
			ajax_queue_current_tag = ajax_data[2];
		}
	}
		
	if(ajax_queue_array.length > 0)
	{
		/** If there's data in the queue we call this function again in 0.5 second **/
		clearTimeout(ajax_queue_timer);
		ajax_queue_timer = setTimeout("ajaxQueue();", 500);
	}
}

function ajaxQueueAdd(method, url, tag, function_to_call_before_sending, function_to_call_on_return, send_body)
{
	if(method != undefined)
	{		
		if(send_body == undefined)
		{
			send_body = null;
		}
		/** Adding the object to the queue **/
		ajax_queue_array.push(new Array(method, url, tag, function_to_call_before_sending, function_to_call_on_return, send_body));
	}
	
	ajaxQueue();
}

var ajax_queue_request_data = "";
var ajax_queue_request_obj = null;

function ajaxQueueObjectReturn(function_to_call)
{
	if (receiveReq_ajax_queue.readyState == 4) 
	{		
		ajax_queue_current_tag = undefined;
		if(ajax_queue_request_validity)
		{
			ajax_queue_request_data = receiveReq_ajax_queue.responseText;
			ajax_queue_request_obj = receiveReq_ajax_queue;
			if(typeof function_to_call == 'function')
			{
				function_to_call();
			}
		}
		else
		{
			ajax_queue_request_validity = true;
		}
	}
}

function ajaxQueueCancelThese(tag)
{	
	for(x in ajax_queue_array)
	{
		if(ajax_queue_array[x][2] == tag)
		{			
			ajax_queue_array.splice(x,1);
		}
	}
	
	if(ajax_queue_current_tag == tag)
	{
		ajax_queue_request_validity = false;
		ajax_queue_current_tag = undefined;
	}	
}

function ajaxRequest(method, url, function_to_call_on_return, send_body)
{
	if(method != undefined)
	{		
		if(send_body == undefined)
		{
			send_body = null;
		}
		
		/** Creating a new object **/
		var ajax_request_object = getXmlHttpRequestObject();
		
		ajax_request_object.open(method, url, true);
		/** Setting the onreadystate function **/
		ajax_request_object.onreadystatechange = function(){ajaxRequestObjectReturn(ajax_request_object, function_to_call_on_return)};
		/** If we're dealing with a post we need to set the headers **/
		if(method.toLowerCase() == 'post')
		{
			ajax_request_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax_request_object.setRequestHeader("Content-length", send_body.length);
			ajax_request_object.setRequestHeader("Connection", "close");
		}
		/** Submitting the request **/
		ajax_request_object.send(send_body);
	}
}

var ajax_request_data = "";
var ajax_request_obj = null;
function ajaxRequestObjectReturn(request_object, function_to_call)
{
	if (request_object.readyState == 4) 
	{		
		ajax_request_data = request_object.responseText;
		ajax_request_obj = request_object;
		if(typeof function_to_call == 'function')
		{
			function_to_call();
		}
		/** Destroying the object **/
		request_object = null;
	}
}

function createVeil(final_fade, do_fade, color, function_to_execute_when_done, pause_between_fade_incrementions)
{
	setDocumentHeightAndWidth();
	var body_dom = document.getElementsByTagName('body')[0];

	/** We create the protective layer that will make sure that the user cannot click on anything except our overlay **/
	var protective_div = document.createElement('div');
	protective_div.id = 'protective_div';				
	
	/** Styling the protective div **/
	protective_div.style.position = "absolute";
	if(browser != "ie")
	{
		protective_div.style.position = "fixed";
	}
	
	protective_div.style.top 	= "0px";
	protective_div.style.left 	= "0px";
	protective_div.style.width 	= document_total_width+"px";
	protective_div.style.height = document_total_height+"px";
	
	if(color == undefined)
	{
		color = "#000000";
	}
	protective_div.style.backgroundColor = color;
	protective_div.style.zIndex = overlay_zindex_counter;
	protective_div.style.opacity = 0;
	protective_div.style.display = 'inline';
	
	/** Appending it to the body **/
	body_dom.appendChild(protective_div);
	
	/** Updating the zindex counter **/
	overlay_zindex_counter++;
	
	if(browser_version == 6)
	{
		hideSelects();
	}
	
	if(final_fade != undefined)
	{
		if(do_fade != undefined && do_fade)
		{
			fadeElement(protective_div, 0, final_fade, 'in', function_to_execute_when_done, pause_between_fade_incrementions);
		}
		else
		{
			if(browser == 'ie')
			{
				protective_div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+final_fade+')';	
				protective_div.style.filter = 'alpha(opacity='+final_fade+');';
			}
			else
			{
				protective_div.style.opacity = final_fade/100;
			}
			
			if(typeof function_to_execute_when_done == 'function')
			{
				function_to_execute_when_done();
			}
		}
	}
	else
	{
		if(browser == 'ie')
		{
			protective_div.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';	
			protective_div.style.filter = 'alpha(opacity=50);';
		}
		else
		{
			protective_div.style.opacity = 0.5;
		}
		
		if(typeof function_to_execute_when_done == 'function')
		{
			function_to_execute_when_done();
		}		
	}
	
	if(browser == 'ie')
	{
		protective_div.onmousewheel = joinFunctions(protective_div.onmousewheel, function(){handleVeilScroll();});
		window.onresize 			= joinFunctions(window.onresize, function(){handleVeilScroll();});
	}		
}

function handleVeilScroll()
{
	var veil = getElement("protective_div");
	if(veil)
	{
		setDocumentHeightAndWidth();
		veil.style.width 	= document_total_width+"px";
		veil.style.height 	= document_total_height+"px";		
	}
}

var number_of_overlay_images_to_preload = 0;
var number_of_overlay_images_preloaded = 0;
function createOverlay(data, position, top, left, id, create_this_overlay_at_this_element, dragable, dragable_element_in_data, allowed_to_exist_outside_of_view, step)
{
	var body_dom = document.getElementsByTagName('body')[0];
	if(step == undefined)
	{
		step = 0;
	}
	
	/** Creating the overlay if it hasn't been so already **/
	var overlay_div = getElement(id);
	if(!overlay_div)
	{
		/** Creating an overlay **/		
		var overlay_div = document.createElement('div');
	
		if(id == undefined)
		{
			id = 'overlay_div_'+overlay_zindex_counter;
		}
		
		/** Setting the id of the overlay **/
		overlay_div.id = id;
		
		/** Addding this overlay's id to the array of active overlays **/
		active_overlay_ids_array.push(id);
		
		/** Now we append this overlay to the body **/
		body_dom.appendChild(overlay_div);		
	}	
	
	if(step == 0)
	{
		/** Inserting the data but masking it with dislay:none so that the user doesn't see us when we preload **/
		overlay_div.style.display = 'none';
		if(data != undefined)
		{
			overlay_div.innerHTML = data;
		}
		
		var children = overlay_div.getElementsByTagName('img');
		
		/** Resetting the preload variables **/
		number_of_overlay_images_to_preload 	= 0;
		number_of_overlay_images_preloaded 		= 0;		
		
		/** Checking the new content for images to preload **/
		var images_to_preload_array = new Array();
		for(x in children)
		{
			if(children[x].src != undefined)
			{				
				number_of_overlay_images_to_preload++;
				images_to_preload_array.push(children[x].src);
			}
		}
		
		if(number_of_overlay_images_to_preload == 0)
		{
			/** No images to preload, we continue **/
			createOverlay(data, position, top, left, id, create_this_overlay_at_this_element, dragable, dragable_element_in_data, allowed_to_exist_outside_of_view, 1);
		}
		else
		{
			/** There's images to preload, so we preload them **/
			for(x in images_to_preload_array)
			{
				preloadThisImage(images_to_preload_array[x], function()
					{
						donePreloadImagesForCreateOverlay(data, position, top, left, id, create_this_overlay_at_this_element, dragable, dragable_element_in_data, allowed_to_exist_outside_of_view, 1);
					}
				);			
			}			
		}		
	}
	else
	{
		/** Making the overlay visible **/
		overlay_div.style.display = '';
		
		if(allowed_to_exist_outside_of_view == undefined)
		{
			allowed_to_exist_outside_of_view = true;
		}
		
		if(top == undefined)
		{
			top = 'center';
		}
		else if(top != 'center' || (create_this_overlay_at_this_element != undefined && getElement(create_this_overlay_at_this_element)))
		{
			top = parseInt(top);
			if(isNaN(top))
			{
				top = 0;
			}
		}
		
		if(left == undefined)
		{
			left = 'center';
		}
		else if(left != 'center' || create_this_overlay_at_this_element != undefined && getElement(create_this_overlay_at_this_element))
		{
			left = parseInt(left);
			if(isNaN(left))
			{
				left = 0;
			}
		}
		
		if(position == undefined)
		{
			position = "fixed";
		}				
		
		/** A few styles **/
		overlay_div.style.position = 'absolute'; // IE doesn't understand fixed so it's always absolute
		if(browser != 'ie')
		{		
			overlay_div.style.position = position;
		}
		
		overlay_div.style.zIndex = overlay_zindex_counter;
		overlay_zindex_counter++;		
		
		if(dragable != undefined && dragable)
		{
			/** This overlay should be dragable **/
			if(dragable_element_in_data != undefined)
			{				
				dragable_element_in_data 				= getElement(dragable_element_in_data);
				dragable_element_in_data.onmousedown	= function(event){startDragging(event, id);};
				dragable_element_in_data.onmouseup		= function(){cancelDragging();};			
			}
			else
			{
				overlay_div.onmousedown	= function(event){startDragging(event, this);};
				overlay_div.onmouseup	= function(){cancelDragging();};
			}
		}
		
		/** Positioning the overlay **/
		if(create_this_overlay_at_this_element != undefined && create_this_overlay_at_this_element == 'mouse')
		{
			if(left == 'center')
			{
				left = 0;
			}
			
			if(top == 'center')
			{
				top = 0;
			}
			
			/** Creating the overlay at the mouse position **/
			setMouseXY();
			overlay_div.style.left 	= (mouseX*1+left)+"px";
			overlay_div.style.top 	= (mouseY*1+top)+"px";
		}
		else if(create_this_overlay_at_this_element != undefined && getElement(create_this_overlay_at_this_element))
		{				
			var obj_positions 		= getPositionOfObject(create_this_overlay_at_this_element).split(";");		
			overlay_div.style.left 	= (obj_positions[0]*1+left)+"px";
			overlay_div.style.top 	= (obj_positions[1]*1+top)+"px";
		}
		else
		{
			if(top == "center")
			{
				centerObject("top", id);
			}
			else
			{
				overlay_div.style.top = top+"px";	
			}
			
			if(left == "center")
			{
				centerObject("left", id);
			}
			else
			{
				overlay_div.style.left = left+"px";
			}
		}
		
		if(position == "fixed" && browser == 'ie')
		{
			window.onscroll = joinFunctions(window.onscroll, function(){ieFixed(id, top, left)});
			window.onresize = joinFunctions(window.onresize, function(){ieFixed(id, top, left)});
		}
		
		if(!allowed_to_exist_outside_of_view)
		{
			if(parseInt(overlay_div.style.top) < 0)
			{
				overlay_div.style.top = '0px';
			}
			if(parseInt(overlay_div.style.left) < 0)
			{
				overlay_div.style.left = '0px';
			}		
		}		
	}
}

function donePreloadImagesForCreateOverlay(data, position, top, left, id, create_this_overlay_at_this_element, dragable, dragable_element_in_data, allowed_to_exist_outside_of_view, step)
{
	number_of_overlay_images_preloaded++;
	if(number_of_overlay_images_preloaded >= number_of_overlay_images_to_preload)
	{		
		createOverlay(data, position, top, left, id, create_this_overlay_at_this_element, dragable, dragable_element_in_data, allowed_to_exist_outside_of_view, step)
	}
}

function addCloseButtonToOverlay(id, onclose_function, top, right, innerhtml)
{
	if(getElement(id))
	{
		if(onclose_function == undefined)
		{
			onclose_function = function(){
				closeOverlay(id);
			};
		}
		
		if(top == undefined)
		{
			top = -10;
		}
		
		if(right == undefined)
		{
			right = -10;
		}
	
		if(innerhtml == undefined)
		{
			innerhtml = '<img src="images/ajax/close.png">';
		}	
		
		
		var overlay_obj 			= getElement(id);
		var close_button 			= document.createElement('div');
		
		close_button.onclick 		= function(){onclose_function()};
		close_button.style.position = 'absolute';
		close_button.style.top 		= top+'px';
		close_button.style.right 	= right+'px';
		close_button.style.cursor 	= 'pointer';
		close_button.innerHTML 		= innerhtml;
		
		overlay_obj.appendChild(close_button);
		
		/** Checking if we need to adjust the overlay object a little to make room for our new object **/
		if(parseInt(overlay_obj.style.top)+top < 0)
		{
			overlay_obj.style.top = top*-1+'px';
		}		
	}
}

function ieFixed(obj, top, left)
{
	var obj = getElement(obj);
	
	if(obj)
	{
		/** Updating window dimensions **/
		updateWindowDimensions();	
		
		if(top == undefined || top == "center")
		{
			centerObject("top", obj);
		}
		else
		{
			obj.style.top = (window_scroll_y+top)+"px";
		}
		
		if(left == undefined || left == "center")
		{
			centerObject("left", obj);
		}
		else
		{
			obj.style.left = (window_scroll_x+left)+"px";
		}
	}
}

function centerObject(type, obj)
{	
	var obj = getElement(obj);
	
	/** Updating window dimensions **/
	updateWindowDimensions();
			
	/** Getting objects dimensions **/
	var width = obj.offsetWidth;
	var height = obj.offsetHeight;		
		
	if(obj.style.position == 'fixed')
	{
		window_scroll_y = 0;
		window_scroll_x = 0;
	}
		
	if(type != undefined && type == "top")
	{		
		obj.style.top 	= (window_scroll_y)+(window_height/2)-(height/2)+"px";
	}
	else if(type != undefined && type == "left")
	{

		obj.style.left 	= (window_scroll_x)+(window_width/2)-(width/2)+"px";
	}
	else
	{
		obj.style.top 	= (window_scroll_y)+(window_height/2)-(height/2)+"px";
		obj.style.left 	= (window_scroll_x)+(window_width/2)-(width/2)+"px";			
	}
	
	if(parseInt(obj.style.top) < 0)
	{
		obj.style.top = '0px';
	}
	if(parseInt(obj.style.left) < 0)
	{
		obj.style.left = '0px';
	}
}

function closeOverlay(id_to_close)
{
	if(id_to_close != undefined)
	{
		if(getElement(id_to_close))
		{
			removeElementFromDom(id_to_close);
			active_overlay_ids_array = unset(active_overlay_ids_array, id_to_close);
		}
	}
	else
	{
		/** We close all overlays **/
		for(x in active_overlay_ids_array)
		{
			removeElementFromDom(active_overlay_ids_array[x]);
		}
		active_overlay_ids_array = new Array();
	}
}

function closeVeil(fadeout, pause_between_fade_incrementions)
{
	var protective_div = getElement('protective_div');
	if(protective_div)
	{
		if(browser == 'ie')
		{
			/** Stopping the onscroll function **/
			protective_div.onmousewheel = '';			
		}
		
		if(fadeout != undefined && fadeout)
		{		
			var current_fade = 0;
			
			if(browser == 'ie')
			{
				current_fade = protective_div.filters[0].opacity;
			}
			else
			{
				current_fade = protective_div.style.opacity*100;
			}
			
			fadeElement(protective_div, current_fade, 0, 'out', function(){ removeElementFromDom("protective_div"); }, pause_between_fade_incrementions);
		}
		else
		{		
			removeElementFromDom("protective_div");
		}		
	}

	if(browser_version == 6)
	{
		showSelects();
	}
}

var fadeTimersArray = new Array();
function fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done, pause_between_fade_incrementions)
{	
	obj = getElement(obj);
	if(obj)
	{		
		if(obj.id == '')
		{
			obj.id = createRandomId();
		}
		
		clearTimeout(fadeTimersArray[obj.id]);
				
		if(direction == undefined)
		{
			direction = 'in';
		}
		
		if(pause_between_fade_incrementions == undefined)
		{
			pause_between_fade_incrementions = 25;
		}
		
		/** Setting the fade to current_fade **/
		obj.style.zoom = 1;
		if(browser == "ie")
		{
			obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+current_fade+')';	
			obj.style.filter = 'alpha(opacity='+current_fade+');';
		}
		else
		{
			obj.style.opacity = current_fade/100;
		    obj.style.MozOpacity = current_fade/100;
		    obj.style.KhtmlOpacity = current_fade/100;		
		}
		
		if(direction == 'in')
		{
			/** Increasing the current_fade by 10% **/
			current_fade += 10;
			
			if(current_fade <= final_fade)
			{
				/** We have some more fading to do **/				
				fadeTimersArray[obj.id] = setTimeout(function(){fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done, pause_between_fade_incrementions)}, pause_between_fade_incrementions);
			}
			else if(typeof function_to_execute_when_done == 'function')
			{
				function_to_execute_when_done();
			}
		}
		else
		{
			/** Decreasing the current_fade by 10% **/
			current_fade -= 10;
			
			if(current_fade >= final_fade)
			{
				/** We have some more fading to do **/
				fadeTimersArray[obj.id] = setTimeout(function(){fadeElement(obj, current_fade, final_fade, direction, function_to_execute_when_done, pause_between_fade_incrementions)}, pause_between_fade_incrementions);
			}
			else if(typeof function_to_execute_when_done == 'function')
			{
				function_to_execute_when_done();
			}
		}
	}
}

function hideSelects()
{
	var select_objects = document.getElementsByTagName("select");	
	for(var i=0;i<select_objects.length;i++)
	{
		select_objects[i].style.display = 'none';
	}
}

function showSelects()
{
	var select_objects = document.getElementsByTagName("select");	
	for(var i=0;i<select_objects.length;i++)
	{
		select_objects[i].style.display = 'inline';
	}
}

function removeElementFromDom(id_or_dom_element)
{
	id_or_dom_element = getElement(id_or_dom_element);
	
	if(id_or_dom_element && id_or_dom_element != null)
	{
		try
		{
			id_or_dom_element.parentNode.removeChild(id_or_dom_element);	
		}
		catch(e)
		{
			// Do nothing
		}
	}
}

function remObj(id_or_dom_element)
{
	removeElementFromDom(id_or_dom_element);
}

function getElement(id_or_dom_element)
{
	if(typeof(id_or_dom_element) != 'object')
	{
		id_or_dom_element = document.getElementById(id_or_dom_element);
	}
	
	if(typeof(id_or_dom_element) != 'object')
	{
		return false;
	}
	else
	{
		return id_or_dom_element;	
	}
}

var window_height = 0;
var window_width = 0;
var window_scroll_y = 0;
var window_scroll_x = 0;
function updateWindowDimensions()
{	
	if(browser == "ie")
	{
		/** Scroll Y **/
		if(document.documentElement && document.documentElement.scrollTop != 0)
		{
			window_scroll_y = document.documentElement.scrollTop;
		}
		else
		{
			window_scroll_y = document.body.scrollTop;
		}
		
		/** Scroll X **/
		if(document.documentElement && document.documentElement.scrollLeft != 0)
		{
			window_scroll_x = document.documentElement.scrollLeft;
		}
		else
		{
			window_scroll_x = document.body.scrollLeft;
		}
		
		/** Height **/
		if(document.documentElement && document.documentElement.clientHeight != 0)
		{
			window_height = document.documentElement.clientHeight;
		}
		else
		{
			window_height = document.body.clientHeight;
		}

		/** Width **/
		if(document.documentElement && document.documentElement.clientWidth != 0)
		{
			window_width = document.documentElement.clientWidth;
		}
		else
		{
			window_width = document.body.clientWidth;
		}	
	}
	else
	{
		window_scroll_y = window.pageYOffset;
		window_scroll_x = window.pageXOffset;
		window_width 	= window.innerWidth;
		window_height 	= window.innerHeight;		
	}
}

var document_total_height = "";
var document_total_width = "";

function setDocumentHeightAndWidth()
{
	var body = document.body, html = document.documentElement;
	
	document_total_height = Math.max( body.scrollHeight, body.offsetHeight,  html.clientHeight, html.scrollHeight, html.offsetHeight );
	document_total_width = Math.max( body.scrollWidth, body.offsetWidth,  html.clientWidth, html.scrollWidth, html.offsetWidth );
		
	if(browser == "ie" && browser_version < 8)
	{
		document_total_height = (Math.max( body.scrollHeight, body.offsetHeight,  html.clientHeight, html.scrollHeight, html.offsetHeight )-5);
		document_total_width = (Math.max( body.scrollWidth, body.offsetWidth,  html.clientWidth, html.scrollWidth, html.offsetWidth )-21);		
	}
}

var current_popup = "";
/** Keep in mind that ie doesn't accept spaces and - in winName **/
function popupWindow(theURL, winName, width, height) 
{	
	if(winName == undefined)
	{
		winName = "popup";
	}
	if(width == undefined)
	{
		width = 200;
	}
	if(height == undefined)
	{
		height = 200;
	}
		
	var features = "width="+width+", height="+height;
	/** We make sure that the popup opens in the middle of the users screen **/
	updateWindowDimensions();
	var top = (window_height/2)-(height/2);
	var left = (window_width/2)-(width/2);	
	features += ", top="+top+", left="+left;
	
	current_popup = window.open(theURL, winName, features);	
	if(current_popup == null)
	{
		/** Popup was catched by a popup blocker **/
		alert("Popup caught by popup blocker");
	}
	else
	{
		if(typeof popupSpecificFunction == 'function')
		{
			window.onfocus = popupSpecificFunction;
		}
		current_popup.focus();				
	}	
}

/** Switches the display of the two provided objects, if the 3rd parameter is sent that object is given focus in the end **/
function sdoto(obj_to_show, obj_to_hide, focus_this_object)
{
	switchDisplayOfTheseObjects(obj_to_show, obj_to_hide, focus_this_object);
}

function switchDisplayOfTheseObjects(obj_to_show, obj_to_hide, focus_this_object)
{
	obj_to_show = getElement(obj_to_show);
	obj_to_hide = getElement(obj_to_hide);
	
	obj_to_show.style.display = 'inline';
	obj_to_hide.style.display = 'none';
	
	if(focus_this_object != undefined)
	{		
		focus_this_object = getElement(focus_this_object);
		focus_this_object.focus();
	}	
}

/** Switches the visibility of the provided object **/
function svote(obj)
{
	switchVisibilityOfThisElement(obj);
}

function switchVisibilityOfThisElement(obj)
{
	obj = getElement(obj);
	if(obj.style.visibility == 'hidden'){obj.style.visibility = 'visible';}	
	else{obj.style.visibility = 'hidden';}	
}

function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject('Microsoft.XMLHTTP');
	}
	else 
	{
		alert('Status: Cound not create XmlHttpRequest Object.' + 'Consider upgrading your browser.');
	}
}

function unset(array, item_to_remove, remove_key)
{
	if(remove_key != undefined && remove_key)
	{
		/** Checking if we're removing a key **/
		if(array[item_to_remove] != undefined)
		{
			delete array[item_to_remove];
		}		
	}
	else
	{
		/** Getting the array position of the element and removing it when found **/
		for(x in array)
		{
			if(array[x] == item_to_remove)
			{
				/** Removing the element from the array **/
				array.splice(x, 1);
				break;
			}
		}		
	}
	
	return array;
}

window.onload = joinFunctions(window.onload, keepAlive);

function keepAlive()
{ 	
	if(typeof(keep_alive_url) !== 'undefined' && keep_alive_url != "")
	{
		setTimeout("keepAlive()", 600000);
		ajaxRequest('GET', keep_alive_url);
	}
}

function getPositionOfObject(obj) 
{
	obj = getElement(obj);
	return baseFindPosX(obj)+";"+baseFindPosY(obj);
}

function baseFindPosX(obj) 
{
	var curleft = 0;
	if (obj) 
	{
		if (obj.offsetParent) 
		{
			while (obj.offsetParent) 
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x) 
		{
			curleft += obj.x;
		}
	}
	return curleft;
}

function baseFindPosY(obj) 
{
	var curtop = 0;
	if (obj) 
	{
		if (obj.offsetParent) 
		{
			while (obj.offsetParent) 
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y) 
		{
			curtop += obj.y;
		}
	}
	return curtop;
}

function getSelectValue(select_obj)
{
	var select_obj = getElement(select_obj);
	return select_obj.options[select_obj.selectedIndex].value;
}

function getSelectText(select_obj)
{
	var select_obj = getElement(select_obj);
	return select_obj.options[select_obj.selectedIndex].text;	
}

/** Validation **/
var validation_array = new Array();
function addValidationElement(element_to_validate, type_of_element, alert_on_error, empty_allowed)
{
	if(empty_allowed == undefined)
	{
		empty_allowed = false;
	}
	
	if(alert_on_error == undefined)
	{
		alert_on_error = '';
	}
	
	if(getElement(element_to_validate) && type_of_element != undefined && type_of_element != '')
	{
		validation_array.push(new Array(element_to_validate, type_of_element, alert_on_error, empty_allowed));
	}
	else
	{
		alert(element_to_validate+" eksistere ikke, har lavet addValidationElement før elementet blev udskrevet?");
	}
}

var validation_error_color = "#D30000";
var validation_ok_color = "";

function doValidation()
{
	var validation_passed = true;

	/** We run through the array, setting all fields to the validation_ok_color **/
	for(x in validation_array)
	{
		if(getElement(validation_array[x][0]))
		{
			var element_to_validate = getElement(validation_array[x][0]);
			var type_of_element = validation_array[x][1];
			
			element_to_validate.style.borderStyle = "";
			element_to_validate.style.borderColor = validation_ok_color;		
		}
	}	
		
	/** Now for the actual validation **/
	for(x in validation_array)
	{
		/** Setting up variables **/
		var element_to_validate = getElement(validation_array[x][0]);
		var type_of_element = validation_array[x][1];
		var alert_on_error = validation_array[x][2];
		var empty_allowed = validation_array[x][3];
		
		if(type_of_element == 'email')
		{						
			var email_validation_string = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/
			
			/** Checks if the element was allowed to be empty **/
			if(empty_allowed && element_to_validate.value == '')
			{
				
			}
			/** Checks if the content is an email **/
			else if(element_to_validate.value.search(email_validation_string) == -1)
			{
				validation_passed = false;
			}
			
		}
		else if(type_of_element == 'text')
		{			
			/** Checks if the element was allowed to be empty **/
			if(empty_allowed && element_to_validate.value == '')
			{
				
			}
			/** Checks if there's content set **/
			else if(element_to_validate.value == "")
			{
				validation_passed = false;
			}			
		}
		else if(type_of_element == 'numeric')
		{
			/** Checks if the element was allowed to be empty **/
			if(empty_allowed && element_to_validate.value == '')
			{
				
			}
			/** Checks if the content is_numeric **/
			else if(!is_numeric(element_to_validate.value))
			{
				validation_passed = false;
			}						
		}
		
		/** Validation failed, we color the field alert the error and stop **/
		if(!validation_passed)
		{
			if(element_to_validate != undefined)
			{
				element_to_validate.focus();
				element_to_validate.style.borderStyle = "solid";
				element_to_validate.style.borderColor = validation_error_color;
			}
						
			if(alert_on_error != '')
			{
				alert(alert_on_error);	
			}			
			break;
		}
	}
	
	return validation_passed;
}

function createSubmitButton(id, value)
{
	var container = getElement(id);
	container.innerHTML = '<input type="submit" value="'+value+'">';
}

var fade_wait_timer = null;
function fadeInFadeOutAndRemove(data, time_to_display, fade_in_time, fade_out_time, position, top, left, function_to_run_when_done)
{	
	if(time_to_display == undefined)
	{
		time_to_display = 2000;
	}
	if(fade_in_time == undefined)
	{
		fade_in_time = 75;
	}
	if(fade_out_time == undefined)
	{
		fade_out_time = 75;
	}
	
	if(position == undefined)
	{
		position = 'fixed';
	}

	clearTimeout(fade_wait_timer);
	createOverlay(data, position, top, left, 'fadeinoutoverlay');
	
	fadeElement("fadeinoutoverlay", 0, 100, "in", function(){fade_wait_timer = setTimeout(function(){fadeElement("fadeinoutoverlay", 100, 0, "out", function(){remObj("fadeinoutoverlay"); if(function_to_run_when_done != undefined){function_to_run_when_done();}}, fade_out_time);}, time_to_display);}, fade_in_time);	
}

var preloaded_images_array = new Array();
function preloadThisImage(complete_image_path, function_to_call_when_done_loading)
{
	var found = false;
	/** First we check if the image has been loaded already **/
	for(x in preloaded_images_array)
	{
		if(preloaded_images_array[x].src == complete_image_path)
		{
			found = true;
			break;
		}
	}
	
	if(found)
	{
		/** The image has been loaded already, so we call the function we are to call when we're done loading, if it is defined **/
		if(function_to_call_when_done_loading != undefined)
		{
			/** The setTimeout is there to allow the calling scripts to finish any remaining code before calling this function **/
			setTimeout(function_to_call_when_done_loading, 0);
		}			
	}
	else
	{
		var array_length = preloaded_images_array.length;
		preloaded_images_array[array_length] = new Image();
		if(function_to_call_when_done_loading != undefined)
		{
			preloaded_images_array[array_length].onload = function_to_call_when_done_loading;
			preloaded_images_array[array_length].onerror = function_to_call_when_done_loading;
		}
		preloaded_images_array[array_length].src = complete_image_path;
	}
}

var mouseX = "";
var mouseY = "";
var presavedMouseX = undefined;
var presavedMouseY = undefined;
function setMouseXY(e) 
{
	if(browser == 'ie' && presavedMouseX != undefined && presavedMouseY != undefined)
	{
		mouseX = presavedMouseX + document.body.scrollLeft;
		mouseY = presavedMouseY + document.body.scrollTop;
	}
	else
	{		
		if (browser == "ie") 
		{ 
			/** grab the x-y pos.s if browser is IE **/
			if(e == null)
			{
				e = window.event;
			}
			
			mouseX = e.clientX + document.body.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop;
		}
		else 
		{  
			/** Grab the x-y pos.s if browser is NS **/ 
			if(e == undefined)
			{
				
				e = current_event;
			}			
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
	}
	
	// catch possible negative values in NS4
	if (mouseX < 0){mouseX = 0;}
	if (mouseY < 0){mouseY = 0;}			
	
	presavedMouseX = undefined;
	presavedMouseY = undefined;
}

var dragged_obj;
var capturedMouseX;
var capturedMouseY;
var objectCapturedX;
var objectCapturedY;
var objectCapturedWidth;
var objectCapturedHeight;
var parentObject;
var parentObjectCapturedX;
var parentObjectCapturedY;

function startDragging(e, obj, corner, min_width, min_height, parent_obj, execute_this_func_while_dragging)
{
	dragged_obj = getElement(obj);
    
	/** If this object isn't the top object, we make it the top object **/
	if((dragged_obj.style.zIndex*1+1) < overlay_zindex_counter)
	{
		dragged_obj.style.zIndex = overlay_zindex_counter;
		overlay_zindex_counter++;
	}
    setMouseXY(e);
    capturedMouseX = mouseX;
    capturedMouseY = mouseY;
    
    parentObject = dragged_obj;
    if(parent_obj != undefined)
    {
    	parentObject = getElement(parent_obj);
    }
    
    objectCapturedX 		= parseFloat(dragged_obj.style.left);
    objectCapturedY 		= parseFloat(dragged_obj.style.top);
    objectCapturedWidth 	= parseFloat(dragged_obj.style.width);
    objectCapturedHeight 	= parseFloat(dragged_obj.style.height);
	parentObjectCapturedX 	= parseFloat(parentObject.style.left);
	parentObjectCapturedY 	= parseFloat(parentObject.style.top);    
    
    document.onmousemove 	= function(event){dragObject(event, corner, min_width, min_height, execute_this_func_while_dragging);};
    document.onmouseup 		= function(){cancelDragging();};
    
	if(browser != 'ie')
	{
		e.preventDefault();
	}
	else
	{
		event.returnValue = false;
	}
	
    return false;
}

function cancelDragging()
{	
	if(ondragstop_array[dragged_obj.id] != undefined && (objectCapturedX != parseFloat(dragged_obj.style.left) || objectCapturedY != parseFloat(dragged_obj.style.top) || objectCapturedWidth != parseFloat(dragged_obj.style.width) || objectCapturedHeight != parseFloat(dragged_obj.style.height)))
	{
		ondragstop_array[dragged_obj.id]();
	}
	document.onmousemove = '';
	document.onmouseup = '';
}

var mouseDistanceTraveledX = 0;
var mouseDistanceTraveledY = 0;
function dragObject(e, corner, min_width, min_height, execute_this_func_while_dragging)
{
	setMouseXY(e);
	
	mouseDistanceTraveledX = mouseX - capturedMouseX;
	mouseDistanceTraveledY = mouseY - capturedMouseY;
	
	if(corner != undefined)
	{
		if(min_width == undefined)
		{
			min_width = 0;
		}
		if(min_height == undefined)
		{
			min_height = 0;
		}
		
		if(corner == 'lower_right')
		{
			if(objectCapturedWidth + mouseDistanceTraveledX >= min_width)
			{
				dragged_obj.style.width 	= objectCapturedWidth + mouseDistanceTraveledX;
			}
			
			if(objectCapturedHeight + mouseDistanceTraveledY >= min_height)
			{
				dragged_obj.style.height	= objectCapturedHeight + mouseDistanceTraveledY;				
			}
		}
		else if(corner == 'lower_left')
		{
			if(objectCapturedWidth + mouseDistanceTraveledX*-1 >= min_width)
			{
				dragged_obj.style.width 	= objectCapturedWidth + mouseDistanceTraveledX*-1;
				parentObject.style.left 	= parentObjectCapturedX + mouseDistanceTraveledX;
			}
			
			if(objectCapturedHeight + mouseDistanceTraveledY >= min_height)
			{
				dragged_obj.style.height	= objectCapturedHeight + mouseDistanceTraveledY;
			}
		}
	}
	else
	{
	    dragged_obj.style.left 	= objectCapturedX + mouseDistanceTraveledX;
	    dragged_obj.style.top 	= objectCapturedY + mouseDistanceTraveledY;
	}
	
	if(execute_this_func_while_dragging != undefined)
	{
		execute_this_func_while_dragging();
	}
	
	if(browser != 'ie')
	{
		e.preventDefault();
	}
	else
	{
		event.returnValue = false;
	}
	
    return false;
}

var ondragstop_array = new Array();
function onDragStop(id, function_to_run)
{
	ondragstop_array[id] = function_to_run;
}

function getObjectDimensions(obj)
{
	var obj = getElement(obj);
	
	if(browser == 'ie')
	{
		var width 	= obj.offsetWidth;
		var height 	= obj.offsetHeight;
	}
	else
	{
		var padding_left = 0;
		if(obj.style.paddingLeft != '')
		{
			padding_left = parseInt(obj.style.paddingLeft)
		}
		
		var padding_right = 0;
		if(obj.style.paddingRight != '')
		{
			padding_right = parseInt(obj.style.paddingRight)
		}
		
		var padding_top = 0;
		if(obj.style.paddingTop != '')
		{
			padding_top = parseInt(obj.style.paddingTop)
		}

		var padding_bottom = 0;
		if(obj.style.paddingBottom != '')
		{
			padding_bottom = parseInt(obj.style.paddingBottom)
		}			
		
		var width 	= obj.clientWidth 	- padding_left 	- padding_right;
		var height 	= obj.clientHeight 	- padding_top 	- padding_bottom;
	}
	
	var array = new Array();
	array.width = width;
	array.height = height;
	
	return array;
}

var expand_contract_running_objects = new Object();
function expandContractStartChecker(obj, time)
{
	if(expand_contract_running_objects[obj] != undefined)
	{
		if(time < expand_contract_running_objects[obj])
		{
			return false;
		}
	}
	
	expand_contract_running_objects[obj] = time;
	
	return true;
}

/** May stutter when uneven numbers are used for incrementations_in_pixels in ie.. **/
var expand_to_this_width = '';
var expand_to_this_height = '';
var expand_object_position_top = "";
var expand_object_position_left = "";
var number_of_expand_images_to_preload = 0;
var number_of_expand_images_preloaded = 0;
function expandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args)
{		
	var obj = getElement(obj);
	if(obj)
	{
		if(extra_args == undefined)
		{
			var extra_args = new Object();
		}
		
		if(extra_args.time == undefined)
		{
			var date = new Date();
			extra_args.time = date.getTime();
		}
		
		if(!expandContractStartChecker(obj, extra_args.time))
		{
			/** A new function has accessed this obj, thus we need to stop **/
			return false;
		}
		
		if(extra_args.step == undefined)
		{
			var step = 0;
		}	
		else
		{
			var step = extra_args.step;
		}
		
		if(extra_args.center_in_position == undefined)
		{
			var center_in_position = true;
		}
		else
		{
			var center_in_position = extra_args.center_in_position;
		}
		
		if(incrementations_in_pixels == undefined)
		{
			incrementations_in_pixels = 20;
		}
		
		if(step == 0)
		{		
			extra_args.step = 1;
			
			/** We need to determine if the content contains any images that we need to preload **/
			var old_content = obj.innerHTML;
			obj.innerHTML = content;		
			var children = obj.getElementsByTagName('img');		
			
			/** Resetting the preload variables **/
			number_of_expand_images_to_preload 	= 0;
			number_of_expand_images_preloaded 	= 0;		
			
			/** Checking the new content for images to preload **/
			var images_to_preload_array = new Array();
			for(x in children)
			{
				if(children[x].src != undefined)
				{				
					number_of_expand_images_to_preload++;
					images_to_preload_array.push(children[x].src);
				}
			}
			
			/** Setting the old content again **/
			obj.innerHTML = old_content;
			
			if(number_of_expand_images_to_preload == 0)
			{
				/** No images to preload, we continue **/
				expandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args);
			}
			else
			{
				/** There's images to preload, so we preload them **/
				for(x in images_to_preload_array)
				{
					preloadThisImage(images_to_preload_array[x], function()
						{
							donePreloadImagesForexpandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args);
						}
					);			
				}			
			}
		}
		else if(step == 1)
		{
			/** Saving the objects current position **/
			expand_object_position_top = obj.style.top;
			expand_object_position_left = obj.style.left;
			
			/** Inserting the new data **/
			obj.innerHTML = content;
			
			/** Getting the expand dimensions **/
			expand_to_these_dimensions 	= getObjectDimensions(obj);
			expand_to_this_width 		= expand_to_these_dimensions.width;
			expand_to_this_height 		= expand_to_these_dimensions.height;
			
			if(!center_in_position)
			{
				expand_object_position_top = parseFloat(expand_object_position_top) + expand_to_this_height/2+"px";
				expand_object_position_left = parseFloat(expand_object_position_left) + expand_to_this_width/2+"px";
			}
			
			/** Setting object dimensions to zero **/
			obj.style.overflow 	= 'hidden';		
			obj.style.width 	= "0px";
			obj.style.height 	= "0px";
			
			/** Adding a div around the content **/
			obj.innerHTML = '<div id="temp_expand_div" style="position:absolute;width:'+expand_to_this_width+'px;height:'+expand_to_this_height+'px;">'+content+'</div>';
			
			extra_args.step = 2;		
			expandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args);
		}
		else if(step == 2)
		{				
			/** Expanding container width **/
			var new_width = expand_to_this_width;
			if(parseFloat(obj.style.width) < expand_to_this_width)
			{
				new_width = parseFloat(obj.style.width)+incrementations_in_pixels;
				if(new_width > expand_to_this_width)
				{
					new_width = expand_to_this_width
				}
				
				obj.style.width = new_width+"px";
			}
			
			/** Expanding container height **/
			var new_height = expand_to_this_height;
			if(parseFloat(obj.style.height) < expand_to_this_height)
			{
				new_height = parseFloat(obj.style.height)+incrementations_in_pixels;
				if(new_height > expand_to_this_height)
				{
					new_height = expand_to_this_height;
				}
				
				obj.style.height = new_height+"px";
			}
			
			/** Centering the container **/
			obj.style.top 	= (parseFloat(expand_object_position_top))-(parseFloat(obj.style.height)/2)+"px";
			obj.style.left 	= (parseFloat(expand_object_position_left))-(parseFloat(obj.style.width)/2)+"px";
			
			if(parseFloat(obj.style.top) < 0)
			{
				obj.style.top = 0;
			}
			if(parseFloat(obj.style.left) < 0)
			{
				obj.style.left = 0;
			}		
	
			/** Centering the content in the container **/
			var temp_div = getElement('temp_expand_div');
			if(temp_div)
			{			
				temp_div_dimensions 	= getObjectDimensions(temp_div);
				
				if(new_width == expand_to_this_width)
				{
					temp_div.style.left = "0px";
				}
				else
				{
					temp_div.style.left = (parseFloat(obj.style.width)/2)-(expand_to_this_width/2)+"px";
				}
				
				if(new_height == expand_to_this_height)
				{
					temp_div.style.top = "0px";
				}
				else
				{
					temp_div.style.top = (parseFloat(obj.style.height)/2)-(expand_to_this_height/2)+"px";
				}
			}		
	
			/** Calling the function again if we aren't done expanding **/
			if((new_width < expand_to_this_width || new_height < expand_to_this_height))
			{
				setTimeout(function(){expandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args);}, 30);
			}
			else
			{
				/** We're done, setting the content as it should be **/
				obj.innerHTML = content;
				obj.style.overflow = '';
				obj.style.width = '';
				obj.style.height = '';
				
				/** Running the function we were to run, if it was set **/
				if(function_to_run_when_done != undefined)
				{
					function_to_run_when_done();
				}
			}
		}
	}
	else
	{
		/** Running the function we were to run, if it was set **/
		if(function_to_run_when_done != undefined)
		{
			function_to_run_when_done();
		}		
	}
}

function donePreloadImagesForexpandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args)
{
	number_of_expand_images_preloaded++;
	if(number_of_expand_images_preloaded >= number_of_expand_images_to_preload)
	{		
		expandElementIntoView(obj, content, incrementations_in_pixels, function_to_run_when_done, extra_args);
	}
}

function contractElementOutOfView(obj, incrementations_in_pixels, function_to_run_when_done, extra_args)
{
	var obj = getElement(obj);
	if(obj)
	{
		var first = false;
		if(extra_args == undefined)
		{
			var extra_args = new Object();
			first = true;
		}
		
		if(extra_args.time == undefined)
		{
			var date = new Date();
			extra_args.time = date.getTime();
		}		
		
		if(!expandContractStartChecker(obj, extra_args.time))
		{
			/** A new function has accessed this obj, thus we need to stop **/
			return false;
		}		
			
		if(incrementations_in_pixels == undefined)
		{
			incrementations_in_pixels = 20;
		}
		
		var object_dimensions = getObjectDimensions(obj);
		
		var new_width = object_dimensions.width-incrementations_in_pixels;
		var new_height = object_dimensions.height-incrementations_in_pixels;
		
		if(new_width < 0)
		{
			new_width = 0;
		}
		
		if(new_height < 0)
		{
			new_height = 0;
		}
		
		if(first)
		{
			/** We create a container for the content **/
			var temp_div = document.createElement('div');
			temp_div.id = 'temp_contract_div';
			temp_div.innerHTML = obj.innerHTML;
			temp_div.style.position = 'absolute';
			obj.innerHTML = '';
			obj.appendChild(temp_div);
		}
		
		obj.style.overflow 	= 'hidden';
		obj.style.width 	= new_width+"px";
		obj.style.height 	= new_height+"px";
		obj.style.top 		= parseFloat(obj.style.top)+(incrementations_in_pixels/2)+"px";
		obj.style.left 		= parseFloat(obj.style.left)+(incrementations_in_pixels/2)+"px";

		object_dimensions = getObjectDimensions(obj);
		
		/** Centering the content in the container **/
		var temp_div = getElement('temp_contract_div');
		if(temp_div)
		{			
			temp_div_dimensions 	= getObjectDimensions(temp_div);			
			temp_div.style.left 	= (object_dimensions.width/2)-(temp_div_dimensions.width/2)+"px";
			temp_div.style.top 		= (object_dimensions.height/2)-(temp_div_dimensions.height/2)+"px";
		}
				
		if(object_dimensions.width > 0 || object_dimensions.height > 0)
		{
			setTimeout(function(){contractElementOutOfView(obj, incrementations_in_pixels, function_to_run_when_done, extra_args);}, 30);
		}
		else
		{
			/** Removing the object from the dom **/
			remObj(obj);
			
			/** Running the function we were to run, if it was set **/
			if(function_to_run_when_done != undefined)
			{
				function_to_run_when_done();
			}
		}		
	}
	else
	{
		/** Running the function we were to run, if it was set **/
		if(function_to_run_when_done != undefined)
		{
			function_to_run_when_done();
		}		
	}
}

function makeElementDragable(obj, drag_handle)
{
	var obj = getElement(obj);
	
	if(drag_handle != undefined)
	{
		dragable_element_in_data 				= getElement(drag_handle);
		dragable_element_in_data.onmousedown	= function(event){startDragging(event, obj);};
		dragable_element_in_data.onmouseup		= function(){cancelDragging();};			
	}
	else
	{
		obj.onmousedown	= function(event){startDragging(event, this);};
		obj.onmouseup	= function(){cancelDragging();};
	}
}

function makeElementUndragable(obj, drag_handle)
{
	var obj = getElement(obj);
	
	if(drag_handle != undefined)
	{
		dragable_element_in_data 				= getElement(drag_handle);
		dragable_element_in_data.onmousedown	= function(){};
		dragable_element_in_data.onmouseup		= function(){};
	}
	else
	{
		obj.onmousedown	= function(){};
		obj.onmouseup	= function(){};
	}	
}

var objects_sliding_array = new Array();
function slideInAndOut(obj, direction, run_this_function_when_done, step)
{
	var obj = getElement(obj);
	
	if(obj)
	{
		if(obj.id == '')
		{
			obj.id = createRandomId();
		}
		
		clearTimeout(objects_sliding_array[obj.id]);
		
		var height = obj.getAttribute('preset_height');
		
		var incrementations = 10;
		if(step == undefined)
		{
			step = 1;
		}
		
		if(step == 1)
		{	
			var obj_dimensions 	= getObjectDimensions(obj);
			
			if(obj.getAttribute('preset_height') == null)
			{
				if(obj.style.height != '')
				{
					obj.setAttribute('preset_height', obj.style.height);
				}
				else
				{
					obj.setAttribute('preset_height', obj_dimensions.height);
				}
			}
	
			obj.style.overflow 	= 'hidden';		
			
			if(direction == 'out' && obj.style.height == '')
			{
				obj.style.height = '0px';
			}
			else if(direction == 'in' && obj.style.height == '')
			{
				obj.style.height = obj_dimensions.height+'px';
			}
	
			
			slideInAndOut(obj, direction, run_this_function_when_done, 2);
		}
		else if(step == 2)
		{
			var stop_sliding = false;
			
			if(direction == 'out')
			{
				var new_height = parseFloat(obj.style.height)+incrementations;
				
				if(new_height >= height)
				{
					new_height 		= height;
					stop_sliding 	= true;
				}			
			}
			else if(direction == 'in')
			{
				var new_height = parseFloat(obj.style.height)-incrementations;
				
				if(new_height <= 0)
				{
					new_height 		= 0;
					stop_sliding 	= true;
				}
			}
			
			obj.style.height = new_height+"px";
			
			if(!stop_sliding)
			{
				objects_sliding_array[obj.id] = setTimeout(function(){slideInAndOut(obj, direction, run_this_function_when_done, 2);}, 50);
			}		
			else if(stop_sliding)
			{
				obj.style.height 	= '';
				obj.style.overflow 	= '';
				
				if(typeof(run_this_function_when_done) === 'function')
				{
					run_this_function_when_done();
				}
			}
		}
	}
}

function createRandomId()
{
	var random_id = '';
	while(random_id == '' || getElement(random_id))
	{
		var random_number = Math.ceil(Math.random() * 100000);
		
		random_id = 'random_'+random_number;
	}
	
	return random_id;
}

var custom_confirm_boxes_counter = 0;
function customConfirm(message, ok_button_value, cancel_button_value, function_to_execute_on_ok, function_to_execute_on_cancel)
{
	if(message != undefined)
	{
		custom_confirm_boxes_counter++;
		if(ok_button_value == undefined)
		{
			ok_button_value = 'OK';
		}
		
		if(cancel_button_value == undefined)
		{
			cancel_button_value = 'Cancel';
		}
		
		var id 			= 'custom_confirm_box_'+custom_confirm_boxes_counter;
		var id_ok 		= 'custom_confirm_box_ok_'+custom_confirm_boxes_counter;
		var id_cancel 	= 'custom_confirm_box_cancel_'+custom_confirm_boxes_counter;
		
		var data 		= '<div style="padding:5px;border:1px solid black;background-color:#FFFFFF;text-align:center;">'+message+'<br><input type="button" value="'+ok_button_value+'" id="'+id_ok+'"> <input type="button" value="'+cancel_button_value+'" id="'+id_cancel+'"></div>';
		createOverlay(data, 'absolute', 'center', 'center', id);
		
		getElement(id_ok).onclick = function(){
			if(function_to_execute_on_ok != undefined)
			{
				function_to_execute_on_ok();	
			}
			closeOverlay(id);
		};
		
		getElement(id_cancel).onclick = function(){
			if(function_to_execute_on_cancel != undefined)
			{
				function_to_execute_on_cancel();	
			}
			closeOverlay(id);
		};		
	}
}

function insertKeyboardPress(obj, keyCode) 
{
	var pressEvent = document.createEvent("KeyboardEvent");
	pressEvent.initKeyEvent(
		"keypress", 
		true, 
		true, 
		window, 
	    false, 
	    false, 
	    false, 
	    false, 
		keyCode, 
		"".charCodeAt(0)
	);
	
	var obj = getElement(obj);
	obj.dispatchEvent(pressEvent);
}

function preventDefault(e)
{
	if(browser == 'ie')
	{
		e.cancelBubble = true;
		e.returnValue = false;
	}
	else
	{
		e.stopPropagation();
		e.preventDefault();		
	}
}

