﻿var toclose=0;
var curzindex=1000;


/*=======================================================*/
/*==========SURCHARGES DES TYPES String et Date==========*/
/*=======================================================*/
String.prototype.trim = function(){
return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
String.prototype.killspaces = function(){
  var reg=new RegExp(" ", "g");  
  return this.replace(reg, "");
}
Date.prototype.addSeconds = function (n) {this.setSeconds (this.getSeconds () + n)}
Date.prototype.toString = function () {return [getDayFromNumber(this.getDay()), habille(this.getDate()), getMonthFromNumber(this.getMonth())].join ('&#160;')+'&#160;:&#160;'+[habille(this.getHours()),habille(this.getMinutes())].join('H')+''}
//Date.prototype.toString = function () {return this.getDay();}




/*=======================================================*/
/*==================FONCTIONS============================*/
/*=======================================================*/

function showId(id, caller, decaltop, decalleft){
  var el=document.getElementById(id);
  if(!el){return;}
  
  el.style.top=findPosY(caller)+decaltop+'px';
  el.style.left=findPosX(caller)+decalleft+'px';
  curzindex++;
  el.style.zIndex=curzindex;
  
  el.style.display='';
}
function hideId(id){
  var el=document.getElementById(id);
  if(!el){return;}  
  
  el.style.display='none';
}

function launchOnEnter(expression) {
  if(event.keyCode==3 || event.keyCode==13) {
    eval(expression);
  }
}

function search(event){
  if((event.keyCode==3)||(event.keyCode==13)) {
    go('1','11',document.getElementById('submitor').rec2.value,'','','');
  }
  void(0);
}



function emptySearch(inputObj,txt){
  if(inputObj.value==txt){
    inputObj.value="";
  } 
}
function fillSearch(inputObj,txt){
  if(inputObj.value==""){
    inputObj.value=txt;
  } 
}

function changePic(Id){
  document.getElementById('grandePhoto').src=eval('grandePhoto'+Id)   
  clearInterval(wait);
}

//On parcours le tableau jusqu'a trouver l'image en cours, et on prend la suivante.
//Si c'est la dernière on reprend la première
function changePicAuto(){
  var iloop=0;
  var exit=false;
  
  while(iloop<=(tabIds.length-2) && exit==false){      
    if(tabIds[iloop]==curId){
      iloop++;
      exit=true;
    }
    else{
      iloop++;
    }      
    if(iloop>tabIds.length-2){
      iloop=0;
      exit==true;    
    }
  }
  document.getElementById('grandePhoto').src=eval('grandePhoto'+tabIds[iloop])
  curId = tabIds[iloop];
}

function displayThis(obj,value){
  obj.style.display=value;
}
function switchClass(obj,zeClass){
  obj.className=zeClass;
}

function goLang(lang) {  
  location.href= root + lang + '/xslt.aspx';
}
function et() {
  return "&";
}

function setEnr(enregistrementid,firstenr,nbenr) {
  document.getElementById('submitor').enregistrementid.value = enregistrementid;
  document.getElementById('submitor').nbenr.value = nbenr;
  document.getElementById('submitor').firstenr.value = firstenr;
  document.getElementById('submitor').method = 'post';
} 
  
function go(typerubrique,rubriqueid,elementid,offset,nbrec,firstrec) {
  document.getElementById('submitor').target='';
  document.getElementById('submitor').typerubrique.value = typerubrique;
  document.getElementById('submitor').rubriqueid.value = rubriqueid;
  document.getElementById('submitor').elementid.value = elementid;
  document.getElementById('submitor').offset.value = offset;
  document.getElementById('submitor').nbrec.value = nbrec;
  document.getElementById('submitor').firstrec.value = firstrec;
  document.getElementById('submitor').submit();
} 

function goUrl(url) {
  location.href=url;
} 

function goPdf(typerubrique,rubriqueid,elementid,offset,nbrec,firstrec) {
  document.forms['submitor'].typerubrique.value = typerubrique;
  document.forms['submitor'].rubriqueid.value = rubriqueid;
  document.forms['submitor'].elementid.value = elementid;
  document.forms['submitor'].offset.value = offset;
  document.forms['submitor'].nbrec.value = nbrec;
  document.forms['submitor'].firstrec.value = firstrec;
  document.forms['submitor'].target="pdf"+Math.floor(Math.random()*10000);
  document.forms['submitor'].action="DirectPdf.aspx";
  document.forms['submitor'].submit();
  document.forms['submitor'].action="xslt.aspx";
  document.forms['submitor'].target="";
}

function soumettreparrain() {
  //document.forms['submitor'].typerubrique.value = '1';
  //document.forms['submitor'].rubriqueid.value = '22';
  for (var i=1; i<7; i++) {
    var el = document.getElementById("email" + i);
    if (el.value!='') {
      if (!isMail(el, 'L\'email fourni a la ligne ' + i + ' n\'a pas un format correct.')) {
        return false;
      }
    }
  }
  document.getElementById('submitor').enregistrementid.value = -99;
  go('1','7','','','','');
}

function isDateField(champ,msg){  
  if(isDateValue(champ.value) == false){
    return notgood(champ,msg);
  }   
  return true;
}
function isHourField(champ,msg){  
  if(isHourValue(champ.value) == false){
    return notgood(champ,msg);
  }   
  return true;
}

function isHourValue(dateStr){
  
  //Modified by DO 12/31/2003
  var datePat = /^(\d{1,2})(h|\:)(\d{1,2})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  var datestatus=true;
  datemsg="";
  
  if (matchArray == null || matchArray[1]==null)
  {
    datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
    return false;
  }
  else
  {
    if(matchArray[3]==null)
    {
      datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
      return false;
    }
  }
  
  hour = parseInt(matchArray[1],10);
  minute = parseInt(matchArray[3],10);  
    
  if (hour > 23)
  {        
    return false;  
  } 
  if (hour < 0)
  {        
    return false;  
  }  
  
  if (minute > 59)
  {        
    return false;  
  } 
  if (minute < 0)
  {        
    return false;  
  }  
   
  return datestatus;

}
  
function isDateValue(dateStr){
  
  //Modified by DO 12/31/2003
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  var datestatus=true;
  datemsg="";
  
  if (matchArray == null || matchArray[1]==null)
  {
    datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
    return false;
  }
  else
  {
    if(matchArray[3]==null || matchArray[5]==null)
    {
      datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
      return false;
    }
  }
  
  month = parseInt(matchArray[3],10);
  day = parseInt(matchArray[1],10);  
  year = parseInt(matchArray[5],10);
  
  if (month < 1 || month > 12)
  { 
    datemsg=datemsg + "----- Month must be between 1 and 12." + "\n";
    datestatus=false;
  }
  
  if (day < 1 || day > 31)
  {
    datemsg=datemsg + "----- Day must be between 1 and 31." + "\n";
    datestatus=false;
  }
  
  if ((month==4 || month==6 || month==9 || month==11) && day==31)
  {
    datemsg=datemsg + "----- Month " + month + " doesn`t have 31 days!" + "\n";
    datestatus=false;
  }
  
  if (month == 2)
  { 
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap))
    {
      datemsg=datemsg + "----- February " + year + " doesn`t have " + day + " days!" + "\n";
      datestatus=false;
    }
  }  
  return datestatus;

}

