// JavaScript Document
var out_color="#888888";
var und;
function cocher(cas,champ,val0,val1){
	if(cas.checked){
		document.getElementById(champ).value=val1;
	}else{
		document.getElementById(champ).value=val0;
	}
}

function nouveau(com,formulaire){
	document.getElementById(formulaire).command.value=com;
	document.getElementById(formulaire).submit();
	
	
	
	
	
	}
function suprim(com,formulaire){
	if(confirm('Cet action est irréversible continuer ?')){
		
		document.getElementById(formulaire).command.value=com;
		document.getElementById(formulaire).submit();
		}
	
	}
	

var und;
function verif_mail(chaine){
	/*var filter=/^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
	if (filter.exec(chaine)==null){
		return false;
	}else{
		return true;
	}*/
	
        if (und!=chaine&&und==chaine.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
                return false;
        }
        else {
                return true;
        }


}

function verifie_mail(champs){
	
	if(!verif_mail(champs.value))window.alert(erreur_mail);
	}
	
function value_of(nom){
	//on  verifie que le champs existe
	if(document.getElementById(nom)!=und){
		//var noeud=
		alert(document.getElementById(nom).nodeName);
		
		}else if(document.getElementByName(nom)!=und){
			
		}
}
function display_block(nom,val){
	if(document.getElementById("error_"+nom)!=und){
		if(val){
			document.getElementById("error_"+nom).style.display="block";
			
		}else{
			document.getElementById("error_"+nom).style.display="none";
				
		}
		
		
	}
	
}
function confirm_champ(champs1,champs2){
	if(document.getElementById(champs1).value!=document.getElementById(champs2).value){
		
		window.alert(erreur_confirm);
		
		document.getElementById(champs1).value="";
		document.getElementById(champs2).value="";
		}
	
	
	
	}
function controle_form(nom_form){
	var verif=true;
	var le_form=document.getElementById(nom_form);
	var ancre="";
	//on reinitialise les champs
	for(var i=0;i<T_obli.length;i++){display_block(T_obli[i][0],false);}
	//on verifie les emails
	for(var i=0;i<T_mail.length;i++){
		
		if(document.getElementById(T_mail[i]).value!=""&&!verif_mail(document.getElementById(T_mail[i]).value)){
		
			display_block(T_mail[i],true);
			verif=false;
			if(ancre=="")ancre=T_mail[i];
		}else{
			display_block(T_mail[i],false);
			}
		
	}
	//on verifie les chomps obli
	
	
		for(var i=0;i<T_obli.length;i++){
			var val="";
			
			var type_champ=T_obli[i][1];
			var ele=document.getElementById(T_obli[i][0]);
			
			switch(type_champ){
				case "LIST":
				val=ele.options[ele.selectedIndex].value;
				if(val==0){val=""};
				break;
				
				case "RADIO":
				var ele=document.getElementsByName(T_obli[i][0]);
				var is_checked=false;
				for(var j=0;j<ele.length;j++){
					if(ele[j].checked){val="ok";break;}
					
					}
				break;
				
				default:
				val=ele.value;
				break;
			}
			if(val==""){
				verif=false;
				if(ancre=="")ancre=T_obli[i][0];
				display_block(T_obli[i][0],true);
			
				
				
			}
		
		}
	//on verifie les champs date par précaution
	//champs confirm
	for(var i=0;i<T_confirm.length;i++){
		var champs1=document.getElementById(T_confirm[i][0]);
		var champs2=document.getElementById(T_confirm[i][1]);
		if(champs1.value!=champs2.value){
			
				verif=false;
				champs1.value="";
				champs2.value="";
				display_block(T_confirm[i][0],true);
				display_block(T_confirm[i][1],true);
				if(ancre=="")ancre=T_confirm[i][0];
			}
		
		
		}
	
	//
	/*for(var i=0;i<T_date.length;i++){
		var val="";
		var ele=document.getElementById(T_date[i][0]);
		if(isdate(ele.value)){
			display_block(T_date[i][0],false);
			
			}else{
				display_block(T_date[i][0],true);
				verif=false;
				}
		
		}*/
	if(verif==false){
		window.alert(champs_obligatoires);
		//window.location="#"+ancre;
		if(ancre!=""){
			document.getElementById(ancre).focus();
			document.documentElement.scrollTop=document.documentElement.scrollTop-50;
		}
		

	}else{
		
		document.getElementById(nom_form).submit();
		
		}
	
	
	}
// JavaScript Document
var tab_close=new Array();
function test_close(){
	
	alert(tab_close['DATE_superette_0']);
	for(var i in tab_close)alert(i);
	//alert(tab_close);
	}
