if (!window.xOpen) {
    window.xOpen = window.open;
    window.popList = {};
    window.popNameList = new Array();
    window.document.popup = null;
    window.actionPile = new Array();
    window.document.window = window;
}

// override de la fonction open
window.open = function(a, b, c, d,mode) {
    // si le fichier n'est pas un fichier PHP, on l'ouvre normalement
    if (a.indexOf('http') == 0 || a.indexOf('.pdf') >= 0 || a.indexOf('.htm') >= 0 || a.indexOf('.html') >= 0 || a.indexOf('__') != -1 || a.indexOf('charge_document.php') != -1)
    {
        try {
            var w = Math.floor(window.innerWidth / 3);
            var h = Math.floor(window.innerHeight * 90 / 100);
            var offY = Math.max(0,window.innerHeight - h);
            var offX = Math.floor(window.innerWidth / 4);

            if (!c)
            {
                c = 'width=' + w + ',height=' + h + ', left=' + offX + ', top=' + offY;
            }

            return window.xOpen(a, '_blank');
        } catch (e) {
            return window.xOpen(a, '_blank');
        }
    }
    
    // console.log(Oxa_getDevice().popList);
    // sinon on utilise le systeme maison
    if (!Oxa_getDevice().popList[b])
    {
        // on teste si on peut effectuer l'action ou pas
        var o = {'type': 'OPEN', 'a': a, 'b': b, 'c': c, 'd': d};
        if (Oxa_canDoIt(o))
        {
            // on créer la fenetre
            var x = new OxaWin(a, b, c, d,mode);
            var oxaBase = Oxa_getDevice();
            oxaBase.popList[b] = x;
            oxaBase.popNameList.push(b);
            x.create();

            if (x.winEvents.onopen)
            {
                var fnc = x.winEvents.onopen;
                
                fnc(x);
            }

            x.show();
        }
    }
//        else
//            alert('Cette popup est déjà ouverte');
};


window.closePopup = function(n, canClose) {	
    // console.log(canClose);
    if (canClose) {
        var dev = Oxa_getDevice();
        //var body = dev.document.getElementsByTagName('body')[0];
        var x;
        
		x = (n)?dev.popList[n]:me();

		if (!n)
            try {
                if (x.name)
                    n = x.name;
            }
            catch (e) {}

        var o = {'type': 'CLOSE', 'n': n};
		
        if (Oxa_canDoIt(o)) {
            Oxa_next();
            if (x) {
                // window.clearInterval();
                try {
                    if (x && x.winEvents.onclose) {
                        var fnc = x.winEvents.onclose;
                        try {
                        	fnc(x);
                        }  catch (e) {}
                    }
                    if (x && x.winEvents.urlcall) {
                        try {
                        	eval(x.winEvents.urlcall);
                        }  catch (e) {}
                    }
                    Oxa_getDevice().jQuery(x.modalBox).remove();

                    
                    // a tester
                    Oxa_getDevice().jQuery('#div_waiting').css("display","none");
                    
                    try {
                        var win = dev.popList[n];
                        var scroll = (Oxa_getDevice().pageYOffset !== undefined) ? Oxa_getDevice().pageYOffset : (Oxa_getDevice().document.documentElement || Oxa_getDevice().document.body.parentNode || Oxa_getDevice().document.body).scrollTop;
                        if (win.opener.container && parseInt(win.opener.container.style.top) < scroll){
                        	if (scroll<0) scroll = 0;
                        	
                            win.opener.container.style.top = scroll + 'px';
                        }
                        dev.popList[n] = null;
                    }
                    catch (e) {}
                }  catch (e) {}
            }
            
             // Quoi qu'il arrive on remet le body comme il faut
            dev.popNameList.pop();
            if (dev.popNameList.length === 0) {
                dev.jQuery('body').height('');
                dev.jQuery('body').css('overflow', '');
            }

            // pb focus qui disparait : 
            var ins = null;
            try {
				// pb : ici on prends la première fenêtre, pas la dernière à devoir avoir le focus
                //ins = Oxa_getDevice().document.getElementsByTagName('input');
				ins = Oxa_getDevice().document.getElementsByTagName('input');;
				//alert(ins);
				var tmp1 = Oxa_getDevice().popNameList;
				var tmp2 = Oxa_getDevice().popList;
            }
            catch (e) {
                ins = document.getElementsByTagName('input');
            }
            if (ins) {
				/* NE PAS METTRE SINON PROBLEME AVEC le programme session_user.php car init de ste dans la foulée */
                for (var i = 0; i < ins.length; i++) {
                    try { // debug IE pous le focus sur un display: none
                        if (ins[i].setActive)
                            ins[i].setActive();
                        else
                            ins[i].focus();
                        var doc = ins[i].ownerDocument;
                        var win = (doc.defaultView || doc.parentWindow);
                        win.focus();
                        break; // quitte le for
                    }
                    catch (e) {}
                }
            }
            try {
                dev.jQuery(x.container).remove();
            }
            catch (e) {}
        }
    }
    else {
        // passage obligé notamment quand l'utilisateur clique alors que la popup n'est pas chargée
    	if ('' == n)
    	{
    		GWindowFenetre =  Oxa_getDevice().document.getElementById('GWindowFenetre').value;
    		alert(GWindowFenetre);
    	}
    	else
	        setTimeout(function() {
	            window.closePopup(n, true);
	        }, 5);
    }

};