function notgood(field,msg) {
  try{
    field.focus();
  }
  catch(err){}
  
  alert(msg);
  return false;
}

function isFullNb(field,msg,minchars){
  field.value=field.value.trim();
  if (field.value.length<minchars) {return notgood(field,msg);}
  return true;
}

function isAllDigits(champ,msg){    
   var chiffres = new RegExp("^[0-9]*$");
   var verif;   
   verif = chiffres.test(champ.value);         
   if(verif == false){
    return notgood(champ,msg);
   }   
   return true;
}

function isFull(field,msg){
  if (field.value=='') {return notgood(field,msg);}
  return true;
}

function isDigit(field,msg) {
  i = field.value.charCodeAt(0);
  if (!((47<i)&&(i<58))){return notgood(field,msg);}
  return true;
}

function isSelect(field,msg){
  if (field.selectedIndex==0 && field.options[0].value=="") {return notgood(field,msg);}
  return true;
}

function isMail(field,msg) {  
  field.value=field.value.killspaces();
  
  var email=field.value;
  var arobase=email.indexOf('@');
  var point=email.indexOf('.',arobase);
  var longueur=email.length;
  if(arobase<=0||point<=arobase+1||longueur<=point+1) {return notgood(field,msg);}
  return true;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setMailaction(obj){
  document.getElementById('mailaction').value=obj;
}


var imgLoad;
function goMail(elementid,zeracine) {
  mailfen = window.open('','mail','scrollbars,toolbar=no,directories=no,width=350,height=200');
  racine=zeracine;
  
  imgLoad =new Image; imgLoad.src= racine + 'img/wait.gif';
  imgLoad.height=73;
  imgLoad.width=75;
  
  mailfen.moveTo((screen.width/2)-(350/2),(screen.height/2)-(200/2));
  
  wait = setTimeout("if(imgLoad.complete){writeMessageBeforeMailAndSend("+elementid+");}",315);    
}  

function writeMessageBeforeMailAndSend(elementid){
  mailfen.document.writeln('<html>');
  mailfen.document.writeln('<head>');
  mailfen.document.writeln('  <title>Envoi de message</title>');
  mailfen.document.writeln('</head>');
  mailfen.document.writeln('<body>    ');
  mailfen.document.writeln('  <table cellpadding="0" border="0" cellspacing="0" width="100%">');
  mailfen.document.writeln('    <tr>');
  mailfen.document.writeln('      <td align="center">');
  mailfen.document.writeln('        &#160;');
  mailfen.document.writeln('      </td>');
  mailfen.document.writeln('    </tr>');
  mailfen.document.writeln('    <tr>');
  mailfen.document.writeln('      <td align="center">');
  mailfen.document.writeln('        &#160;');
  mailfen.document.writeln('      </td>');
  mailfen.document.writeln('    </tr>');
  mailfen.document.writeln('    <tr>');
  mailfen.document.writeln('      <td align="center">');
  mailfen.document.writeln('        <span class="texte">');
  mailfen.document.writeln('          <img height="73" width="75" id="wait" src="' + racine + 'img/wait.gif"/>');
  mailfen.document.writeln('        </span>');
  mailfen.document.writeln('      </td>');
  mailfen.document.writeln('    </tr>');
  mailfen.document.writeln('    <tr>');
  mailfen.document.writeln('      <td align="center" style="font-family:verdana;font-size:10px;">');
  mailfen.document.writeln('        <span class="texte">');
  mailfen.document.writeln('          Veuillez patienter...');
  mailfen.document.writeln('        </span>');
  mailfen.document.writeln('      </td>');
  mailfen.document.writeln('    </tr>');
  mailfen.document.writeln('  </table>   ');    
  mailfen.document.writeln('</body>');
  mailfen.document.writeln('</html>');
  
  document.getElementById('submitor').target = 'mail';
  document.getElementById('submitor').action = racine + 'xsltMailer.aspx';  
  document.getElementById('submitor').elementid.value = elementid;
  document.getElementById('submitor').submit();
  document.getElementById('submitor').reset();
  document.getElementById('submitor').target = '';
  document.getElementById('submitor').action='xslt.aspx';    
}
  

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1) 
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}


