<!-- Fonction qui permet d'afficher un DIV ou de le cacher-->
<!-- Fonction utilisée dans la ZPT recherche_ecomateriaux.pt-->

function ShowHide() {			
	if(document.getElementById('switchAvancee').value==rec_a) {
		document.getElementById('switchAvancee').value=rec_s;
		document.getElementById('zoneAvancee').style.visibility='visible';
		document.getElementById('zoneAvancee').style.display='block';
	} else {
		document.getElementById('switchAvancee').value=rec_a;
		document.getElementById('zoneAvancee').style.visibility='hidden';	
		document.getElementById('zoneAvancee').style.display='none';		
	}  
}

function ShowHideInfosConfidentielles() {			
	if(document.getElementById('switchConfidentielle').value==dc_visible) {
		document.getElementById('switchConfidentielle').value=dc_cache;
		document.getElementById('zoneConfidentielle').style.visibility='visible';
		document.getElementById('zoneConfidentielle').style.display='block';
	} else {
		document.getElementById('switchConfidentielle').value=dc_visible;
		document.getElementById('zoneConfidentielle').style.visibility='hidden';			
		document.getElementById('zoneConfidentielle').style.display='none';
	}  
}

function Trim(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }
 // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function VerifChampsNonVides() {		
	if(!isValidDateFormat(document.formAjoutEM.date_validation)){
    alert("Le format de date n'est pas bon! JJ/MM/AAAA");
    return false;
	}
	
	if(!isValidDate(document.formAjoutEM.date_validation)){
    alert("La date est invalide!");
    return false;
	}
	if(Trim(document.formAjoutEM.intitule.value).length<1){
		alert("L'intitulé ne doit pas être vide");
		return false;	
	}
	return true;
}

function VerifChamps_EM_Creation() {
	if(!isValidDateFormat(document.form_EM_Creation.date_validation)){
    alert("Le format de date n'est pas bon! JJ/MM/AAAA");
    return false;
	}
	
	if(!isValidDate(document.form_EM_Creation.date_validation)){
    alert("La date est invalide!");
    return false;
	}
	if(Trim(document.form_EM_Creation.libelle.value).length<1){
		alert("L'intitulé ne doit pas être vide");
		return false;	
	}
	if(document.form_EM_Creation.target_corps_d_etats.length==0) {
	  alert("Il faut choisir au moins un corps d'état");
	  return false;
	}
	if(document.form_EM_Creation.target_destinations.length==0) {
	  alert("Il faut choisir au moins une destination");
	  return false;
	}
	if(document.form_EM_Creation.target_categories.length==0) {
	  alert("Il faut choisir au moins une catégorie");
	  return false;
	}
	
	selectAllOptions(document.form_EM_Creation.target_corps_d_etats);
	selectAllOptions(document.form_EM_Creation.target_destinations);
	selectAllOptions(document.form_EM_Creation.target_categories);
	
	return true;
}

function VerifChamps_EM_Creation_demande() {
  if(Trim(document.form_EM_Creation.libelle.value).length<1){
		alert("L'intitulé ne doit pas être vide");
		return false;	
	}
	if(document.form_EM_Creation.target_corps_d_etats.length==0) {
	  alert("Il faut choisir au moins un corps d'état");
	  return false;
	}
	if(document.form_EM_Creation.target_destinations.length==0) {
	  if(Trim(document.getElementById('proposeDestination').value).length<1){
	    alert("Il faut choisir au moins une destination ou en proposer une nouvelle");
	    return false;
	  }	  
	}
	if(document.form_EM_Creation.target_categories.length==0) {
	  if(Trim(document.getElementById('proposeCategorie').value).length<1){
	    alert("Il faut choisir au moins une catégorie ou en proposer une nouvelle");
	    return false;
	  }
	}
	
	selectAllOptions(document.form_EM_Creation.target_corps_d_etats);
	selectAllOptions(document.form_EM_Creation.target_destinations);
	selectAllOptions(document.form_EM_Creation.target_categories);
	
	return true;  
}

function VerifChamps_EM_Modification() {
	if(!isValidDateFormat(document.form_EM_Modification.date_validation)){
    alert("Le format de date n'est pas bon! JJ/MM/AAAA");
    return false;
	}
	
	if(!isValidDate(document.form_EM_Modification.date_validation)){
    alert("La date est invalide!");
    return false;
	}
	if(Trim(document.form_EM_Modification.libelle.value).length<1){
		alert("L'intitulé ne doit pas être vide");
		return false;	
	}
	if(document.form_EM_Modification.target_corps_d_etats.length==0) {
	  alert("Il faut choisir au moins un corps d'état");
	  return false;
	}
	if(document.form_EM_Modification.target_destinations.length==0) {
	  alert("Il faut choisir au moins une destination");
	  return false;
	}
	if(document.form_EM_Modification.target_categories.length==0) {
	  alert("Il faut choisir au moins une catégorie");
	  return false;
	}
	
	selectAllOptions(document.form_EM_Modification.target_corps_d_etats);
	selectAllOptions(document.form_EM_Modification.target_destinations);
	selectAllOptions(document.form_EM_Modification.target_categories);
	
	return true;
}

