var base_url = 'http://www.ridair-brema.nl/';

function SelectLanguage(language) {
	
	var form = language;
	
	document.forms[form].submit();	
}

function addMenuFunctions()
{
	var ulMenu = document.getElementById('menuTop');
	var liItems = ulMenu.getElementsByTagName('li');
		
	for(var i=0; i<liItems.length; i++)
	{
		if(liItems[i].childNodes.length > 1)
		{
			addEvent(liItems[i], 'click', _menuToggle);
			var ulItems = liItems[i].getElementsByTagName('ul');
			
			for(var j=0; j<ulItems.length; j++)
			{
				ulItems[j].className='hide';
			}
		}
	}	
}

function _menuToggle(ev, obj)
{
	var ul = this.getElementsByTagName('ul');

	if(ul[0].className == 'hide')
	{
		ul[0].className='show';
	}
	else
	{
		ul[0].className='hide';
	}
	
	calculateSize();
	
	stopSomething(ev);
}

function addStyleSheetFunctions()
{
	var printCss = document.getElementById('print');
	var smallCss = document.getElementById('small');
	var normalCss = document.getElementById('normal');
	var bigCss = document.getElementById('big');
	
	if(printCss != null)
	{
		// attatch event for printer css
		addEvent(printCss, 'click', _addprintFunctions);
	}
	
	if(smallCss != null)
	{
		// attatch event for small css
		addEvent(smallCss, 'click', _addsmallFunctions);
	}
	
	if(normalCss != null)
	{
		// attatch event for normal css
		addEvent(normalCss, 'click', _addnormalFunctions);
	}
	
	if(bigCss != null)
	{
		// attatch event for big css
		addEvent(bigCss, 'click', _addbigFunctions);
	}
}

function _addprintFunctions(ev, obj)
{
	/*var location = self.location
	location = urlencode(location);
	
	base_url = base_url;
	var toopen = base_url;

	
	/*alert(toopen);*/
	/*window.open(toopen+'41/'+location,'mywin','width=800,height=600,toolbar=0,resizable=1,scrollbars=1,menubar=1');*/
	
	var location = self.location
	location = urlencode(location);
	window.open(base_url+'37/'+location,'mywin','width=800,height=600,toolbar=0,resizable=1,scrollbars=1,menubar=1');
}

function urlencode(str) {
str = escape(str);
str = str.replace(/\+/gi, '%2B');
str = str.replace(/%20/gi, '+');
str = str.replace(/\*/gi, '%2A');
str = str.replace(/\//gi, '%2F');
str = str.replace(/@/gi, '%40');
return str;
}

function _addsmallFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='small';
	document.cssfile.submit();
}

function _addnormalFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='normal';
	document.cssfile.submit();
}

function _addbigFunctions(ev, obj)
{
	var input = document.getElementById('css_file');
	input.value='big';
	document.cssfile.submit();
}

function stopSomething(e)
{
	if(!e) var e = window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
}

function openUp(page_id)
{
	var menu = document.getElementById('menu');	
	var aItems = menu.getElementsByTagName('a');
	var new_url = '';
	var new_url2 = '';
	
	for(var i=0; i<aItems.length; i++)
	{	
		var re = new RegExp('(/' + escape(page_id) + ').*$', 'i'); 

		new_url = aItems[i].href.replace(/%20/gi,"");
		
		if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version == '6')
		{
			new_url_2 = new_url.replace(/\"/gi,"%22");			
		}
		else
		{
			new_url_2 = new_url.replace(/%22/gi,"%2522");			
		}

		if(re.test(new_url_2))
		{
			var item=aItems[i]
			while(item.id != 'menuTop')
			{
				item = item.parentNode
				
				if(item.nodeName=='UL')
				{
					item.className='show';
				}
			}
		}
	}
	
	calculateSize();
	
	return;
}

function calculateSize()
{
	var menuDiv = document.getElementById("menu");
	var contentDiv = document.getElementById("content");
	var height;
	
	if(menuDiv.clientHeight == 515)
	{
		height = 510;
	}
	else if(menuDiv.clientHeight == 383)
	{
		height = 378;
	}
	else if(menuDiv.clientHeight == 647)
	{
		height = 642;
	}
	else
	{
		height = 510;
	}
	
	if(menuDiv.clientHeight-15 > height)
	{
		contentDiv.style.height=menuDiv.clientHeight+'px';
	}
	else
	{
		contentDiv.style.height=height+'px';
	}
}

function session_check()
{
	makeRequest(base_url+'include/functions/session_time.fnc.php/get_session_time');
}