function value_off(champ){
		var ele;
		var commut=true;
		if(document.getElementById(champ)!=und){
			if(document.getElementById(champ).value==0)commut=false;
			if(document.getElementById(champ).type=="radio"){
			var les_radios=document.getElementsByName(champ);
				for(var i=0;i<=les_radios.length;i++){
					if(les_radios[i].checked){
						if(les_radios[i].value=="0"){
							commut=false;
							
						}else{
							commut=true;
							
						}
						break;
					}
				}
			}
			
			
			for (var i=1; i<arguments.length; ++i){
				tab_close[arguments[i]]=false;
				if(!commut)tab_close[arguments[i]]=true;
				if(document.getElementById(arguments[i])!=und){
					var ele=document.getElementById(arguments[i]);
					
				}else if(document.getElementsByName(arguments[i])!=und){
						var ele=document.getElementsByName(arguments[i]);
				}
				if(ele!=und&&ele.nodeName!=undefined){
					//alert(ele.nodeName.toLowerCase());
					
					switch(ele.nodeName.toLowerCase()){
						case "input":
							if(commut){
								
								ele.style.backgroundColor= "";
								switch(ele.type){
									case("hidden"):
									if(document.getElementById("__"+arguments[i])!=und){
										if(document.getElementById("__"+arguments[i]).type=="checkbox"){
											document.getElementById("__"+arguments[i]).removeAttribute("disabled");
											document.getElementById("__"+arguments[i]).parentNode.style.color= "";
											
										
										}
									
									}
									break;
									
									case("text"):
									if(document.getElementById("trans_"+arguments[i])!=und)document.getElementById("trans_"+arguments[i]).className="form_calend";
									
									ele.style.backgroundColor= "";
									ele.style.borderColor= "";
									ele.removeAttribute("readOnly");
									break;
									
									case("radio"):
									var ele2=document.getElementsByName(arguments[i]);
									for(var j=0;j<ele2.length;j++){
										
										ele2[j].removeAttribute("disabled");
										ele2[j].parentNode.style.color= "";
										
										}
									
									break;
								
								
								
								}
								//on remonte a java total
					var par=ele.parentNode;
					while(par!=und&&par.id!="span_"+arguments[0]){
						if(par.style!=und&&par.style.color!=und)par.style.color= "";
						par=par.parentNode;
					}
							}else{
								
									switch(ele.type){
									case("hidden"):
									if(document.getElementById("__"+arguments[i])!=und){
										if(document.getElementById("__"+arguments[i]).type=="checkbox"){
											document.getElementById("__"+arguments[i]).setAttribute("disabled","disabled");
											document.getElementById("__"+arguments[i]).checked=false;
											document.getElementById("__"+arguments[i]).parentNode.style.color= out_color;
											ele.value="";
										
										}
									
									}
									break;
									
									case("text"):
									ele.value="";
									ele.style.backgroundColor= "#FFFFFF";
									ele.style.borderColor= "#DDDDDD";
									//ele.setAttribute("readonly","readonly");
									ele.setAttribute("readOnly","readOnly");
									var u=ele.parentNode;
									document.getElementById("span_"+arguments[i]).style.color= out_color;
									if(document.getElementById("trans_"+arguments[i])!=und)document.getElementById("trans_"+arguments[i]).className="form_calend_out";
									while(u.id!="span_"+arguments[i]){
									
									u.style.color= out_color;
									u=u.parentNode;
									}
									break;
									
									case("select-one"):
									alert("ok");
									ele.selectedIndex=0;
									ele.setAttribute("disabled","disabled");
									ele.style.borderColor= "#DDDDDD";
									
									break;
									
									case("radio"):
									var ele2=document.getElementsByName(arguments[i]);
									for(var j=0;j<ele2.length;j++){
										if(ele2[j].value==""||ele2[j].value=="0"){
										ele2[j].checked=true;
										}else{
											ele2[j].checked=false;
											}
										ele2[j].setAttribute("disabled","disabled");
										ele2[j].parentNode.style.color= out_color;
										
										}
									
									break;
									
									default:
									alert(ele.type);
									
									
									
								
								
								}
								
								
								
					//on remonte a java total
				/*	var par=ele.parentNode;
					while(par.id!="span_"+arguments[0]){
						if(par.style.color!=und)par.style.color= out_color;
						par=par.parentNode;
					}*/
					//document.getElementById("span_"+arguments[0]).style.color= out_color;
							}
							
						break;
						case("select"):
						if(commut){
							ele.style.borderColor= "";
							ele.style.backgroundColor= "";
							ele.removeAttribute("disabled");
						
							
						}else{
							ele.selectedIndex=0;
							ele.setAttribute("disabled","disabled");
							ele.style.borderColor= "#DDDDDD";
							ele.style.backgroundColor= "#FFFFFF";
							
							//alert(ele.parentNode.firstChild.tagName);
							
						}
						
						break;
						
						
						}
					
					
					
					}
						
			
			}
		}
  }
 function isnum(champs){
	var valeur=champs.value;
	champs.value=valeur.replace(",",".");
	if(isNaN(champs.value)&&champs.value!=""){
		return false;
		

	}else{
		return true;
		
		}
}
function isdate(val) {
	
	var retour=true;
	if(val!=""){
		var tab_date=val.split('/');
		if ((tab_date.length != 3) || isNaN(parseInt(tab_date[0])) || isNaN(parseInt(tab_date[1])) || isNaN(parseInt(tab_date[2]))){
			
			retour=false;
		}else{
			//if ((Math.abs(tab_date[2])+"").length == 2) annee = parseInt(tab_date[2])+ 2000;
			var verifdate = new Date(parseInt(tab_date[2]),parseInt(tab_date[1])-1,parseInt(tab_date[0]));
			
			var annee=verifdate.getFullYear();
			
		//	alert(parseInt(verifdate.getDate())+"/"+parseInt(verifdate.getMonth())+"/"+parseInt(annee) +" "+ parseInt(tab_date[0])+"/"+ (parseInt(tab_date[1])-1)+"/"+parseInt(tab_date[2]));
			if((parseInt(verifdate.getDate()) != parseInt(tab_date[0])) || (parseInt(verifdate.getMonth()) != parseInt(tab_date[1])-1) || (parseInt(annee) != parseInt(tab_date[2]))){
				
				retour=false;
				}
			
			}
			
		
		}
		if(ANNEE_MIN!=und&&retour){
			if(Number(tab_date[2])<Number(ANNEE_MIN)){
				retour=false;
				window.alert(erreur_annee);
			
				
				
				}
			
			}
		return(retour);
}
function is_alerte(champs,type){
	var retour=true;
	var val=champs.value;
	if(type=="date"){
		
		if(!isdate(val)){
			window.alert(erreur_date);
			var retour=false;
			champs.value="";
		}
			champs.focus();
	}else if(type=="num"){
		if(!isnum(champs)){
			window.alert(erreur_num);
			var retour=false;
			champs.value="";
			}
	}else if(type=="mail"){
			if(!verif_mail(val)){
			window.alert(erreur_mail);
			var retour=false;}
		
		}
	//if(!retour){champs.focus();champs.select();}
		
		
}
function calendrier_change(nom_champ,date_d){
	document.getElementById(nom_champ).value=date_d;
	document.getElementById("calend_"+nom_champ).style.display="none";
	
	}