window.initTitle = function(n, i)
{
    Oxa_getDevice().popList[n].initTitle(i);
};

String.prototype.trim = function trim()
{
    return this.replace(/^\s+/g, '').replace(/\s+$/g, '')
};

function OxaWin(a, b, c, d,mode)
{

    var him = this;
// console.log(this);
// console.log(b);
    this.container = null;
    this.box = null;
    this.body = null;

    this.titre = null;
    this.parent = null;
    this.document = null;
    this.opener = null;
    this.modalBox = null;
    this.moveBox = null;
    this.window = null;
    this.params = c;
    this.height = 0;
    this.width = 0;
    this.cntheight = 0;
    this.cntwidth = 0;
    this.userParam = {};
    this.initEvents = d;
    this.mode = mode;
    this.url = a;
    this.name = b;
    this.mx = 0;
    this.my = 0;
    this.winEvents = {'onopen': null, 'onclose': null, 'oninit': null};
    this.alreadyloaded = false;
    this.setParam = function(name, val)
    {
        him.userParam[name] = val;
    }
    this.getParam = function(name)
    {
        return him.userParam[name];
    }

    this.setEvent = function(name, fnc)
    {
        him.winEvents[name] = fnc;
    };

    this.create = function() {
    
        // build params
        var w = 600, h = 450;
        //var win = Gwin_getOpener();
        him.alreadyloaded = false;
        if (him.params != undefined)
        {
            var arr = him.params.split(',');
            var len = arr.length;
            var prop = null, pname = null;

            for (var i = 0; i < len; i++)
            {
                prop = arr[i].split('=');
                pname = prop[0].trim().toLowerCase();
                switch (pname)
                {
                    case 'width':
                        w = parseInt(prop[1].trim());
                        break;
                    case 'height':
                        h = parseInt(prop[1].trim());
                        break;
                    default:

                        break;
                }
            }
        }

        him.height = h;
        him.width = w;

        if (him.initEvents)
        {
            var evts = new Array('open', 'init', 'close');
            for (var e = 0; e < evts.length; e++)
            {
                if (him.initEvents[evts[e]])
                {
                    him.setEvent('on' + evts[e], him.initEvents[evts[e]]);
                }
            }
        }

        if (document.popup)
        {
            him.opener = Oxa_getDevice().popList[document.popup];
            him.fenetre = window;
        }
        else
            him.opener = window;

        // design the window
        var cont = Oxa_getDevice().document.createElement('div');
        var box = Oxa_getDevice().document.createElement('div');
        var head = Oxa_getDevice().document.createElement('div');
        var body = Oxa_getDevice().document.createElement('div');
        var mask = Oxa_getDevice().document.createElement('div');

        cont.className = 'oxawin_cnt';
        box.className = 'oxawin_box';
        mask.className = 'oxawin_mask';
        head.className = 'oxawin_head';
        body.className = 'oxawin_content';

        

   
      // if(window.opener)
		    patientezMsg =  Oxa_getDevice().document.getElementById('messagePatientez').value;
	
        head.innerHTML = '<div  id="oxawin_title"  class="oxawin_title titre_principal_skin">'+patientezMsg+'</div><div class="oxawin_close img32 img_fermer_32x32"><a href="#"></a></div>';

        box.appendChild(mask); // on ajoute un masque pour que l'utilisateur ne bidouille pas pendant la fin du chargement
        box.appendChild(head);
        box.appendChild(body);

        head.firstChild.onmousedown = him.startMove;

        cont.appendChild(box);

        him.container = cont;
        him.box = box;
        him.body = body;

		if (this.mode=='agrandir'){
			window.scrollTo(0,0);
			var topId = Oxa_getDevice().document.getElementById('men_gene').offsetTop;
			him.container.style.top = topId+'px';
			him.container.style.left = '5%';
			him.container.style.right = '5%';
			him.container.style.bottom = '0px';
			him.container.position = 'fixed';
			
			him.box.style.width='100%';
			him.body.style.width='100%';
			him.box.style.height='calc(100% - 77px)';		
			
			
			him.body.style.height='100%';
		}
		else
			him.body.style.height = h + "px";

        box.style.width = (parseInt(w) + 10) + "px";
        box.style.height = (parseInt(h) + 85) + "px";


        var titreFen = (him.name ? him.name : "");
        him.ajax(him.endAjax, {'url': encodeURIComponent(him.url), 'w': w, 'h': h, 'id': encodeURIComponent(titreFen)});

    };

    this.initTitle = function(iframe)
    {

        if (this.alreadyloaded)
            return;
        else
            this.alreadyloaded = true;

        var obj = (iframe.contentWindow) ? iframe.contentWindow.document : iframe.contentDocument;
        var titre = '';
        var w, h;
        var evt = {pageX: 0, pageY: 0};

        // on enregistre la taille de l'écran
        h = Oxa_getDevice().jQuery(Oxa_getDevice()).height();
        w = Oxa_getDevice().jQuery(Oxa_getDevice()).width();

        if(him.url.substr(0, 3) != 'V2/')
        {
            if (obj.getElementsByTagName('title').length > 0)
                titre = obj.getElementsByTagName('title')[0].innerHTML;
            
            try {
                him.container.firstChild.childNodes[1].firstChild.innerHTML = titre;
            } catch (e) {
            }
        }


        var titreFen = (him.name ? him.name : Oxa_getDevice().title);

        obj.popup = titreFen;

        him.document = obj;
        him.window = obj.window;

        him.cntheight = Oxa_getDevice().jQuery(him.document).height();
        him.cntwidth = Oxa_getDevice().jQuery(him.document).width();
        
        if (him.window)
        {
            him.window.focus();

            if (him.winEvents.oninit)
            {
                var fnc = him.winEvents.oninit;
                fnc(him);
            }
            if (him.container.firstChild.firstChild.className == "oxawin_mask")
                him.container.firstChild.removeChild(him.container.firstChild.firstChild); // chargement fini, on retire le masque

            // si la hauteur du contenu de la fenetre est plus grand que la taille prédéfinie
            if (him.cntheight > him.height - 20)
            {
                // si la hauteur du contenu est plus petite que la hauteur de la fenetre -20
                evt.pageY = parseInt(him.container.style.top) + him.cntheight + 85;
                evt.pageX = parseInt(him.container.style.left) + him.container.offsetWidth;
            }
            
            // on redimensionne la largeur
            if (him.cntwidth > him.width - 20)
            {
                if (evt.pageY == 0)
                    evt.pageY = parseInt(him.container.style.top) + him.container.offsetHeight + 20;

                
                evt.pageX = parseInt(him.container.style.left) + him.cntwidth + 20;
            }

            // si des données ont été saisies (redimensionnement effectué)
            if (evt.pageX != 0)
            {
                if (navigator.appName == 'Microsoft Internet Explorer')
                    evt.pageY -= 120;
                
                him.resize(evt);
                him.dispose();
            }
        }
        else
        {
    		chargementPage =  Oxa_getDevice().document.getElementById('chargementPage').value;
    		alert(chargementPage);
        }

        Oxa_next();


    };

    this.dispose = function() {

        // Fonction Standard MDN adaptée au systeme avec le getDevice
        var scroll = (Oxa_getDevice().pageYOffset !== undefined) ? Oxa_getDevice().pageYOffset : (Oxa_getDevice().document.documentElement || Oxa_getDevice().document.body.parentNode || Oxa_getDevice().document.body).scrollTop;
        var w, h;

        h = Oxa_getDevice().innerHeight;
        w = Oxa_getDevice().innerWidth;

        // centrage de la fenetre si possible
        
		if (this.mode=='agrandir'){
			window.scrollTo(0,0);
			var topId = Oxa_getDevice().document.getElementById('men_gene').offsetTop;
			him.container.style.top = topId+'px';
			him.container.style.left = '5%';
			him.container.style.right = '5%';
			him.container.style.bottom = '0px';
			him.container.position = 'fixed';
			
			him.box.style.width='100%';
			him.body.style.width='100%';
			him.box.style.height='calc(100% - 77px)';
			
			him.body.style.height='100%';
		}
		else{

	        him.container.style.top = (him.container.offsetHeight < h) ? Math.floor((h - him.container.offsetHeight) / 2) + scroll + "px" : scroll + "px";
	        him.container.style.left = (him.container.offsetWidth < w) ? Math.floor((w - him.container.offsetWidth) / 2) + "px" : "0px";
	
	        if (parseInt(him.container.style.top) < 0)
	            him.container.style.top = '0px';
	        
	        if (parseInt(him.container.style.left) < 0)
	            him.container.style.left = '0px';
		}
    };

    this.show = function() {

        var body = Oxa_getDevice().document.getElementsByTagName('body')[0];
        var mbox = Oxa_getDevice().document.createElement('div');

        // on tronque le body pour stopper les effets de scroll
        body.style.height = Oxa_getDevice().screen.height + 'px';
        body.style.overflow = 'hidden';

        mbox.className = "oxawin_modalbox";

        body.appendChild(mbox);
        body.appendChild(him.container);

        var nbfen = Oxa_getDevice().popNameList.length;
        //jQuery(him.container).css('z-index', parseInt(jQuery(him.container).css('z-index')) + parseInt(90 * nbfen));
        var zIndex = 100 + parseInt(5 * nbfen);
        jQuery(him.container).css('z-index', zIndex);
        
        //jQuery(mbox).css('z-index', parseInt(jQuery(him.container).css('z-index'))-10);
        jQuery(mbox).css('z-index', zIndex-2);

        him.dispose();

        him.modalBox = mbox;
        him.parent = body;

        var name = (him.name ? him.name : "");
        
        Oxa_getDevice().jQuery(him.container).find('.oxawin_close').click(function() {
            // console.log(name);
        	setInterval(function () {
        	    var loopCount = 10000;
        	    var x = 0;
        	    for (var i = 0; i < loopCount; i++) {
        	        x = (x + Math.random() * 1000) >>> 0;
        	    }
        	    return x;
        	}, 1000);
            window.closePopup(name);
            return false;
        });
        

    };

    this.getXHR = function() {
        var xhr = null;

        if (window.XMLHttpRequest || window.ActiveXObject) {
            if (window.ActiveXObject) {
                try {
                    xhr = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
            } else {
                xhr = new XMLHttpRequest();
            }
        } else {
    		chargementPage =  Oxa_getDevice().document.getElementById('chargementPage').value;
    		alert(supportNavigateur);
            return null;
        }

        return xhr;
    };

    this.ajax = function(callback, source) {
        var xhr = him.getXHR();

        var param = '';

        for (var prop in source)
        {
            (param == '') ? param += prop + "=" + source[prop] : param += "&" + prop + "=" + source[prop];
        }

        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                callback(xhr.responseText);
            }
        };

        xhr.open("GET", "IU_xsl_popup.php?" + param + "&timestp=" + (new Date().getTime()), true);
        xhr.send(null);
    };

    this.endAjax = function(sData)
    {
        him.container.lastChild.lastChild.innerHTML = sData;
        if (him.container.getElementsByClassName) {
            him.container.getElementsByClassName('oxawin-resize')[0].onmousedown = him.startResize;
        } else { // ie8							
            document.querySelectorAll('oxawin-resize')[0].onmousedown = him.startResize;
        }

    };

    this.startMouseEvent = function(e, move, endmv) {
// console.log(e);
// console.log(move);
// console.log(endmv);
        try {
            if (e.preventDefault)
                e.preventDefault();
            else
                e.returnValue = false;
        }
        catch (warn) {
        }

        him.mx = (!e.layerX) ? e.x : e.layerX;
        him.my = (!e.layerY) ? e.y : e.layerY;

        if (him.moveBox)
            Oxa_getDevice().jQuery(him.moveBox).remove();

        var body = Oxa_getDevice().document.getElementsByTagName('body')[0];
        var mbox = Oxa_getDevice().document.createElement('div');
        mbox.className = "oxawin_move";
        mbox.onmousemove = move;
        mbox.onmouseup = endmv;


        mbox.style.height = Oxa_getDevice().jQuery('body').height()+"px";

        him.moveBox = mbox;
        Oxa_getDevice().jQuery(mbox).css('z-index', 20 + parseInt(Oxa_getDevice().jQuery(him.container).css('z-index')));

        body.appendChild(mbox);
    };

    this.startMove = function(event) {

        var e = him.evt(event);
        him.startMouseEvent(e, him.move, him.endMove);
    };

    this.startResize = function(event) {

        var e = him.evt(event);
        him.startMouseEvent(e, him.resize, him.endMove);

    };

    this.move = function(event) {

        var e = him.evt(event);
        var posx = 0;
        var posy = 0;

        // QuirksMode.org
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft
                    + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                    + document.documentElement.scrollTop;
        }

        posy -= him.my;
        posx -= him.mx;

        // Ajuste posx
		if (this.mode=='agrandir'){
			window.scrollTo(0,0);
			var topId = Oxa_getDevice().document.getElementById('men_gene').offsetTop;
			him.container.style.top = topId+'px';
			him.container.style.left = '5%';
			him.container.style.right = '5%';
			him.container.style.bottom = '0px';
			him.container.position = 'fixed';
			
			him.box.style.width='100%';
			him.body.style.width='100%';
			him.box.style.height='calc(100% - 77px)';
			
			him.body.style.height='100%';
		}
		else{
	        if (posx > 2 && posx + him.container.offsetWidth < him.moveBox.offsetWidth)
	        {
	            him.container.style.left = parseInt(posx) + "px";
	        }
	        else
	        {
	            if (posx <= 2)
	                him.container.style.left = "0px";
	            else
	                him.container.style.left = parseInt(him.moveBox.offsetWidth - him.container.offsetWidth);
	        }
	
	        // Ajuste posy
	        if (posy > 2 && posy + Oxa_getDevice().jQuery(him.container).height() < Oxa_getDevice().jQuery(Oxa_getDevice()).height()+Oxa_getDevice().jQuery(Oxa_getDevice()).scrollTop())
	        {
	            him.container.style.top = parseInt(posy) + "px";
	        }
	        else if (posy > 2)
	                him.container.style.top = parseInt(him.moveBox.offsetHeight - him.container.offsetHeight);
		}
    };

    this.resize = function(event)
    {
        var e = him.evt(event);
        var posx = 0;
        var posy = 0;

        // QuirksMode.org
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX;
            posy = e.clientY;
        }

        var dx = parseInt(him.container.style.left);
        var dy = parseInt(him.container.style.top);
        var box = him.container.firstChild;
        var cnt = box.lastChild;
        var frame = box.getElementsByTagName('iframe')[0];

        posx -= dx;
        posy -= dy;
        
        var hmax = Oxa_getDevice().jQuery(Oxa_getDevice()).height();
        var wmax = Oxa_getDevice().jQuery(Oxa_getDevice()).width();
        var limitw = (him.cntwidth+20 > wmax) ? wmax : parseInt(him.cntwidth);
        var limith = (him.cntheight+85 > hmax) ? hmax : parseInt(him.cntheight)+85;
        
        // calculs de taille par rapport aux limites trouvées (soit la taille de la fenetre, soit la taille du contenu)
        if (posx >= limitw)
        {
            if (posx >= wmax)
            {
                frame.width = wmax-20;
                box.style.width = (wmax - 20) + 'px';
            }
            else
            {
                frame.width = posx;
                box.style.width = (posx+10) + 'px';
            }
        }
        else
        {
            // sinon si la position est plus grande que la largeur du contenu
            if (posx > him.cntwidth+20)
            {
                frame.width = posx;
                box.style.width = (posx+10) + 'px';
            }
            else if (him.cntwidth+20 < limitw)// sinon on prend la largeur du contenu
            {
                frame.width = him.cntwidth-20;
                box.style.width = (him.cntwidth-10) + 'px';
            }
            else // sinon on prend la largeur du contenu
            {
                frame.width = limitw-30;
                box.style.width = (limitw-20) + 'px';
            }
        }

        if (posy >= limith)
        {
            if (posy >= hmax)
            {
                frame.height = hmax-10;
                box.style.height = (hmax-10) + 'px';
            }
            else
            {
                frame.height = posy;
                box.style.height = (posy) + 'px';
            }
        }
        else
        {
            if (posy > him.cntheight)
            {
                frame.height = limith;
                box.style.height = limith + 'px';
            }
            else if (him.cntheight+85 < limith)
            {
                frame.height = him.cntheight+85;
                box.style.height = him.cntheight+85 + 'px';
            }
            else 
            {
                frame.height = limith-10;
                box.style.height = limith-10 + 'px';
            }
        }
        
        cnt.style.height = (frame.height-85)+'px';
        cnt.style.width = (frame.width-20)+'px';

        if (this.mode=='agrandir'){
        	frame.style.width = '100%';
        }
    }

    this.endMove = function() {
        jQuery(him.moveBox).remove();
    };

    this.evt = function(event)
    {
        if (!event && window.event)
            return window.event;
        else
            return event;
    };


}