function and(a,b){
  if(a&&b) {return true;}
  else{return false;}
}

function isSup(a,b){
  if(a>b) {return true;}
  else{return false;}
}
function isInf(a,b){
  if(a<b) {return true;}
  else{return false;}
}


/*=======================================================*/
/*==========CALCULS GMT-4 ET MANIPULATIONS DATES=========*/
/*=======================================================*/

function getHour(){
        
  //wait = new activsoft.ajax.util.WaitPanelObject();
  //wait.showInPosition("WAIT", "Recherche des données...",(YAHOO.util.Dom.getClientHeight() / 2)+YAHOO.util.Dom.getDocumentScrollTop(),YAHOO.util.Dom.getClientWidth() / 2);

  var request = new activsoft.ajax.RequestToSend();          
  request.page = "xslt.aspx";           
  request.request = 'typerubrique=1&rubriqueid=-2';     
  request.method = "POST";     
  request.afterLoadCallBack = getHourBack ;          
  request.send();
}

function getHourBack(){    
  var racine = activsoft.ajax.util.xpathNodeList('//BODY',this.xmlDocumentLoaded.documentElement,this.xmlDocumentLoaded.documentElement)[0];
  if(!racine){return;}
  if(!racine.firstChild){return;}
  var node = racine.firstChild;
  
  while(node){     
    var t=node.attributes[0].nodeValue.split(",");
    zeDate=new Date(t[0],t[1],t[2],t[3],t[4],t[5]);       
    var node = node.nextSibling;
  }  
}

