// globalfunctions.js

//
addEvent( window, "load", LoginScreen );

// check if this browser is a W3C compatible browser
var W3CDOM = document.getElementById && document.createElement;

function addEvent( obj, type, fn ) 
{
	if (obj.addEventListener)
	{
		obj.addEventListener( type, fn, false );
	}
	else if ( obj.attachEvent ) 
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() 
		{ 
			obj["e"+type+fn]( window.event ); 
		}
		obj.attachEvent( "on"+type, obj[type+fn] );
	} 
}

function removeEvent( obj, type, fn ) 
{
	if ( obj.detachEvent ) 
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
	} 
	else if (obj.removeEventListener)
	{
		obj.removeEventListener( type, fn, false );
	}
}

function trim( text )
{
	return text.replace( /^\s*|\s*$/g, '' );
}

var XMLHttpFactories = [ 
	function() { return new XMLHttpRequest() }
	, function() { return new ActiveXObject( 'Msxml2.XMLHTTP' ) }
	, function() { return new ActiveXObject( 'Msxml3.XMLHTTP' ) }
	, function() { return new ActiveXObject( 'Microsoft.XMLHTTP' ) }
];

function GetXmlHttpObject()
{
	var xmlHttp = false;
	
	for( var i = 0; i < XMLHttpFactories.length; i++ )
	{
		try
		{
			xmlHttp = XMLHttpFactories[i]();
		}
		catch( e )
		{
			continue;
		}
		break;
	}
	return xmlHttp;
}

function sendRequest( url, callback, postData, preloadAction, addObj ) 
{
	var req = GetXmlHttpObject();
	
	if (!req) return;
	
	var method = ( postData ) ? "POST" : "GET";
	
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	
	if (postData)
	{
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	}
	
	req.onreadystatechange = function() 
	{
		if( preloadAction != null )
		{
			preloadAction( req.readyState );
		}
		
		if( req.readyState != 4 ) 
		{
			return;
		}
		else
		{
			callback( req, addObj );
		}
	}
	
	if (req.readyState == 4)
	{
		return;
	}
	
	req.send( postData );
}

// This will display debug info in a DIV elment with classname js_debug
// just below the body in a PRE element
function debugInfo( info )
{
	if( !info )
	{
		info = 'No info available';
	}
	var text = document.createTextNode( 'Debug info: '+info );
	var oPre = document.createElement( 'PRE' );
	var jsDebugLayer = document.createElement( 'DIV' );
	var oBody = document.getElementsByTagName( 'BODY' )[0];
	
	oPre.appendChild( text );
	jsDebugLayer.appendChild( oPre );
	jsDebugLayer.className = 'js_debug';
	oBody.appendChild( jsDebugLayer );
}


function LoginScreen()
{
	if( W3CDOM )
	{
		if( document.getElementById( 'login-link' ) && document.getElementById( 'login-window' ) )
		{
			var LoginScreen 	= document.getElementById( 'login-window' );
			var LoginLink 		= document.getElementById( 'login-link' );
			LoginLink.className	= 'enhance-link-main';
			LoginLink.onclick 	= function() 
			{ 
				ToggleDisplay( LoginScreen ); 
				return false; 
			};
			
			var oCancel = document.getElementById( 'cancel' );
			oCancel.onclick = function()
			{
				ToggleDisplay( LoginScreen ); 
				return false;
			}
		}
	}
}

function ToggleDisplay( obj )
{
	if( obj.style.display == 'none' )
	{
		obj.style.display = 'block';
	}
	else if( obj.style.display == 'block' )
	{
		obj.style.display = 'none';
	}
	else
	{
		obj.style.display = 'block';
	}
}

xmlDoc = null;
function GetXML( type, xml, callback )
{
	if ( document.implementation 
			&& document.implementation.createDocument )
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function() { 
		
			callback( xmlDoc );
		};
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
		
			if (xmlDoc.readyState == 4) 
			{
				callback( xmlDoc );
			}
		};
 	}
	else
	{
		alert( 'De wegwerkzaamheden kunnen niet worden getoond, omdat uw browser dit niet ondersteunt.' );
		return;
	}
	
	
	xmlDoc.load( xml );
	
}
