// Object sensing routine
var isNN4up = (window.Event)? true : false;

// Key press handler designed for cross platform use
function key_press_submit(e)
{
	if (isNN4up)
	{
	 var whichKey = e.which;
	}
	else
	{
	 var whichKey = window.event.keyCode;
	}
	if (whichKey == 13) {
		recherche_contrevenants(document.frm_contrevenants);
	}
	
}

// Register event handler for Netscape
if (isNN4up)
{
	document.captureEvents(Event.KEYPRESS);
}

// Register event handler for MSIE
document.onkeypress = key_press_submit;

// JavaScript Document
var mois = new Array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");
var mot_clef = "";
var icritere = 0;
var no_mois = 0;
//Fonctions servant à l'affichage du formulaire de la recherche par mois

function Set_mois(flag){
//S'il y a un flag, c'est qu'on doit dresser la liste de l'année en cours
	var dhui = new Date();
	var lemois = dhui.getMonth();
	//var html = "<select name=\"lemois\">"; 
	/*if(arguments.length<1)//tous les mois
	{*/
		var html = "<select name=\"touslesmois\">"; 
		html += "<option value='0'>mois</option>";
		for(var i=0;i<mois.length;i++)
		{
			html += "<option value='" + i +"'>"+mois[i]+"</option>";	
		}
	/*}
	else
	{
		lemois--;
		if(lemois<0)
		{
			no_mois=1;
		}
		else
		{
			for(var i=0;i<=lemois;i++)
			{
				if(i==lemois)
				{
					html += "<option value='" + i +"' selected >"+mois[i]+"</option>";	
				}
				else
				{
					html += "<option value='" + i +"'>"+mois[i]+"</option>";	
				}
			}
		}
		
	}*/
	html += "</select>"; 
	document.write(html);
}

function Set_annee(){
	var dhui = new Date();
	var an = dhui.getFullYear();
	if(no_mois==1)
	{
		an--;
	}
	var html = "<select name=\"lannee\" onchange=\"Javascript: ShowMonths()\">";
	do
	{
		html += "<option value='"+an+"'>"+an+"</option>";
		an--;
	}
	while(an>2007)
	html += "</select>"; 
	document.write(html);
}

function ShowMonths(){
	var mois_courant = document.getElementById("current_months");
	var tous_mois = document.getElementById("all_months");
	if(document.frm_rech_mois.lannee.selectedIndex>0)
	{
		mois_courant.style.display="none";
		tous_mois.style.display="block";
	}
	else
	{
		if(no_mois==1)
		{
			
			mois_courant.style.display="none";
			tous_mois.style.display="block";
		}
		else
		{
			var dhui = new Date();
			var an = dhui.getYear();
			if(parseInt(document.frm_rech_mois.lannee.options[0].value)<an)
			{
				mois_courant.style.display="none";
				tous_mois.style.display="block";
			}
			else
			{
				mois_courant.style.display="block";
				tous_mois.style.display="none";
			}
		}
	}
}	
//Fin des fonctions servant à l'affichage du formulaire de la recherche par mois


function recherche_contrevenants(obj,my_start_pos){
	var mylist=document.getElementById("p_critere_recherche");
	var le_xsl = document.getElementById("p_url_xml").value +"?" ;
	if(mylist.selectedIndex==0)
	{
		mylist.selectedIndex=5;	
	}
	var url=Get_context()+"/pls/portal/portalcon.contrevenants_recherche?p_mot_recherche="+document.getElementById("p_mot_recherche").value+","+mylist.options[mylist.selectedIndex].value+",";
	if(arguments.length>1)
	{
		 le_xsl= le_xsl+"start="+my_start_pos+"&";
	}
	
	if(Validate_search(obj))
	{
		
		/*var champ = document.getElementById("p_mot_recherche");
		champ.value=le_xsl+"annee=0&url_xml="+url*/
		document.location=le_xsl+"annee=0&url_xml="+url;
	}
	else
	{
		alert("Erreur: vous devez entrer un mot-clé.");
		mylist.selectedIndex=0;
	}
}

function recherche_contrevenants_test(obj,my_start_pos){
	var mylist=document.getElementById("p_critere_recherche");
	var le_xsl = document.getElementById("p_url_xml").value +"?" ;
	if(mylist.selectedIndex==0)
	{
		mylist.selectedIndex=5;	
	}
	var url=Get_context()+"/pls/portal/portalcon.contrevenants_recherche_test?p_mot_recherche="+document.getElementById("p_mot_recherche").value+","+mylist.options[mylist.selectedIndex].value+",";
	if(arguments.length>1)
	{
		 le_xsl= le_xsl+"start="+my_start_pos+"&";
	}
	
	if(Validate_search(obj))
	{
		
		/*var champ = document.getElementById("p_mot_recherche");
		champ.value=le_xsl+"annee=0&url_xml="+url*/
		document.location=le_xsl+"annee=0&url_xml="+url;
	}
	else
	{
		alert("Erreur: vous devez entrer un mot-clé.");
		mylist.selectedIndex=0;
	}
}


