var receiveReq = getXmlHttpRequestObject();
var receiveReq_keepalive = getXmlHttpRequestObject();
var keep_alive_url = "ajax/keepalive.php";
var do_keep_alive = true;
var window_height = 0;
var window_width = 0;
var window_scroll_y = 0;
var window_scroll_x = 0;
var current_popup = "";
var header_written = false;
/** 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();
    }
}

function createOverlay(action, div_width, div_height)
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{
		var body_dom = document.getElementsByTagName('body')[0];
		
		/** Updating window dimensions **/
		updateWindowDimensions();
		
		/** We make sure that the correct styles are set **/
		writeOverlayHeader();
				
		/** 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';				
		protective_div.className = 'opaque';
		
		/** Styling the protective div **/
		if(BrowserDetect.browser != "Explorer")
		{
			protective_div.style.top = "0px";
			protective_div.style.position = "fixed";			
		}
				
		protective_div.style.left = "0px";		
		protective_div.style.width = "100%";
		protective_div.style.height = "100%";
		protective_div.style.backgroundColor = "#000000";
		protective_div.style.zIndex = "999998";
		protective_div.style.opacity = ".5";
		
		/** Prepending it to the body **/
		body_dom.insertBefore(protective_div, document.body.childNodes[0]);
		
		/** Creating an overlay **/		
		var overlay_div = document.createElement('div');
		overlay_div.id = 'overlay_div';
		
		/** Now to style the div **/
		if(div_width == "")
		{
			div_width = 900;
		}
		
		if(div_height == "")
		{
			div_height = 600;
		}
		
		overlay_div.style.position = "absolute";
		overlay_div.style.left = (window_width/2)-(div_width/2)+"px";
		overlay_div.style.top = (window_height/2)-(div_height/2)+"px";
		if(parseInt(overlay_div.style.top) < 0)
		{
			overlay_div.style.top = "0px";
		}		
		overlay_div.style.border = "2px solid black";
		overlay_div.style.backgroundColor = "#667E96";
		overlay_div.style.width = div_width+"px";
		overlay_div.style.height = div_height+"px";
		overlay_div.style.zIndex = "999999";
		
		/** Close button **/
		overlay_div.innerHTML  = '<div align="right"><span onclick="closeOverlay(); if(typeof specificOverlayCloseFunction == \'function\'){specificOverlayCloseFunction(\''+action+'\');}" style="cursor: pointer;">[X]</span></div>';
		/** Loading animation **/
		overlay_div.innerHTML += '<div id="overlay_data" align="center" style="height:'+(div_height-15)+'px; width:'+div_width+'px;overflow-y:auto;overflow-x:hidden;"><img src="images/layout/ajax-loader_large.gif"></div>';
		
		/** Now we append it to the body **/
		body_dom.appendChild(overlay_div);
		
		/** Getting the data **/
		if(action == undefined){action = "";}
		else{action = "?action="+action;}
		
		receiveReq.open('GET', ajax_file_path+action, true);
		receiveReq.onreadystatechange = handleCreateOverlay;
		receiveReq.send(null);
	}		
}

function handleCreateOverlay()
{
	if (receiveReq.readyState == 4)
	{
		var data = JSON.parse(receiveReq.responseText);
		var obj = document.getElementById("overlay_data");
		obj.innerHTML = html_entity_decode(data);		
	}
}

function writeOverlayHeader()
{
	if(!header_written)
	{
		var head_dom = document.getElementsByTagName('head')[0];	
		/** If this browser is IE we write a special header that will make our protective layer work properly **/
		if(BrowserDetect.browser == "Explorer")
		{			
			var style_element = document.createElement('style');
			var style_rules = document.createTextNode("#protective_div{position: absolute;top: expression( ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) + 'px' );} .opaque{filter: alpha(opacity=50);}");
			style_element.type = 'text/css';
			if(style_element.styleSheet){style_element.styleSheet.cssText = style_rules.nodeValue;}
			else{style_element.appendChild(style_rules);}
			head_dom.appendChild(style_element);
		}	
		
		header_written = true;	
	}
}

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 closeOverlay()
{
	removeElementFromDom("overlay_div");
	removeElementFromDom("protective_div");
}

