var suggest;

window.onload = function() {
	suggest=document.getElementById('suggest');
	
	var item1=document.getElementById('item1');
	var item2=document.getElementById('item2');
	if (item1.offsetHeight>item2.offsetHeight) {
		$('#item2').height($('#item1').height());
	} else {		
		$('#item1').height($('#item2').height());
	}
	
	$('.commentaire').corner('top 20px cc:#FFF');	
	$('.onglet').corner('top cc:#FFF');
	$('#cite').corner('20px cc:#FFF');
	$('#forum').corner('20px cc:#FFF');
	$('#agenda').corner('20px cc:#FFF');
	$('.groupe_actu').corner('cc:#8D8D8D');
	$('#arbo').corner('cc:#8D8D8D');	
	$('.header_perimetre').corner('cc:#FFF');	
	
	$('.style6').corner('10px cc:#FFF');
	
	GoPopupMsg('');
}

/***************************************/

function GetXmlHttp() {
	var xhr=null;
	if(window.XMLHttpRequest){ // Firefox et autres
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ // Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e1) {
				xhr = null;
			}
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	}
	return xhr;
}

var http=GetXmlHttp();

function GetListeMotsCles() {
	http.open('GET','modules/infos4js.php?mode=1&nocache='+Math.random(),true);
	http.onreadystatechange=HandleHttpResponse;
	http.send(null);
}

function HandleHttpResponse() {
	if (http.readyState==4) {
		html=http.responseText;		
		document.getElementById("mots-cle").innerHTML=html;
	}
}

/***************************************/