function getDayFromNumber(n){
  switch(n)
  {
  case 1:
    return 'Lundi';
    break;    
  case 2:
    return 'Mardi';
    break;
  case 3:
    return 'Mercredi';
    break;
  case 4:
    return 'Jeudi';
    break;
  case 5:
    return 'Vendredi';
    break;
  case 6:
    return 'Samedi';
    break;
  case 0:
    return 'Dimanche';
    break;
  default:
    return '';
  } 
}
function getMonthFromNumber(n){
  switch(n)
  {
  case 0:
    return 'Janvier';
    break;    
  case 1:
    return 'Février';
    break;
  case 2:
    return 'Mars';
    break;
  case 3:
    return 'Avril';
    break;
  case 4:
    return 'Mai';
    break;
  case 5:
    return 'Juin';
    break;
  case 6:
    return 'Juillet';
    break;
  case 7:
    return 'Août';
    break;
  case 8:
    return 'Septembre';
    break;
  case 9:
    return 'Octobre';
    break;
  case 10:
    return 'Novembre';
    break;
  case 11:
    return 'Décembre';
    break;
  default:
    return '';
  } 
}

//Complément à 2 : 10H4 ==> 10H04
function habille(n){
  if(n.toString().length<2){
    return '0'+n; 
  } 
  else{
    return n;
  }
}


/*=======================================================*/
/*==========TOOLTIPS=====================================*/
/*=======================================================*/


function remplirTexteDL(idtext, input){
  var listInput = document.getElementByTagName
}

function addToolTip(){
  var tt=document.getElementById("tooltip");
  if (tt==null){
    document.write("<div id='tooltip'></div>");
  }
}
// the tooltip object
var tooltip = {
    // setup properties of tooltip object
    id:"tooltip",
    offsetx : 10,
    offsety : 10,
    _x : 0,
    _y : 0,
    _tooltipElement:null,
    _saveonmouseover:null
}

function showTool(e, elem) {
  var el =document.getElementById(elem);  
  if(el){tooltip.show(el)};
}

function hideTool(e, elem) {
  var el =document.getElementById(elem);
  if(el){tooltip.hide(el)};
}

