/*
 * Fonction javascript pour l'ouverture d'une fenetre de selection
 * ===============================================================
 */
Rico.loadModule('Calendar','ColorPicker');

var popupRico = null;
var popupCpt = 10;
var divCpt = 10;
var okPopup = false;
var boxRico = null;
var boxCpt = 100;
var okBox = false;
var textAreaCpt = 10;

 
/**
 * Ouvre une popup de type fenetre.
 */
function OuvrirPopup(page,nom,option)
//=================================== 
{
  lstOption = 'toolbar=no,location=no,directories=no,menubar=no,status=no,' + option;
  window.open(page,nom,option);
}

function OuvrirPopup2(page,nom,option)
//==================================== 
{
  lstOption = 'toolbar=no,location=no,directories=no,menubar=no,status=no,' + option;
  var pop=window.open(page,nom, lstOption);
	return pop;
}

/**
 * Popup de type window.
 */
function openPopupRico(titre,texte,haut,larg,posX,posY,position,margeX,margeY)
//============================================================================
{

  if (okBox) closeBoxRico();
  
  if (position != '') {
	  divCpt++;
	  var idDiv = 'boxFrm'+divCpt; 
	  var texte = '<div id="'+idDiv+'">'+texte+'</div>';
  }

  var options = {hideOnClick: false, zIndex: ++boxCpt };
  boxRico = new Rico.Popup(options);
  boxRico.options.canDragFunc = true;
  boxRico.createWindow(titre,texte,haut,larg);
  boxRico.contentDiv.style.backgroundColor = '#FFFFFF';
  boxRico.contentDiv.style.color = '#000000'; 
 
  if (position != '') {
    posXY = getPositionAff(idDiv,posX,posY,position,margeX,margeY);
  }
  
  boxRico.openPopup(posX,posY);
  okBox = true;

}

/**
 * Popup de type window pour photo.
 */
function openPopupPhoto(titre,texte,cursorX,cursorY,position)
//===========================================================
{
  if (okPopup) closePopupRico();
  
  var txt = '<TABLE border="0" cellspacing="0" cellpadding="0" onclick="closePopupRico();">';
  txt += '<tr><td width="*">'+titre+'</td>';
  txt += '<td align="right"><img src="../../gene/icone/closeBlanc.gif" border="0" style="cursor:pointer;" /></td><tr>';
  txt += '<tr><td colspan="2">'+texte+'</td></tr></table>';
  
  openInfoBulle(txt,cursorX,cursorY,position);

}

/**
 * Popup de type infobulle.
 */
function openInfoBulle(texte,cursorX,cursorY,position)
//====================================================
{
  if (okPopup) closePopupRico();

  divCpt++;
  var idDiv = 'infBulle'+divCpt; 
  var txt = '<div id="'+idDiv+'" class="infBulle">'+texte+'</div>';

  var options = {hideOnClick: false, zIndex: ++popupCpt };
  popupRico = new Rico.Popup(options);
  popupRico.options.canDragFunc = true;
  popupRico.createPopup(document.body,txt,'auto','auto');
  popupRico.contentDiv.style.backgroundColor = '#666666';
  popupRico.contentDiv.style.color = '#FFFFFF'; 
 
	posXY = getPositionAff(idDiv,cursorX,cursorY,position,25,25);
	
  popupRico.openPopup(posXY[0],posXY[1]);
  okPopup = true;

}

/**
 * Popup de type infobulle.
 */
function openInfo(txt,curX,curY,pos,classe,col,bgCol)
//==================================================
{
  if (okPopup) closePopupRico();

  divCpt++;
  var idDiv = 'infBulle'+divCpt; 
  var txt = '<div id="'+idDiv+'" class="'+classe+'">'+txt+'</div>';

  var options = {hideOnClick: false, zIndex: ++popupCpt };
  popupRico = new Rico.Popup(options);
  popupRico.options.canDragFunc = true;
  popupRico.createPopup(document.body,txt,'auto','auto');
  popupRico.contentDiv.style.backgroundColor = bgCol;
  popupRico.contentDiv.style.color = col; 
 
  posXY = getPositionAff(idDiv,curX,curY,pos,25,25);
  
  popupRico.openPopup(posXY[0],posXY[1]);
  okPopup = true;

}

/**
 * Retourne la position X et Y ideale pour afficher l'element sans qu'il sorte de l'ecran 
 */ 