//Enlève les %20 pour l'affichage
function convert_space(mot){
	var mot_converted = unescape(mot);
	//alert (mot_converted);
	return mot_converted;
	
}

function Get_amendes(){
	var dues = 0;
	for(var i=0;i<amendes.length;i++)
	{
		dues += amendes[i];	
	}
	document.write(dues);
}

function getMonthNumber(p_mois)
{
	var mois = new Array("JANVIER","FEVRIER","MARS","AVRIL","MAI","JUIN","JUILLET","AOUT","SEPTEMBRE","OCTOBRE","NOVEMBRE","DECEMBRE");
	for(var x = 0; x < mois.length; x++) if(mois[x] == p_mois) return x;
	return -1;
}

function Set_titre(imois,annee){
	var titre = "";
	var ici = document.location.href;
	if(ici.indexOf("p_mot_recherche")!=-1)
	{
		if(arguments.length<1)
		{
			var comps = ici.split("p_mot_recherche=");
			var elems = comps[1].split(",");
			mot_clef = elems[0];
			icritere = Get_index(elems[1]);
			titre = "Résultats de recherche portant sur \""+convert_space(elems[0])+"\" et ayant comme critère \""+Get_equivalent(elems[1])+"\".";
		}
		else
		{
			imois = (isNaN(imois)) ? getMonthNumber(imois) : imois;
			titre = mois[imois] + " " + annee;	
		}
	}
	document.write(titre);	
}

function Get_equivalent(txt){
	txt = txt.replace("proprietaire","nom d'établissement");
	txt = txt.replace("categorie","catégorie");
	return txt;
}

function Validate_search(obj){
	if(obj.p_mot_recherche.value.trim()=="")
	{
		return false;	
	}
	return true;
}

function Get_index(txt){
	var mylist=document.getElementById("p_critere_recherche");
	for(var i=0;i<mylist.options.length;i++)
	{
		if(mylist.options[i].value == txt)
		{
			return i;	
		}
	}
	return 0;
}

function ReSet_form(){
	var mylist=document.getElementById("p_critere_recherche");
	var le_mot_cle = document.getElementById("p_mot_recherche");
	le_mot_cle.value = unescape(mot_clef);
	mylist.selectedIndex = icritere;
}

function SetHiddenForm(obj,pos,valeur){
	//On ne tient pas compte de pos
	recherche_contrevenants(obj,valeur);	
}

function Recherche_mois(obj,fichier_test){
	
	var le_xml ="/pls/portal/portalcon.contrevenants_recherche?p_mot_recherche=,tous,";
	if(arguments.length>1)
	{
		le_xml= "/pls/portal/portalcon.contrevenants_recherche_test?p_mot_recherche=,tous,";	
		
	}
	
	var page_url = document.location.href.toUpperCase();
	var div_touslesmois = document.getElementById("all_months");
	var indice=0;
	if(obj.touslesmois.selectedIndex!=0)
	{
		indice = obj.touslesmois.selectedIndex-1;
	}
	
	/*if(div_touslesmois.style.display=="block")
	{*/
		var le_xsl = document.getElementById("p_url_xml").value +"?" + "start=1&mois="+mois[indice].noAccents().toUpperCase()+"&annee="+ obj.lannee.options[obj.lannee.selectedIndex].value+"&imois="+indice+"&";	
	        le_xml = le_xml + obj.lannee.options[obj.lannee.selectedIndex].value;
	/*}
	else
	{
		var le_xsl = document.getElementById("p_url_xml").value +"?" + "start=1&mois="+mois[obj.lemois.selectedIndex].noAccents().toUpperCase()+"&annee="+ obj.lannee.options[obj.lannee.selectedIndex].value+"&imois="+obj.lemois.selectedIndex+"&";
	}*/
	
	var url=Get_context()+le_xml;//Production d'un xml contenant toutes les fiches
	document.location=le_xsl+"url_xml="+url;
}
function Show_results(indicateur){
//Montre les résultats et les outils d'information selon les résultats et le type de recherche
	var div_no_resultats = document.getElementById("bro_no_results");
	if(indicateur<1)
	{
		var div_resultats = document.getElementById("gdresultats");
		if((total==0)&&(indicateur!=-1))
		{
			div_no_resultats.style.display="block";
			div_resultats.style.display="none";
		}
		else
		{
			div_resultats.style.display="block";
		}
	}
	else
	{
		var div_resultats = document.getElementById("mois_resultats");
		var div_barre = document.getElementById("barre_resultats");
		if(total==0)
		{
			div_no_resultats.style.display="block";
			div_resultats.style.display="block";//ICI
			div_barre.style.display="none";
		}
		else
		{
			div_resultats.style.display="block";
			div_barre.style.display="block";
		}
	}
}

