function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MakeArray(n) {
	this.length = n;
	return this;
}
monthNames = new MakeArray(12);
monthNames[1] = "janvier";
monthNames[2] = "février";
monthNames[3] = "mars";
monthNames[4] = "avril";
monthNames[5] = "mai";
monthNames[6] = "juin";
monthNames[7] = "juillet";
monthNames[8] = "août";
monthNames[9] = "septembre";
monthNames[10] = "octobre";
monthNames[11] = "novembre";
monthNames[12] = "décembre";

dayNames = new MakeArray(7);
dayNames[1] = "dimanche";
dayNames[2] = "lundi";
dayNames[3] = "mardi";
dayNames[4] = "mercredi";
dayNames[5] = "jeudi";
dayNames[6] = "vendredi";
dayNames[7] = "samedi";

function DecomposeDate(LeParam1){ 

// Sépare les jours, les mois et les années dans une date de type "22/05/1981"
// Renvoye le tout dans un tableau de taille 3

LeRetour = new Array(3);
LeJour="";
LeMois="";
LeAnnee="";

// Extraction du jour
i=0;
while((LeParam1.charAt(i)!="/")&&(i<10)){
LeJour+=LeParam1.charAt(i);
i++;
}
if(LeJour.charAt(0)=="0"){
LeJour=LeJour.charAt(1);
}
LeParam1=LeParam1.substring(i+1,LeParam1.length);

// Extraction du mois
i=0;
while((LeParam1.charAt(i)!="/")&&(i<10)){
LeMois+=LeParam1.charAt(i);
i++;
}
if(LeMois.charAt(0)=="0"){
LeMois=LeMois.charAt(1);
}
LeParam1=LeParam1.substring(i+1,LeParam1.length);


// Extraction de l'année
LeAnnee=LeParam1;
LeRetour[0]=LeJour;
LeRetour[1]=LeMois;
LeRetour[2]=LeAnnee;
return LeRetour;
}



function customDateString(StringDate) {
	var LaDate=DecomposeDate(StringDate);
	var oneDate=new Date(LaDate[2],LaDate[1]-1,LaDate[0]);
	var theDay = dayNames[oneDate.getDay() + 1];
	var theMonth = monthNames[oneDate.getMonth() + 1];
	var theYear = oneDate.getYear() + 1900;
	return theDay + " " + oneDate.getDate() + " " + theMonth;
}


function writeText(txt,typ)
{
	if (typ=='arrdt')
	{
		if (txt!='')
		{ if (txt=='Belgique frontalière' || txt=='Picardie' || txt=='Nord Pas-de-Calais')
			{
				document.getElementById("arrdt").innerHTML=txt;
				AffiComm(23);
			}
			else
				document.getElementById("arrdt").innerHTML='arrondissement de '+txt;
		}
		else
		{	if (document.form1.arrt.value!='')
			{
				document.getElementById("arrdt").innerHTML='arrondissement de '+document.form1.arrt.value
				if (document.getElementById("rdate").innerHTML!='')
					AffiComm(4)
				else
					AffiComm(21);
			}
			else
				initarrt();
		}
	}
	if (typ=='rarrdt')
	{
		if (txt!='') 
		{	document.getElementById("arrdt").innerHTML='arrondissement de '+txt;
			document.form1.arrt.value=txt;
			if (document.getElementById("rdate").innerHTML!='')
				AffiComm(4)
			else
				AffiComm(21);
		}
		else
			initarrt();
	}
	if (typ=='rdate')
	{
		if (txt!='') 
			document.getElementById("rdate").innerHTML=txt
		else
			initdate();
	}
}

function initdate() {
	for(var i=0; i<document.form1.dat.length; i++)
		document.form1.dat[i].checked=false;
	document.getElementById("rdate").innerHTML='';
}

function initarrt() {
	document.form1.arrt.value='';
	document.getElementById("arrdt").innerHTML='';
}