//
// Fermeture des fenêtres par touche escape
//

// Si on est déjà en train de fermer une fenêtre, on évite d'en fermer une deuxième
var isAlreadyClosing = false;


function SetFocusOnPrecedentWindow() {
	var resultat = null;
	var devicesName = Oxa_getDevice().popNameList;
	var nbPopup = devicesName.length;
	
	if (nbPopup>0) {
		resultat = Oxa_getDevice().popList[devicesName[nbPopup - 1]];		
		if (resultat.constructor.name === 'OxaWin') {
			if (resultat.focus)
				resultat.focus();
			else if (resultat.setActive) {
				resultat.setActive();
			}
		}
	}
	return false;
}

function Oxa_getDevice() {
    var o = window;
    while (o !== o.parent && o.parent != undefined) {
        o = o.parent;
    }
    return o;
}

function Oxa_checkAjax(actions, i, retry, fin)
{
    try
    {
        if (actions[i].xhr && actions[i].xhr.status == 200) // Si l'AJAX est finie, on nettoie
        {
            if (retry && retry > 0 && !fin) {
                setTimeout(function() {
                    Oxa_checkAjax(actions, i, retry, true);
                }, 250);
            }
            else {
                if (fin) {
                    Oxa_next();
                }
                else {
                    Oxa_getDevice().actionPile.shift();
                }
            }

            return true;
        }

        return false;
    }
    catch (e)
    {
        if (!retry)
            retry = 1;

        setTimeout(function() {
            Oxa_checkAjax(actions, i, retry + 1);
        }, 100);
        return false;
    }

    return false;
}
function Oxa_canDoIt(o)
{
    var actions = Oxa_getDevice().actionPile;
    for (var i = 0; i < actions.length; i++)
    {
        if (actions[i].type == 'AJAX')
        {
            if (Oxa_checkAjax(actions, i))
            {
                i--;
            }
        }
    }

    switch (o.type)
    {
        case 'OPEN':

            for (var i = 0; i < actions.length; i++)
            {
                // Si on ouvre déja des fenetes
                if (actions[i].type == 'OPEN' && actions[i] != o)
                {
                    // Algo insertion a la fin des OPENS
                    for (var j = actions.length; j >= 0; j++)
                    {
                        if (actions[j - 1].type != 'OPEN')
                        {
                            actions[j] = actions[j - 1];
                        }
                        else
                        {
                            actions[j] = o;
                            break;
                        }
                    }

                    Oxa_getDevice().actionPile = actions;
                    return false;
                }


            }
            break;

        case 'CLOSE':
            for (var i = 0; i < actions.length; i++)
            {
                // Si on ouvre déja des fenetes
                if (actions[i].type != 'CLOSE')
                {
                    actions.push(o); // on ajoute la fermeture à la fin

                    Oxa_getDevice().actionPile = actions;
                    return false;
                }
            }
            break;

        default:
            alert(o.type + " : Action inconnue...");
            break;

    }

    actions.push(o); // on peut : on enregistre comme quoi on réalise l'action

    Oxa_getDevice().actionPile = actions;

    return true;
}

