var NOVA_CONTEXT_PATH = "/novaserv";
var labels = new Array();

function isTop(){
	var w=self;
	var x=getTop();
	if (w!=x){
		//console.info("isTop : false")
		return false;
	} else {
		//console.info("isTop : true")
		return true;
	}
}

function getTop(){
    var w=self;
    
    try {
	    if (typeof (top.framesOn)=='undefined'){
	          // le top n'est pas Novacom  
	          var it=0;
	          var itMax=10;           
	          while (typeof (w.framesOn)=='undefined' && it<itMax){
	               // on cherche si le parent est la fenetre principale Novacom
	        w=w.parent;
	        it++;
	          }
	          // pas de frame Novacom trouvé, on doit être sur la page de login
	          if (it==itMax) {
	               w=self;
	          }
	    }else{
	          // le top est novacom
	          w=top;
	    }
	    //console.info('getTop : '+w)
	    return w;
    } catch (e){
    	// la plateforme est encapsulé dans un iframe/frame sur page de login
    	try {
    		//console.info('iframe/frame');
	    	 // le top n'est pas Novacom  
	        var it=0;
	        var itMax=10;           
	        while (typeof (w.framesOn)=='undefined' && it<itMax){
	             // on cherche si le parent est la fenetre principale Novacom
	        	w=w.parent;
	        	it++;
	        }
	        // pas de frame Novacom trouvé, on doit être sur la page de login
	        if (it==itMax) {
	        	//console.info('getTop catch1 : '+w) 
	        	w=self;
	        }
    	} catch(e){
    		//console.info('getTop catch2 : '+w)
    		w=self;
    	}
    }
    return w;
}

function getLabel(key) {
	if(labels[key] == null) return key;
	return labels[key];
}

function goTarget(target, theUrl) {
   if ( target != null && target != '') {
      parent[target].location.href = theUrl;
   }
}

// Affiche une fen?tre centr?e et dimensionn?e avec l'url et le nom demand?
function showCenterWindow(winUrl, winName, winWidth, winHeight){
    return window.open(winUrl,winName,
        "directories=no,location=no,toolbar=no,menubar=no,scrollbars=yes,status=no,resizable=yes,"+
        "width="+winWidth+
        ",height="+winHeight+
        ",top="+((screen.height-winHeight)/2)+
        ",left="+((screen.width-winWidth)/2)
    );
}

//Fermeture de la fen�tre
function closeWindow() {
	self.close();
}

// Affiche la fen?tre de t?l?chargement
function showDownloadWindow(winUrl,winName){
    return window.open(winUrl,winName,
        "directories=yes,location=yes,toolbar=yes,menubar=yes,scrollbars=yes,status=yes,resizable=yes,"+
        "width=1000,height=800,top="+((screen.height-800)/2)+",left="+((screen.width-1000)/2)
    );
}

// Affiche une fen?tre de pr?sentation d'impression
function showPrintWindow(winUrl)
{
  return showCenterWindow( winUrl, "PrintWindow", 1100, 600 );
}

// Affiche la fen?tre cartographique
function showCartoWindow(urlCarto) {
	try {
		var myCartoWindow = getTop().menu_gauche.popupCartoWindow;
	} catch (e){
		var myCartoWindow = parent.parent.menu_gauche.popupCartoWindow;
	}
    if(myCartoWindow != null && !myCartoWindow.closed) {
        myCartoWindow.focus();
    }
    else {
        try {
        	getTop().menu_gauche.popupCartoWindow = showCenterWindow(urlCarto,"popupCartoWindow",930,650);
        	getTop().menu_gauche.popupCartoWindow.focus();
        } catch(e){
        	parent.parent.menu_gauche.popupCartoWindow = showCenterWindow(urlCarto,"popupCartoWindow",930,650);
        	parent.parent.menu_gauche.popupCartoWindow.focus();
        }
    }
}

// Affiche la cartographie en refermant la fen?tre ?ventuellement ouverte
function showCarto(urlCarto) {
	try {
		var myCartoWindow = getTop().menu_gauche.popupCartoWindow;
	} catch(e){
		var myCartoWindow = parent.parent.menu_gauche.popupCartoWindow;
	}
	
    if(myCartoWindow != null && !myCartoWindow.closed) {
        myCartoWindow.parent.close();
    }
    try {
    	getTop().droite.location.href=urlCarto;
    } catch(e){
    	parent.parent.droite.location.href=urlCarto;
    }
}

// Affiche une fen?tre d'aide
function showHelpWindow(fileName)
{
     helpWindow = showCenterWindow(fileName,'helpWindow',700,700);
     helpWindow.focus();
}

// Imprime la frame courante
function printCurrentFrame() {
    // Teste si c'est la fen?tre principale (sinon c'est une fen?tre popup)
    if ( (parent.droite!=null) && (parent.droite!='undefined')){
        // Teste si c'est une fen?tre avec "filtrage" (type JDB)
        if ((parent.droite.bas!=null) && (parent.droite.bas!='undefined')){
          parent.droite.bas.window.focus();
          parent.droite.bas.window.print();
        } else { // Fen?tre classique
            parent.droite.window.print();
        }
    } else { // fen?tre popup
        window.print(); // l'imprimer enti?rement
    }
}

