/*
	Gabriel BONDAZ
	Service PRACTICE - Université Claude Bernard LYON 1
	2006

	dSENPP_classes.js : detection du Système d'Exploitation, du Navigateur, des Plugins, des Popups
*/

	// retourne vrai si le caractère passé en paramètre est "numérique",
	// retourne faux sinon. 
	function isNumeric(c)
	{
		codeAsc = c.charCodeAt(0);
		
		zero = '0'.charCodeAt(0);
		neuf = '9'.charCodeAt(0);

		if (zero<=codeAsc && codeAsc<=neuf)
		{
			return(true);
		}
		return(false);
	}

	// retourne vrai si le caractère passé en paramètre est "alpha",
	// retourne faux sinon. 
	function isAlpha(c)
	{
		codeAsc = c.charCodeAt(0);
		
		a = 'a'.charCodeAt(0);
		z = 'z'.charCodeAt(0);
		A = 'A'.charCodeAt(0);
		Z = 'Z'.charCodeAt(0);

		if ((a<=codeAsc && codeAsc<=z) || (A<=codeAsc && codeAsc<=Z))
		{
			return(true);
		}
		return(false);
	}

	// retourne vrai si le caractère passé en paramètre est "alphanumérique",
	// retourne faux sinon. 
	function isAlphaNumeric(c)
	{
		if (isAlpha(c) || isNumeric(c))
		{
			return(true);
		}
		return(false);
	}

	// retourne vrai si le caractère passé en paramètre fait partit de la version,
	// retourne faux sinon.
	function isCharVersion(c)
	{
		codeAsc = c.charCodeAt(0);

		point = '.';
		underscore = "_";

		if(isNumeric(c) || codeAsc==point.charCodeAt(0) || codeAsc==underscore.charCodeAt(0))
		{
			return(true);
		}
		return(false);
	}

	// Permet d'extraire une chaine de caractères dans une autre, suivant certain critéres.
	// Ici on cherche à extraire la version d'un moteur de recherche.
	function f_getVersion(s_source, i_posDebut)
	{
		var version="";

		while (i_posDebut<s_source.length && !isNumeric(s_source.charAt(i_posDebut)))
		{
			i_posDebut++;
		}

		for (var i=i_posDebut; i<s_source.length && isCharVersion(s_source.charAt(i)); i++)
		{
			version += s_source.charAt(i);
		}
		return(version);
	}
	
	// Sans commentaires
	function canDetectPlugins()
	{
  		if(window.navigator.plugins && window.navigator.plugins.length > 0)
		{
    		return true;
  		}
    	return false;
	}

	// Sans commentaires
	function canDetectMimeTypes()
	{
  		if(window.navigator.mimeTypes && window.navigator.mimeTypes.length > 0)
		{
    		return true;
  		}
    	return false;
	}

	// pour detecter la présence de plugin avec IE
	function isInstalled(progId)
    {
		try
		{
			var plg = new ActiveXObject(progId);
			return true;
		}
		catch(e)
		{
			return false;
		}
	}

/*****************************************************************************/

	var WN	 	= window.navigator;				// Objet navigator
	var WNUA 	= window.navigator.userAgent;	// Objet userAgent
	var WNP		= window.navigator.plugins;		// Objet plugins
	var WNMT	= window.navigator.mimeTypes;	// Objet mimeTypes

