//  if (e instanceof TypeError)

//Exceptions
const AUTHORISATION_REFUSED	= "Permission UniversalBrowserRead denied";
const AJAX_UNSUPPORTED			= "Browser cannot use XMLHttpRequest";
const METHODE_UNSUPPORTED		= "This method is not supported";

//some default value
const FLASH_VERSION_UNKNOWN	= -1;
const FLASH_VERSION_NONE		= 0;

//Configuration keys
const JAVASCRIPT_SUPPORTED	= 'JAVASCRIPT_SUPPORTED';
const AJAX_SUPPORTED			= 'AJAX_SUPPORTED';
const FLASH_SUPPORTED		= 'FLASH_SUPPORTED';
const LOADER					= 'LOADER';

//Configuration array
var CONFIG = {
		  JAVASCRIPT_SUPPORTED : false,
		  AJAX_SUPPORTED : false,
		  FLASH_SUPPORTED : false,
		  LOADER	: []
	 };

// Utilities functions

function isInternetExplorer(){
	 /*FIXME*/
  //if(window.ActiveXObject){
  return navigator.appName=="Microsoft Internet Explorer";
}

// Ajax function

function makeRequest(url,method,query,alertContents,synch,perm){
	var http_request = null;
	if(isInternetExplorer()){
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(ee){ throw AJAX_UNSUPPORTED; }
		}
	}else if(window.XMLHttpRequest){
		try{
			if(perm == null ? false : perm)
				netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
			http_request = new XMLHttpRequest();
		}catch(e){ throw AUTHORISATION_REFUSED; }
	}else{ throw AJAX_UNSUPPORTED; }
	//if(http_request.overrideMimeType)
	//	http_request.overrideMimeType('text/xml');

	http_request.onreadystatechange = function(){
		if(http_request.readyState == 4)
		if(http_request.status == 200)
		alertContents(http_request.responseText);
	};
	if(method=='GET'){
		http_request.open('GET', url + query, (synch==null?true:synch));
		http_request.send(null);
	}else if(method=='POST'){
		http_request.open('POST', url, (synch==null?true:synch));
		http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		http_request.send(query);
	}else{ throw METHODE_UNSUPPORTED; }
}

function ajaxIsSupported(){
	var http_request = null;
	if(isInternetExplorer()){
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(ee){}
		}
	}else if(window.XMLHttpRequest){
		try{
			http_request = new XMLHttpRequest();
		}catch(e){}
	}
  return http_request!=null;
}

// Flash function
/* FIXME flash fonctionne que si le javascript fonctionne... */
function flash_version(){
  if(navigator.plugins && navigator.plugins.length){
	 var plugin = navigator.plugins["Shockwave Flash"];
	 if(plugin){
		if(plugin.description){
		  var y = plugin.description;
		  return y.charAt(y.indexOf('.')-1);
		}
		return FLASH_VERSION_UNKNOWN;
	 }
	 if(navigator.plugins["Shockwave Flash 2.0"])return 2;
  }else if (navigator.mimeTypes && navigator.mimeTypes.length){
	 var plugin = navigator.mimeTypes['application/x-shockwave-flash'];
	 if(plugin && plugin.enabledPlugin) return FLASH_VERSION_UNKNOWN;
	 return FLASH_VERSION_NONE;
  }else{
	 for(var i=7; i>0; i--)
		try{
		  var flash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
		  return i;
		}catch(e){
		  return FLASH_VERSION_NONE;
		}
  }
}

// Startup function and utilities

function addWindowListener(func){
  CONFIG[LOADER][CONFIG[LOADER].length]=func;
  return true;
}
function removeWindowListener(func){
  for(var i=0;i<CONFIG[LOADER].length;i++)
	 if(CONFIG[LOADER][i]==func){
		if(i>0){
		  for(var j=i+1;j<CONFIG[LOADER].length;j++)
			 CONFIG[LOADER][j-1]=CONFIG[LOADER][j];
		}
		CONFIG[LOADER].length=CONFIG[LOADER].length-1;
		return true;
	 }
  return false;
}

function di_init(){
  CONFIG[JAVASCRIPT_SUPPORTED]	= true;
  CONFIG[AJAX_SUPPORTED]			= ajaxIsSupported();
  for(var i=0;i<CONFIG[LOADER].length;i++)
	 CONFIG[LOADER][i]();
}

window.onload = di_init();