function Oxa_next()
{
    Oxa_getDevice().actionPile.shift();

    if (Oxa_getDevice().actionPile.length > 0)
    {
        action = Oxa_getDevice().actionPile.shift();

        switch (action.type)
        {
            case 'OPEN':
                window.open(action.a, action.b, action.c,action.mode);
                break;

            case 'CLOSE':
                window.closePopup(action.n);
                break;

            case 'AJAX':
                // On remet l'action AJAX démolie ligne 480
                Oxa_getDevice().actionPile.unshift(action);

                break;
        }
    }
}

function me(n)
{
	if (n){
		var liste = Oxa_getDevice().popList;
		var name = liste[liste.length-1-n];
		return Oxa_getDevice().popList[name];
	}
	else
    {
        //if (typeof window.document.popup !== "undefined") { 
            return Oxa_getDevice().popList[window.document.popup];
        // }else
        // {
        //     return Oxa_getDevice()
        // }
    }
}

/**	
 * Rafraichir le menu lors d'un changement autormatique
 * @returns
 */
function refreshNotification(){
	jQuery.ajax({
		url: 'session_ipad.php?refrech',
		type: 'POST',
		dataType: 'html',
		success: function (htmlData) {
            var result = jQuery('<div />').append(htmlData).find('#identification_client').html();
            
                window.jQuery('#identification_client',Oxa_getDevice()).html(result);
                jQuery('#identification_client').html(result);
              
		}
	});
 
}


