﻿Date.prototype.addSeconds = function (n) {this.setSeconds (this.getSeconds () + n)}
Date.prototype.toStringFr = function () {return habille(this.getDate()) + '/' + habille(this.getMonth()+1) + '/' + this.getFullYear()}
Date.prototype.toStringHours = function () {return [habille(this.getHours()),habille(this.getMinutes()),habille(this.getSeconds())].join(':')+''}
Date.prototype.toStringHoursBigFormatted = function () {return [habille(this.getHours()),habille(this.getMinutes()),habille(this.getSeconds())].join('<span class="deuxpoints">:</span>')+''}

function getDayFromNumber(n){
  switch(n)
  {
  case 1:
    return 'Lundi';
    break;    
  case 2:
    return 'Mardi';
    break;
  case 3:
    return 'Merc.';
    break;
  case 4:
    return 'Jeudi';
    break;
  case 5:
    return 'Vend.';
    break;
  case 6:
    return 'Sam.';
    break;
  case 0:
    return 'Dim.';
    break;
  default:
    return '';
  } 
}
function getMonthFromNumber(n){
  switch(n)
  {
  case 0:
    return 'Janv.';
    break;    
  case 1:
    return 'Févr.';
    break;
  case 2:
    return 'Mars';
    break;
  case 3:
    return 'Avr.';
    break;
  case 4:
    return 'Mai';
    break;
  case 5:
    return 'Juin';
    break;
  case 6:
    return 'Juil.';
    break;
  case 7:
    return 'Août';
    break;
  case 8:
    return 'Sept.';
    break;
  case 9:
    return 'Oct.';
    break;
  case 10:
    return 'Nov.';
    break;
  case 11:
    return 'Déc.';
    break;
  default:
    return '';
  } 
}

//Complément à 2 : 10H4 ==> 10H04
function habille(n){
  if(n.toString().length<2){
    return '0'+n; 
  } 
  else{
    return n;
  }
}

function isInf(a,b){
  if(a<b){ return true;}
  else{return false;}   
}

function isInfEgal(a,b){
  if(a<=b){ return true;}
  else{return false;}   
}

function notgood(field,msg) {
  field.focus();
  alert(msg);
  return false;
}

function isFull(field,msg){
  if (field.value=='') {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)||(champ.value=='')){
    return notgood(champ,msg);
   }
   return true;
}