function getPositionAff(idDiv,cursorX,cursorY,position,margeX,margeY)
//===================================================================
{
  var txtWidth = Element.getWidth(idDiv);
  var txtHeight = Element.getHeight(idDiv);
  
  var posX = cursorX;
  var posY = cursorY;
  
  switch (position) {
    case 'HautGauche':
      posX = cursorX - txtWidth - margeX;
      posY = cursorY - txtHeight - margeY;
      break;
    case 'HautCentre':
      posX = cursorX - (txtWidth / 2);
      posY = cursorY - txtHeight - margeY;
      break;
    case 'HautDroit':
      posX = cursorX + margeX;
      posY = cursorY - txtHeight - margeY;
      break;
    case 'MilieuGauche':
      posX = cursorX - txtWidth - margeX;
      posY = cursorY - (txtHeight / 2);
      break;
    case 'MilieuDroit':
      posX = cursorX + margeX;
      posY = cursorY - (txtHeight / 2);
      break;
    case 'BasGauche':
      posX = cursorX - txtWidth - margeX;
      posY = cursorY;
      break;
    case 'BasCentre':
      posX = cursorX - (txtWidth / 2);
      posY = cursorY;
      break;
  }
  
  var minWidth = document.body.scrollLeft;
  var minHeight = document.body.scrollTop;
  if (posX < minWidth) posX = minWidth + margeX;
  if (posY < minHeight) posY = minHeight + margeY;
  var maxWidth = document.body.clientWidth + document.body.scrollLeft;
  var maxHeight = document.body.clientHeight + document.body.scrollTop;
  if ((posX+txtWidth) > maxWidth) posX = maxWidth - txtWidth - margeX;
  if ((posY+txtHeight) > maxHeight) posY = maxHeight - txtHeight - margeY;
  
  return new Array(posX,posY);
}

function closePopupRico()
//=======================
{
  if (okPopup) {
	  popupRico.closePopup();
	  okPopup = false;
  }
}

function closeBoxRico()
//=====================
{
  if (okBox) {
    boxRico.closePopup();
    okBox = false;
  }
}

function majZonePopup(zone, nom, type)
//====================================
{
  var valeur = "";
  if (type == 'input') valeur = zone.value;
  if (type == 'checkbox') valeur = zone.checked;
  $(nom).value = valeur;
}

/**
 * Ouverture d'une infobulle en fonction de l'evenement.
 * @param String Message.
 * @param Event Evenement. 
 * @param String Position [HautGauche,HautCentre,HautDroit,BasGauche,...] 
 */
function openInfBulEvent(mess,evt,position)
//==========================================
{
  var pos = getPosition(evt);
  openInfoBulle(mess,pos[0],pos[1],position);
  
  Event.stop(evt);
}

/**
 * Fenetre d'erreur avec boutons "OK"
 */
function openPopupErreur(titre,texte,action)
//==========================================
{
  openPopupErreurWithPos(titre,texte,action,450,300)
}

/**
 * Fenetre d'erreur avec boutons "OK"
 */
function openPopupErreurWithPos(titre,texte,action,posX,posY) {
  if (action == null || action == '') action = 'closeBoxRico()';
  var txt = '<TABLE border="0" width="100%"><TR>';
  txt += '<TD align="center" valign="middle" height="75px"><IMG SRC="../../gene/icone/Warning.png" width="50px" height="50px" /></TD>';
  txt += '<TD align="center" valign="middle">'+texte+'</TD>';
  txt += '</TR><TR>';
  txt += '<TD align="center" colspan="2">';
  txt += '<div class="btVertValidPt" onclick="'+action+';">'+popup_bt_ok+'</div>';
  txt += '</TD></TR></TABLE>';

  openPopupRico(titre,txt,'7em','20em',posX,posY,'HautCentre',0,0);
}

/**
 * Fenetre d'information avec boutons "OK"
 */
function openPopupInfo(titre,texte,action,align)
//==============================================
{
  var txt = '<TABLE border="0" ><TR>';
  txt += '<TD align="center" valign="top" height="75px" width="55px"><br>';
  txt += '<IMG SRC="../../gene/icone/Info.png" width="50px" height="50px" /></TD>';
  txt += '<TD align="'+align+'" valign="middle" width="*">'+texte+'</TD>';
  txt += '</TR><TR>';
  if (action == '') action = 'closeBoxRico()';
  txt += '<TD align="center" colspan="2">';
  txt += '<div class="btVertValidPt" onclick="'+action+';">'+popup_bt_ok+'</div>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  openPopupRico(titre,txt,'auto','auto',450,300,'',0,0);
}

/**
 * Fenetre d'information avec boutons "OK"
 */
