function recherche(){
	document.location = "main.php?srv=Accueil&op=recherche&motsCles="+document.getElementById("motsCles").value;
}

function sendAjax(idDivAjax, idForm, get, calledAction) {
	document.body.style.cursor='progress';
	url = 'main.php?';
	if (get!=null && get!='undefined') {
		url+=get+'&';
	}
	if (calledAction==null || calledAction=='undefined') {
		calledAction = receiveAjax;
	}
	
	if (idForm!=null && idForm!=''){
		url+=getFormData(idForm);
		ajx = new ajax (url, {
			postBody: getFormData(idForm), 
			onComplete: calledAction
		});
	} else {
		ajx = new ajax (url, {
			onComplete: calledAction
		});
	}
	ajx.idDivAjax = idDivAjax; 
}

function receiveAjax(requete) {
	document.getElementById(this.idDivAjax).innerHTML=requete.responseText;
	document.body.style.cursor='auto';
	manageAdminIcones();
}

// Permet à un tableau scrollable de s'ouvrir et se fermer
function switchTableauScrollable(nomTableau) {
	var divTableau = 'div_'+nomTableau;
	if(document.getElementById(divTableau).clientHeight=='135'){
		document.getElementById(divTableau).style.height = document.getElementById(nomTableau).offsetHeight+'px';
	}else {
		document.getElementById(divTableau).style.height = '135px';
	}
	// on appelle 2 fois la fonction pour éviter de voir apparaitre furtivemeent la barre de défilement horizontale sur firefox
	document.getElementById(nomTableau).style.width = (document.getElementById(divTableau).clientWidth)+'px';
	setTimeout("document.getElementById('"+nomTableau+"').style.width = (document.getElementById('"+divTableau+"').clientWidth)+'px';", 0);;
}

// Initialise un tableau
function initTableauScrollable(nomTableau) {
	var divTableau = 'div_'+nomTableau;
	// on appelle 2 fois la fonction pour éviter de voir apparaitre furtivemeent la barre de défilement horizontale sur firefox
	// Si il n'y a pas de barre de défilement, on réduit la hauteur du tableau
	if (document.getElementById(divTableau).clientWidth-document.getElementById(nomTableau).clientWidth > 10) {
		setTimeout('switchTableauScrollable("'+nomTableau+'")', 300);
		setTimeout('switchTableauScrollable("'+nomTableau+'")', 300);
		switchTableauScrollable(nomTableau);
		document.getElementById('tout-afficher-'+nomTableau).style.display="none";
	} else {
		document.getElementById(nomTableau).style.width = (document.getElementById(divTableau).clientWidth)+'px';
		setTimeout("document.getElementById('"+nomTableau+"').style.width = (document.getElementById('"+divTableau+"').clientWidth)+'px';", 0);;
	}
}


// Soumet un formulaire et test si il n'a pas d?ja ?t? soumis
var isFormSubmited = false;
function sendForm(numForm) {
	if (numForm==null)numForm = 0;
	if (!isFormSubmited) {
		document.forms[numForm].submit();
		isFormSubmited= true;
	} /*else {
		alert("Veuillez patienter quelques instants : vous avez d?ja valid? ce formulaire.");
	}*/
}

// Récup_re les éléments d'un formulaire et les mets dans une chaine type get
function getFormData(numForm) {
	if (numForm==null)numForm = 0;
	data = "";
	elements = document.forms[numForm].elements;
	for (i=0;i<elements.length;i++) {
		element = elements[i];
		if (element.multiple == true){
			var result = "";
			for (j=0; j<element.length; j++) {
		        if (element.options[j].selected) {
		            if (element.options[j].value != "") result += element.options[j].value + ";";
		        }
		    }
			data += element.name+"="+escape(result)+"&";
		}
		else{
			if ((element.type=="radio" && element.checked) || (element.type=="checkbox" && element.checked) || element.type!="radio"){
				data += element.name+"="+escape(element.value)+"&";
			}
		}
	}
	return data;
}

function putRadioValue(nomChamp, value){
	elt = eval("document.forms[0]."+nomChamp);
	for (i=0;i<elt.length;i++){
		if (elt[i].value==value){
			elt[i].checked=true;
			return true;
		}
	}
	return false;
}

function fckEditor(nomChamp, value, toolbar){
	if (toolbar == null)toolbar = 'Basic';
	var oFCKeditor = new FCKeditor(nomChamp) ;
	oFCKeditor.BasePath	= 'presentation/FCKeditor/';
	oFCKeditor.Value	= value;
	oFCKeditor.ToolbarSet = toolbar;
	oFCKeditor.Height = "100%";
	oFCKeditor.Create() ;
	return oFCKeditor;
}

function calendrier(nomChamp) {
	document.write('<img align="absmiddle" src="presentation/pics/commun/boutons/bt_cal.gif" id="f_trigger_'+nomChamp+'" style="cursor:pointer;">');
    Calendar.setup({
        inputField     :    nomChamp,
        ifFormat       :    "%d/%m/%Y",
        button         :    "f_trigger_"+nomChamp,
        align          :    "bR",
        singleClick    :    true
    });
}

