﻿/*---------------------------------------------------------------------*/
/*-----------------------Bloc le saviez-vous---------------------------*/
/*---------------------------------------------------------------------*/
function majSaviezVous(){
  $('#bloc_LeSaviezVous .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=100');
}


/*---------------------------------------------------------------------*/
/*-----------------------Bloc Médias-----------------------------------*/
/*---------------------------------------------------------------------*/
var timerMedias;
var blocsMedias=new Array();
var curMedias=0;
function majMedias(direction){
  
  if(direction){clearInterval(timerMedias);}
  else{direction='up';}
  
  var curDiv=$('#bloc_Medias .one_media_bloc:visible');
  var nextDiv;
  var curIndex=curMedias;
  //debugger;
  
  switch(direction)    
    {
    case 'up':
    
      if((curIndex+1)>=blocsMedias.length){
        nextDiv=$('#'+blocsMedias[0]);
        curMedias=0;
      }
      else{
        nextDiv=$('#'+blocsMedias[curIndex+1]);
        curMedias+=1;
      }
    break;
    case 'down':
      if((curIndex-1)<0){
        nextDiv=$('#'+blocsMedias[blocsMedias.length-1]);
        curMedias=blocsMedias.length-1;
      }
      else{
        nextDiv=$('#'+blocsMedias[curIndex-1]);
        curMedias-=1;
      }
    break;   
  }
  
  //debugger;
  //alert(curDiv.id);
  //alert(nextDiv.id);
  
  $(curDiv).hide();
  $(nextDiv).show();
  
}

/*---------------------------------------------------------------------*/
/*-----------------------Bloc sondage----------------------------------*/
/*---------------------------------------------------------------------*/

function setSondage(){  
  if(!$('#bloc_Sondage input:checked')[0]){alert('Veuillez choisir une réponse.');return;}
  $('#bloc_Sondage .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=101&actionner=ENRSONDAGE&valeur='+$('#bloc_Sondage input:checked').val());
  $('#bloc_Sondage .btnValider img').remove();
}

/*---------------------------------------------------------------------*/
/*-----------------------Photothèque-----------------------------------*/
/*---------------------------------------------------------------------*/

var callerPhototheque
function setPhoto(arbo,caller){  
  callerPhototheque=caller;
  $('#fiche_photo .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=103&elementid='+arbo, function(){showPop('fiche_photo',callerPhototheque);}); 
}

function goFormulaireAcces(docid,titreImg){
	var titreImage = encodeURIComponent(titreImg);
	$('#obtenirCode .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=113&elementid='+titreImage+'&docId='+docid, function(){
		$('#photoToHide').fadeOut(500, function(){
			$('#obtenirCode').css('height','300px');
			$('#obtenirCode').fadeIn(500);
		});
	});
}

function sendCodeByMail(mail){
  var mail = encodeURIComponent($('#mail_demandeur').val());
  var nom = encodeURIComponent($('#nom_demandeur').val());
  var prenom = encodeURIComponent($('#prenom_demandeur').val());
  var media = encodeURIComponent($('#media_demandeur').val());
 $('#obtenirCode .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=113&elementid='+mail+'&actionner=MAIL_SENT&mail_demandeur='+mail+'&nom_demandeur='+nom+'&prenom_demandeur='+prenom+'&media_demandeur='+media,function(){}); 
}

function montrerFormAcces(){
	$('#demandeCode').fadeOut(500, function(){
		$('#obtenirCode').css('height','381px');
		$('.container_top').fadeIn(500);
	});
}

function sendMailCode(){
	if(verifSendMailCode() == true){
		sendCodeByMail($('#mail_demandeur').val());
	}
}

function verifSendMailCode(){
	if($('#mail_demandeur').val() == ''){
		alert('Veuillez saisir votre adresse email.');
		return false;
	}
	else if ($('#nom_demandeur').val() == ''){
		alert('Veuillez saisir votre nom.');
		return false;
	}
	else if ($('#media_demandeur').val() == ''){
		alert('Veuillez saisir votre média');
		return false;
	}
	else if ($('#accept_condition').val() == false){
		alert('Veuillez accepter les conditions d\'utilisation pour continuer');
		return false;
	}
	if(!isMail(document.getElementById('mail_demandeur'),'Cette adresse mail semble incorrecte.')){
		return false;
	} 
	return true;
}