/*****************************************************************************/

	/* Classe c_SystemeExploitation
	 *
	 *	s_keywordName		: Nom de l'os, mot clé à recherché (en minuscule !)
	 *	s_name				: Nom de l'os à afficher
 	 *	s_keywordVersion	: Version de l'os, mot clé à recherché (en minuscule !)
	 *	s_version			: Nom de la version à afficher
	 *	s_pathIcon			: Emplacement de l'icon associé
	 *	s_url				: Lien associé
	 */

	var g_arr_SystemeExploitation = new Array();

	function c_SystemeExploitation(s_keywordName, s_name, s_keywordVersion, s_version, s_pathIcon, s_url)
	{
		this.keywordName 		= s_keywordName;
		this.name				= s_name;
		this.keywordVersion 	= s_keywordVersion;
		this.version			= s_version;
		this.pathIcon			= s_pathIcon;
		this.url				= s_url;

		// Méthode getMySE
		this.getMySE			= c_SystemeExploitation_getMySE;
		function c_SystemeExploitation_getMySE()
		{
			// initialisation de variables de position
			var sen = 0; // Système Exploitation Nom
			var sev = 0; // Système Exploitation Version
		
			// On parcourt le vecteur g_arr_SystemeExploitation (Commence à 1 car l'indice 0 est réserver pour les erreurs)
			for(var i=1; i<g_arr_SystemeExploitation.length; i++)
			{
				// Si on a trouvé un SystemeExploitation (présence de keywordName dans WNUA)
				if (WNUA.toLowerCase().indexOf(g_arr_SystemeExploitation[i].keywordName)!=-1)
				{
					// On mémorise cette position
					sen = i;
					// Si on a trouvé la version (présence de keywordVersion dans wnuA)
					if (WNUA.toLowerCase().indexOf(g_arr_SystemeExploitation[i].keywordVersion)!=-1)
					{
						// On mémorise cette position
						sev = i;
						// On arrête la recherche car on a trouvé ce que l'on cherché.
						break;
					}
				}
			}

			this.keywordName 	= g_arr_SystemeExploitation[sen].keywordName;
			this.name 			= g_arr_SystemeExploitation[sen].name;
			this.keywordVersion = g_arr_SystemeExploitation[sev].keywordVersion;
			this.version		= g_arr_SystemeExploitation[sev].version;
			this.pathIcon		= g_arr_SystemeExploitation[sen].pathIcon;
			this.url			= g_arr_SystemeExploitation[sen].url;
		}

		// Ajouter à la liste
		this.addSystemeExploitation = c_SystemeExploitation_addSystemeExploitation;
		function c_SystemeExploitation_addSystemeExploitation()
		{
			g_arr_SystemeExploitation[g_arr_SystemeExploitation.length] = this;
		}

		// Méthode affiche
		this.affiche = c_SystemeExploitation_affiche;
		function c_SystemeExploitation_affiche(w)
		{
			var nv = this.name+" "+this.version;
			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			w.document.write("<img src=\""+this.pathIcon+"\" alt=\""+nv+"\" title=\""+nv+"\" border=\"0\" />");
			w.document.write("</a><br />");
			w.document.write("<b>"+nv+"</b>");
		}

		// Méthode afficheImage
		this.afficheImage = c_SystemeExploitation_afficheImage;
		function c_SystemeExploitation_afficheImage(w)
		{
			var nv = this.name+" "+this.version;
			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			w.document.write("<img src=\""+this.pathIcon+"\" alt=\""+nv+"\" title=\""+nv+"\" border=\"0\" />");
			w.document.write("</a>");
		}

		// Méthode afficheNom
		this.afficheNom = c_SystemeExploitation_afficheNom;
		function c_SystemeExploitation_afficheNom(w)
		{
			var nv = this.name+" "+this.version;
			w.document.write("<b>"+nv+"</b>");
		}

		// Méthode affiche4Spiral
		this.affiche4Spiral = c_SystemeExploitation_affiche4Spiral;
		function c_SystemeExploitation_affiche4Spiral(w)
		{
			w.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
			w.document.write("<tr><th colspan=\"2\">Syst&egrave;me d'Exploitation :</th></tr>");
			w.document.write("<tr><td colspan=\"3\">&nbsp;</td></tr>");
			w.document.write("<tr><td align=\"center\">");
			this.afficheImage(w);
			w.document.write("</td><td class=\"text\">");
			this.afficheNom(w);
			w.document.write("</td></tr>");
			w.document.write("</table>");
		}
	}
	
	// Constructeur vide
	function SystemeExploitation()
	{
		return(new c_SystemeExploitation("", "", "", "", "", ""));
	}

