// JavaScript Document
/*************************************************************
 * Fonctions générales utiles au programme
 * Contient toutes les fonctions utiles accessibles de toute l'application
 *************************************************************/

var TamFnUtils = {
  
  // Récupère la largeur de la fenêtre en cours
  getWindowWidth: function () {
  	if (document.all) {
  		largeurFen = document.body.clientWidth;
  	}
  	else {
  		largeurFen = self.innerWidth;
  	}
  	return largeurFen;
  },
  
  // Récupère la hauteur de la fenêtre en cours
  getWindowHeight: function () {
  	if (document.all) {
  		hauteurFen = document.body.clientHeight;
  	}
  	else {
  		hauteurFen = self.innerHeight;
  	}
  	return hauteurFen;
  },
  
  /*******************************************************
  * Fonction de chargement d'un js dans un fichier html 
  * ******************************************************/  
  chargeJs: function (sUrlJs) {
    var e = document.createElement("script");
    e.src = sUrlJs;
    e.type="text/javascript";
    document.getElementsByTagName("body")[0].appendChild(e);
  },
  
  /**********************************************************
   *   Fonction de lancement d'une formulaire Ajax
   *   *****************************************************
  loadFormAjax: function (sForm, sPhp, method, newPopup, xPos, yPos, larg, haut, minW, minH, maxW, maxH, title) {
    oForm = document.getElementsByName(sForm)[0];
  
    aEle = oForm.elements;
    sParam='';
  
    for (i=0; i<oForm.length; i++) {
      if (aEle[i].type=='radio') {
        if (aEle[i].checked) sParam = sParam + aEle[i].name + '=' + aEle[i].value + '&';
      }
      else {
        if (aEle[i].type=='checkbox') {
          if (aEle[i].checked) sParam = sParam + aEle[i].name + '=' + aEle[i].value + '&';
        }
        else {
          if (aEle[i].type!='submit') sParam = sParam + aEle[i].name + '=' + aEle[i].value + '&';
        }
      }
    }
    //alert(sParam);
    this.ouvreURL(sPhp + sParam, newPopup, '', title, larg, haut, method, xPos, yPos, minW, minH, maxW, maxH);
    return false;
  },
  
  /**********************************************************
   *   Fonction d'execution d'un javascript
   *   *****************************************************/
  execJs: function() {
    win = Windows.getFocusedWindow();
    strContent = win.getContent().innerHTML;
    
    //alert(strContent);
    strContent.evalScripts();
  },
  
  /***********************************************************
  * Ouvrir l'url soit dans le même module (newPopup=0)
  * soit dans un autre module (newPopup=1) Soit dans une boite de dialog (newPopup=2) 
  * *********************************************************
  ouvreURL: function(sUrl, newPopup, column, title, larg, haut, method, xPos, yPos, minW, minH, maxW, maxH, icone, lenRefresh) {
    if (method==null || method=='') method='get';
  
    win = Windows.getFocusedWindow();
		
		if(win!=null)
			myClass = win.options.className;
		else
			myClass = "class1"; //classe par défaut
		
    if (newPopup==0) {
      if (title!=null){
        var formerWin = TamFnUtils.getWindowFromTitle(title);
        formerWin.setAjaxContent(sUrl);
      } else {
        win.setAjaxContent(sUrl);
      }
    }
    else if (newPopup==1) {
    
      if (title==null) title=win.options.title;
      
      
      var largeur = desktop.getListsWidth(column);
      var formerWin = TamFnUtils.getWindowFromTitle(title);
      
      if(formerWin!=-1) // si le titre existe déjà, on ouvre le lien dans la fenêtre qui existe déjà avec ce titre
      {
      	Windows.focusedWindow = formerWin;
      	TamFnUtils.ouvreURL(sUrl, 0);
      }
      else
      {
	      desktop.createNewWindowTo(column,
	                              {
	                                className:myClass,
	                                resizable: false,
	                                title: title,
	                                url: sUrl,
	                                width: largeur,
	                                showEffect:Element.show,
	                                hideEffect: Effect.SwitchOff,
	                                draggable: false,
	                                ico: icone,
	                                refreshAuto : lenRefresh
	                                //minHeight: custWinHeight
	                              }
	                            );
      }
    }
    else if (newPopup==2) {
             
        Dialog.alert(
                    {
                      url: sUrl, 
                      options: {method: method}
                    }, 
                    {
                      windowParameters: {
                                          className: 'class1', 
                                          height:haut, 
                                          width:larg, 
                                          closable:false,
                                          draggable:false,
                                          scrollable:true,
                                          title:title
                                        },
											okLabel: "OK",
											buttonClass: "myButtonClass",
											id: "myModalDialogId"
                    }
                    );
      WindowCloseKey.init();
    }

  },
 
 /***********************************************************
  * Ouvrir l'url soit dans le même module (newPopup=0)
  * soit dans un autre module (newPopup=1) Soit dans une boite de dialog (newPopup=2) 
  * *********************************************************/
  ouvreNewURL: function(params) {
    
    if (params.method==null || params.method=='') params.method='get';
  
    win = Windows.getFocusedWindow();
		
		if(win!=null)
			params.className = win.options.className;
		else
			params.className = "class1"; //classe par défaut
		
    if (params.win==0) {
      if (params.title!=null){
        var formerWin = TamFnUtils.getWindowFromTitle(params.title);
        formerWin.setAjaxContent(params.url);
      } else {
        win.setAjaxContent(params.url);
      }
    }
    else if (params.win==1) {
    
      if (params.title==null) params.title=win.options.title;
      
      
      //var params. = desktop.getListsWidth(params.column);
      var formerWin = TamFnUtils.getWindowFromTitle(params.title);

      if(formerWin!=-1) // si le titre existe déjà, on ouvre le lien dans la fenêtre qui existe déjà avec ce titre
      {
      	Windows.focusedWindow = formerWin;
      	params.win=0;
      	TamFnUtils.ouvreNewURL(params);
      }
      else
      {
	      desktop.createNewWindowTo(params.column, params);
      }
    }
    else if (params.win==2) {
             
        Dialog.alert(
                    {
                      url: params.url, 
                      options: {method: params.method}
                    }, 
                    {
                      windowParameters: {
                                          className: 'class1', 
                                          height:params.height, 
                                          width:params.width, 
                                          closable:true,
                                          draggable:false,
                                          scrollable:true,
                                          title:params.title
                                        },
											okLabel: "Fermer",
											buttonClass: "myButtonClass",
											id: "myModalDialogId"
                    }
                    );
      WindowCloseKey.init();
    }

  },
  
  /***********************************************************
  * Retourne l'objet fenêtre en fonction du titre
  * Renvoie -1 si le titre n'est pas trouvé 
  * *********************************************************/ 
  getWindowFromTitle: function(titre)
  {
  		var win = -1;
      for(var i=0;i<Windows.windows.length;i++)
      {
      	if(Windows.windows[i].options.title == titre)
      	{
      		win = Windows.windows[i];
      		break;
      	}
      }
      return win;
  }
}