function AffichePopup(id,type) {		
	var Width;
	var Height;
	var Titre;

	html="<form name='"+type+"' method='post' onSubmit='return VerifiePopup(\""+type+"\")'>";
		html+="<input type='hidden' name='action' value='"+type+"'>";
		if (type=='newsletter') {
			Width=270;
			Height=220;
			Titre="Abonnez-vous à la lettre du Ceepi";
			html+="votre email : <br><input class='grand' name='email' type='text'>";
			html+="<br><br>";
			html+="votre nom/prénom (facultatif) : <br><input class='grand' name='name' type='text'>";
			html+="<br><br>";
			html+="<input type='radio' name='choix' value='subscribe' checked>Abonnement";
			html+="<input type='radio' name='choix' value='unsubscribe'>Désabonnement";			
			html+="<br>";
			html+="<br>";
			html+="<input type='submit' value='envoyer'>";
		}		
		if (type=='friend') {
			Width=400;
			Height=320;
			Titre="Envoyer un mail à un ami, contenant un lien vers la page";
			html+="<input class='grand' name='name' type='text'> votre nom";
			html+="<br>";
			html+="<input class='grand' name='email_from' type='text'> votre email <span class='important'>*</span>";
			html+="<br>";
			html+="<input class='grand' name='email_dest' type='text'> l'email du destinataire <span class='important'>*</span>";
			html+="<br>";
			html+="<br>";
			html+="votre message";
			html+="<br>";
			html+="<textarea name='content' style='width:300px; height:100px;'></textarea>";
			html+="<br><br>";
			html+="<span class='important'>* champs obligatoires</span>";
			html+="<br><br>";
			html+="<input type='submit' value='envoyer'>";			
		}		
		if (type=='login') {
			Width=160;
			Height=180;
			Titre="Connexion";
			html+="utilisateur : <input class='petit' name='user' type='text'>";
			html+="<br><br>";
			html+="mot de passe : <input class='petit' name='password' type='password'>";
			html+="<br><br>";
			html+="<input type='submit' value='connecter'>";
		}
		if (type=='logout') {
			Width=180;
			Height=80;		
			Titre="Déconnexion";
			html+="<br><span style='text-align:center'>";
				html+="<input type='submit' value='déconnecter'>";			
			html+="</span>";
		}
		
		if (type=='search') {
			Width=450;
			Height=350;		
			Titre="Rechercher";
			search_string=document.forms['simple_search'].search_string.value;
			_cible1=document.forms['simple_search'].search_target1.value;
			_cible2=document.forms['simple_search'].search_target2.value;
			_cible3=document.forms['simple_search'].search_target3.value;
			_cible4=document.forms['simple_search'].search_target4.value;
			option=document.forms['simple_search'].search_option.value;

			if (_cible1=='1') s_cible1=' checked'; else s_cible1='';
			if (_cible2=='1') s_cible2=' checked'; else s_cible2='';
			if (_cible3=='1') s_cible3=' checked'; else s_cible3='';			
			if (_cible4=='1') s_cible4=' checked'; else s_cible4='';			
			if (option=='or') s_option_or=' checked'; else s_option_or='';
			if (option=='and') s_option_and=' checked'; else s_option_and='';
			if (option=='exact') s_option_exact=' checked'; else s_option_exact='';

			html+="<div>";	
				html+="<textarea name='search_string' style='width:400px; height:2em;'>"+search_string+"</textarea>";				
				html+="<br>";
				html+="<a onClick=\"vide_champ_recherche('search')\">effacer</a>";
			html+="</div>";	
			
			html+="<div style='float:left; margin-right:20px; width:210px;'>";
				html+="<br>";
				html+="<b>parmi :</b>";
				html+="<br>";
				html+="<input type='checkbox' name='search_target4' value='1'"+s_cible4+">les titres";
				html+="<br>";
				html+="<input type='checkbox' name='search_target2' value='1'"+s_cible2+">les corps de texte";
				html+="<br>";
				html+="<input type='checkbox' name='search_target1' value='1'"+s_cible1+">les mots-clés";
				html+="<br>";
				html+="<input type='checkbox' name='search_target3' value='1'"+s_cible3+">les auteurs";
				html+="<br><br>";
				html+="<b>options :</b>";
				html+="<br>";			
				html+="<input type='radio' name='search_option' value='and'"+s_option_and+">tous les termes";
				html+="<br>";
				html+="<input type='radio' name='search_option' value='or'"+s_option_or+">au moins un des termes";			
				html+="<br>";
				html+="<input type='radio' name='search_option' value='exact'"+s_option_exact+">expression exacte";						
				html+="<br><br>";			
			html+="</div>";
			html+="<div style='float:left; width:210px; height:200px;'>";
				html+="<b>Vous pouvez sélectionner un ou plusieurs mots-clé déjà définis :</b><br>";
				html+="<div id='mots-cle'></div>";
			html+="</div>";
			html+="<div class='spacer'><br></div>";
			html+="<input type='submit' value='rechercher'>";			
		}
		if (type=='comment') {
			Width=320;
			Height=360;
			Titre="Ecrire un commentaire";
			html+="<input type='hidden' name='id' value='"+id+"'>";
			html+="votre nom ou pseudo : <input name='writer' type='text'>";
			html+="<br>";
			html+="votre adresse émail : <input name='email' type='text'><br><span class='important'>(elle ne sera pas visible sur le site)</span>";
			html+="<br><br>";
			html+="votre commentaire : <span class='important'></span><br>";
			html+="<textarea name='content' cols='36' rows='10'></textarea>";			
			html+="<br>";
			//html+="<span class='important'>* champs obligatoires</span>";
			html+="<br><br>";
			html+="<input type='submit' value='envoyer'>";
		}
		html='<b>'+Titre+'</b>'+LienClosePopup(2)+'<hr>'+html+LienClosePopup(1);
	html+="</form>";		

	GoPopup(Width,Height,html);
	
	if (type=='search') GetListeMotsCles();
}

function add_champ_recherche(type,s) {
	value=document.forms[type].search_string.value;
	if (value=='') result=s; else result=value+' '+s;
	document.forms[type].search_string.value=result;
}

function set_champ_recherche(type,s) {
	document.forms[type].search_string.value=s;
}

function vide_champ_recherche(type) {
	document.forms['search'].search_string.value='';
}

function Contient(chaine,c) {
	return (chaine.indexOf(c,0)>=0);
}

function VerifieMail(email) {
	reg_exp=new RegExp('^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,4}$','g');	
	return reg_exp.exec(email);
}

