// JavaScript Document
function Modifier(){
	document.getElementById('viewTable').style.display='none';
	document.getElementById('modifTable').style.display='';
}
function Annuler(){
	document.getElementById('viewTable').style.display='';
	document.getElementById('modifTable').style.display='none';
}




//------------------------------------------------------------
function DemandeConfirmation(form){
	if(confirm('Vous êtes sur le point de modifier le contenu de la base de données. Êtes vous sûr de vouloir continuer?!')==true)
	document.getElementById(form).submit();
}
//------------------------------------------------------------
function ReplaceSpecChar(str){
	str=str.replace('&amp;', '&');
	str=str.replace('&ccedil;', 'ç');
	str=str.replace('&eacute;', 'é');
	str=str.replace('&egrave;', 'è');
	str=str.replace('&ecirc;', 'ê');
	str=str.replace('&ocirc;', 'ô');
	str=str.replace('&icirc;', 'î');
	return str;
}
//------------------------------------------------------------
var UserColorsInSelect=false;
function AfficheOptions(SelectOptions, MultiSelect, txt){
	  
	opts='';	 
	txt=ReplaceSpecChar(txt);
	if(MultiSelect==true)
		{
			tab=txt.split(',');
			for(var k=0;k<SelectOptions.length;k++){
				opts+='<option value="'+SelectOptions[k].value+'"';
				for(e=0;e<tab.length;e++)
					if(ReplaceSpecChar(SelectOptions[k].text)==tab[e]) opts+=' selected';
				if(UserColorsInSelect==true) opts+=' style="background-color:'+SelectOptions[k].color+'"';
				opts+='>'+SelectOptions[k].text+'</option>';
			}
		}
	else 
	for(var k=0;k<SelectOptions.length;k++){		 
		opts+='<option value="'+SelectOptions[k].value+'"';
		if(SelectOptions[k].text==txt) opts+=' selected';
		opts+='>'+SelectOptions[k].text+'</option>';
	}
	return opts;
	
}
//------------------------------------------------------------
function GetTextByValue(SelectOptions, v){
	for(var j=0;j<SelectOptions.length;j++)
		if(v==SelectOptions[j].value) return SelectOptions[j].text;
	return '';
}
//------------------------------------------------------------
function AddOptions(SelectOptions, obj){
	for(var n=0;n<SelectOptions.length;n++){
		opt=document.createElement('option');
		opt.value=SelectOptions[n].value;
		opt.text=SelectOptions[n].text;
		obj.add(opt);	
		}
}
//------------------------------------------------------------
function AddCell(field, len, Row)
{
	 
	cellName=field.name+'_'+len;
	type=field.type;
	width=field.dim;
	multi=field.multi;
	var cell=document.createElement("TD");  
	var input;	
	if(type=='select')
		 {
			 input=document.createElement("select");
			 if(field.multi==true) {input.size=6; input.multiple=true;}
			 AddOptions(Tableau(field.name), input);
		 }
	else {
			input=document.createElement("input");
			input.type=type;	
		}
	input.id=cellName;
	input.name=cellName;	 
	input.style.border="#CCCC99 1px solid";
	input.style.width=width;	
	input.style.background='#FFFFFF'; 
	input.style.color="#330000";
	input.style.font.size="10px";

	cell.appendChild(input);
	Row.appendChild(cell);	
	return cell;
}
//------------------------------------------------------------
function ExtractfileName(url){
		tab=url.split('/');
		return tab[tab.length-1];
}
//------------------------------------------------------------
var typeFile='img';
var AutoIncKey=false;
var ForeignKey="";

//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------
//------------------------------------------------------------