function openPopupInfoWithPos(titre,texte,action,align,posX,posY)
//===============================================================
{
  var txt = '<TABLE border="0" ><TR>';
  txt += '<TD align="center" valign="top" height="75px" width="55px"><br>';
  txt += '<IMG SRC="../../gene/icone/Info.png" width="50px" height="50px" /></TD>';
  txt += '<TD align="'+align+'" valign="middle" width="*">'+texte+'</TD>';
  txt += '</TR><TR>';
  if (action == '') action = 'closeBoxRico()';
  txt += '<TD align="center" colspan="2">';
  txt += '<div class="btVertValidPt" onclick="'+action+';">'+popup_bt_ok+'</div>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  openPopupRico(titre,txt,'auto','auto',posX,posY,'',0,0);
}

/**
 * Fenetre boutons "OK" et "ANNULER"
 */
function openPopupQuestion(titre,texte,actionOk)
//==============================================
{
  var txt = '<TABLE border="0" width="100%"><TR>';
  txt += '<TD align="center" valign="middle" height="75px"><IMG SRC="../../gene/icone/Question.png" width="50px" height="50px" /></TD>';
  txt += '<TD align="center" valign="middle">'+texte+'</TD>';
  txt += '</TR><TR>';
  txt += '<TD align="center" colspan="2">';
  txt += '<table border="0"><tr><td width="50%" align="right"><div class="btVertValidPt" onclick="'+actionOk+';">'+popup_bt_ok+'</div></td>';
  txt += '<td width="50%" align="left"><div class="btBleuCroixPt" onclick="closeBoxRico();">'+popup_bt_annuler+'</div></td></tr></table>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  openPopupRico(titre,txt,'7em','18em',450,300,'',0,0);
}

function openPopupQuestionNouv(titre,texte,actionOk,posX,posY)
//============================================================
{
  var txt = '<TABLE border="0" width="100%"><TR>';
  txt += '<TD align="center" valign="middle" height="75px"><IMG SRC="../../gene/icone/Question.png" width="50px" height="50px" /></TD>';
  txt += '<TD align="center" valign="middle">'+texte+'</TD>';
  txt += '</TR><TR>';
  txt += '<TD align="center" colspan="2">';
  txt += '<table border="0"><tr><td width="50%" align="right"><div class="btVertValidPt" onclick="'+actionOk+';">'+popup_bt_ok+'</div></td>';
  txt += '<td width="50%" align="left"><div class="btBleuCroixPt" onclick="closeBoxRico();">'+popup_bt_annuler+'</div></td></tr></table>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  openPopupRico(titre,txt,'7em','18em',posX,posY,'',0,0);
}

/**
 * Fenetre boutons "OK" et "ANNULER"
 */
function openPopupComment(titre,texte,zoneMaj,actionOk,maxLenght)
//===============================================================
{
  var txt = '<TABLE border="0" width="100%"><TR>';
  txt += '<TD align="center" valign="middle" height="75px"><IMG SRC="../../gene/icone/Comment.bmp" width="60px" height="70px" /></TD>';
  txt += '<TD align="center" valign="middle">';
  txt += '<TEXTAREA name="comm" onKeyUp="limite(this,'+maxLenght+');" onKeyDown="limite(this,'+maxLenght+');" rows=4 cols=20 onblur="majZonePopup(this,\''+zoneMaj+'\',\'input\');">'+texte+'</TEXTAREA>'; 
  txt += '</TD>';
  txt += '</TR><TR>';
  txt += '<TD align="center" colspan="2">';
  txt += '<table border="0"><tr><td width="50%" align="right"><div class="btVertValidPt" onclick="'+actionOk+';">'+popup_bt_ok+'</div></td>';
  txt += '<td width="50%" align="left"><div class="btBleuCroixPt" onclick="closeBoxRico();">'+popup_bt_annuler+'</div></td></tr></table>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  var haut = '115px';
  var version = navigator.appVersion;
  if (navigator.appName == 'Netscape' && version.indexOf('Chrome') == -1 && version.indexOf('Safari') == -1 ) haut = '125px';
  var larg = '300px';
  openPopupRico(titre,txt,haut,larg,450,300,'',0,0);
}

/**
 * Fenetre boutons "OK" et "ANNULER"
 */
