/*POP UP*/
function popup(pop_url,pop_width,pop_height) {
	pop_left = (window.screen.availWidth / 2) - (pop_width / 2);
	pop_top = (window.screen.availHeight / 2) - (pop_height / 2);
	window.open(pop_url,"","left=" + pop_left + ",top=" + pop_top + ",width=" + pop_width + ",height=" + pop_height + ",scrollbars=yes,noresize");
}

/*FERME/OUVRE UN BLOC*/
function display_block(nom){
	if(document.getElementById(nom).style.display == 'none') document.getElementById(nom).style.display = 'block';
	else document.getElementById(nom).style.display = 'none';
}

/*AFFICHE LES FLASH*/
function RunFoo(filename, width, height) {
	document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\""+width+"\" height=\""+height+"\">");
	document.write("<param name=\"movie\" value=\""+filename+"\" />");
	document.write("<param name=\"quality\" value=\"high\" />");
	document.write("<param name=\"menu\" value=\"false\" />");
	document.write("<param name=\"wmode\" value=\"transparent\" />");
	document.write("<embed src=\""+filename+"\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" menu=\"false\" width=\""+width+"\" height=\""+height+"\" wmode=\"transparent\"></embed>");
	document.write("</object>");
}

//FONCTION AJAX
function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr
}

//AFFICHE AVEC AJAX
function ouvre_page(div,page,variable,type){
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			document.getElementById(div).innerHTML = xhr.responseText;
		}
	}
	xhr.open(type,page,true);
	if(type=="POST"){
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhr.send(variable);
	}
	else xhr.send(null);
}

//AFFICHE LA GRANDE IMAGE DANS LE DETAIL D'UNE REALISATION
function aff_gde_img(id, num){
	ouvre_page('gde_image_rea', 'rea_detail.php?aff_gde_img=ok&id='+id+'&num='+num, '', 'GET')
}

//STYLE DE TEXTE
function style_texte(debut, fin, champ, php) {
	//var input = document.forms[formulaire].elements["champtexte"];
	var input = document.getElementById(champ);
	input.focus();
	if(typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = debut + insText + fin;
		range = document.selection.createRange();
		if (insText.length == 0) range.move('character', -fin.length);
		else range.moveStart('character', debut.length + insText.length + fin.length);
		range.select();
	}
	else if(typeof input.selectionStart != 'undefined') {
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		input.value = input.value.substr(0, start) + debut + insText + fin + input.value.substr(end);
		var pos;
		if (insText.length == 0) pos = start + debut.length;
		else pos = start + debut.length + insText.length + fin.length;
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
	else {
		var pos;
		var re = new RegExp('^[0-9]{0,3}$');
		while(!re.test(pos)) {
		  pos = prompt("Insertion à la position (0.." + input.value.length + "):", "0");
		}
		if(pos > input.value.length) pos = input.value.length;
		var insText = prompt("Veuillez entrer le texte à formater:");
		input.value = input.value.substr(0, pos) + debut + insText + fin + input.value.substr(pos);
	}
	preview_texte(champ, php);
}

//PREVISUALISATION DES TEXTES
function preview_texte(champ, php){
	ouvre_page('bloc_preview_texte', php+'.php', 'preview=ok&texte='+document.getElementById(champ).value, 'POST');
}

//VERIFIE LES INFORMATIONS SAISIES POUR UNE ACTUALITE (AJOUT OU MODIF)
function verif_actu(){
	if(document.forms.form_actu.titre.value==""){
		alert('Veuillez saisir le titre de cette actualité');
		return;
	}
	if(document.forms.form_actu.chapo.value==""){
		alert('Veuillez saisir le chapeau de cette actualité');
		return;
	}
	if(document.forms.form_actu.texte.value==""){
		alert('Veuillez saisir le texte de cette actualité');
		return;
	}
	document.forms.form_actu.submit();
}

//AFFICHE/MASQUE LE DETAIL D'UN CONTACT
function aff_detail_contact(id){
	if(document.getElementById('detail_contact_'+id).style.display=='none'){
		var mot_ereg = new RegExp("detail_contact_","g");	
		var liste_contacts = document.getElementById('liste_contact').getElementsByTagName('div');
		for(var i=0;i<liste_contacts.length;i++){
			if(liste_contacts[i].id.match(mot_ereg)){
				if(liste_contacts[i].id=="detail_contact_"+id){
					liste_contacts[i].style.display = 'block';
					ouvre_page('detail_contact_'+id, 'contact.php?aff_detail='+id, '', 'GET');
				}
				else{
					liste_contacts[i].innerHTML = '';
					liste_contacts[i].style.display = 'none';
				}
			}
		}
	}
	else{
		document.getElementById('detail_contact_'+id).innerHTML = '';
		document.getElementById('detail_contact_'+id).style.display = 'none';
	}
}


//VERIFIE LES INFORMATIONS SAISIES POUR UNE REALISATION (AJOUT OU MODIF)
function verif_rea(){
	if(document.forms.form_rea.titre.value==""){
		alert('Veuillez saisir le titre de cette réalisation');
		return;
	}
	if(document.forms.form_rea.texte.value==""){
		alert('Veuillez saisir le texte de cette réalisation');
		return;
	}
	if(document.forms.form_rea.action.value=="ajout" && document.forms.form_rea.image1.value=="" && document.forms.form_rea.image2.value=="" && document.forms.form_rea.image3.value=="" && document.forms.form_rea.image4.value=="" && document.forms.form_rea.image5.value=="" && document.forms.form_rea.image6.value==""){
		alert('Veuillez saisir au moins une image');
		return;
	}
	document.forms.form_rea.submit();
}