/*****************************************************************************/

	/* Classe c_Navigateur
	 *
	 *	s_moteurName		: Nom du moteur web
	 *	s_navigateurName	: Nom du navigateur
 	 *	s_version			: Version du navigateur
	 *	s_pathIcon			: Emplacement de l'icon associé
	 *	s_url				: Lien associé
	 *	s_info				: Information supplémentaire
	 */

	var g_arr_Navigateur = new Array();

	function c_Navigateur(s_keywordMoteur, s_keywordName, s_name, s_pathIcon, s_url, s_info)
	{
		this.keywordMoteur 		= s_keywordMoteur;
		this.keywordName		= s_keywordName;
		this.name				= s_name;
		this.version 			= "";
		this.pathIcon			= s_pathIcon;
		this.url				= s_url;
		this.info				= s_info;

		// Ajouter à la liste
		this.addNavigateur = c_Navigateur_addNavigateur;
		function c_Navigateur_addNavigateur()
		{
			g_arr_Navigateur[g_arr_Navigateur.length] = this;
		}

		// Méthode getMyNav()
		this.getMyNav = c_SystemeExploitation_getMyNav;
		function c_SystemeExploitation_getMyNav()
		{
			// initialisation de variables de position
			var nam = 0; // NAvigateur Moteur
			var nan = 0; // NAvigateur Nom
			var nav = ""; // NAvigateur Version

			// On parcourt le vecteur g_arr_Navigateur (Commence à 1 car l'indice 0 est réserver pour les erreurs)
			for(var i=1; i<g_arr_Navigateur.length; i++)
			{
				// Si on a trouvé un moteur de Navigateur (présence de keywordMoteur dans WNUA)
				if (WNUA.toLowerCase().indexOf(g_arr_Navigateur[i].keywordMoteur)!=-1)
				{
					// On mémorise cette position
					nam = i;	
				
					// Si on a trouvé le nom du navigateur (présence de keywordName dans WNUA)
					if ((pos = WNUA.toLowerCase().indexOf(g_arr_Navigateur[i].keywordName))!=-1)
					{
						// Récupérer la version du navigateur
						vpos = pos+g_arr_Navigateur[i].keywordName.length;
						nav = f_getVersion(WNUA,vpos);

						// On mémorise cette position
						nan = i;
						// On arrête la recherche car on a trouvé ce que l'on cherché.
						break;
					}
				}
			}
		
			this.keywordMoteur	= g_arr_Navigateur[nam].keywordMoteur;
			this.keywordName	= g_arr_Navigateur[nan].keywordName;
			this.name			= g_arr_Navigateur[nan].name;
			this.version		= nav;
			this.pathIcon		= g_arr_Navigateur[nan].pathIcon;
			this.url			= g_arr_Navigateur[nan].url;
			this.info			= g_arr_Navigateur[nan].info;
		}
	
		// Méthode affiche
		this.affiche = c_Navigateur_affiche;
		function c_Navigateur_affiche(w)
		{ 
			var nv = this.name+" "+this.version;
			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			w.document.write("<img src=\""+this.pathIcon+"\" alt=\""+nv+"\" title=\""+nv+"\" border=\"0\" />");
			w.document.write("</a><br />");
			w.document.write("<b>"+nv+"</b>");
		}

		// Méthode afficheImage
		this.afficheImage = c_Navigateur_afficheImage;
		function c_Navigateur_afficheImage(w)
		{ 
			var nv = this.name+" "+this.version;
			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			w.document.write("<img src=\""+this.pathIcon+"\" alt=\""+nv+"\" title=\""+nv+"\" border=\"0\" />");
			w.document.write("</a>");
		}

		// Méthode afficheNom
		this.afficheNom = c_Navigateur_afficheNom;
		function c_Navigateur_afficheNom(w)
		{ 
			var nv = this.name+" "+this.version;
			w.document.write("<b>"+nv+"</b>");
		}

		// Méthode affiche4Spiral
		this.affiche4Spiral = c_Navigateur_affiche4Spiral;
		function c_Navigateur_affiche4Spiral(w)
		{
			w.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
			w.document.write("<tr><th colspan=\"2\">Navigateur :</th></tr>");
			w.document.write("<tr><td colspan=\"3\">&nbsp;</td></tr>");
			w.document.write("<tr><td align=\"center\">");
			this.afficheImage(w);
			w.document.write("</td><td class=\"text\">");
			this.afficheNom(w);
			w.document.write("</td></tr>");
			if (this.info!="")
			{
				w.document.write("<tr><td align=\"center\">");
				w.document.write("<img src=\""+imgPath+"info.gif\" alt=\"Information\" title=\"Information\" border=\"0\" />");
				w.document.write("</td><td class=\"info\">");				
				w.document.write("<i>"+this.info+"</i>");
				w.document.write("</td></tr>");
			}			
			w.document.write("</table>");
		}
	}
	
	// Constructeur vide
	function Navigateur()
	{
		return(new c_Navigateur("", "", "", "", "", ""));
	}


