function popupWindow(url) {
window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=800,height=200,screenX=150,screenY=150,top=150,left=150')
}

function popup(url,larg,haut) {
window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width='+larg+',height='+haut+',screenX=150,screenY=150,top=150,left=150')
}
function popupno(url,larg,haut) {
window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+larg+',height='+haut+',screenX=150,screenY=150,top=150,left=150')
}

function checkAll(form) {
  for (var i = 1; i < form.elements.length; i++){    
    eval("form.elements[" + i + "].checked = form.sel.checked");  
  }
}
function checktout(nom,valeur) {
  for (var i = 0; i < document.getElementsByName(nom).length; i++){    
    document.getElementsByName(nom)[i].checked=valeur;
  }  
}
function popupWindowpicture(url) {
window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=710,height=530,screenX=30,screenY=30,top=30,left=30')
}

function popupWindowvideo(url) {
window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=500,height=390,screenX=30,screenY=30,top=30,left=30')
}

function changeliste(list1,list2) {
list2.options[list2.options.length]=new Option(list1.options[list1.selectedIndex].text,list1.options[list1.selectedIndex].value);
list1.options[list1.selectedIndex]=null;
}
function selectliste(list) {
for(i=0; i<list.options.length; i++) {
list.options[i].selected=true;
}
if(list.name.lastIndexOf("[]")==-1) {
list.name=list.name+"[]";
}
}


function switch_form(disp,compare,tag) {
var lignes=document.getElementById('amod').getElementsByTagName(tag);
for(var i=0; i<lignes.length; i++) {
    if((lignes[i].className.lastIndexOf(compare)>=0)||(lignes[i].className=="")) {
        lignes[i].style.display=disp;
    } else {
        lignes[i].style.display='none';
    }
}
}

var req;
var xhrEnCours;

function loadXMLDoc(url,fonction) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		var lafonction = fonction;
		var b = new Array();
		if(arguments.length > 2) {
			for(var i = 2; i < arguments.length; i++) {
				b.push(arguments[i]);
			}
		}
		req.onreadystatechange = function() { lafonction.apply(null, b); };
		req.open("GET", url, true);
		req.send(null);
	}
}
function loadXMLDocSync(url,fonction) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		var b = new Array();
		if(arguments.length > 2) {
			for(var i = 2; i < arguments.length; i++) {
				b.push(arguments[i]);
			}
		}
		req.open("GET", url, false);
		req.send(null);
		fonction.apply(null, b);
	}
}

function rcdc(id) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			var nouveau=document.createElement('select');
			var opt; 
			nouveau.setAttribute("name","id_bien[]");
			var du = req.responseXML.getElementsByTagName('du').item(0).getAttribute("value");
			var au = req.responseXML.getElementsByTagName('au').item(0).getAttribute("value");
			for(var i=0; i<req.responseXML.getElementsByTagName('item').length; i++) {
				opt=document.createElement('option');
				opt.setAttribute('value', req.responseXML.getElementsByTagName('item')[i].getAttribute('id_bien'));
				opt.appendChild(document.createTextNode(unescape(req.responseXML.getElementsByTagName('item')[i].getAttribute('nom'))));
				nouveau.appendChild(opt);
			}
			document.getElementById(id).appendChild(nouveau);
			document.getElementById(id).appendChild(document.createTextNode(' '+du+' '));
			var input1=document.createElement("input");
			input1.setAttribute("value", req.responseXML.getElementsByTagName('racine')[0].getAttribute('datedebut'));
			input1.setAttribute("type", "text");
			input1.setAttribute("name", "date_debut[]");
			input1.setAttribute("size", "10");
			input1.setAttribute("maxlength", "10");
			document.getElementById(id).appendChild(input1);
			document.getElementById(id).appendChild(document.createTextNode(' '+au+' '));
			var input2=document.createElement("input");
			input2.setAttribute("value",  req.responseXML.getElementsByTagName('racine')[0].getAttribute('datefin'));
			input2.setAttribute("type", "text");
			input2.setAttribute("size", "10");
			input2.setAttribute("maxlength", "10");
			input2.setAttribute("name", "date_fin[]");
			document.getElementById(id).appendChild(input2);
			document.getElementById(id).appendChild(document.createTextNode(' '));
			var bouton=document.createElement("input");
			bouton.setAttribute("value", " + ");
			bouton.setAttribute("type", "button");
			bouton.setAttribute("onclick", "loadXMLDoc('ajax/rajoutercdc.php', rcdc, 'divid');");
			document.getElementById(id).appendChild(bouton);
			document.getElementById(id).appendChild(document.createElement('br'));
        }
    }
}