function BeforeSubmit(){
	//Courriels

	var email_a="inspectiondesaliments";
	var arobas="@";
	var email_b="ville.montreal.qc.ca";

	document.form_courriel.recipient.value=email_a+arobas+email_b;

	with(document.form_courriel)
	{
		if((courriel.value.trim()!= "" ) && ((courriel.value.indexOf("@")==-1)||(courriel.value.indexOf(".")==-1)))
		{
				alert("Votre adresse de courrier électronique n'est pas présentée dans un bon format.");
				courriel.focus();
				return false;
		}
		if(etablissement.value=="")
		{
			alert("Vous devez spécifier un nom d'établissement.");
			etablissement.focus();
			return false;
		}
		if(adresse_etablissement.value=="")
		{
			alert("Vous devez spécifier l'adresse d'établissement.");
			adresse_etablissement.focus();
			return false;
		}
		if(date_incident.value=="")
		{
			alert("Vous devez spécifier la date de l'incident.");
			date_incident.focus();
			return false;
		}
		if(incident.value=="")
		{
			alert("Vous devez spécifier une description de l'incident.");
			incident.focus();
			return false;
		}
		if(courriel.value.trim()=="")
		{
			courriel.value = "anonyme@anonyme.com"
		}		
		return true;
	}
}


function Manipulateur(){
	var lediv = document.getElementById("manipulateur");
	if(lediv.style.display=="block")
	{
		lediv.style.display="none";
	}
	else
	{
		lediv.style.display="block";
	}
}

function Set_Initial_Query(){
	//Lance une recherche par mois
	/*var dhui = new Date();
	var mois_courant = dhui.getMonth();
	var annee = dhui.getYear();
	mois_courant--;
	if(mois_courant<0)
	{
		mois_courant=11;
		annee--;
	}*/
	/*var url = Get_context()+"/pls/portal/url/PAGE/ALIMENTS_FR/rep_condamnations/contrevenants_liste?start=1&mois="+mois[mois_courant].noAccents().toUpperCase()+ "&annee="+annee+"&imois="+mois_courant+"&url_xml="+Get_context()+ "/pls/portal/portalcon.contrevenants_recherche?p_mot_recherche=,tous";	*/
	//var url = Get_context()+"/pls/portal/url/PAGE/ALIMENTS_FR/rep_condamnations/contrevenants_liste?start=1&mois=INITIAL&annee="+annee+"&imois="+mois_courant+"&url_xml="+Get_context()+ "/pls/portal/portalcon.contrevenants_recherche?p_mot_recherche=,tous,";
	var url = Get_context()+"/pls/portal/url/PAGE/ALIMENTS_FR/rep_condamnations/contrevenants_liste?start=1&mois=INITIAL&url_xml="+Get_context()+ "/pls/portal/portalcon.contrevenants_recherche?p_mot_recherche=,tous,";
	document.location = url;
}
function Get_context(){

	var page_url = document.location.href.toUpperCase();
	return page_url.substr(0,page_url.indexOf("QC.CA")+5);
}
function Set_YearMonth(){
	if(document.location.href.indexOf("imois")!=-1)
	{
		var params = document.location.href.split("&");
		for(var i=0;i<params.length;i++)
		{
			if(params[i].indexOf("annee")!=-1)
			{
				var egal = params[i].indexOf("=");
				egal++;
				var annee = params[i].substr(egal);
			}
			if(params[i].indexOf("imois")!=-1)
			{
				var egal = params[i].indexOf("=");
				egal++;
				var imois = params[i].substr(egal);
			}
		}
		//Ajuster l'année
		for(i=0;document.frm_rech_mois.lannee.options.length;i++)
		{
			if(document.frm_rech_mois.lannee.options[i].value==annee)
			{
				document.frm_rech_mois.lannee.selectedIndex = i;
				/*if(i==0)//annee courante
				{
					document.frm_rech_mois.lemois.selectedIndex = imois;	
				}*/
				/*else
				{*/
					document.frm_rech_mois.touslesmois.selectedIndex = imois + 1;	
				/*}*/
				break;
			}
		}
	}
}

