/**
* sniffer.js
*
* @package		HTML
* @version		Version 1.0 - 2005-01-18
* @author		Sebastien Garrouste
* @copyright	Imago - ? Copyright Metapages - All rights reserved.
*/

/*
* Script de verification de version de navigateur
*
* ce script permet de trouver la plateforme, le navigateur et sa version et ? partir de c'est informations
* de dire si oui ou non un navigateur ? les droits requis pour acc?der ? l'application.
*
* PARAMETRES : 
* 	- Permet d'autoriser un navigateur sur une plateforme particuliere
*	- Permet de choisir la version min. d'un navigateur sur une plateforme particuliere
*
* RETOUR : 
*	La fonction "detection_navigateur()" attend un parametre qui peut avoir 2 valeur (redirection, recapitulatif)
*		- redirection : Si tout les test sont valide retourne true sinon false (la redirection est a faire en dehors de ce script)
*		- recapitulatif : Retourne un tableau avec les informations necessaires (plateforme, navigateur, version reelle, version necessaire, autorisation)
*/

///////////////////////////////////////////////////////////////////////////////////////////////////
// Parametre des autorisations
var autorise_ie_win = true;
var autorise_ie_mac = false;
var autorise_kq_linux = false;
var autorise_ns_win = false;
var autorise_ns_mac = false;
var autorise_ns_linux = false;
var autorise_mz_linux = true;
var autorise_mz_win = true;
var autorise_mz_mac = true;
var autorise_saf_mac = true;

// Parametre des versions
var vers_ie_win = 6;
var vers_ie_mac = 5;
var vers_kq_linux = 2;
var vers_ns_win = 6;
var vers_ns_mac = 7;
var vers_ns_linux = 6;
var vers_mz_linux = 1.7;
var vers_mz_win = 1.7;
var vers_mz_mac = 1.7;
var vers_saf_mac = 120;	// Safari est particulier la version est 1.0 => 85, 1.2 => 127 (A VERIFIER)

// Lien de telechargement des navigateur
var ie_win_dl = 'http://www.microsoft.com/windows/ie/downloads/default.mspx';
var ie_mac_dl = 'http://www.microsoft.com/mac/products/internetexplorer/internetexplorer.aspx?pid=internetexplorer';
var kq_linux_dl = 'NC';
var ns_win_dl = 'NC';
var ns_mac_dl = 'NC';
var ns_linux_dl = 'NC';
var mz_linux_dl = 'http://www.mozilla.com';
var mz_win_dl = 'http://www.mozilla.com';
var mz_mac_dl = 'http://www.mozilla.com';
var saf_mac_dl = 'http://www.apple.com/safari/download/';

// Autre lien pour les navigateur
var ie_win_other = '';
var ie_mac_other = 'Mozilla, Firefox, Safari';
var kq_linux_other = 'Mozilla, Firefox';
var ns_win_other = 'Mozilla, Firefox';
var ns_mac_other = 'Mozilla, Firefox, safari';
var ns_linux_other = 'Mozilla, Firefox';
var mz_linux_other = '';
var mz_win_other = '';
var mz_mac_other = '';
var saf_mac_other = '';
///////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////
// Variables
var is_ie = false;
var vers_ie = 0;
var true_vers_ie = 0;
var is_ns = false;
var vers_ns = 0;
var true_vers_ns = 0;
var is_mz = false;
var vers_mz = 0;
var true_vers_mz = 0;
var is_kq = false;
var vers_kq = 0;
var true_vers_kq = 0;
var is_saf = false;
var vers_saf = 0;
var true_vers_saf = 0;

var glb_agent;
var glb_version;
var glb_platform;

function isole_version(chaine, dec_av_ch){
	var tmp_nb;
	var tmp_ch;
	
	tmp_nb = glb_agent.indexOf(chaine);
	tmp_ch = glb_agent.substring(tmp_nb, glb_agent.length);
	tmp_nb = tmp_ch.indexOf(";");
	return tmp_ch.substring(dec_av_ch, tmp_nb);
}

function isole_version_netscape(chaine){
	var tmp_nb;
	var tmp_ch;
	
	tmp_nb = glb_agent.indexOf(chaine);
	tmp_ch = glb_agent.substring(tmp_nb, glb_agent.length);
	tmp_nb = tmp_ch.indexOf("/");
	return tmp_ch.substring(tmp_nb+1, tmp_ch.length);
}