function rselect(id, nomselect, urlplus, fonction) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			var nouveau=document.createElement('select');
			var opt; 
			nouveau.setAttribute("name", nomselect);
			for(var i=0; i<req.responseXML.getElementsByTagName('item').length; i++) {
				opt=document.createElement('option');
				opt.setAttribute('value', req.responseXML.getElementsByTagName('item')[i].getAttribute('valeur'));
				opt.appendChild(document.createTextNode(unescape(req.responseXML.getElementsByTagName('item')[i].getAttribute('nom'))));
				nouveau.appendChild(opt);
			}
			document.getElementById(id).appendChild(nouveau);
			document.getElementById(id).appendChild(document.createTextNode(' '));
			var bouton=document.createElement("input");
			bouton.setAttribute("value", " + ");
			bouton.setAttribute("type", "button");
			bouton.setAttribute("onclick", "loadXMLDoc('"+urlplus+"', "+fonction+", '"+id+"');");
			document.getElementById(id).appendChild(bouton);
			document.getElementById(id).appendChild(document.createElement('br'));
        }
    }
}

function aff(id) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            var retour = req.responseText;
            document.getElementById(id).innerHTML=retour;
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

	function cacher(ev, acacher, input, hidden_ville, hidden_dep) {
		if(Event.element(ev).descendantOf($(acacher))) {
			$(input).value=Event.element(ev).firstChild.data;
			if(Event.element(ev).className.indexOf("ville", 0) != -1) {
				$(hidden_ville).value = Event.element(ev).className.substring(5);
			} else if(Event.element(ev).className.indexOf("departement", 0) != -1) {
				$(hidden_dep).value = Event.element(ev).className.substring(11);
			}
			document.getElementById(acacher).style.display="none";
			Event.stop(ev);
			Event.stopObserving(document.body, "click", cacher.bindAsEventListener(window, acacher, input, hidden_ville, hidden_dep));
		}
	}
	
	function fleche(ev, divparent) {
		var encours;
		var suivant;
		if(ev.keyCode==40) {
			liens = $(divparent).select('a.selectFleche');
			if(liens.length==0) {
				encours = $(divparent).select('a')[0];
				encours.addClassName("selectFleche");
				suivant = encours;
			} else {
				encours = liens[0];
				try {
					suivant = encours.parentNode.next().firstDescendant();
				} catch(e) {
					suivant = encours;
				}
				
			}
			encours.removeClassName("selectFleche");
			suivant.addClassName("selectFleche");
			ev.stop();
		} else if(ev.keyCode==38) {
			liens = $(divparent).select('a.selectFleche');
			if(liens.length==0) {
				encours = $(divparent).select('a')[0];
				encours.addClassName("selectFleche");
				suivant = encours;
			} else {
				encours = liens[0];
				try {
					suivant = encours.parentNode.previous().firstDescendant();
				} catch(e) {
					suivant = encours;
				}
				
			}
			encours.removeClassName("selectFleche");
			suivant.addClassName("selectFleche");
			ev.stop();
		}
	}
	
	function placer_div(input, div) {
		pos = $(input).cumulativeOffset();
		$(div).setStyle({top: pos.top+$(input).offsetHeight+"px", left: pos.left+"px"});
	}
	
	Ajax.Request.prototype.abort = function() {
		// prevent and state change callbacks from being issued
		this.transport.onreadystatechange = Prototype.emptyFunction;
		// abort the XHR
		this.transport.abort();
		// update the request counter
		Ajax.activeRequestCount--;
	};
	
	function init_div_ville(langue, input, div, hidden_ville, hidden_dep) {
				
		placer_div(input, div);
		$(input).observe("keyup", function(ev) {
			/*
			if(ev.keyCode >= 37 && ev.keyCode <=40) {
				fleche(ev, div);
			} else {
			*/
				$(hidden_ville).value="";
				$(hidden_dep).value="";
				if(typeof xhrEnCours == 'object') {
					xhrEnCours.abort();
				}
				
				xhrEnCours = new Ajax.Request('/'+langue+'/ajax/villes.php', 
					{
						parameters: {i: $(input).value},
						onSuccess: function(retour) {
							if(retour.responseXML.getElementsByTagName("req").item(0).firstChild.data == "Rien") {
								Event.stopObserving(document.body, "click", cacher.bindAsEventListener(window, div, input, hidden_ville, hidden_dep));
								document.getElementById(div).style.display="none";
							} else if(retour.responseXML.getElementsByTagName("req").item(0).firstChild.data == $(input).value) {
								if(retour.responseXML.getElementsByTagName("commun").item(0).firstChild.data == "Rien") {
									Event.stopObserving(document.body, "click", cacher.bindAsEventListener(window, div, input, hidden_ville, hidden_dep));
									document.getElementById(div).style.display="none";
								} else {
									Event.observe(document.body, "click", cacher.bindAsEventListener(window, div, input, hidden_ville, hidden_dep));
									document.getElementById(div).innerHTML = retour.responseXML.getElementsByTagName("commun").item(0).firstChild.data;
									document.getElementById(div).style.display="block";
								}
							}
						}
					}
				);
			// }
		});
	
	}
	
	function yScroll() {

		var y;
	
		if (self.pageYOffset) {
			y = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			y = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			y = document.body.scrollTop;
		}
	
		return y;
	}
	
	function xScroll() {

		var x;
	
		if (self.pageXOffset) {
			x = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
			x = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			x = document.body.scrollLeft;
		}
	
		return x;
	}