function initvil() {
	document.form1.rvil.value='';
}



function testcheckDate() {
	var testcheck=false;
	for(var i=0; i<document.form1.dat.length; i++)
	{	if (document.form1.dat[i].checked) testcheck=true;
	}
	return testcheck; 
}

function checkDate()
{ 
	var k=99;l=99;
  for(var i=0; i<document.form1.dat.length; i++)
  {   if(document.form1.dat[i].checked)
      { if (k==99) k=i;
      	if (k!=99 && l==99) l=i;
      	if (k!=99 && l!=99 && i>l) l=i;
      }
  }
  var txt='';
  if(k<99)
  {
	  for(var i=k; i<=l; i++ )
	  {
			document.form1.dat[i].checked=true;
		}
		if (k==l)
			txt=customDateString(document.form1.dat[k].value)
		else
			txt='du '+ customDateString(document.form1.dat[k].value)+' au '+ customDateString(document.form1.dat[l].value);
	}
	else
		txt='';
	document.getElementById("rdate").innerHTML=txt;
	if (document.form1.arrt.value!='')
		AffiComm(4)
	else
		AffiComm(22);
}

function AffiComm(numcom) {
		//alert(numcom);
		switch(numcom){
		case 1:
			if (document.form1.arrt.value=='' && !testcheckDate(document.form1.dat.value))
				document.getElementById("comm").innerHTML="cliquez sur un arrondissement de la carte";
			break;
		case 11:
			if (document.form1.arrt.value=='' && !testcheckDate(document.form1.dat.value))
				document.getElementById("comm").innerHTML="cochez une ou plusieurs date(s)";
			break;
		case 2:
			if (document.form1.arrt.value=='' && !testcheckDate(document.form1.dat.value))
				document.getElementById("comm").innerHTML="cliquez dessus<br>puis cochez une ou plusieurs date(s)<br>puis cliquez sur GO";
			break;
		case 21:
			if (!testcheckDate(document.form1.dat.value))
				document.getElementById("comm").innerHTML="cochez une ou plusieurs date(s)<br>si vous le souhaitez<br>puis cliquez sur GO";
			break;
		case 22:
			if (document.form1.arrt.value=='')
				document.getElementById("comm").innerHTML="vous pouvez limiter à un arrondissement de la carte<br>si vous le souhaitez<br>puis cliquez sur GO";
			break;
		case 23:
			document.getElementById("comm").innerHTML="cliquez pour rechercher les braderies de cette région";
			break;
		case 3:
			document.getElementById("comm").innerHTML="tapez le nom d'une ville ou ses premi&egrave;res lettres<br>puis cliquez sur GO";
			break;
		case 4:
			document.getElementById("comm").innerHTML="cliquez sur GO";
			break;
		default:
			if (document.form1.arrt.value=='' && !testcheckDate(document.form1.dat.value) && document.form1.rvil.value=='')
				document.getElementById("comm").innerHTML="promenez-vous sur les dates<br>la carte ou la case villes";
			break;
	}
}



var docObj = (document.all) ? "document.all." : "document.";

function ChgColor(rowNum,Color) { 
thisRow = eval(docObj + rowNum); 
thisRow.bgColor = "#" + Color ;
} 


function SubmitForm() {
	errfound=false;
	if (document.form1.arrt.value=='' && !testcheckDate() && document.form1.rvil.value=='')
	{		alert('veuillez selectionner au moins un critère de recherche !');
			//document.form1.rvil.focus();
			errfound=true;
	}
	if (document.form1.rvil.value!='' && document.form1.rvil.value.length<2)
	{		alert('veuillez taper au moins 2 caractères !');
			document.form1.rvil.focus();
			errfound=true;
	}

	if (!errfound)
	{
		if (document.form1.rvil.value=='')
			document.form1.action='liste.asp'
		else
			document.form1.action='villes.asp';

		document.form1.submit();
	}
	return !errfound;

}

