/* Fonctions utilisées pour module de recherhe de photos : 
lod, affiche_option,cocher_decocher,verif_option */ 

function lod()
{
	if (document.form_recherche_photo.type_recherche.selectedIndex == 1)
		document.getElementById('options').style.visibility="visible";
}

function affiche_option(div_id)
{
	if (document.form_recherche_photo.type_recherche.selectedIndex == 1)
		document.getElementById(div_id).style.visibility="visible";
	else
		document.getElementById(div_id).style.visibility="hidden"; 
}

function verif_option(div_id)
{
	if(document.getElementById(div_id).style.visibility=="visible")
	{	
		if(document.form_recherche_photo.option[0].checked || document.form_recherche_photo.option[1].checked)
			return true;
		else
		{
			alert('Vous devez choisir au moins une option : Générique ou Détail');
			return false;
		}
	}			
}

function cocher_decocher(action,le_form,name_element)
{
	var elts=document.forms[le_form].elements[name_element+'[]'];
	
	if(action=='cocher')
	{
		for (var i = 0; i < elts.length; i++) {
			elts[i].checked=true;}
	}
	else if(action=='decocher')
	{
		for (var i = 0; i < elts.length; i++) {
			elts[i].checked=false;}
	}	
}


function verif_choix(valeur_radio)
{
	var valeur=false;
	
	for (var i=0; i<valeur_radio.length;i++) 
	{
         if (valeur_radio[i].checked) 
		 {
            valeur=true;
         }
     }
	 if(valeur==false)
	 	alert('Vous devez choisir le ou les site(s) de destination');
	
	return valeur;
}

function verifmail()
{
	var mail=document.form_email.new_email.value;
	var testm = false ;
	for (var j=1 ; j<(mail.length) ; j++) 
	{
		if (mail.charAt(j)=='@') 
		{
			if (j<(mail.length-4))
			{
    			for (var k=j ; k<(mail.length-2) ; k++) 
				{ 
     				if (mail.charAt(k)=='.') 
							testm = true;
    			}
   			}
  		}
 	}	
	
	if (testm==false) alert("L'adresse email saisie n'est pas au bon format.");
	return testm;
}

function verifmail_cpte(langue)
{
	//Vérif pour l'email
	var mail=document.modif_compte_form.email.value.replace(/(^\s*)|(\s*$)/g,'');
	var testm = false ;
	
	if (mail!='')
	{
		for (var j=1 ; j<(mail.length) ; j++) 
		{
			if (mail.charAt(j)=='@') 
			{
				if (j<(mail.length-4))
				{
					for (var k=j ; k<(mail.length-2) ; k++) 
					{ 
						if (mail.charAt(k)=='.') 
								testm = true;
					}
				}
			}
		}		
		if (testm==false) 
		{	
			if(langue=='F')
				alert("L'adresse email saisie n'est pas au bon format.");
			
			if(langue=='S')
				alert("Dirección email errónea. Rogamos rellene correctamente ese campo.");
				
			if(langue=='A')
				alert("E-mail address incorrect - please complete this field correctly.");	
		}
	}
	else
		testm=true;
	
	//Vérif pour l'email 2
	var mail_b=document.modif_compte_form.email_b.value.replace(/(^\s*)|(\s*$)/g,'');
	
	var testmb = false ;
	
	if (mail_b!='')
	{
		for (var j=1 ; j<(mail_b.length) ; j++) 
		{
			if (mail_b.charAt(j)=='@') 
			{
				if (j<(mail_b.length-4))
				{
					for (var k=j ; k<(mail_b.length-2) ; k++) 
					{ 
						if (mail_b.charAt(k)=='.') 
								testmb = true;
					}
				}
			}
		}		
		if (testmb==false) 
		{
			if(langue=='F')
				alert("L'adresse email 2 saisie n'est pas au bon format.");
			
			if(langue=='S')
				alert("Dirección email 2 errónea. Rogamos rellene correctamente ese campo.");
				
			if(langue=='A')
				alert("E-mail address 2 incorrect - please complete this field correctly.");	
			
		}
	}
	else
		testmb=true;
	
	if(testm==true && testmb==true)
		return true;
	else
		return false;
}

function tohtml(editeurId,divHtmlId,txtHtmlId) {
	editeurhtml=document.getElementById(editeurId+'_html');
	editeurtxt=document.getElementById(editeurId+'_txt');
	divHtml=document.getElementById(divHtmlId);
	txtHtml=document.getElementById(txtHtmlId);
	
	editeurhtml.style.display="none";
	editeurtxt.style.display="block";
	txtHtml.value=divHtml.innerHTML;
	return false;
}

function towysiwyg(editeurId,divHtmlId,txtHtmlId) {
	editeurhtml=document.getElementById(editeurId+'_html');
	editeurtxt=document.getElementById(editeurId+'_txt');
	divHtml=document.getElementById(divHtmlId);
	txtHtml=document.getElementById(txtHtmlId);
	
	editeurhtml.style.display="block";
	editeurtxt.style.display="none";
	divHtml.innerHTML=txtHtml.value;
	return false;
}