function removeElementFromDom(id_or_dom_element)
{
	if(typeof(id_or_dom_element) != 'object')
	{
		id_or_dom_element = document.getElementById(id_or_dom_element);
	}
	
	if(id_or_dom_element != undefined)
	{
		id_or_dom_element.parentNode.removeChild(id_or_dom_element);
	}
}

function updateWindowDimensions()
{	
	if (parseInt(navigator.appVersion)>3) 
	{
		if (navigator.appName=="Netscape") 
		{			
			window_scroll_y = window.pageYOffset;
			window_scroll_x = window.pageXOffset;
			window_width 	= window.innerWidth;
			window_height 	= window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
			if(BrowserDetect.version < 8)
			{
				window_scroll_y = document.body.scrollTop;
				window_scroll_x = document.body.scrollLeft;				
				window_height 	= document.body.clientHeight;
				window_width 	= document.body.clientWidth;				
			}
			else
			{
				window_scroll_y = document.documentElement.scrollTop;
				window_scroll_x = document.documentElement.scrollLeft;
				window_height 	= document.documentElement.clientHeight;
				window_width 	= document.documentElement.clientWidth;				
			}
		}
	}
}

function popupWindow(theURL, winName, width, height) 
{	
	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();				
	}	
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function switchVisibiltyOfTheseObjects(obj_to_show, obj_to_hide, focus_this_object)
{
	if(typeof obj_to_show != "object")
	{
		obj_to_show = document.getElementById(obj_to_show);
	}
	
	if(typeof obj_to_hide != "object")
	{
		obj_to_hide = document.getElementById(obj_to_hide);
	}
	
	obj_to_show.style.display = 'inline';
	obj_to_hide.style.display = 'none';
	
	if(focus_this_object != undefined)
	{		
		if(typeof focus_this_object != "object")
		{
			focus_this_object = document.getElementById(focus_this_object);
		}	
		focus_this_object.focus();
		focus_this_object.onblur = function(){
			switchVisibiltyOfTheseObjects(obj_to_hide, obj_to_show);
		};
	}
}

function genericAjaxCall(ajax_script_string, function_to_call_on_ready_state_change)
{
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
	{
		receiveReq.open('GET', ajax_script_string, true);
		receiveReq.onreadystatechange = function()
		{
			if(receiveReq.readyState == 4 && function_to_call_on_ready_state_change != undefined)
			{
				function_to_call_on_ready_state_change();
			}
		}
		receiveReq.send(null);
	}
}

function getXmlHttpRequestObject() 
{
	if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) 
	{
		return new ActiveXObject('Microsoft.XMLHTTP');
	}
	else 
	{
		document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.' +	'Consider upgrading your browser.';	
	}
}

function unset(array, item_to_remove)
{
	/** 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;
}

function keepAlive()
{ 		
	setTimeout("keepAlive()", 600000);
	if (receiveReq_keepalive.readyState == 4 || receiveReq_keepalive.readyState == 0) 
	{
		receiveReq_keepalive.open('GET', keep_alive_url, true);
		receiveReq_keepalive.send(null);
	}
}

if(do_keep_alive)
{
	keepAlive();
}

function getPositionOfObject(obj) 
{
	return findPosX(obj)+";"+findPosY(obj);
}

function findPosX(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 findPosY(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 switchVisibilityOfThisElement(id)
{
	var obj = document.getElementById(id);
	if(obj.style.visibility == 'hidden')
	{
		obj.style.visibility = 'visible';
	}	
	else
	{
		obj.style.visibility = 'hidden';
	}
}