function showBordureArticle(elt) {
	try {
		elt.style.border='1px dotted black';
		elt.style.padding='0px 0px 0px 0px';
	} catch (ex){
	}
}

function hideBordureArticle(elt) {
	try {
		elt.style.border='1px none';
		elt.style.padding='1px 1px 1px 1px';
	} catch (ex){
	}
}


function generateAdminArticleDebut(idArticle) {
	document.write("<table><tr><td valign='top'><a href='main.php?srv=FormulaireBack&op=generateFormPage&idArticle="+idArticle+"'><img onmouseout='hideBordureArticle(div_article_"+idArticle+")' onmouseover='showBordureArticle(div_article_"+idArticle+")' src='presentation/pics/commun/icones/edit.gif'></a><br>");
	document.write("<a href=''><img onmouseout='hideBordureArticle(div_article_"+idArticle+")' onmouseover='showBordureArticle(div_article_"+idArticle+")' src='presentation/pics/commun/icones/poubelle.gif'></a></td><td valign='top'>");
	document.write("<div id='div_article_"+idArticle+"'>");
}

function generateAdminArticleFin(){
	document.write("</td></tr></table>");
	document.write("</div></td></tr></table>");
}

function setCookie(name,value, expiration) {
	var argv=arguments;
	var argc=arguments.length;
	var	expires=new Date();
	if (expiration == null) {
		expires.setMonth(expires.getMonth()+12);
	} else if (expiration==0){
		expires = null;
	} else {
		expires = expiration;
	}
	var path=(argc>3) ? argv[3] : null;
	var domain=(argc>4) ? argv[4] : null;
	var secure=(argc>5) ? argv[5] : false;
	document.cookie=name+"="+escape(value) +
		((expires === null) ? "" : ("; expires="+expires.toUTCString())) +
		((path === null) ? "" : ("; path="+path)) +
		((domain === null) ? "" : ("; domain="+domain)) +
		((secure === true) ? "; secure" : "");
}

function getCookie(name) {
	var arg=name+"=";
	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 EatCookieVal(j);
		}
		i=document.cookie.indexOf(" ",i) + 1;
		if (i === 0) {break;}
	}
	return null;
}

function EatCookieVal(offset) {
   var endstr=document.cookie.indexOf(";",offset);
   if (endstr == -1) {endstr=document.cookie.length;}
   return unescape(document.cookie.substring(offset,endstr));
}


/**
* 	Cache ou affiche les icones d'administration
* 	1 param?tre optionel : true pour afficher les icones et false sinon
*/
function switchAdminIcones(isOn) {
	if (isOn != null) {
		if (isOn)setCookie("adminIcone", "true");
		else setCookie("adminIcone", "false");
	} else {
		var adminIcone = getCookie("adminIcone");
		if (adminIcone == null || adminIcone == "false"){
			adminIcone = "true";
		} else adminIcone = "false";
		setCookie("adminIcone", adminIcone);
	}
	manageAdminIcones();
}

function manageAdminIcones() {
	var adminIcone = getCookie("adminIcone")
	images = document.images;
	for (i=0;i<images.length;i++){
		s=images[i].src;
		if (s.substring(s.length-12, s.length)=='edit_off.gif' ||
			s.substring(s.length-15, s.length)=='ajouter_off.gif' || 
			s.substring(s.length-16, s.length)=='poubelle_off.gif' 
			){
			if (adminIcone == "true")images[i].style.display='';
			else images[i].style.display='none';
		}
	}
}

function cleanPrintIcone() {
	images = document.images;
	for (i=0;i<images.length;i++){
		s=images[i].src;
		if (s.substring(s.length-13, s.length)=='print_off.gif'){
			images[i].style.display='none';
		}
	}
}

function imprimerArticle(source){
	//document.location.href="main.php?srv=CommunAction&op=showArticlePrint&cheminArticle="+source;
	frameTemp.document.location.href="main.php?srv=CommunAction&op=showArticlePrint&cheminArticle="+source;
}

function MO(e){
	if (!e)	var e=window.event;
	var S=e.srcElement;
	while (S.tagName!="TD")	{
		S=S.parentElement;
	}
	S.className="T";
}

function MU(e){
	if (!e)var e=window.event;
	var S=e.srcElement;
	while (S.tagName!="TD"){
		S=S.parentElement;
	}
	S.className="P";
}

function getAbsolutePos(el) {
	end = false;
	var r = { x: 0, y: 0 };
	while (!end){
		var SL = 0, ST = 0;
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
		var tmp = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
		r.x += tmp.x;
		r.y += tmp.y;
		if (el.offsetParent) {
			el = el.offsetParent;
		} else {
			end = true;
/*			alert(el.offsetParent.tagName);
			var tmp = this.getAbsolutePos(el.offsetParent);*/
		}
	}
	return r;
};