function convertLayer(editeurId,divHtmlId,txtHtmlId) {
	editeurhtml=document.getElementById(editeurId+'_html');
	editeurtxt=document.getElementById(editeurId+'_txt');
	divHtml=document.getElementById(divHtmlId);
	txtHtml=document.getElementById(txtHtmlId);

	if (editeurhtml.style.display=="none") 	divHtml.innerHTML=txtHtml.value;
	txtHtml.value=divHtml.innerHTML;
}

function getCookieVal(offset)
{
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom)
{
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	
	}
	return null;
}

function EcrireCookie(nom, valeur)
{
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}

function switchDiv(noCat){
	imgsec=document.getElementById('img'+noCat);
	divsec=document.getElementById(noCat);
	if (divsec.style.display=="none"){
		divsec.style.display="block";
		EcrireCookie('switchdiv_'+noCat,'O');
		if (imgsec){
			imgsec.src='images/arbo/tree_expand.gif';
	   	}
	} else {
		if ( (divsec.style.display=="block") || (divsec.style.display=="") ){
			EcrireCookie('switchdiv_'+noCat,'F');
	   		divsec.style.display="none";
			if (imgsec){
				imgsec.src='images/arbo/tree_collapse.gif';
			}
		}
	}
	return false;
	
}
function updateDiv(noCat){
	imgsec=document.getElementById('img'+noCat);
	divsec=document.getElementById(noCat);
	valcook=LireCookie('switchdiv_'+noCat)
	if (valcook=='O'){
		divsec.style.display="block";
		if (imgsec){
			imgsec.src='images/arbo/tree_expand.gif';
	   	}
	}
	return false;
	
}
function switchTreeDiv(div,imagepath) {
	imgsec=document.getElementById('img'+div);
	divsec=document.getElementById(div);
	if (divsec.style.display=="none") { // ouvrir
		divsec.style.display="";
		
		if (imgsec)
		{
			imgsec.src=imagepath+'/tree_collapse.gif';
		}
   	}else {  // fermer
   		divsec.style.display="none";
		EcrireCookie('div','F');
		if (imgsec)
		{
			imgsec.src=imagepath+'/tree_expand.gif';
		}
   	}
	return false;
}

function is_mozilla()
{
	var ua=navigator.userAgent.toLowerCase();
	return (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
}

function getElementsByTagAndName(tagname, name)
{
	if (is_mozilla()) return document.getElementsByName(name);
	var elems = document.getElementsByTagName(tagname);
	var result=new Array();
	for (i=0; i<elems.length; i++)
	{
		var elem = elems[i];
		if (elem.name == name) result.push (elem);
	}
	return result;
}

function menu_admin_switch(id, url_root)
{
  var tr_list = getElementsByTagAndName('tr', id);		
  var show = (tr_list[0].style.display=='none');
  do_menu_admin_switch(id, url_root, tr_list, show);  
}

function do_menu_admin_switch(id, url_root, tr_list, show) 
{
  var img = document.getElementById(id+"_img");
  var i;
  for (i=0; i<tr_list.length; i++)
  {
    tr = tr_list[i];
    if (show) {
	  tr.style.display = '';
	  img.src = url_root+'images/treemenu2/tree_collapse.gif';
    } else {
      tr.style.display = 'none';
	  img.src = url_root+'images/treemenu2/tree_expand.gif';
	}

	var children = getElementsByTagAndName('tr', tr.id);
	if (!show && (0<children.length)) do_menu_admin_switch(tr.id, url_root, children, show)
  }
}

function writeEmailingField(modid, tabfield, selectionObj){
    // Zone d'affichage de la liste des champs personnalisables disponible
    var field = document.getElementById(modid+"_listfield");    

    // Si la zone existe, on remplit avec la liste des champs personnalisables
    if( field != undefined ){
        selectionIndex=selectionObj.selectedIndex;
        field.innerHTML = tabfield[selectionIndex];
    }
}

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   string   name nom du champs
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form,name, do_check)
{
    var elts= document.forms[the_form].elements[name+'[]'];                  
   
    if (elts.length>0) {
        for (var i = 0; i < elts.length; i++) {
            elts[i].checked = do_check;
        } // end for
    } else {
        elts.checked        = do_check;
    } // end if... else

    return true;
} // end of the 'setCheckboxes()' function

/**
* Controle si on a selectionné au moins une checkbox
* @param string form name
* @param  string name
* @return boolean 
**/
function controlcheckbox(the_form,name,langue)
{
      var elts= document.forms[the_form].elements[name+'[]'];                  
      
    if (elts.length>0) {
  
        for (var i = 0; i < elts.length; i++) {
   
          if (elts[i].checked==true) return true;
        } // end for
    } //end if
    else {
         if (elts.checked==true) return true;
    } // end if... else
	
	if(langue=='F')
    	alert("Vous devez sélectionner au moins une référence");
		
	if(langue=='S')
		alert("Debe seleccionar por lo menos una referencia");
		
	if(langue=='A')
		alert("You must select at least one reference.");
		
    return false;
}