function VerifiePopup(type) {
	var msg='';
	result=true;

	if (type=='friend') {
		email_from=document.forms[type].email_from.value;
		reg_exp=new RegExp('^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,4}$','g');	
		if (!reg_exp.exec(email_from)) {			
			msg=AjouteMsg(msg,"L'adresse de l'expéditeur doit être de la forme nom@domaine.com");		
			result=false;
		}	
		email_dest=document.forms[type].email_dest.value;		
		reg_exp=new RegExp('^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,4}$','g');	
		if (!reg_exp.exec(email_dest)) {			
			msg=AjouteMsg(msg,"L'adresse du destinataire doit être de la forme nom@domaine.com");		
			result=false;
		}
	}
	if (type=='newsletter') {
		email=document.forms[type].email.value;
		reg_exp=new RegExp('^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,4}$','g');	
		if (!reg_exp.exec(email)) {			
			msg=AjouteMsg(msg,"L'adresse email doit être de la forme nom@domaine.com");		
			result=false;
		}	
	}	
	
	if (type=='login') {
		if (document.forms[type].user.value=="") { 
			msg=AjouteMsg(msg,"Veuillez renseigner le nom d'utilisateur");
			result=false; 
		}
		if (document.forms[type].password.value=="") { 
			msg=AjouteMsg(msg,"Veuillez renseigner le mot de passe");
			result=false;
		}
	}
	
	if ((type=='search') || (type=='simple_search')) {
		s=document.forms[type].search_string.value;		
		reg_exp=new RegExp('^[\x20\s]*$','g');
		if (reg_exp.exec(s)) {
			msg=AjouteMsg(msg,"Veuillez indiquer des termes à rechercher, séparés par des espaces");
			result=false; 
		}
		if (Contient(s,'"') || Contient(s,'\\') || Contient(s,'?')) {
			msg=AjouteMsg(msg,'Caractères interdits pour les termes de recherche : guillemets, antislash, points d\'intérrogation');
			result=false; 
		}
	}
	if (type=='comment') {
		s1=document.forms[type].content.value;
		s2=document.forms[type].writer.value;
		s3=document.forms[type].email.value;
		if (s1=="") { 
			msg=AjouteMsg(msg,"Veuillez ajouter un commentaire");
			result=false; 
		}	
		if (s2.length>24) {
			msg=AjouteMsg(msg,"Pseudo trop long (24 caractères maximum)");
			result=false; 		
		}
		if (Contient(s1,'<') || Contient(s1,'>') || Contient(s2,'<') || Contient(s2,'>')) {
			msg=AjouteMsg(msg,"Caractères interdits: <,>");
			result=false;
		}
		if (s2=="") { 
			result=result && confirm("Souhaitez-vous commenter cette page de manière anonyme ?");
		}
		if (s3!="") {
			if (!VerifieMail(s3)) {
				msg=AjouteMsg(msg,"L'adresse émail n'est pas valide");
				result=false;
			}
		}
	}
	
	if (msg!='') {
		msg="Erreur<br>"+msg;
		GoPopupMsg(msg);		
	}
	return result;
}

/***************************************/
	
function Suggest() {
	//x=champ.offsetLeft+50;
	//document.getElementById('suggest').style.left=x+'px';
	champ=document.forms['simple_search'].search_string.value;
	
	if (champ.length>1) {		
		if ((http.readyState==0) ||(http.readyState==4)) {
			http.open('GET','modules/infos4js.php?mode=2&chaine='+champ+'&nocache='+Math.random(),true);
			http.onreadystatechange=SuggestReceive;
			http.send(null);
		}
	} else {
		SuggestClose();
	}
}

function SuggestReceive() {
	if (http.readyState==4) {
		html=http.responseText;		
		if (Contient(html,'<none>')) {
			SuggestClose(); 
		} else {
			SuggestOpen();	
			suggest.innerHTML=html;
		}
	}
}

function SuggestOpen() {
	suggest.style.visibility='visible';
}

function SuggestClose() {
	suggest.style.visibility='hidden';
}