function telechargeImage(docid,titreImage){
	var code = $('#codeAcces').val();
  var documentId = docid;
  var titreImg = encodeURIComponent(titreImage);
	 $('#obtenirCode .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=113&elementid='+titreImg+'&docId='+documentId+'&actionner=TELECHARGEMENT_IMG&codeAcces='+code,function(){}); 
}

/*---------------------------------------------------------------------*/
/*-----------------------Chercher un installateur----------------------*/
/*---------------------------------------------------------------------*/

function majRechercheInstallateur(elementid,func){
  $('#fiche_instal').css('height','369px');
  
  $('#fiche_instal .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=104&elementid='+elementid, func);
  
}

function majInstallateurAndMail(){
  var coordonnee = encodeURIComponent($('#contact_coordonnee').val());
  var email = encodeURIComponent($('#contact_mail').val());
  var objet = encodeURIComponent($('#contact_objet').val());
  var demande = encodeURIComponent($('#contact_demande').val());
  var entreprise=encodeURIComponent($('#entrepriseId').val());
 $('#contactInstallateur .contentToRefresh').load(root+'index.php?elementid='+entreprise+'&typerubrique=1&rubriqueid=105&actionner=CONTACT_INSTALLATEUR&contact_mail='+email+'&contact_coordonnee='+coordonnee+'&contact_objet='+objet+'&contact_demande='+demande); 
}


function goContactInstallateur(entrepriseId){
  $('#contenuWithOutFormContact').hide();
  $('#contactInstallateur .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=105&elementid='+entrepriseId);  
  $('#fiche_instal').css('height','454px');
  $('#contactInstallateur').show();

}


/*--------------------------------------------------------------------*/
/*-------------------------ADMIN BLOCS--------------------------------*/
/*--------------------------------------------------------------------*/

function showAdmin(arbo, caller){
  $('.bloc_Admin').fadeOut('normal');
  //$('#bloc_Admin').hide();
  //$('#bloc_Admin').load(root+'index.php?typerubrique=1&rubriqueid=102&elementid='+arbo,function(){
    positionAdmin(arbo,caller);
  //});
  
}
function positionAdmin(arbo,caller){
  var pos=$(caller).position();
  
  $('#bloc_Admin_'+arbo).css('top',pos.top+'px');
  $('#bloc_Admin_'+arbo).css('left',pos.left+'px');
  $('#bloc_Admin_'+arbo).fadeIn('normal');
  $('#bloc_Admin_'+arbo+' .croix_Admin').css('left',$('#bloc_Admin_'+arbo).width()+15+'px');
}


/*---------------------------------------------------------------------*/
/*-----------------------Appel ajax login espace reserve---------------*/
/*---------------------------------------------------------------------*/
function ajaxcallReserve(){    
  var request = new activsoft.ajax.RequestToSend();          
  request.page = root+"index.php";           
  request.request = "typerubrique=1&rubriqueid=106&nbrec=2&firstrec=2&rec2="+$('#login_reserve').val()+"&rec3="+MD5($('#password_reserve').val());          
  request.afterLoadCallBack = retourajaxcallReserve ;          
  request.send();
}

function checkName(){
  $('#toRefreshPass').load(root+'index.php?typerubrique=1&rubriqueid=111&elementid='+$('#userpass').val()); 
}

function sendPass(){
  $('#toRefreshPass').load(root+'index.php?typerubrique=1&rubriqueid=112&actionner=OUBLI_PASS&elementid='+$('#userpasshid').val()); 
   
}