function PopupCentrer(page,largeur,hauteur,options) 
{
  var top=(screen.height-hauteur)/2;
  var left=(screen.width-largeur)/2;
  window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

String.prototype.trim = function () 
{
   return this.replace(/^\s*|\s*$/,"");
}

function Deplacer(l1,l2) 
{
	var no_code=0;
	for (var n=0;n<l1.length;n++)
	{
		if (l1.options.selectedIndex>=0) 
		{
				o=new Option(l1.options[l1.options.selectedIndex].text,l1.options[l1.options.selectedIndex].value);
				l2.options[l2.options.length]=o;
				l1.options[l1.options.selectedIndex]=null;
				n--;
				no_code=1;
		}
	}
	if(no_code==0)
	{
		alert("Aucun code client choisi");
	}
}

function Ajouter_liste(l1,l2) 
{
	var no_ref=0;
	var exist=0;
	for (var n=0;n<l1.length;n++)
	{		
		if (l1.options[n].selected==true) 
		{
				for(var k=0;k<l2.length;k++)
				{
					if(l2.options[k].value==l1.options[n].value)
					{
						exist=1;
						break;
					}
					else
						exist=0;
				}
				if(exist==0)
				{
					o=new Option(l1.options[n].text,l1.options[n].value);
					l2.options[l2.length]=o;
					no_ref=1;
				}
		}
	}
	if(no_ref==0)
	{
		alert("Aucune référence choisie");
	}
}

function Supprimer_liste(l1) 
{
	var no_ref=0;
	for (var n=0;n<l1.length;n++)
	{
		if (l1.options[n].selected==true) 
		{
				l1.options[n]=null;
				n--;
				no_ref=1;
		}
	}
	if(no_ref==0)
	{
		alert("Aucune référence choisie");
	}
}

function Supprimer_tout(l1) 
{
	var total=l1.length;
	for (var n=0;n<total;n++)
	{
		l1.options[0]=null;
	}
}

function affich_img_grav(id_sel,id_img, mtc_sel)
{
	//On récupére le libellé associé à l'option choisie
	var opt_texte=document.getElementById(id_sel).options[document.getElementById(id_sel).selectedIndex].text;
	var type_grav=opt_texte.substring(0,1); //On récupère le type de la gravure (1ère letrre du libellé)
	//document.getElementById(id_img).src="";
	document.getElementById(id_img).src="images/gravures/gravure_";
	//Si c'est une gravure externe
	if(id_img=="img_type_gravure_ext")
		document.getElementById(id_img).src+="ext_";
	
	document.getElementById(id_img).src+=type_grav+"_"+mtc_sel+".jpg";
}

function affich_msg_grav(id_msg)
{
	document.getElementById(id_msg).style.visibility="visible";		
}

function cache_msg_grav(id_msg)
{	
	document.getElementById(id_msg).style.visibility="hidden";
}

function ajout_carac_speciaux(id_textarea,value_carac)
{
	var field  = document.getElementById(id_textarea); 
	//Si le textarea n'est pas désactivé
	if(field.disabled!=true)
	{
		var scroll = field.scrollTop;
		field.focus();
		
		if (window.ActiveXObject) { // C'est IE
				var textRange = document.selection.createRange();  
				var currentSelection = textRange.text;
				textRange.text = value_carac + currentSelection;
				textRange.select();
		} else { // Ce n'est pas IE
				var startSelection   = field.value.substring(0, field.selectionStart);
				var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
				var endSelection     = field.value.substring(field.selectionEnd);
				
				field.value = startSelection + value_carac + currentSelection + endSelection;
				field.focus();
				field.setSelectionRange(startSelection.length + value_carac.length, startSelection.length + value_carac.length + currentSelection.length);
		} 
	
		field.scrollTop = scroll; // et on redéfinit le scroll.
	}
}

function verif_load()
{
	if(end_load==0)
		setTimeout("verif_load()",100);
	else
		eval(fct);
}

function Popup_prix(id,ref)
{
	var lien="";
	
	if(id!='' && id!=false)
	 lien="?numid="+id;
	 
	if(ref!='' && ref!=false)
	  lien="?ref="+ref;
	//var h=Math.round(screen.availHeight/3);
	//var w=Math.round(screen.availWidth/3);
	var h=170;
	var w=270;
	var w=window.open('prix_cdj.php'+lien,'Prix','menubar=no,location=no,resizable=no,width='+w+',height='+h+',top='+(screen.availHeight-h)/2+',left='+(screen.availWidth-w)/2);
	w.document.close();
    w.focus();
}