function hideSelect(obj) {
	p = getAbsolutePos(obj);
	cx1=p.x;
	cx2=p.x+obj.offsetWidth;
	cy1=p.y;
	cy2=p.y+obj.offsetHeight;
	listSelect = document.getElementsByTagName("select");
	for (i=0;i<listSelect.length;i++){
		p = getAbsolutePos(listSelect[i]);
		ex1=p.x;
		ex2=p.x+listSelect[i].offsetWidth;
		ey1=p.y;
		ey2=p.y+listSelect[i].offsetHeight;
		if (cx2>ex1 && cx1<ex2 && cy2>ey1 && cy1<ey2) {
			listSelect[i].style.visibility="hidden";
		}
	}
}

function showSelect(obj) {
	listSelect = document.getElementsByTagName("select");
	for (i=0;i<listSelect.length;i++){
		listSelect[i].style.visibility="visible";
	}
}

/**
*	Cette fonction permet de redimentionner automatiquement une fen?tre en hauteur par rapport au contenu
**/
function scaleWindow(largeur) {
	var ww=largeur;
	var hh=200;
	var step=15;
	x=document.body.scrollWidth;
	ww=(x<(screen.width-step) && x>ww) ? x : ww;
	x=document.body.scrollHeight;
	hh=(x<(screen.height-step) && x>hh) ? x : hh;
	window.resizeTo(largeur, document.body.scrollHeight+50)
	
	ok=1;
	while(ok==1) {
		ok=0;
		document.body.scrollLeft=2;
		if ((document.body.scrollLeft>0) && (ww<(screen.width-step))) {
			ww+=step;
			self.window.resizeTo(ww,hh);
			ok=1;
		}
		document.body.scrollTop=2;
		if (document.body.scrollTop>0 && (hh<(screen.height-step))) {
			hh+=step;
			self.window.resizeTo(ww,hh);
			ok=1;
		}
	}
}

	/**
	* Affiche une popup avec une fiche locataire en exemple pour trouver le numéro locataire
	**/
	function popupImageExempleNumeroLocataire(codeResidence) {
		residence = 'efidis';
		if (codeResidence == 'V'){
			residence = 'valestis';
		} else if (codeResidence == 'S') {
			residence = 'sageco';
		}
		titre="Exemple"; 
		w=open("",'image','width=400,height=400,toolbar=no,scrollbars=no,resizable=no'); 
		w.document.write("<HTML><HEAD><TITLE>"+titre+"</TITLE></HEAD>"); 
		w.document.write("<SCRIPT language=javascript>function checksize() { if (document.images[0].complete) { window.resizeTo(document.images[0].width+10,document.images[0].height+30); window.focus();} else { setTimeout('checksize()',250) } }</"+"SCRIPT>"); 
		w.document.write("<BODY onload='checksize()' onblur='window.close()' onclick='window.close()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0>");
		w.document.write("<TABLE width='100%' border='0' cellspacing='0' cellpadding='0' height='100%'><TR>");
		w.document.write("<TD valign='middle' align='center'><IMG src='presentation/pics/avis_echeance_"+residence+".jpg' border=0 alt='Mon image'>"); 
		w.document.write("</TD></TR></TABLE>");
		w.document.write("</BODY></HTML>"); 
		w.document.close(); 
	} 

/**
*	Cette fonction est utilisée pour faire défiler les photos des résidences
**/
function photoSuivante(image1,image2,num) {
	numero = numPhoto[num];
	numero ++;
	if (typeof(nomsImages[num][numero]) == 'undefined' ) numero = 0;
	document.getElementById(image1).src = nomsImages[num][numero];
	document.getElementById(image2).href = nomsImages[num][numero];
	document.getElementById(image1).title = legendesImages[num][numero];
	document.getElementById(image2).title = legendesImages[num][numero];
	numPhoto[num] = numero;
}
/**
*	Cette fonction est utilisée pour faire défiler les photos des résidences
**/
function photoPrecedente(image1,image2,num) {
	numero = numPhoto[num];
	numero --;
	if (numero == -1) numero = nomsImages[num].length-1;
	document.getElementById(image1).src = nomsImages[num][numero];
	document.getElementById(image2).href = nomsImages[num][numero];
	document.getElementById(image1).title = legendesImages[num][numero];
	document.getElementById(image2).title = legendesImages[num][numero];
	numPhoto[num] = numero;
}

/** 
* Pour initialiser un cookie, utile pour la suppression de cookie
**/
function setCook2(nom,valeur,jours) {
		var expDate = new Date()
		expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000))
		document.cookie = nom + "=" + escape(valeur)
            + ";expires=" + expDate.toGMTString()
	}
/** 
* Pour supprimer le cookie de la résidence
**/
function delCook(nom) {
		setCook2(nom,"",-1);
		location.reload() ; 
	}