function retourajaxcallReserve(){  
  tmpRepXml=this.xmlDocumentLoaded.documentElement;
  var id=tmpRepXml.firstChild.attributes[0].value;
  var ap=tmpRepXml.lastChild.attributes[0].value;
    
  if(id!=''){
    
    /*if(ap.indexOf('Partenaire')!=-1){
      $('#submitor')[0].action="https://www.qualypso.fr/authentification_extranet.php?url=http%3A%2F%2Fv1.qualit-enr.org%2Fpartenaire-login-qualitenr.php";
      $('#submitor').submit();
      return;
    }
    
    if(ap.indexOf('Qualisol')!=-1){
      $('#submitor')[0].action="https://www.qualypso.fr/authentification_extranet.php?url=http%3A%2F%2Fv1.qualit-enr.org%2Finstallateur-login-qualisol.php";
      $('#submitor').submit();
      return;
    }
    if(ap.indexOf('QualiPV')!=-1){
      $('#submitor')[0].action="https://www.qualypso.fr/authentification_extranet.php?url=http%3A%2F%2Fv1.qualit-enr.org%2Finstallateur-login-qualipv.php";
      $('#submitor').submit();
      return;
    }
    if(ap.indexOf('Qualibois')!=-1){
      $('#submitor')[0].action="https://www.qualypso.fr/authentification_extranet.php?url=http%3A%2F%2Fv1.qualit-enr.org%2Finstallateur-login-qualibois.php";
      $('#submitor').submit();
      return;
    }
    
    
    $('#submitor').action="https://www.qualypso.fr/authentification_extranet.php?url=http%3A%2F%2Fv1.qualit-enr.org%2Finstallateur-login-qualisol.php";
    $('#submitor').submit();
    return;
    $('#submitor').submit();*/
    
    setEnr(1,10000,2);
    go('1','1000','','','','');
    
    
  }
  else{          
    alert('Identifiants incorrects.');
    return;
  }      
}


/*---------------------------------------------------------------------*/
/*----------------------Envoi à un ami---------------------------------*/
/*---------------------------------------------------------------------*/

function sendMailInstallateur(){
  if(verifSendMailInstallateur() == true){
    majInstallateurAndMail();
  }
}

function sendMailAmi(){
  if(document.getElementById('contact_ami_mail').value == ''){
    alert('Veuillez saisir votre adresse email.');
    return;
  }
  if(document.getElementById('contact_ami_nom').value == ''){
    alert('Veuillez saisir votre nom.');
    return;
  }
  if(document.getElementById('contact_ami_mail_ami').value == ''){
    alert('Veuillez saisir votre l\'adresse email de votre ami.');
    return;
  }    
  
  if(!isMail(document.getElementById('contact_ami_mail'),'Cette adresse mail semble incorrecte.')){
    return;
  } 
  
  if(!isMail(document.getElementById('contact_ami_mail_ami'),'Cette adresse mail semble incorrecte.')){
    return;
  } 
  
  
  var email = encodeURIComponent($('#contact_ami_mail').val());
  var nom = encodeURIComponent($('#contact_ami_nom').val());
  var emailami = encodeURIComponent($('#contact_ami_mail_ami').val());
  var message = encodeURIComponent($('#contact_ami_message').val());  
  
  var reg=new RegExp("&", "g");
  var regSess=new RegExp("sessionuniqueid=.*&typerubrique", "gi");
  $('#fiche_envoi_ami .contentToRefresh').load(root+'index.php?typerubrique=1&rubriqueid=107&actionner=CONTACT_AMI&contact_mail='+email+'&contact_message='+message+'&contact_mail_ami='+emailami+'&contact_nom='+nom+'&contact_lien='+location.href.replace(regSess,'typerubrique').replace(reg,'*')); 
    
}


/*---------------------------------------------------------------------*/
/*----------------------Mes installations------------------------------*/
/*---------------------------------------------------------------------*/

function majInstallation(){
  
  if($('input:radio[name=rec4]:checked')[0]){
    $('#ctn_actu_detail #toRefresh').load(root+'index.php?typerubrique=1&rubriqueid=1021&elementid='+encodeURIComponent($('input:radio[name=rec4]:checked').val())); 
  }
}