// Recherche l'element demand?
function findElement(elementId, aDocument){
  if (!aDocument) aDocument = document;
  return aDocument.all ? aDocument.all(elementId) : aDocument.getElementById(elementId);
}

function loadSendingHistory(aUrl){
    aUrl+="&historyLength="+history.length;
    document.location = aUrl;
}

function refreshFrame(frmName, idEtape, idEvent)
{
    var doc = null;
    //Fenetre principale
	if(parent.droite) {
   		try {
			doc = parent.droite.bas.document;
		} catch (e){
			doc = document;
		}
    } else if(parent.bas) {//Fenetre popup
      doc = parent.bas.document;
    } else {
    	doc = document;
    }
    var frm = doc.forms[frmName];

    var buttonDiv = CROSS_BROWSER.getDiv("dataRefreshButton",doc);

    if (idEtape) frm.idEtape.value = idEtape;
    if (idEvent) frm.idEvent.value = idEvent;

    if (buttonDiv){
        doc.isSubmitted=true;
        CROSS_BROWSER.setDivVisible("dataRefreshButton",false,doc);
        CROSS_BROWSER.setDivVisible("dataRefreshProgressBar",true,doc);
    }

    frm.submit();
}

function refreshMenuFrame(){
    var doc = null;
    //Fenetre principale
    if(parent.droite) {
      doc = parent.droite.bas.document;
    }
    //Fenetre popup
    else {
      doc = parent.bas.document;
    }
    var buttonDiv = CROSS_BROWSER.getDiv("dataRefreshButton",doc);

    if (buttonDiv){
        doc.isSubmitted=false;
        CROSS_BROWSER.setDivVisible("dataRefreshButton",true,doc);
        CROSS_BROWSER.setDivVisible("dataRefreshProgressBar",false,doc);
    }
}

function dataRefresh(frmName, idEtape, idEvent) {
    if(CROSS_BROWSER.getDiv("tableau")) CROSS_BROWSER.setDivVisible("tableau", false);
    refreshFrame(frmName, idEtape, idEvent);
}

function hideRefreshButton() {
    CROSS_BROWSER.setDivVisible("dataRefreshButton", false);
    CROSS_BROWSER.setDivVisible("dataRefreshProgressBar", false);
}

function showRefreshButton() {
    CROSS_BROWSER.setDivVisible("dataRefreshProgressBar", false);
    CROSS_BROWSER.setDivVisible("dataRefreshButton", true);
}

function showLoadingButton() {
      CROSS_BROWSER.setDivVisible("dataRefreshButton", false);
     CROSS_BROWSER.setDivVisible("dataRefreshProgressBar", true);
}

function RollOverImage(name, urlImage, urlImageOver, width, height, tooltip) {
  this.name = name;
  this.urlImage = urlImage;
  this.urlImageOver = urlImageOver;
  this.tooltip = tooltip;
  this.width = width;
  this.height = height;

   // Image preload
  var tempImg = new Image();
  if (this.urlImage){
    tempImg.src=this.urlImage;
  }
  if (this.urlImageOver){
    tempImg.src=this.urlImageOver;
  } else if (this.urlImage){
    this.urlImageOver = this.urlImage;
  }

  this.getName = function() {
    return this.name;
  }

  this.getUrlImage = function() {
    return this.urlImage;
  }

  this.getUrlImageOver = function() {
    return this.urlImageOver;
  }

  this.getWidth = function() {
    return this.width;
  }

   this.getHeight = function() {
    return this.height;
  }

  this.getTooltip = function() {
    return this.tooltip;
  }
}

function ImageManager() {
    this.elements = new Array();

    this.add = function(rollOverImage){
        this.elements[rollOverImage.getName()]=rollOverImage;
    }

    this.get = function(imageName){
      return this.elements[imageName];
    }

    this.getImage = function(imageId){
      return document.images[imageId];
    }

    this.swapImage = function(imageId, imageName) {
      var myRollOverImage = this.get(imageName);
      var myImage = this.getImage(imageId);
      myImage.src = myRollOverImage.getUrlImageOver();
    }

    this.restoreImage = function(imageId, imageName) {
      var myRollOverImage = this.get(imageName);
      var myImage = this.getImage(imageId);
      myImage.src = myRollOverImage.getUrlImage();
    }

    this.getImageCode = function(imageId, imageName) {
      var myRollOverImage = this.get(imageName);
      var str = "";
      str = '<img id="'+imageId+'" name="'+imageId+'" border="0" src="'+myRollOverImage.getUrlImage()+'"';
      str+=' onmouseover="IMAGE_MANAGER.swapImage('+"'"+imageId+"'"+', '+"'"+imageName+"'"+')"';
      str+=' onmouseout="IMAGE_MANAGER.restoreImage('+"'"+imageId+"'"+','+"'"+imageName+"'"+')"';
      if(myRollOverImage.getWidth()) str+=' width="'+myRollOverImage.getWidth()+'"';
      if(myRollOverImage.getHeight()) str+=' height="'+myRollOverImage.getHeight()+'"';
      if(myRollOverImage.getTooltip()) str+=' title="'+myRollOverImage.getTooltip()+'"';
      str+='>';
      return str;
    }

    this.writeImageTagCode = function(imageId, imageName) {
      document.write(this.getImageCode(imageId, imageName));
    }
}