function isGoodPass(champ,msg){
   var regPass = /(\s)/;
   var verif;
   verif = regPass.test(champ.value);
   if(verif == true || champ.value.length<5 || champ.value.length>8) {
     return notgood(champ,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 checkedRadio(radio) {
  for (var i=0; i<radio.length ; i++) {
    if (radio[i].checked) {
      return i ;
    }
  }
}

function isMail(field,msg) {
  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;
}

function isSup(a,b){
  if(a>b){ return true;}
  else{return false;}   
}

function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(sec) {
  //debugger;
  if(sec<3800){return 'N/A'};
  
  sec = Math.floor(sec);
  //var sec = Math.floor(ms/1000)
  //ms = ms % 1000
  //t = three(ms)
  
  var min = Math.floor(sec/60);
  sec = sec % 60;
  t = two(sec); //+ ":" + t
  
  var hr = Math.floor(min/60);
  min = min % 60;
  t = two(min) + ":" + t;
  
  var day = Math.floor(hr/60);
  //hr = hr % 60
  t = two(hr) + ":" + t;
  //t = day + ":" + t
  
  return t;
}

function isValidEmailAddress(emailAddress) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

function copyXML(parentEl) {
  var tempNode = parentEl.firstChild;
  var text="";
  while (tempNode != null) {
    switch (tempNode.nodeType) {
      case 1 /*ELEMENT*/ :
        text+="<" + tempNode.nodeName;
        if(tempNode.hasAttributes()) {
          var att = tempNode.attributes;
          for(var i=0;i<att.length;i++) {
            text+=" " + att[i].name + "=\"" + att[i].value + "\"";
          }
        }
        text+=">" + copyXML(tempNode) + "</" + tempNode.nodeName + ">";
      case 3 /*TEXT_NODE*/ :
        if(tempNode.nodeValue) {
          text+=tempNode.nodeValue;
        }
        break;
      case 4 /*CDATA_SECTION_NODE*/ :
        text+=tempNode.nodeValue;
        break;
      default :
        break;
    }
    tempNode = tempNode.nextSibling;
  }
  return text;
}

function et(){
  return '&';
}

function and(a,b){
  return a&&b; 
}

/*--------------------------------------------------------------------*/
/*------------------------POPIN + GLASS-------------------------------*/
/*--------------------------------------------------------------------*/
divShowed=null;
divGlass=null;
var divToShow_;
var caller_;
var rubriqueid_;
var elementid_;

var heightcorrection_=145;
var widthcorrection_=466;
function showPop(divToShow, caller, rubriqueid,elementid, supp){
  //debugger;  
    
  divToShow_ = divToShow;
  caller_ = caller;
  rubriqueid_ = rubriqueid;
  elementid_ = elementid;
  if(!supp){supp='';}
  
  //Switch de popin
  if(divGlass){
    debugger;
    
    var leftShowed=$('#'+divShowed).position().left;
    var topShowed=$('#'+divShowed).position().top;
    $('#'+divToShow).css('left',leftShowed+'px');
    $('#'+divToShow).css('top',topShowed+'px');
    $('#'+divShowed)
    
    $('#'+divShowed).fadeOut('2000');  
    $('#'+divToShow).fadeIn('1000')  
    
    
    divShowed=divToShow;
    return;
  }
  
  if(rubriqueid && !elementid){
    $('#'+divToShow+' .contentToRefresh').load('xslt.aspx?typerubrique=1&rubriqueid='+rubriqueid+'&'+supp, function(){showPopSuite();}); 
  }
  else if(rubriqueid && elementid){
    $('#'+divToShow+' .contentToRefresh').load('xslt.aspx?typerubrique=1&rubriqueid='+rubriqueid+'&elementid='+elementid+'&'+supp, function(){showPopSuite();}); 
  }
  else{
    showPopSuite()
  }  
}

function showPopSuite(){
  
  var divToShow = divToShow_;
  var caller = caller_;
  var rubriqueid = rubriqueid_;
  var elementid = elementid_;
      
  if(divGlass){return;}
   
    
  if(caller){
            
    var w_height = $(window).height();
    
    var w_scrollTop = $(window).scrollTop();
    
    //alert($(caller).position().top+'-'+w_height+'-'+w_scrollTop);
    
    var finalH = w_scrollTop+(w_height/2)-heightcorrection_; 
    
    
    $('#'+divToShow).css('top',finalH+'px');
    
    
  }  
  
  var w_width = $('body').width();
  var finalW = (w_width/2)-(widthcorrection_/2);   
  $('#'+divToShow).css('left',finalW+'px');
  
  
  //Création du fond
  var d=document.createElement('div');
  //$(d).addClass('glasspaint');
  $(d).attr('id','glasspaint');
  $(d).text('  ');
  $(d)[0].style.width=$('body').width()+'px'  ;
  
  if($('body').height()<$('window').height()){
    $(d)[0].style.height=$('window').height()+'px' ;
  }
  else{
    $(d)[0].style.height=$('body').height()+'px' ;
  }  
  
  $(d)[0].style.display='none';
  $('#global').append($(d));
  divGlass=d;
  
  /*$(divGlass).fadeTo("fast",0.72);       */
  $(divGlass).fadeTo("fast"); 
  $(divGlass).fadeIn("fast");    
  
  $('#'+divToShow).fadeIn('normal');
  divShowed=divToShow;  
}

function removeGlass(){
  if(!divShowed){return;}  
  
  $('#'+divShowed).fadeOut('fast');  
  $(divGlass).fadeOut('normal', function(){$(divGlass).remove();});    
  
  
  divShowed=null;
  divGlass=null;
}