function showInstall(isNew){
  if(isNew){
    $('#toRefreshRight').load(root+'index.php?typerubrique=1&rubriqueid=1025&elementid='+encodeURIComponent(isNew), function(){      
      $('#toRefreshRight').slideDown(); 
    });     
  }
  else{
    
    if(!$('#enr3').val()){
      return;
    }
    
    $('#toRefreshRight').load(root+'index.php?typerubrique=1&rubriqueid=1026&elementid='+encodeURIComponent($('#enr3').val()), function(){      
      $('#toRefreshRight').slideDown(); 
    }); 
    
  }
}
function hideInstall(){
  $('#toRefreshRight').slideUp();  
  
}

function newInstall(){
  
  if($('#installation_date').val()==''){
    alert('Veuillez saisir une date.')
    return;
  }
  if($('#installation_client_cp').val()==''){
    alert('Veuillez saisir un code postal.')
    return;
  }
  if($('#installation_client_ville').val()==''){
    alert('Veuillez saisir une ville.')
    return;
  }
   
  var dv; 
  if($('#installation_partenaire_dv')[0]){
    if($('#installation_partenaire_dv')[0].checked){
      dv=1;
    }
    else{
      dv=0;
    }
  }
  else{
    dv=0;
  }
        
  $.ajax({
     type: "POST",
     url: "index.php",
     data: 'typerubrique=1&enregistrementid=1014&rubriqueid=1021&nbenr=3&firstenr=2&elementid='+encodeURIComponent($('input:radio[name=rec4]:checked').val())+'&enr2='+encodeURIComponent($('#installation_date').val())+'&enr3='+encodeURIComponent($('#installation_type').val())+'&enr4='+encodeURIComponent($('#installation_client_nom').val())+'&enr5='+encodeURIComponent($('#installation_client_adresse1').val())+'&enr6='+encodeURIComponent($('#installation_client_adresse2').val())+'&enr7='+encodeURIComponent($('#installation_client_cp').val())+'&enr8='+encodeURIComponent($('#installation_client_ville').val())+'&enr9='+encodeURIComponent($('#installation_client_telephone1').val())+'&enr10='+encodeURIComponent($('#installation_surface_puissance').val())+'&enr11=&enr12='+encodeURIComponent(dv)+'&enr13='+encodeURIComponent($('#installation_client_telephone2').val())+'&enr14='+encodeURIComponent($('#installation_client_email').val()),
     success: function(msg){
       $('#toRefreshRight').slideUp(function(){
         majInstallation();
       });
     }
   });    
  
}

function modInstall(){
  
    
  if($('#installation_date').val()==''){
    alert('Veuillez saisir une date.')
    return;
  }
  if($('#installation_client_cp').val()==''){
    alert('Veuillez saisir un code postal.')
    return;
  }
  if($('#installation_client_ville').val()==''){
    alert('Veuillez saisir une ville.')
    return;
  }
   
  var dv; 
  if($('#installation_partenaire_dv')[0]){
    if($('#installation_partenaire_dv')[0].checked){
      dv=1;
    }
    else{
      dv=0;
    }
  }
  else{
    dv=0;
  }
    
  
        
  $.ajax({
     type: "POST",
     url: "index.php",
     data: 'typerubrique=1&enregistrementid=1015&rubriqueid=1021&nbenr=3&firstenr=2&elementid='+encodeURIComponent($('#enr3').val())+'&enr2='+encodeURIComponent($('#installation_date').val())+'&enr3='+encodeURIComponent($('#installation_type').val())+'&enr4='+encodeURIComponent($('#installation_client_nom').val())+'&enr5='+encodeURIComponent($('#installation_client_adresse1').val())+'&enr6='+encodeURIComponent($('#installation_client_adresse2').val())+'&enr7='+encodeURIComponent($('#installation_client_cp').val())+'&enr8='+encodeURIComponent($('#installation_client_ville').val())+'&enr9='+encodeURIComponent($('#installation_client_telephone1').val())+'&enr10='+encodeURIComponent($('#installation_surface_puissance').val())+'&enr11=&enr12='+encodeURIComponent(dv)+'&enr13='+encodeURIComponent($('#installation_client_telephone2').val())+'&enr14='+encodeURIComponent($('#installation_client_email').val()),
     success: function(msg){
       $('#toRefreshRight').slideUp(function(){
         majInstallation();
       });
     }
   });    
  
}