function openPopupSaisie(titre,aide,texte,actionOk,maxLenght)
//===========================================================
{
  textAreaCpt++;
  var txt = '<TABLE border="0" width="100%"><TR>';
  var largAide = '';
  var colsTexte = '75';
  if (aide != '') {
    largAide = '75px';
    colsTexte = '60';
  }
  txt += '<TD align="left" valign="top" height="'+largAide+'">'+aide+'</TD>';
  txt += '<TD align="center" valign="middle">';
  txt += '<TEXTAREA id="txtSais'+textAreaCpt+'" name="comm" onKeyUp="limite(this,'+maxLenght+');" onKeyDown="limite(this,'+maxLenght+');" rows=20 cols='+colsTexte+'>'+texte+'</TEXTAREA>'; 
  txt += '</TD>';
  txt += '</TR><TR>';
  txt += '<TD align="center" colspan="2">';
  txt += '<table border="0"><tr><td width="50%" align="right"><div class="btVertValidPt" onclick="'+actionOk+';">'+popup_bt_ok+'</div></td>';
  txt += '<td width="50%" align="left"><div class="btBleuCroixPt" onclick="closeBoxRico();">'+popup_bt_annuler+'</div></td></tr></table>';
  txt += '</TD>';
  txt += '</TR></TABLE>';
  
  var haut = '360px';
  var larg = '650px';
  openPopupRico(titre,txt,haut,larg,225,150,'',0,0);
  
  return textAreaCpt;
}

/**
 * Creation d'une fenetre calendrier
 *
 * @param Zone a mettre a jour
 * @param Date preselectionnee (DD/MM/YYYY)
 * @param Libelle du jour selectionne.
 */
function createCalend(zoneMaj,zoneVal,datJ,jour_lib)
//==================================================
{
  var dVerif = '';
  var today = new Date();
  var datMini = new Date(today.getFullYear()-50,0,1);
  if (datJ != '') {
	  dVerif = parseInt(datJ.substring(6,10)+datJ.substring(3,5)+datJ.substring(0,2));
	  datMini = new Date(getAnnee(datJ),getMois(datJ)-1,getJour(datJ));
  }
  var objCalend = new Rico.CalendarControl("objCalend", {showWeekNumber:0, startAt : 1, minDate : datMini });
  if (jour_lib != '') {
    objCalend.addHoliday(getJour(datJ),getMois(datJ),getAnnee(datJ),jour_lib,'#869ec4','white');
  }
  objCalend.atLoad();
  objCalend.returnValue = function(newVal) {
    var jr = newVal.substring(8,10);
    var ms = newVal.substring(5,7);
    var an = newVal.substring(0,4);
    if (datJ != '' && parseInt(an+ms+jr) < dVerif) {
      openPopupErreur(popup_msg_errdate+' : '+jr+'/'+ms+'/'+an,popup_msg_datsupdelai+' : '+datJ);
    } 
    else {
	    $(zoneMaj).value = jr+'/'+ms+'/'+an; 
	    $(zoneVal).value = newVal;
	  }
  };
  
  return objCalend;
}

/**
 * Ouverture du calendrier
 *
 * @param id du bouton zoom
 * @param Date preselectionnee (DD/MM/YYYY)
 * @param Event.
 */
function openCalendrier(objCalend,btZoom,dateJour,evt)
//====================================================
{
  if (Element.visible(objCalend.container)) {
    objCalend.close();
  } else {
    RicoUtil.positionCtlOverIcon(objCalend.container,$(btZoom));
    if (dateJour != '') {
      var datJ = new Date(getAnnee(dateJour),getMois(dateJour)-1,getJour(dateJour));
      objCalend.open(datJ);
    }
    else {
      objCalend.open();
    }
  }
  Event.stop(evt);
}

/**
 * Retourne le jour
 * @param date au format dd/mm/yyyy
 * @return Integer : jour sur 2
 */
function getJour(dateJour)
//========================
{
  if (dateJour.substring(0,1) == '0') {
    return parseInt(dateJour.substring(1,2));
  }
  return parseInt(dateJour.substring(0,2));
}

/**
 * Retourne le mois
 * @param date au format dd/mm/yyyy
 * @return Integer : mois sur 2
 */
function getMois(dateJour)
//========================
{
  if (dateJour.substring(3,4) == '0') {
    return parseInt(dateJour.substring(4,5));
  }
  return parseInt(dateJour.substring(3,5));
}

/**
 * Retourne l'annee
 * @param date au format dd/mm/yyyy
 * @return Integer : annee sur 4 
 */
function getAnnee(dateJour)
//=========================
{
  return parseInt(dateJour.substring(6,10));
}

function getPosition(evt)  {
  return new Array(Event.pointerX(evt), Event.pointerY(evt));
}

function limite(zone,max)
//=======================
{
  if(zone.value.length>=max){
    zone.value=zone.value.substring(0,max);
  }
}