function calendrier(nom_champ){
	var undi;
	var http=window.location.protocol;
	
	var adresse_calend=http+"//www.kawan-group.eu/GLOBAL_JS/calend_2010.swf";
	//alert(tab_close[nom_champ]);
	if(!tab_close[nom_champ]){
	//on detect si lechamp précedent existe
	var  pre=nom_champ.substring(0,nom_champ.lastIndexOf("_")+1)+"0"+nom_champ.substring(nom_champ.lastIndexOf("_")+2);
	//alert(pre);
	if(document.getElementById(pre)!=und&&document.getElementById(pre).value!=""&&document.getElementById(nom_champ).value==""){
		var the_date=document.getElementById( pre ).value;
	}else if(document.getElementById( nom_champ).value!=""){
		var the_date=document.getElementById( nom_champ).value;
	}else{
		var the_date="next_year";
		
		}
				
	var flashvar='lg='+lang+'&date_d='+the_date+'&date_f=&separateur=/&champs='+nom_champ+'&test=1';
	var width=180;
	var height=235;
	document.getElementById('calend_content_'+nom_champ).innerHTML='<'+'object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+http+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+width+'" height="'+height+'" '+'><'+'param name="movie" value="'+adresse_calend+'"'+'><'+'param name="quality" value="high"'+'><'+'param name="allowScriptAccess" value="always"'+'><'+'param name="flashvars" value="'+flashvar+'"'+'><'+'embed src="'+adresse_calend+'" width="'+width+'" height="'+height+'" quality="high" pluginspage="'+http+'//www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"  allowscriptaccess="always" flashvars="'+flashvar+'"'+'><'+'/embed'+'><'+'/object'+'>';
							
	document.getElementById("calend_"+nom_champ).style.display="block";
	//document.getElementById("calend_"+nom_champ).style.zIndex=1010;
	
	}
	
	}
function close_calendrier(nom_champ){
	document.getElementById("calend_"+nom_champ).style.display="none";
	
	}
	
function longueur_chaine(champ,longueur){
	ele=champ.getAttribute("id");
	nb_max=longueur;
	if(und==document.getElementById("compteur_"+ele)){
		var start = champ.selectionStart;
		var end = champ.selectionEnd;

		var le_texte = document.createElement("div");
		//le_texte.setAttribute("type", "text");
		le_texte.setAttribute("id","compteur_"+ele);
		//le_texte.setAttribute("readonly","readonly");
		//le_texte.setAttribute("size",20);
		le_texte.setAttribute("class","form_compteur");
	

	champ.parentNode.appendChild(le_texte);
	champ.selectionStart = start;
	champ.selectionEnd = end;

}
 
 
	if(champ.value.length>longueur){
		champ.value=champ.value.substr(0,longueur);
		document.getElementById("compteur_"+ele).style.color="#FF0000";
	
	}else{
	document.getElementById("compteur_"+ele).style.color="";
	}
document.getElementById("compteur_"+ele).innerHTML=(longueur-champ.value.length)+" / "+longueur+" chr Max"
}