function suppInstall(){
  
  
  
  if($('#enr3').val()){
    
    if(!confirm('Voulez-vous supprimer cette installation ?')){return;}
        
    $.ajax({
       type: "POST",
       url: "index.php",
       data: 'typerubrique=1&enregistrementid=1010&rubriqueid=1021&elementid='+encodeURIComponent($('#enr3').val()),
       success: function(msg){
         $('#toRefreshLeft').slideUp(function(){
           majInstallation();
         });
         
       }
     });
   
  }
}



function showRef(isNew){
  if(isNew){
    $('#toRefreshLeft').load(root+'index.php?typerubrique=1&rubriqueid=1023', function(){      
      $('#toRefreshLeft').slideDown(); 
    });     
  }
  else{
    
    if(!$('#enr2').val()){
      return;
    }
    
    $('#toRefreshLeft').load(root+'index.php?typerubrique=1&rubriqueid=1024&elementid='+encodeURIComponent($('#enr2').val()), function(){      
      $('#toRefreshLeft').slideDown(); 
    }); 
    
  }
}
function hideRef(){
  $('#toRefreshLeft').slideUp();  
  
}

function newRef(){
  
  if($('#enr10').val()==''){
    alert('Veuillez saisir un nom.')
    return;
  }
  if($('#enr11').val()==''){
    alert('Veuillez saisir un prénom.')
    return;
  }
    
  
        
  $.ajax({
     type: "POST",
     url: "index.php",
     data: 'typerubrique=1&enregistrementid=1012&rubriqueid=1021&nbenr=3&firstenr=2&enr2='+encodeURIComponent($('#enr10').val())+'&enr3='+encodeURIComponent($('#enr11').val())+'&enr4='+encodeURIComponent($('#enr12').val()),
     success: function(msg){
       $('#toRefreshLeft').slideUp(function(){
         majInstallation();
       });
     }
   });    
  
}

function modRef(){
  
    
  if($('#enr10').val()==''){
    alert('Veuillez saisir un nom.')
    return;
  }
  if($('#enr11').val()==''){
    alert('Veuillez saisir un prénom.')
    return;
  }
    
  
        
  $.ajax({
     type: "POST",
     url: "index.php",
     data: 'typerubrique=1&enregistrementid=1013&rubriqueid=1021&nbenr=3&elementid='+encodeURIComponent($('#enr2').val())+'&firstenr=2&enr2='+encodeURIComponent($('#enr10').val())+'&enr3='+encodeURIComponent($('#enr11').val())+'&enr4='+encodeURIComponent($('#enr12').val()),
     success: function(msg){
       $('#toRefreshLeft').slideUp(function(){
         majInstallation();
       });
     }
   });    
  
}

function suppRef(){
  
  
  
  if($('#enr2').val()){
    
    if(!confirm('Voulez-vous supprimer ce référent ?')){return;}
        
    $.ajax({
       type: "POST",
       url: "index.php",
       data: 'typerubrique=1&enregistrementid=1011&rubriqueid=1021&elementid='+encodeURIComponent($('#enr2').val()),
       success: function(msg){
         majInstallation();
       }
     });    
  }
}


/*---------------------------------------------------------------------*/
/*----------------------Bloc entreprise--------------------------------*/
/*---------------------------------------------------------------------*/

function majBlocEntreprise(){
  
  if($('#entrepriseid').val()!=''){
    $('#toRefreshTDB').load(root+'index.php?typerubrique=1&rubriqueid=1030&elementid='+encodeURIComponent($('#entrepriseid').val())); 
  }
}


/*------------------------------------------------------------------------*/
/*-----------------------VALIDATION TEXTE ADMIN---------------------------*/
/*------------------------------------------------------------------------*/

function majTexteValid(){
  


$.ajax({
     type: "POST",
     url: "index.php",
     data: 'typerubrique=1&enregistrementid=1032&rubriqueid=1000&nbenr=1&elementid='+encodeURIComponent($('#tmpid').val())+'&firstenr=2&enr2='+encodeURIComponent($('#modif').val()),
     success: function(msg){
       go('1','1031','','','','')
     }
   }); 
}