function AddLine(OldLines)
{	 	
	var Fields=GetFields(OldLines);
	var newLines=GetnewLines(OldLines);
	var NbLines=GetNbLines(OldLines);
	var ListeNew=GetListeNew(OldLines);
	var AddBtn=GetAddBtn(OldLines);
	var row;	
	len=document.getElementById(NbLines).value;
	row=document.createElement("TR"); 
	row.id=len;
	row.style.background='#FFFFCC';

	for(i=0;i<Fields.length;i++)
		AddCell(Fields[i], len, row);
	
	cell3=document.createElement("TD"); 
	cell3.align='center'; 
	cell3.style.cursor='pointer';
	cell3.innerHTML='<img src="../images/go.gif" align=absmiddle onClick="ValiderLine(\''+OldLines+'\', \''+row.id+'\')">&nbsp;<img src="../images/stop.gif" align=absmiddle onClick="AnnulerLine(\''+OldLines+'\', \''+row.id+'\')">';
	row.appendChild(cell3);
	
	document.getElementById(newLines).appendChild(row);
	document.getElementById(NbLines).value=eval(document.getElementById(NbLines).value)+1;
	document.getElementById(AddBtn).style.display="none";
	
	document.getElementById(ListeNew).value+=row.id+',';
	 
}
//------------------------------------------------------------
function DelLine(OldLines, id){
	if(confirm("Confirmez la suppression de cette ligne. Êtes vous sûr ?!")==false) return;
	row=getRowById(OldLines, id);
	
	var newLines=GetnewLines(OldLines);
	var ListeNew=GetListeNew(OldLines);
	var ListeDel=GetListeDel(OldLines);
	var AddBtn=GetAddBtn(OldLines);
	found=false;
	
	for(i=0; i<document.getElementById(newLines).rows.length;i++)
		if(document.getElementById(newLines).rows[i].id==id) { found=true; break; }
	if(found){				
		document.getElementById(newLines).removeChild(row);
		document.getElementById(AddBtn).style.display="";
		s=document.getElementById(ListeNew).value;
		document.getElementById(ListeNew).value=s.replace(id+',', '');
		}
	else{
		if(AutoIncKey==true) document.getElementById(ListeDel).value+=row.name+',';
		else document.getElementById(ListeDel).value+=row.cells[0].innerHTML+',';
		
		document.getElementById(OldLines).removeChild(row);
		}
}