/**
* Open ToolTip. The title attribute of the htmlelement is the text of the tooltip
* Call this method on the mouseover event on your htmlelement
* ex :  <div id="myHtmlElement" onmouseover="tooltip.show(this)"...></div>
*/
tooltip.show = function (htmlelement) {

    // we save text of title attribute to avoid the showing of tooltip generated by browser
    var text=htmlelement.getAttribute("title");
    htmlelement.setAttribute("title","");
    htmlelement.setAttribute("title_saved",text);

	if(document.getElementById){
        this._tooltipElement = document.getElementById(this.id);
	}else if ( document.all ) {
        this._tooltipElement = document.all[this.id].style;
	}

    this._saveonmouseover = document.onmousemove;
    document.onmousemove = this.mouseMove;

    this._tooltipElement.innerHTML = text;

    this.moveTo(this._x + this.offsetx , this._y + this.offsety);

    if(this._tooltipElement.style){
        this._tooltipElement.style.visibility ="visible";
    }else{
        this._tooltipElement.visibility = "visible";
    }
   return false;
}

/**
* hide tooltip
* call this method on the mouseout event of the html element
* ex : <div id="myHtmlElement" ... onmouseout="tooltip.hide(this)"></div>
*/
tooltip.hide = function (htmlelement) {
    htmlelement.setAttribute("title",htmlelement.getAttribute("title_saved"));
    htmlelement.removeAttribute("title_saved");

    if(this._tooltipElement.style){
        this._tooltipElement.style.visibility ="hidden";
    }else{
        this._tooltipElement.visibility = "hidden";
    }
    document.onmousemove=this._saveonmouseover;
}



// Moves the tooltip element
tooltip.mouseMove = function (e) {
   // we don't use "this" because this method is assign to an event of document
   // and so is dereferenced
    if(e == undefined)
        e = event;

    if( e.pageX != undefined){ // gecko, konqueror,
        tooltip._x = e.pageX;
        tooltip._y = e.pageY;
    }else if(event != undefined && event.x != undefined && event.clientX == undefined){ // ie4 ?
        tooltip._x = event.x;
        tooltip._y = event.y;
    }else if(e.clientX != undefined ){ // IE6,  IE7, IE5.5
        if(document.documentElement){
            tooltip._x = e.clientX + ( document.documentElement.scrollLeft || document.body.scrollLeft);
            tooltip._y = e.clientY + ( document.documentElement.scrollTop || document.body.scrollTop);
        }else{
            tooltip._x = e.clientX + document.body.scrollLeft;
            tooltip._y = e.clientY + document.body.scrollTop;
        }
    /*}else if(event != undefined && event.x != undefined){ // IE6,  IE7, IE5.5
        tooltip.x = event.x + ( document.documentElement.scrollLeft || document.body.scrollLeft);
        tooltip.y = event.y + ( document.documentElement.scrollTop || document.body.scrollTop);
    */
    }else{
        tooltip._x = 0;
        tooltip._y = 0;
    }
    tooltip.moveTo( tooltip._x +tooltip.offsetx , tooltip._y + tooltip.offsety);

}

// Move the tooltip element
tooltip.moveTo = function (xL,yL) {
    if(this._tooltipElement.style){
        this._tooltipElement.style.left = xL +"px";
        this._tooltipElement.style.top = yL +"px";
    }else{
        this._tooltipElement.left = xL;
        this._tooltipElement.top = yL;
    }
}
function enableAllCtrl(){
   var count = document.forms[0].elements.length;
   for(i=0; i<count; i++) 
   {
      var element = document.forms[0].elements[i]; 
      if (element.disabled) { 
      	 element.disabled=false;
      } 
   }  
}

function disableCtrl(startWith){
   var count = document.forms[0].elements.length;
   var l=startWith.length;
   for(i=0; i<count; i++) 
   {
      /*
      if(document.forms[0].elements[i].id.substring(0,startWith.lenght)!=''){
        alert(startWith +' -- ' +document.forms[0].elements[i].id.substring(0,l));
        document.forms[0].elements[i].disabled=true;
      }
      */
      if(startWith==document.forms[0].elements[i].id.substring(0,l)){        
        document.forms[0].elements[i].disabled=true;
      }
   }  
}