function isole_version_mozilla(chaine, dec_av_ch){
	var tmp_nb;
	var tmp_ch;
	
	tmp_nb = glb_agent.indexOf(chaine);
	tmp_ch = glb_agent.substring(tmp_nb, glb_agent.length);
	tmp_nb = tmp_ch.indexOf(")");
	return tmp_ch.substring(dec_av_ch, tmp_nb);
}

function isole_version_safari(chaine, dec_av_ch){
	var tmp_nb;
	var tmp_ch;
	
	tmp_nb = glb_agent.indexOf(chaine);
	return glb_agent.substring(dec_av_ch+tmp_nb, glb_agent.length);
}

function supprime_caract_en_trop(caractere,chaine){
	var tmp_nb;
	var tmp_ch;
	
	if(chaine.indexOf(caractere) != -1){
		tmp_nb = chaine.indexOf(caractere);
		tmp_ch = chaine.substring(0, tmp_nb+1);
		chaine = chaine.substring(tmp_nb+1, chaine.length);
	
		while(1){
			if(chaine.indexOf(caractere) != -1){
				tmp_ch += chaine.substring(0, chaine.indexOf(caractere));
				chaine = chaine.substring(chaine.indexOf(caractere)+1, chaine.length);
			} else {
				if(chaine.length != 0){ tmp_ch += chaine; }
				break;
			}
		}
		return tmp_ch;
	} else {
		return chaine;
	}
}