//------------------------------------------------------------
function AnnulerLine(OldLines, id){
 
	var Fields=GetFields(OldLines);
	var newLines=GetnewLines(OldLines);
	var NbLines=GetNbLines(OldLines);
	var ListeNew=GetListeNew(OldLines);
	var ListeDel=GetListeDel(OldLines);
	var ListeUpdate=GetListeUpdate(OldLines);
	var AddBtn=GetAddBtn(OldLines);
	
	row=getRowById(OldLines, id);
	found=false;
	for(i=0; i<document.getElementById(newLines).rows.length;i++)
		if(document.getElementById(newLines).rows[i].id==id) { found=true; break; }
	if(found){				
		document.getElementById(newLines).removeChild(row);
		document.getElementById(AddBtn).style.display="";
		s=document.getElementById(ListeNew).value;
		document.getElementById(ListeNew).value=s.replace(id+',', '');
		}
	else{
		//-- ici
		
		for(i=0;i<Fields.length;i++){
				if(document.getElementById('H'+Fields[i].name+'_'+id).value=='')row.cells[i].innerHTML='';
				else
						if(Fields[i].type=='file')
							{
								if(typeFile=='img')
						 	 		row.cells[i].innerHTML='<a nohref onClick="fenetre2(\''+document.getElementById('H'+Fields[i].name+'_'+id).value+'\',640,600)" style="cursor:pointer"><img name="'+Fields[i].name+'_'+id+'" src="'+document.getElementById('H'+Fields[i].name+'_'+id).value+'" width=100><br><img src="images/zoom.gif" border=0></a>';
								else row.cells[i].innerHTML='<a type="link" name="'+Fields[i].name+'_'+id+'" target="_blank" href="'+document.getElementById('H'+Fields[i].name+'_'+id).value+'">'+ExtractfileName(document.getElementById('H'+Fields[i].name+'_'+id).value)+'</a>';
							}
						else if(Fields[i].type=='select')
								{				 
									row.cells[i].innerHTML=document.getElementById('H'+Fields[i].name+'_'+id).value;
								}
							 else row.cells[i].innerHTML=document.getElementById('H'+Fields[i].name+'_'+id).value;
			}
		var cellBtn=document.getElementById('cellBtn_'+id);
		if(cellBtn==null) cellBtn=row.cells[Fields.length];
		cellBtn.innerHTML=(CanDuplicate?'<img src="images/duplik.gif" align=absmiddle alt="Dupliquer" onClick="DuplikLine(\''+OldLines+'\', \''+id+'\')">&nbsp;':'')+'<img src="../images/modif.gif" align=absmiddle onClick="ModifLine(\''+OldLines+'\', \''+id+'\')">&nbsp;<img src="../images/suppr.gif" align=absmiddle onClick="DelLine(\''+OldLines+'\', \''+id+'\')">';		
		
		s=document.getElementById(ListeUpdate).value;
		document.getElementById(ListeUpdate).value=s.replace(id+',', '');
		} 
}
//------------------------------------------------------------
function ValiderLine(OldLines, which){
		if(document.getElementById(Key+'_'+which)!=null && document.getElementById(Key+'_'+which).value=='' ){ alert("Le champs "+Key+" ne peut pas être vide"); document.getElementById(Key+'_'+len).focus(); return; }

	var Fields=GetFields(OldLines);
	var newLines=GetnewLines(OldLines);
	var NbLines=GetNbLines(OldLines);
	var ListeNew=GetListeNew(OldLines);
	var ListeDel=GetListeDel(OldLines);
	var ListeUpdate=GetListeUpdate(OldLines);
	var AddBtn=GetAddBtn(OldLines);

	row=getRowById(OldLines, which);
		row.style.backgroundColor='#FFFFCC';
		// ici
		
		var Values = Array();
		for(i=0;i<Fields.length;i++)
		//if(CanModify[i]==true)
		{			
			 
			Values[i]=document.getElementById(Fields[i].name+'_'+which).value;
			if(Fields[i].type=='file') {
				}
			else if(Fields[i].type=='select') 
					{	
						if(Fields[i].multi==true) {
									Values[i]='';	
									T='';
									for (j=0;j< document.getElementById(Fields[i].name+'_'+which).length;j++) 
										if(document.getElementById(Fields[i].name+'_'+which).options[j].selected){
											Values[i]+=document.getElementById(Fields[i].name+'_'+which).options[j].value+",";
											T+=document.getElementById(Fields[i].name+'_'+which).options[j].text+",";
										}
									row.cells[i].innerHTML=T;
							}
						else row.cells[i].innerHTML=GetTextByValue(Tableau(Fields[i].name), Values[i]);
					}
			else row.cells[i].innerHTML=Values[i];
		}
		
		
		var cellBtn=document.getElementById('cellBtn_'+which);
		if(cellBtn==null) cellBtn=row.cells[Fields.length];
	
		cellBtn.innerHTML=(CanDuplicate?'<img src="images/duplik.gif" align=absmiddle alt="Dupliquer" onClick="DuplikLine(\''+OldLines+'\',\''+which+'\')">&nbsp;':'')+'<img src="../images/modif.gif" align=absmiddle onClick="ModifLine(\''+OldLines+'\', \''+which+'\')">&nbsp;<img src="../images/suppr.gif" align=absmiddle onClick="DelLine(\''+OldLines+'\', \''+which+'\')">';		
		
		// pour les tables avec clé auto_increment, on stocke la valeur de la clé dans le champs name du tr ensuite on le reccupère ici dans un champs hidden
		if(AutoIncKey==true) cellBtn.innerHTML+='<input type=hidden id="'+Key+'_'+which+'" name="'+Key+'_'+which+'" value="'+row.name+'">';
		else 				 cellBtn.innerHTML+='<input type=hidden id="'+Key+'_Old'+which+'" name="'+Key+'_Old'+which+'" value="'+row.name+'">';

		if(ForeignKey!="") cellBtn.innerHTML+='<input type=hidden id="'+ForeignKey+'_'+which+'" name="'+ForeignKey+'_'+which+'" value="'+document.getElementById(ForeignKey).value+'">';
		
		for(i=0;i<Fields.length;i++)
			//if(CanModify[i]==true){ alert(Fields[i]+'_'+which+'='+Values[i]);
				cellBtn.innerHTML+='<input type=hidden id="'+Fields[i].name+'_'+which+'" name="'+Fields[i].name+'_'+which+'" value="'+Values[i]+'">';
			//}
	if(document.getElementById(AddBtn)!=null)		 
	document.getElementById(AddBtn).style.display="";
	
}
//------------------------------------------------------------
function getRowById(OldLines, id){
	row=null;
	var newLines=GetnewLines(OldLines);
	
	if(document.getElementById(newLines)!=null)
	for(i=0; i<document.getElementById(newLines).rows.length;i++)
			if(document.getElementById(newLines).rows[i].id==id) 
					{ row=document.getElementById(newLines).rows[i]; break; }
 
	if(row==null)
	for(i=0; i<document.getElementById(OldLines).rows.length;i++)
			if(document.getElementById(OldLines).rows[i].id==id) 
					{ row=document.getElementById(OldLines).rows[i]; break; }
 
	return row;	
}
//------------------------------------------------------------
function ModifLine(OldLines, id){
	
	var Fields=GetFields(OldLines);
	var ListeUpdate=GetListeUpdate(OldLines);

	
	row=getRowById(OldLines, id);
	  
	// ici
	for(i=0;i<Fields.length;i++){
		if(Fields[i].modif==false) readonly=" readonly";
		else readonly="";
		if(Fields[i].type=='file')
			{ 
			 	src='';
			 	if(document.getElementById(Fields[i].name+'_'+id)!=null)	{
						typeFile=document.getElementById(Fields[i].name+'_'+id).type;			 
						if(typeFile=='img')
			     	 		 src=document.getElementById(Fields[i].name+'_'+id).src;
			 			else src=document.getElementById(Fields[i].name+'_'+id).href;
					}
			 	row.cells[i].innerHTML='<input type="file" name="'+Fields[i].name+'_'+id+'" id="'+Fields[i].name+'_'+id+'" value="'+src+'" class=inputs1 style="width:'+Fields[i].dim+'px;"><input type=hidden id="H'+Fields[i].name+'_'+id+'" value="'+src+'">';
				if(src!='')
					row.cells[i].innerHTML+='<br><input type=checkbox name="Supp_'+Fields[i].name+'_'+id+'" id="Supp_'+Fields[i].name+'_'+id+'" value=false onclick="this.value=this.checked;"> Supprimer';
			}
		else if(Fields[i].type=='select')
			{
				txt=row.cells[i].innerHTML;
				if(Fields[i].multi==true) multi='size=6 multiple';
//				if(MultiSelect==true) multi='size=6 multiple';
				else multi='';
				row.cells[i].innerHTML='<select name="'+Fields[i].name+'_'+id+'" id="'+Fields[i].name+'_'+id+'" class=inputs1 style="width:'+Fields[i].dim+'px;"'+multi+'>'+
										AfficheOptions(Tableau(Fields[i].name), Fields[i].multi, txt)+'</select><input type=hidden id="H'+Fields[i].name+'_'+id+'" value="'+txt+'">';
				 
			}
		else row.cells[i].innerHTML='<input type='+Fields[i].type+' name="'+Fields[i].name+'_'+id+'" id="'+Fields[i].name+'_'+id+'" value="'+row.cells[i].innerHTML+'" class=inputs1'+readonly+' style="width:'+Fields[i].dim+'px;"><input type=hidden id="H'+Fields[i].name+'_'+id+'" value="'+row.cells[i].innerHTML+'">';
		
	}

	var cellBtn=document.getElementById('cellBtn_'+id);
	if(cellBtn==null) cellBtn=row.cells[Fields.length];
	cellBtn.innerHTML='<img src="../images/go.gif" align=absmiddle onClick="ValiderLine(\''+OldLines+'\', \''+id+'\')">&nbsp;<img src="../images/stop.gif" align=absmiddle onClick="AnnulerLine(\''+OldLines+'\', \''+id+'\')">';

for(i=0;i<Fields.length;i++) if(Fields[i].modif==true)
	{ document.getElementById(Fields[i].name+'_'+id).focus(); break;}
	
	document.getElementById(ListeUpdate).value+=id+',';
}
//------------------------------------------------------------
function DuplikLine(OldLines, id){
	var ListeDup=GetListeDup(OldLines);
	
	 row=getRowById(OldLines, id);
	 if(AutoIncKey==true) 
		  dupkey=row.name;
	 else dupkey=row.cells[0].innerHTML;
	 document.getElementById(ListeDup).value+=dupkey+',';
	 row.style.backgroundColor='#ffffcc';
}
//------------------------------------------------------------
// AJAX
//------------------------------------------------------------
var xhr = null;
function getXhr(){
	if(window.XMLHttpRequest) // Firefox et autres
		xhr = new XMLHttpRequest();
	else 
	if(window.ActiveXObject){ // Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
		}
	}
//------------------------------------------------------------
//------------------------------------------------------------
	mouse_x = 0;
	mouse_y = 0;

function position(e) {
mouse_x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.parentNode.scrollLeft;
mouse_y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.parentNode.scrollTop;
//window.status = "Souris x:"+mouse_x+" | y:"+mouse_y;
}

document.onmousemove = position;
//------------------------------------------------------------
function Surbrillance(id, etat){
if(etat=='on')
 	 id.style.backgroundColor="#C9A054";
else id.style.backgroundColor="#AC7C3B";
}
//------------------------------------------------------------
function isValideDate(d){
	tab=d.split('/');
	if(tab.length!=3) return false;
	if(isNaN(tab[0]) || isNaN(tab[1]) || isNaN(tab[2])) return false;
	if(tab[0]<1 || tab[0]>31) return false;
	if(tab[1]<1 || tab[1]>12) return false;
	if(tab[2].length!=4) return false;
	return true;
}
//------------------------------------------------------------
function fenetre2(file, xwidth, xheight)
{
  xleft=(screen.width-xwidth)/2;
  xtop=(screen.height-xheight)/2-60;
return window.open(file, "","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1,width="+xwidth+",height="+xheight+",left="+xleft+",top="+xtop);
}
//------------------------------------------------------------