var IMAGE_MANAGER = new ImageManager();

function addRollOverImage(imagePath, imageName, imageWidth, imageHeight, tooltip) {
  IMAGE_MANAGER.add(new RollOverImage(imageName, imagePath+"/"+imageName+".gif", imagePath+"/"+imageName+"_f2.gif", imageWidth, imageHeight, tooltip));
}

function writeImageTagCode(imageTagId, imageName) {
  IMAGE_MANAGER.writeImageTagCode(imageTagId, imageName);
}

function dataRefreshOnEnterKeyPressed(ev, formName, idEtape, idEvent) {
  var crossEvent = new CrossBrowserEvent();
  crossEvent.set(ev);
  if(crossEvent.enterKey) dataRefresh(formName, idEtape, idEvent)
}

function trace( msg ){
  if( typeof( jsTrace ) != 'undefined' ){
    jsTrace.send( msg );
  }
}

function parseJSONObject(JSON_text) {
	try {
		return eval("(" + JSON_text + ")");
	}
	catch(err) {
	}
}

/**
 * Determines if a form is dirty by comparing the current value of each element
 * with its default value.
 *
 * @param {Form} form the form to be checked.
 * @return {Boolean} true if the form is dirty, false otherwise.
 */
function addFormChangedHandler(form, formChangeHandler)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        var element = form.elements[i];
        var type = element.type;
        if (type == "checkbox" || type == "radio")
        {
            element.onpropertychange = formChangeHandler;
        }
        else if (type == "password" || type == "text" || type == "textarea")
        {
            element.onpropertychange = formChangeHandler;
        }
        else if (type == "select-one" || type == "select-multiple")
        {
            element.onpropertychange = formChangeHandler;
        }
    }
}

function onElementChanged() {
	formChanged = true;
}

function initFormChangeHandler() {
	if(CROSS_BROWSER.ie) addFormChangedHandler(document.frm, onElementChanged);
	formChanged=false;
}

function isFormSaved() {
	var frame = null;
  //Fenetre principale
  if(parent.droite) {
      frame = parent.droite;
  }
  else {//Fenetre popup
      frame = parent;
  }
  if(frame && frame.formSaved != undefined && !frame.formSaved && frame.showConfirmDialog) frame.showConfirmDialog();
}

function checkEnterKeyPressed(e){ //e is event object passed from function invocation
    var crossEvent = new CrossBrowserEvent();
    crossEvent.set(e);
    if(crossEvent.enterKey && onEnterKeyPressed) onEnterKeyPressed();
}

function logOut(urlLogout, logOutLinkId, comboSCName, comboCName, comboLangName) {
	// On desactive le lien quitter
	if(document.getElementById(logOutLinkId)!=null && typeof document.getElementById(logOutLinkId)!='undefined'){
		document.getElementById(logOutLinkId).style.visibility = 'hidden';	
	}
	// On desactive les combo client si possible
	if(typeof getTop()!='undefined' && typeof getTop().location!='undefined'){
		if(getTop().menu_gauche!=null && typeof getTop().menu_gauche!='undefined' ){
			var myForm = getTop().menu_gauche.document.forms[0];
			if(myForm!=null && typeof myForm!='undefined' ){
				if(myForm.elements[comboSCName]!=null && typeof myForm.elements[comboSCName]!='undefined'){
					myForm.elements[comboSCName].disabled = true;
				}
				if(myForm.elements[comboCName]!=null && typeof myForm.elements[comboCName]!='undefined'){
					myForm.elements[comboCName].disabled = true;
				}
				if(myForm.elements[comboLangName]!=null && typeof myForm.elements[comboLangName]!='undefined'){
					myForm.elements[comboLangName].disabled = true;
				}
			}
		}
		
		getTop().location.href = urlLogout;
	}
}

// Fonction utilise pour bloquer les boutons :
// isBarAction = true => mise en place du masquage de la bar de bouton par la progressBar
// isBarAction = false => desactivation des boutons
function disableAllButton(isBarAction,classToModify, classToModifyWait){
	if( typeof jQuery!='undefined' ){
		if(isBarAction){
			$('.'+classToModify).each(function(){
				$(this).css('display','none');
			});
			$('.'+classToModifyWait).each(function(){
				$(this).css('display','block');
			});
		}else{
			$('.'+classToModify).each(function(){
				$(this).attr('href','Javascript:void(0)'); 
				$(this).click(function(){return false;});
			});	
		}
	}
}