/*****************************************************************************/

	/* Classe c_Plugin
	 *
	 *	s_keywordName		: Nom du plugin, mot clé à recherché (en minuscule !)
	 *	s_name				: Nom du plugin à afficher
	 *	s_version			: Version du plugin
	 *	s_mimeType			: Type mime correspondant
	 *	a_lProgId			: Vecteur contenant les progId
	 *	a_lVersion			: Vecteur contenant les version du plugin associé au progId
	 *	b_pluginPresent		: Boolean indiquant la présence ou non du plugin
	 *	b_mimeTypePresent	: Boolean indiquant la présence ou non du type mime géré par le plugin
	 *	s_pathIconOk		: Emplacement de l'icon associé si le plugin est installé
	 *	s_pathIconErr		: Emplacement de l'icon associé si le plugin n'est pas installé
	 *	s_url				: Lien associé
	 *	s_info				: information sur le plugin
	 */

	var g_arr_Plugins 				= new Array();	// Tableau des plugins à tester.

	function c_Plugin(s_keywordName, s_name, s_mimeType, s_pathIconOk, s_pathIconErr, s_url, s_info)
	{
		this.keywordName		= s_keywordName;
		this.name				= s_name;
		this.version			= "";
		this.mimeType			= s_mimeType;
		this.lProgId			= new Array();
		this.lVersion			= new Array();
		this.pluginPresent		= false;
		this.mimeTypePresent	= false;
		this.pathIconOk			= s_pathIconOk;
		this.pathIconErr		= s_pathIconErr;
		this.url				= s_url;
		this.info				= s_info;

		// Ajouter à la liste
		this.addPlugin = c_Plugin_addPlugin;
		function c_Plugin_addPlugin()
		{
			g_arr_Plugins[g_arr_Plugins.length] = this;
		}

		// Méthode addProgId
		this.addProgId = c_Plugin_addProgId;
		function c_Plugin_addProgId(s_progId, s_version)
		{
			this.lProgId[this.lProgId.length]	= s_progId
			this.lVersion[this.lVersion.length]	= s_version;
		}
		
		// Définir le statut
		this.defineStatut = c_Plugin_defineStatut;
		function c_Plugin_defineStatut()
		{
			if (this.pluginPresent)
			{
				return("<font color=\"#487420\">Install&eacute;</font>");
			}
			
			if(this.mimeTypePresent)
			{
				return("<font color=\"#0000ff\">Compatible</font>");
			}

			return("<font color=\"#ff0000\">Manquant</font>")
		}

		// Méthode affiche
		this.affiche = c_Plugin_affiche;
		function c_Plugin_affiche(w)
		{ 
			var nvt = this.name+" "+this.version+" "+this.mimeType;

			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			w.document.write("<b>"+nvt+"</b>");
			w.document.write("</a>");
			
			w.document.write("&nbsp;&nbsp;");

			if (this.pluginPresent)
			{w.document.write("<font color=\"#00FF00\">Plugin</font>");}
			else
			{w.document.write("<font color=\"#FF0000\">Plugin</font>");}
			
			w.document.write("&nbsp;-&nbsp;");

			if (this.mimeTypePresent)
			{w.document.write("<font color=\"#00FF00\">mimeType</font>");}
			else
			{w.document.write("<font color=\"#FF0000\">mimeType</font>");}

			w.document.write("<br />");			
		}

		// Méthode AfficheImage
		this.afficheImage = c_Plugin_afficheImage;
		function c_Plugin_afficheImage(w)
		{ 
			var nvt = this.name+" "+this.version+" "+this.mimeType;

			w.document.write("<a href=\""+this.url+"\" target=\"_blank\">");
			
			if (this.pluginPresent || this.mimeTypePresent)
			{
				w.document.write("<img src=\""+this.pathIconOk+"\" alt=\""+nvt+"\" title=\""+nvt+"\" border=\"0\" />");
			}
			else
			{
				w.document.write("<img src=\""+this.pathIconErr+"\" alt=\""+nvt+"\" title=\""+nvt+"\" border=\"0\" />");
			}
			w.document.write("</a>");
		}

		// Méthode afficheNom
		this.afficheNom = c_Plugin_afficheNom;
		function c_Plugin_afficheNom(w)
		{ 
			var nv = this.name+" "+this.version;
			w.document.write("<b>"+nv+"</b>");
		}

		// Méthode afficheInfo
		this.afficheInfo = c_Plugin_afficheInfo;
		function c_Plugin_afficheInfo(w)
		{ 
			w.document.write(this.info);
		}

		// Méthode afficheEtat
		this.afficheEtat = c_Plugin_afficheEtat;
		function c_Plugin_afficheEtat(w)
		{ 
			w.document.write(this.defineStatut());
		}
	}

	// Liste des plugins
	function c_lstPlugin()
	{
		// Detecter la présence ou non de chaque plugin contenu dans le vecteur 'g_arr_Plugins'
		// positionner les données membres 'pluginPresent' et 'mimeTypePresent' à true ou false.
		
		// Variable
		var i=0;
		var cdp = canDetectPlugins();
		var cdmt = canDetectMimeTypes();
			
		// Parcourt de la liste des plugins 
		for(var plg=0; plg<g_arr_Plugins.length; plg++)
		{
			// Si on peut detecter des plugins via l'objet 'navigator.plugins'
			if(cdp)
			{
				// On recherche la présence d'un plugin dans l'objet "navigator.plugins"
				i=0;
				while(i<WNP.length && WNP[i].name.toLowerCase().indexOf(g_arr_Plugins[plg].keywordName)==-1)
				{
					i++;
				}
				// On sort de la boucle si on a trouvé la présence du plugin ou non
		
				// Si on a trouvé
				if (i < WNP.length)
				{	
					g_arr_Plugins[plg].pluginPresent = true;
					// TODO : A améliorer
					g_arr_Plugins[plg].version = f_getVersion(WNP[i].name+WNP[i].description,0);
				}
			}

			// Si on peut detecter des type via l'objet 'navigator.mimeTypes'
			if(cdmt)
			{
				// On recherche la présence d'un type dans l'objet 'navigator.mimeTypes'
				i=0;
				while(i<WNMT.length && WNMT[i].type.indexOf(g_arr_Plugins[plg].mimeType)==-1)	
				{
					i++;
				}
				// On sort de la boucle si on a trouvé la présence du type du plugin ou non
	
				// Si on a trouvé
				if(i < WNMT.length)
				{
					g_arr_Plugins[plg].mimeTypePresent = true;
				}
			}

			// Si on ne peut detecter ni 'navigator.plugins' et ni 'navigator.mimeTypes' (navigateur avec moteur msie !)
			if(!cdp && !cdmt)
			{
				// Cas particulier pour le plugin Java
				if(g_arr_Plugins[plg].keywordName == "java")
				{
					if(window.navigator.javaEnabled())
					{
						g_arr_Plugins[plg].pluginPresent = true;
						g_arr_Plugins[plg].mimeTypePresent = true;
					}
				}
				else
				{
					// On recherche la présence du progId
					i=0;
					while(i<g_arr_Plugins[plg].lProgId.length && !isInstalled(g_arr_Plugins[plg].lProgId[i]))
					{
						i++;
					}
					// On sort de la boucle si on a trouvé la présence du plugin ou non
	
					// Si on a trouvé
					if(i < g_arr_Plugins[plg].lProgId.length)
					{
						g_arr_Plugins[plg].pluginPresent = true;
						g_arr_Plugins[plg].mimeTypePresent = true;
						g_arr_Plugins[plg].version = g_arr_Plugins[plg].lVersion[i];
					}
				}
			}
		}

		// Méthode Affiche
		this.affiche = c_lstPlugin_affiche;
		function c_lstPlugin_affiche(w)
		{
			for(var i=0; i<g_arr_Plugins.length; i++)
			{
				g_arr_Plugins[i].affiche(w);
			}
		}

		// Méthode Affiche4Spiral
		this.affiche4Spiral = c_lstPlugin_affiche4Spiral;
		function c_lstPlugin_affiche4Spiral(w)
		{
			w.document.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">");
			w.document.write("<tr><th colspan=\"3\">Liste des plugins :</th></tr>");
			for(var i=0; i<g_arr_Plugins.length; i++)
			{

				w.document.write("<tr><td colspan=\"3\">&nbsp;</td></tr>");

				w.document.write("<tr>");

					w.document.write("<td rowspan=\"2\">");
					g_arr_Plugins[i].afficheImage(w);
					w.document.write("</td>");

					w.document.write("<td class=\"text\">");
					g_arr_Plugins[i].afficheNom(w);
					w.document.write("</td>");

					w.document.write("<td class=\"etat\" rowspan=\"2\">");
					g_arr_Plugins[i].afficheEtat(w);
					w.document.write("</td>");

				w.document.write("</tr>");

				w.document.write("<tr>");
					w.document.write("<td class=\"info\">");
					g_arr_Plugins[i].afficheInfo(w);
					w.document.write("</td>");
				w.document.write("</tr>");
			}
			w.document.write("</table>");
		}
	}

	// Constructeur vide
	function lstPlugin()
	{
		return(new c_lstPlugin());
	}