function detection_navigateur(sortie) {
	glb_agent = navigator.userAgent.toLowerCase();
	glb_version = navigator.appVersion.toLowerCase();
	
	// Detection de la plateforme
	if (glb_agent.indexOf("win") > -1) {
		glb_platform = "win";
	} else if (glb_agent.indexOf("mac") > -1) {
		glb_platform = "mac";
	} else if (glb_agent.indexOf("x11") > -1) {
		glb_platform = "linux";
	} else {
		glb_platform = "other";
	}

	// Detection d'internet explorer
	if (glb_agent.indexOf("msie") != -1 && document.all){
		is_ie = true;
		
		vers_ie = isole_version("msie", 5);
		true_vers_ie = vers_ie;
		vers_ie = supprime_caract_en_trop(".", vers_ie);
		
		if( (glb_platform == "win") && ((vers_ie < vers_ie_win) || !autorise_ie_win) ){ 
			if(sortie == "redirection"){ return false; }
		}
		else {
			if( (glb_platform == "mac") && ((vers_ie < vers_ie_mac) || !autorise_ie_mac) ){
				if(sortie == "redirection"){ return false; }
			} else {
				if(sortie == "redirection"){ return true; }
			}
		}
	}
	
	// Detection de konqueror
	if (glb_agent.indexOf("konqueror") != -1 && document.getElementById && document.getElementsByTagName && document.childNodes && document.nodeName){
		is_kq = true;
	
		vers_kq = isole_version("konqueror", 10);
		true_vers_kq = vers_kq;
		vers_kq = supprime_caract_en_trop(".", vers_kq);
		
		if( (glb_platform == "linux") && ((vers_kq < vers_kq_linux)|| !autorise_kq_linux) ){
			if(sortie == "redirection"){ return false; }
		} else {
			if(sortie == "redirection"){ return true; }
		}
	}

	// Detection de safari
	if(!is_ie && !is_kq){
		if (glb_version.indexOf("safari") != -1 && document.getElementById && document.getElementsByTagName && document.childNodes && document.hasAttributes && document.nodeName){
			is_saf = true;
			
			vers_saf = isole_version_safari("safari/", 7);
			true_vers_saf = vers_saf;
			vers_saf = supprime_caract_en_trop(".", vers_saf);
			
			if( (glb_platform == "mac") && ((vers_saf < vers_saf_mac) || !autorise_saf_mac) ){
				if(sortie == "redirection"){ return false; }
			} else {
				if(sortie == "redirection"){ return true; }
			}
		}
	}

	// Detection de mozilla
	if(!is_ie && !is_kq && !is_saf){
		if (glb_agent.indexOf("mozilla") != -1 && glb_agent.indexOf("netscape") == -1 && document.getElementById && document.getElementsByTagName && document.childNodes && document.hasAttributes && document.nodeName){
			is_mz = true;
			
			vers_mz = isole_version_mozilla("rv:", 3);
			true_vers_mz = vers_mz;
			vers_mz = supprime_caract_en_trop(".", vers_mz);
			
			if( (glb_platform == "linux") && ((vers_mz < vers_mz_linux) || !autorise_mz_linux) ){
				if(sortie == "redirection"){ return false; }
			} else { 
				if( (glb_platform == "mac") && ((vers_mz < vers_mz_mac) || !autorise_mz_mac) ){
					if(sortie == "redirection"){ return false; }
				} else { 
					if( (glb_platform == "win") && ((vers_mz < vers_mz_win) || !autorise_mz_win) ){
						if(sortie == "redirection"){ return false; }
					} else {
						if(sortie == "redirection"){ return true; }
					}
				}
			}
		}
	}
	
	// Detection de netscape
	if(!is_ie && !is_kq && !is_mz){
		if (glb_agent.indexOf("netscape") != -1 || glb_agent.indexOf("mozilla") != -1){
			is_ns = true;
		
			if(parseInt(glb_version) < 5){
				vers_ns = parseFloat(glb_version);
				true_vers_ns = vers_ns;
			} else {
				vers_ns = isole_version_netscape("netscape");
				true_vers_ns = vers_ns;
				vers_ns = supprime_caract_en_trop(".", vers_ns);
			}
			
			if( (glb_platform == "win") && ((vers_ns < vers_ns_win) || !autorise_ns_win) ){
				if(sortie == "redirection"){ return false; }
			} else {
				if( (glb_platform == "mac") && ((vers_ns < vers_ns_mac) || !autorise_ns_mac) ){
					if(sortie == "redirection"){ return false; }
				} else { 
					if( (glb_platform == "linux") && ((vers_ns < vers_ns_linux) || !autorise_ns_linux) ){
						if(sortie == "redirection"){ return false; }
					} else {
						if(sortie == "redirection"){ return true; }
					}
				}
			}
		}
	}
	
	// Ecriture du recapitulatif
	if(sortie == "recapitulatif"){
		var tretour = new Array();
		
		// Plateforme
		switch(glb_platform){
			case "win": tretour[0] = "Windows";break;
			case "mac": tretour[0] = "Mac";break;
			case "linux": tretour[0] = "Linux";break;
			case "other": tretour[0] = "Inconnue";break;
			default:break;
		}
		
		// Navigateur et version
		if(is_ie){ tretour[1] = "Internet Explorer";tretour[2] = true_vers_ie; }
		if(is_kq){ tretour[1] = "Konqueror";tretour[2] = true_vers_kq; }
		if(is_mz){ tretour[1] = "Mozilla";tretour[2] = true_vers_mz; }
		if(is_ns){ tretour[1] = "Netscape";tretour[2] = true_vers_ns; }
		if(is_saf){ tretour[1] = "Safari";tretour[2] = true_vers_saf; }
		
		// Version attendu et autorisation
		switch(glb_platform){
			case "win":
				if(is_ie){ tretour[3] = vers_ie_win; tretour[4] = autorise_ie_win; tretour[5] = ie_win_dl; tretour[6] = ie_win_other}
				if(is_ns){ tretour[3] = vers_ns_win; tretour[4] = autorise_ns_win; tretour[5] = ns_win_dl; tretour[6] = ns_win_other}
				if(is_mz){ tretour[3] = vers_mz_win; tretour[4] = autorise_mz_win; tretour[5] = mz_win_dl; tretour[6] = mz_win_other}
				break;
			case "mac":
				if(is_ie){ tretour[3] = vers_ie_mac; tretour[4] = autorise_ie_mac; tretour[5] = ie_mac_dl; tretour[6] = ie_mac_other}
				if(is_ns){ tretour[3] = vers_ns_mac; tretour[4] = autorise_ns_mac; tretour[5] = ns_mac_dl; tretour[6] = ns_mac_other}
				if(is_mz){ tretour[3] = vers_mz_mac; tretour[4] = autorise_mz_mac; tretour[5] = mz_mac_dl; tretour[6] = mz_mac_other}
				if(is_saf){ tretour[3] = vers_saf_mac; tretour[4] = autorise_saf_mac; tretour[5] = saf_mac_dl; tretour[6] = saf_mac_other}
				break;
			case "linux":
				if(is_kq){ tretour[3] = vers_kq_linux; tretour[4] = autorise_kz_linux; tretour[5] = kz_linux_dl; tretour[6] = kz_linux_other}
				if(is_ns){ tretour[3] = vers_ns_linux; tretour[4] = autorise_ns_linux; tretour[5] = ns_linux_dl; tretour[6] = ns_linux_other}
				if(is_mz){ tretour[3] = vers_mz_linux; tretour[4] = autorise_mz_linux; tretour[5] = mz_linux_dl; tretour[6] = mz_linux_other}
				break;
			default:break;
		}
		
		return tretour;
	} else {
		return true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