function makeRequest(url) {
    var httpRequest;

    if (window.XMLHttpRequest) 
    { 														// Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) 
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
   	{ 														// IE
        try 
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
        	try 
        	{
            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
   	}

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    httpRequest.onreadystatechange = function() 
    { 
    	alertContents(httpRequest); 
   	};
    
   	httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function makeRequest_nocallback(url) {
    var httpRequest;

    if (window.XMLHttpRequest) 
    { 														// Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) 
        {
            httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) 
   	{ 														// IE
        try 
        {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
        	try 
        	{
            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
   	}

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
        
   	httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function alertContents(httpRequest) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
        	var items = httpRequest.responseXML.getElementsByTagName('time_remaining');
			var itCnt = items.length;
			var time = items[0].getAttribute("label");
			
			time = parseInt(time);
			
			if(time <= 90 && time != 0)						/** When session is less then 1.5 minutes, set interval to 1 second (timer on warning) */
			{
        		clearInterval(timer);
        		timer = window.setInterval("session_check()", 1000);
        		
        		var grayout = document.getElementById('grayout');
        		var dialog = document.getElementById('dialog');
        		var body = document.getElementById('body');
        		var timetext = '';
        		timesec = time;
        		
        		if(time >= 60)
        		{
        			timetext = '1 minuut ';
        			timesec = time - 60;
        		}
        		        		
        		timetext += parseInt(timesec) + ' seconden';
        		
        		grayout.style.display = 'block';
        		if(time == 1)
        		{
        			set_dialog('Uw sessie is vervallen', 'Let op! Uw sessie is vervallen.<br>Dit houdt in dat uw pagina niet opgeslagen kan worden.', '', '',time);
        		}
        		else
        		{
        			set_dialog('Uw sessie gaat vervallen', 'Let op! Uw sessie gaat vervallen.<br>Dit houdt in dat uw pagina niet opgeslagen kan worden.<br><br>Uw sessie verloopt in '+timetext+'. <br><br>Klik op de knop "sessie behouden" om uw sessie te verlengen', '', '',time);
        		}
        		
        		dialog.style.display = 'block';				
        		set_to_full_window();
        		body.style.overflow = 'hidden';
        		
			}
			
			if(time == 0)						/** Stop the timer if session does not excist or expired */
			{
				clearInterval(timer);
			}
        	
        } else {
           // alert('There was a problem with the request.'); /* don't show the error */
        }
    }
}

function set_to_full_window()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var dialogwidth = dialogdiv.clientWidth;
	var dialogheight = dialogdiv.clientHeight;
	var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var windowwidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;

	var halfWidth = windowwidth / 2;
	var halfHeight = windowHeight / 2;
	var halfdialogwidth = dialogwidth / 2;
	var halfdialogheight = dialogheight / 2;
	var offsetl = halfWidth - halfdialogwidth;
	var offseth = halfHeight - halfdialogheight;
	
	grayoutdiv.style.height = windowHeight;	
	
	dialogdiv.style.left = offsetl + 'px';
	dialogdiv.style.top = offseth + 'px';
}

function set_dialog(header,content,action,footer,time)
{
	var headerspan = document.getElementById('dialog_header_text');
	var footerspan = document.getElementById('dialog_footer_text');
	var contentdiv = document.getElementById('dialog_content_text');
	var contentdivp = contentdiv.getElementsByTagName('p');
					
	if(contentdivp[0] != null)
	{
		contentdiv.removeChild(contentdivp[0]);
	}
	
	var content = content.split("<br>");
	var divcontent = document.createElement('p');
	
	for(var i=0; i<content.length; i++)
	{
		divcontent.appendChild(document.createTextNode(content[i]));
		divcontent.appendChild(document.createElement('br'));
	}
	
	if(parseInt(time) != 1)
	{
		var image = document.createElement('img');
		image.src = '/include/images/Sessie_behouden.png';
		image.id = 'session_keep_image';
		divcontent.appendChild(image);
	}
	else
	{
		var image = document.createElement('img');
		image.src = '/include/images/ok.png';
		image.id = 'ok_image';
		divcontent.appendChild(image);
	}
	
	headerspan.firstChild.nodeValue = header;
	footerspan.firstChild.nodeValue = footer;
	
	contentdiv.appendChild(divcontent);
	
	if(parseInt(time) != 1)
	{
		var button = document.getElementById('session_keep_image');
		addEvent(button, 'click', _keep_session_click);	
	}
	else
	{
		var button = document.getElementById('ok_image');
		addEvent(button, 'click', _ok_click);
	}
}

function _keep_session_click()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var body = document.getElementById('body');
	
	clearInterval(timer);
    timer = window.setInterval("session_check()", 30000);
	makeRequest_nocallback('include/functions/session_time.fnc.php/add_session_time');
	grayoutdiv.style.display = 'none';
	dialogdiv.style.display = 'none';
	body.style.overflow = 'auto';
}

function _ok_click()
{
	var grayoutdiv = document.getElementById('grayout');
	var dialogdiv = document.getElementById('dialog');
	var body = document.getElementById('body');
	
	clearInterval(timer);
    timer = window.setInterval("session_check()", 30000);
    grayoutdiv.style.display = 'none';
	dialogdiv.style.display = 'none';
	body.style.overflow = 'auto';
}

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: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		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.platform,
		subString: "Linux",
		identity: "Linux"
	}
]

};

BrowserDetect.init();