function VerifChamps_EM_Creation_creer_fiche() {
  if(document.EM_creation_fiche.target_destinations.length==0) {
	  alert("Il faut choisir au moins une destination pour créer une fiche");
	  return false;
	}
  selectAllOptions(document.EM_creation_fiche.target_destinations);
  return true;
}

function isNumeric(c) {
  return !(c.charCodeAt(0) < '0'.charCodeAt(0) || c.charCodeAt(0) > '9'.charCodeAt(0));
 }

function isValidDateFormat(field) {   
  isValid = (field.value.length == 10 && field.value.charAt(2) == '/' && field.value.charAt(5) == '/');
  for (i=0; isValid && i<=9; i++) {
   if (i != 2 && i != 5 && !isNumeric(field.value.charAt(i))) isValid = 0;
  }
  return isValid;
 }
 
 // Check for date validity
 function isValidDate(field)
 {  
  var DAYS_BY_MONTH = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  try {
   var arr = field.value.split("/");
   if (arr.length != 3) return false;   
   var digits = arr[2].length;
   var y = parseInt(arr[2],10);
   if (digits == 2)  // check year
    y+=(y>50)?1900:2000;// turn 03 into 2003 etc.
   else if (digits != 4) // otherwise must be 4 digits
    return false;   
   if (y < 1) return false;   
   var m = parseInt(arr[1],10); // check month
   if (m < 1 || m > 12) return false;   
   var d = parseInt(arr[0],10); // check day
   var isleap = (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));    
   if ((d < 1 ) || (d > DAYS_BY_MONTH[m-1]) || ((m == 2) && !isleap && (d > 28))) 
    return false;      
  } catch (e) {
   alert("*"+e);
   return false;
  }  
  return true;  
 }

function Show(id) {			
						document.getElementById(id).style.visibility='visible';
}	

function Hide(id) {			
						document.getElementById(id).style.visibility='hidden';
}

function open_image_browser(input_id,width_id, height_id,thumb_id,legende_id) {
	browser_window = window.open('/portal_mediatheque/MediathequeBrowse/MediathequeBrowse?input_id='+input_id+'&thumb_id='+thumb_id+'&width_id='+width_id+'&height_id='+height_id+'&legende_id='+legende_id, 'ImageBrowser', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=700, height=500')
	if(!browser_window.opener) browser_window.opener = window
}

function ouvrePopUpFabFour(flag){
  if (flag == 0) {
  var s = document.getElementById('liste_fab_four_ee').value;
    if (s != -1) {
      parent.mychild = window.open('popUpDetailFabFour?flagEE=1&s='+s,'popUpDetailFabFour', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=400, height=300');
    }
  }
  if (flag == 1) {
  var s = document.getElementById('liste_fab_four_em').value;
    if (s != -1) {
      parent.mychild = window.open('popUpDetailFabFour?flagEM=1&s='+s,'popUpDetailFabFour', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=400, height=300');
    }
  }
  
  if (flag == 2) {
  var s = document.getElementById('liste_fab_four_em').value;
    if (s != -1) {
      parent.mychild = window.open('popUpDetailFabFour?flagC=1&s='+s,'popUpDetailFabFour', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=400, height=300');
    }
  }
 
  FocusOnChild();
}

function ouvrePopUpTraduction(lg,id_parametre,libelle,champ) {
  parent.mychild = window.open('ouvrePopUpTraduction?lg='+lg+'&id_parametre='+id_parametre+'&libelle='+libelle+'&champ='+champ,'ouvrePopUpTraduction', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=600, height=50');
}

function ouvrePopUpTraductionModification(lg,id_traduction,libelle) {
  parent.mychild = window.open('ouvrePopUpTraductionModification?lg='+lg+'&id_traduction='+id_traduction+'&libelle='+libelle,'ouvrePopUpTraductionModification', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=600, height=50');
}

function ouvrePopUpTraductionDemandeModification(lg,id_traduction,id_parametre,libelle,champ) {
  parent.mychild = window.open('ouvrePopUpTraductionDemandeModification?lg='+lg+'&id_traduction='+id_traduction+'&id_parametre='+id_parametre+'&libelle='+libelle+'&champ='+champ,'ouvrePopUpTraductionModification', 'toolbar=0, scrollbars=1, location=0, statusbar=0, menubar=0, resizable=0, dependent=1, width=400, height=50');
}

/***********************************************************
Fonction : focus sur la fenêtre fille si elle existe
***********************************************************/
function FocusOnChild()
{		
	try{parent.mychild.focus();
	}
	catch(e){}
}

function VerifSiret(id){
	return true;
}

function countCheckbox(){
  var cpt=0;
  for(i=1;i<7;i++){
    if(document.getElementById('r4.'+i).checked){
      cpt++;
    }
  }
  if(cpt>3){
    alert('Vous ne pouvez cocher que 3 réponses maximum');
    return false;
  }
  return true;
}

