var gTabbableTags=new Array("a","button","textarea","input","iframe");
var gTabIndexes=new Array();
var gPopupMask=null;
var gPopupContainer=null;
var gPopFrame=null;
var gPopTitleBar=null;
var gReturnFunc;
var gPopupIsShown=false;
var gHideSelects=true;

function montrePopup(url,containerW,containerH,frameW,frameH,returnFunc,templateID){
	if(containerW<frameW) containerW=frameW;
	if(containerH<frameH) containerH=frameH;
	
	gPopupMask=document.getElementById("popupMasque" + templateID);
	gPopupContainer=document.getElementById("popupConteneur" + templateID);
	gPopFrame=document.getElementById("popupFrame" + templateID);
	gPopTitleBar=document.getElementById("PopupTitleBar" + templateID);
	
	//on vérifie la version de IE pour masque les select
	var brsVersion=parseInt(window.navigator.appVersion.charAt(0), 10);
	if(brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1){
		gHideSelects=true;
	}
	gPopupIsShown=true;
	disableTabIndexes();
	//gPopupMask.style.display="block";
	gPopupContainer.style.display="block";
	//on calcul l'emplacement de la fenêtre
	centerPopWin(containerW,containerH);
	
	gPopupContainer.style.width=containerW+"px";

	if(gPopTitleBar != null){
		var titleBarHeight=parseInt(gPopTitleBar.offsetHeight, 10);
		gPopupContainer.style.height=(containerH+titleBarHeight) + "px";
		gPopFrame.style.width=parseInt(gPopTitleBar.offsetWidth, 10) + "px";
	}
	else{
		gPopupContainer.style.height=containerH+"px";
		gPopFrame.style.width=frameW+"px";
	}
	
	gPopFrame.style.height=frameH+"px";
	
	// set the url
	if(url != '') gPopFrame.src=url;
	
	gReturnFunc=returnFunc;
	// for IE
	if(gHideSelects == true){
		hideSelectBoxes();
	}
}

function cachePopup(callReturnFunc,templateID){
	gPopupMask=document.getElementById("popupMasque" + templateID);
	gPopupContainer=document.getElementById("popupConteneur" + templateID);
	gPopFrame=document.getElementById("popupFrame" + templateID);
	
	gPopupIsShown=false;
	//restoreTabIndexes();
	if(gPopupMask == null){
		return;
	}
	gPopupMask.style.display="none";
	gPopupContainer.style.display="none";
	if(callReturnFunc == true && gReturnFunc != null){
		gReturnFunc(window.frames["popupFrame" + templateID].returnVal);
	}
	//gPopFrame.src='/blank.htm';
	// display all select boxes
	if(gHideSelects == true){
		displaySelectBoxes();
	}
}

//Pour IE
function disableTabIndexes(){
	if(document.all){
		var i=0;
		for(var j=0; j < gTabbableTags.length; j++){
			var tagElements=document.getElementsByTagName(gTabbableTags[j]);
			for(var k=0 ; k < tagElements.length; k++){
				gTabIndexes[i]=tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

//Pour IE
function restoreTabIndexes(){
	if(document.all){
		var i=0;
		for(var j=0; j < gTabbableTags.length; j++){
			var tagElements=document.getElementsByTagName(gTabbableTags[j]);
			for(var k=0 ; k < tagElements.length; k++){
				gTabIndexes[i]=tagElements[k].tabIndex;
				tagElements[k].tabIndex="1";
				i++;
			}
		}
	}
}

var gi=0;
function centerPopWin(width,height){
	if(gPopupIsShown == true){
		if(width == null || isNaN(width)){
			width=gPopupContainer.offsetWidth;
		}
		if(height == null){
			height=gPopupContainer.offsetHeight;
		}
		
		var fullHeight=getViewportHeight();
		var fullWidth=getViewportWidth();
		
		var theBody=document.documentElement;
		
		var scTop
		if(document.documentElement && document.documentElement.scrollTop){
			scTop=parseInt(theBody.scrollTop,10);
		}
		else if(document.body){
			scTop=parseInt(document.body.scrollTop,10)
		}
		else{
			scTop=parseInt(theBody.scrollTop,10);
		}

		var scLeft=parseInt(theBody.scrollLeft,10);
		
		gPopupMask.style.height=fullHeight + "px";
		gPopupMask.style.width=fullWidth + "px";
		gPopupMask.style.top=scTop + "px";
		gPopupMask.style.left=scLeft + "px";
		
		var titleBarHeight=0;
		
		if(gPopTitleBar != null) titleBarHeight=parseInt(gPopTitleBar.offsetHeight, 10);
		
		gPopupContainer.style.top=(scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left=(scLeft + ((fullWidth - width) / 2)) + "px";
	}
}
addEvent(window,"resize",centerPopWin);
window.onscroll=centerPopWin;

function hideSelectBoxes(){
	for(var i=0; i < document.forms.length; i++){
		for(var e=0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT"){
				document.forms[i].elements[e].style.visibility="hidden";
			}
		}
	}
}

function displaySelectBoxes(){
	for(var i=0; i < document.forms.length; i++){
		for(var e=0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT"){
			document.forms[i].elements[e].style.visibility="visible";
			}
		}
	}
}

function addEvent(obj, evType, fn){
	if(obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if(obj.attachEvent){
		var r=obj.attachEvent("on"+evType, fn);
		return r;
	}
	else{
		return false;
	}
}

function getViewportHeight(){
	if(window.innerHeight!=window.undefined) return window.innerHeight;
	if(document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if(document.body) return document.body.clientHeight;
	return window.undefined;
}
function getViewportWidth(){
	if(window.innerWidth!=window.undefined) return window.innerWidth;
	if(document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if(document.body) return document.body.clientWidth;
	return window.undefined;
}