/*****************************************************************************/

	/* Classe c_AntiPopups
	 *
	 *	isAllow	: Boolean indiquant la présence ou non d'un bloquer de popups 
	 */

	function c_AntiPopups()
	{
		this.isAllow	= false;

		// Tester la présence d'un anti popup !
		try
		{
  			w=open("",'popup','width=10,height=10,toolbar=no,scrollbars=yes,resizable=yes');
			this.isAllow = true;
    	    w.document.close();
			w.close();
  		}
		catch(err) 
		{
  			this.isAllow	= false;
  		}

		// Méthode Affiche
		this.affiche = c_AntiPopups_affiche;
		function c_AntiPopups_affiche(w)
		{
			if(this.isAllow)
			{
				w.document.write("Les popups sont acceptes");
			}
			else
			{
				w.document.write("Les popups ne sont pas acceptes");
			}	
		}

		// Méthode affiche4Spiral
		this.affiche4Spiral = c_AntiPopups_affiche4Spiral;
		function c_AntiPopups_affiche4Spiral(w)
		{
			if(this.isAllow)
			{
				w.document.write("<img src=\""+imgPath+"popup_ok.jpg\" style=\"vertical-align:middle;\" alt=\"Popup OK\" title=\"Popup OK\" />");
				w.document.write("Les popups sont accept&eacute;s");
			}
			else
			{
				w.document.write("<img src=\""+imgPath+"popup_err.jpg\" style=\"vertical-align:middle;\" alt=\"Popup ERREUR\" title=\"Popup ERREUR\" />");
				w.document.write("Les popups ne sont pas accept&eacute;s");
			}	
		}
	}
	
	function AntiPopups()
	{
		return(new c_AntiPopups());
	}

