var NOVA_CONTEXT_PATH = "/novaserv";
var labels = new Array();

function getLabel(key) {
	if(labels[key] == null) return key;
	return labels[key];
}

function goTarget(target, theUrl) {
   if ( target != null && target != '') {
      top[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) {
	var myCartoWindow = top.menu_gauche.cartoWindow;
	
    if(myCartoWindow != null && !myCartoWindow.closed) {
        myCartoWindow.focus();
    }
    else {
        top.menu_gauche.cartoWindow = showCenterWindow(urlCarto,"cartoWindow",930,650);
        top.menu_gauche.cartoWindow.focus();
    }
}

// Affiche la cartographie en refermant la fen?tre ?ventuellement ouverte
function showCarto(urlCarto) {
	var myCartoWindow = top.menu_gauche.cartoWindow;
    if(myCartoWindow != null && !myCartoWindow.closed) {
        myCartoWindow.top.close();
    }

    top.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 ( (top.droite!=null) && (top.droite!='undefined')){
        // Teste si c'est une fen?tre avec "filtrage" (type JDB)
        if ((top.droite.bas!=null) && (top.droite.bas!='undefined')){
          top.droite.bas.window.focus();
          top.droite.bas.window.print();
        } else { // Fen?tre classique
            top.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;
    //Fen?tre principale
	if(top.droite) {
   		try {
			doc = top.droite.bas.document;
		} catch (e){
			doc = document;
		}
    } else if(top.bas) {//Fen?tre popup
      doc = top.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;
    //Fen?tre principale
    if(top.droite) {
      doc = top.droite.bas.document;
    }
    //Fen?tre popup
    else {
      doc = top.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;
  //Fen�tre principale
  if(top.droite) {
      frame = top.droite;
  }
  else {//Fen�tre popup
      frame = top;
  }
  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();
}