function checkName(caller, imgid, djid){
  
  //Appel Ajax pour vérifier le login
  wait = new activsoft.ajax.util.WaitPanelObject();
  wait.showInPosition("WAIT", "Vérification du nom...",(YAHOO.util.Dom.getClientHeight() / 2)+YAHOO.util.Dom.getDocumentScrollTop(),YAHOO.util.Dom.getClientWidth() / 2);

  var request = new activsoft.ajax.RequestToSend();          
  request.page = "xslt.aspx";           
  request.request = "typerubrique=1&rubriqueid=34&nbrec=1&firstrec=2&rec2="+djid+"&elementid="+imgid+"&urlvalue="+caller.value;          
  request.afterLoadCallBack = retourcheckName ;          
  request.send();
}

function setBloc(){  
  //Si un des blocs gauche ou droit est plus grand on resize
  var left=document.getElementById("smallLeft"); 
  var right=document.getElementById("bigRight"); 
  var toresize=document.getElementById("divToResize"); 
  
  if(!left || !right){return;}
    
  //Le gauche est plus grand
  if((right.offsetHeight<=left.offsetHeight)){   
    toresize.style.height=(left.offsetHeight)-52+'px';    
    return;
  }   
  
}

function isSiretValide(siret) { 
  var estValide; 
  if ( (siret.length != 14) || (isNaN(siret)) ) 
    estValide = false; 
  else { 
     // Donc le SIRET est un numérique à 14 chiffres 
     // Les 9 premiers chiffres sont ceux du SIREN (ou RCS), les 4 suivants 
     // correspondent au numéro d'établissement 
     // et enfin le dernier chiffre est une clef de LUHN. 
    var somme = 0; 
    var tmp; 
    for (var cpt = 0; cpt<siret.length; cpt++) { 
      if ((cpt % 2) == 0) { // Les positions impaires : 1er, 3è, 5è, etc... 
        tmp = siret.charAt(cpt) * 2; // On le multiplie par 2 
        if (tmp > 9) 
          tmp -= 9;    // Si le résultat est supérieur à 9, on lui soustrait 9 
      } 
     else 
       tmp = siret.charAt(cpt); 
       somme += parseInt(tmp); 
    } 
    if ((somme % 10) == 0) 
      estValide = true; // Si la somme est un multiple de 10 alors le SIRET est valide 
    else 
      estValide = false; 
  } 
  return estValide; 
}

var TimeInfoBulle;
function setInfoBulle(text,time){
  clearTimeout(TimeInfoBulle);
  document.getElementById('infoBulle').innerHTML = text;
  document.getElementById('infoBulle').className='visible';
  TimeInfoBulle=setTimeout(resetInfoBulle,time);
}

function resetInfoBulle(){
  document.getElementById('infoBulle').innerHTML = '&#160;';  
  document.getElementById('infoBulle').className='notvisible';
}

function getEtAmp(){
  return '&'; 
}


function checkNum(obj) {
  var  reg=new  RegExp("[^\\d]*", "g");
  obj.value = obj.value.replace(reg,'');
}


function calculTaux(){
  if($('#enr21').val()==''){return;}
  
  var tmpval = $('#enr21').val();
  if($('#enr22').val()!=''){tmpval = $('#enr22').val();}
  
 
 $('#toReplaceBudg').load('xslt.aspx?typerubrique=1&rubriqueid=100&elementid='+encodeURIComponent(tmpval), function(){
   $('#hintBudg').removeClass('hint');
   $('#hintBudg').removeClass('hint_ok');
   if($('#taux').val()=='insuffisant' || $('#taux').val()=='moyen'){       
     $('#hintBudg').addClass('hint');
   }
   if($('#taux').val()=='normal' || $('#taux').val()=='excellent'){        
     $('#hintBudg').addClass('hint_ok');
   }
   $('#hintBudg').fadeTo(800,0.95);
    Budgtimer=setTimeout("hidehint('hintBudg',3000)",5000);
 });   
  
}