﻿// Fonction pour générer un pop-up centré
	
	var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;	// Detecting IE
    
	function PopupFullscreen(page, target, ratioX, ratioY, options) {	
	    // On peut utiliser les paramètres ratioX et ratioY pour limiter le plein écran au ratio spécifié
	    var w = screen.availWidth ? screen.availWidth : screen.width; // On utilise la propriété availWidth si possible
	    var h = screen.availHeight ? screen.availHeight : screen.height; // On utilise la propriété availWidth si possible
	    var correctX = 0;
	    var correctY = 0;
		
	    var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;	// Detecting IE
	    if(isIE) { w -= 8; h -= 34; } // Ajustement de la taille, pour IE (wow!!)
		
	    if(isNaN(ratioX) && (String(options) == "undefined" || String(options) == "null")) {
		    // Pas de ratio, seulement les options sont passées
		    options = String(ratioX);
	    } else {
		    if(!isNaN(ratioX) && !isNaN(ratioY)){
			    ratioX = Number(ratioX);
			    ratioY = Number(ratioY);
			    var newW = w;
			    var newH = h;
			    // On a un ratio, on calcul
			    newH = (ratioY * w) / ratioX;
			    if(newH > h){
				    // trop grand, on calcul selon la hauteur
				    newH = h;
				    newW = ((h-58) * ratioX) / ratioY;
			    }
			    if(newW < w) correctX = (w - newW) / 2;
			    if(newH < h) correctY = ((h - newH) / 2) - 30;
			    w = newW;
			    h = newH;
		    }
	    }
	   	
	   	
	    // Traitement des options (nettoyage si aucune)
	    if(String(options) == "undefined" || String(options) == "null") options = "";
	    else options = "," + options;
		
	    var newWin = window.open(page,target,"top=" + correctY + ",left=" + correctX + ",width=" + w + ",height=" + h + options);
	    if(newWin == null){
		    // Bloquée
		    if(langString == "en") alert("Error: You must autorize the site to execute such script.\n\nTo do so, simply click on the yellow bar, upper on this page, and click on \"Autorize blocked content...\".");
		    else alert("Erreur: Vous devez autoriser le site à éxécuter ce script.\n\nPour ce faire cliquez sur la bande jaune, plus haut sur cette page, et cliquez sur \"Autoriser le contenu bloqué...\".");
	    } else newWin.focus();
	    return newWin;
    }
   
  
     
	function openViaWin(UserId, SessionID, url, forceGMode){
			// G mode
			if(forceGMode || Key.isDown(71)) {
				var myForm = document.forms[0];
				if(myForm) {
					var newE = document.createElement("input");
						 newE.type = "hidden";
						 newE.name = "gAccess";
						 newE.id = "gAccess";
						 newE.value = "1";
					if(!myForm.appendChild(newE)) alert("Warning: Failed to launch the G mode (info: " + myForm + "/" + newE + ")");
				} else {
					alert("Warning: Failed to launch the G mode (info: Form object not found)");
				}
			}
			if(Key.isDown(70)) {
				// Fullscreen
				var newWin = window.open( url + "Application/ViaMain.aspx?sessionID=" + urlencode(SessionID), "via" + UserId ,"fullscreen=1,resizable=0,status=0,scrollbars=0,titlebar=0,toolbar=0,location=0,menubar=0");
			} else {
				// Standard
				var newWin = PopupFullscreen(url + "Application/ViaMain.aspx?sessionID=" + urlencode(SessionID), "via" + UserId ,"resizable=1,status=1,scrollbars=0,titlebar=0,toolbar=0,location=0,menubar=0");
			}
	}


	function encodeURL(url) {
		var TempQuery;
		var Query = "";
		var FinalQuery = "";		
		
		//si il y au moin un parametre
		if (url.indexOf('?') != -1){	
			TempQuery = url.split('?');
			FinalQuery = TempQuery[0] + '?';
			Query = TempQuery[1];
				
			//plusieur parametre	
			if (Query.indexOf('&') != -1){
				param = Query.split('&');	
				
				for (i=0; i<param.length; i++) {
				
					tmpVal =  param[i].split('=');
					FinalQuery += "&" + tmpVal[0] + "=" + urlencode(tmpVal[1]);
				}									 
					//un seul parametre		  			 
				 } else {		    
					 
					 param = Query.split('=');					
					 FinalQuery += param[0] + "=" + urlencode(param[1]);				    
			}
			return FinalQuery;
		}
		return url;
	}

	function urlencode(str) {
		//str = escape(str);				
		str = str.replace(/\+/g,"%2B");
		str = str.replace(/\*/g,"%2A");
		str = str.replace(/\//g,"%2F");
		str = str.replace(/\@/g,"%40");
		
		return str;
	}
   
   
    function PopupCenter(pageURL, target, largeur, hauteur, langString, options, returnWin) {
		//alert("PopupCenter: " + pageURL + ", " + target + ", " + largeur + ", " + hauteur + ", " + langString + ", '" + options + "', " + returnWin);
		if(pageURL == null) return null;	
			
		pageURL = encodeURL(pageURL);	
			 
		if(target == null) target = "";
		
		var screenWidth = screen.availWidth ? screen.availWidth : screen.width;
		var screenHeight = screen.availHeight ? screen.availHeight : screen.height;
		var top = 0; var left = 0;
		var deltaL = 0; var deltaH = 0;
		
		if(largeur == null) {
			largeur = screenWidth;
			if(isIE) deltaL = 12;
		} else {
			left = (screenWidth - largeur) / 2;
		}
		
		if(hauteur == null) {
			hauteur = screenHeight;
			if(isIE) deltaH = 38;
		} else {
			top = (screenHeight - hauteur) / 2;
		}
		
		var newWin = window.open(pageURL, target, "top=" + top + ", left=" + left + ", width=" + (largeur-deltaL) + ", height=" + (hauteur-deltaH) + ", " + options);
		if(newWin == null){ // Bloquée par un popup bloquer
			if(langString == "en") alert("Error: You must autorize the site to execute such script.\n\nTo do so, simply click on the yellow bar, upper on this page, and click on \"Autorize blocked content...\".");
			else alert("Erreur: Vous devez autoriser le site à éxécuter ce script.\n\nPour ce faire cliquez sur la bande jaune, plus haut sur cette page, et cliquez sur \"Autoriser le contenu bloqué...\".");
		}
		newWin.focus();
		if(returnWin != false) return newWin;
	}
	

	
	
	
	
// ---- KEY management
	// KeyLib v1 (http://www.n-son.com/scripts/keyLib/)
	// - Reduced version - (removed addListeners, onkeydown and onkeyup management)
	var Key = { 
		_keys:[], LEFT:37, RIGHT:39, UP:38, DOWN:40, BACKSPACE:8, CAPSLOCK:20, CONTROL:17, DELETEKEY:46, 
		END:35, ENTER:13, ESCAPE:27, HOME:36, INSERT:45, TAB:9, PGDN:34, PGUP:33, SPACE:32, SHIFT:16
	};

	Key.isDown = function (key) {
		return Boolean(this._keys[key]);
	};

	Key.init = function () {
		document.onkeydown = function (e) {
			e = e?e:event;
			Key._keys[e.keyCode] = true;
		};
		document.onkeyup = function (e) {
			e = e?e:event;
			Key._keys[e.keyCode] = false;
		};
		for (var num = 0; num < 256; num++) {
			this._keys[num] = false;
		}
	};

	Key.init();