function initCS(cs,value,champ){
	jQuery.ajax({
		url: 'V2/ajax/init-cs',
        type: 'POST',
        dataType: 'JSON',
        data : {'TABDESTI' : cs, 'CODENR' : value, 'TAB_SOUR' :champ },
		success: function (data) {
            
            jQuery('#CS_0_'+data.NUM_FIX+'_VALEUR').val(data.VALEUR_SOURCE);
            
              
		}
	});
 
}





function notification(message, destinataire,onclick,url)
{
	var now = new Date();
//$.datepicker.formatDate('dd M yy', new Date())
	socket.emit('envoiNotification',  
			{ 
				nom : jQuery('.nom').html(),
				initiale : jQuery('#form_gestion > .initiale').html(),
				destinataire : destinataire,
				onclick : onclick,
				url : url,
                nomSociete : jQuery('.nomSocieteHeader').html(),
                date : $.datepicker.formatDate('dd M yy', new Date()),
				texte : now.getHours()+':'+ now.getMinutes()+ ', '+message,
			
			
			}
		); 	
}


function OpenPopup(url, name, largeur, hauteur,rez,mode) {
	var hWin;
	var top = (screen.height - hauteur) / 3;
	var left = (screen.width - largeur) / 2;
	 var rezisable = true;
	 if (rez){
		 rezisable = false;
	 }
	 if (mode == undefined)
		 mode = null;

	hWin = window.open(url, name, "'top=" + top + ",left=" + left
			+ ",dependent,locationbar=no,scrollbars=yes,resizable="+(rezisable ? 'yes' : "no")+",width="
			+ largeur + ",height=" + hauteur+"'", false,mode);
	//hWin.focus();

	patientezMsg =  Oxa_getDevice().document.getElementById('messagePatientez').value;
		
	return '<input type="hidden" id="messagePatientez" name="messagePatientez" value="'+patientezMsg+'" />'+hWin;

}

function OpenPopupNew(url, name, largeur, hauteur,rez,mode) {
	var hWin;
	var top = (screen.height - hauteur) / 3;
	var left = (screen.width - largeur) / 2;
	 var rezisable = true;
	 if (rez){
		 rezisable = false;
	 }
	 if (mode == undefined)
		 mode = null;

	hWin = window.open(url , name,  "'top=" + top + ",left=" + left
			+ ",dependent,locationbar=no,scrollbars=yes,resizable="+(rezisable ? 'yes' : "no")+",width="
			+ largeur + ",height=" + hauteur+"'", true,mode);
		

}