function form_msg (span,msg) {
	$(span).update('<img src="pic/pointe.gif" width="6" height="14" alt="Pointe" class="dist_pointe"  /><span class="info_verif" id="msg_comment">'+msg+'</span><img src="pic/attention.gif" width="10" height="14" alt="Attention" />');
}

function verif_form (index) {
	form="formcomm"+index;
	valid=1;
	verif_input("auteur",form,index);
	verif_input("comment",form,index);
	
	//verification de l'email
	email=$(form).elements["email"].value;
	if (email.match(/^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$/)) {
		$("noti_mail_"+index).hide();
	} else {
		$("noti_mail_"+index).style.display="inline";
		form_msg ("noti_mail_"+index,mail_invalide);
		valid=0;
	}
	
	//verification un seul mot
	comment=$(form).elements["comment"].value;
	if (!comment.match(/\s+/)) {
		$("noti_comment_"+index).hide();
	} else {
		$("noti_comment_"+index).style.display="inline";
		form_msg ("noti_comment_"+index,one_word);
		valid=0;
	}
	
	//on rajoute http si absent
	link=$(form).elements["www"].value;
	if (link!='') {
		if (!link.match(/\./)) {
			$("noti_www_"+index).style.display="inline";
			form_msg ("noti_www_"+index,mauvais_site);
			valid=0;
		} else if (!link.match('http://')) {
			$(form).elements["www"].value='http://'+link;
		};
	};
	
	// envoi des commentaires et actualisation du nb de commentaires et des commentaires
	if (valid==1) {
		new Ajax.Request("post_comm.php", {
		    method: "post",
		    parameters: $(form).serialize(true),
			evalScripts: true,
			onSuccess: function () {
		  		new Ajax.Updater('comm'+index, 'commentaires.php', {method: 'get', parameters:"ajax=1&idpost="+index,evalScripts: true});
				new Ajax.Updater('nb_comm'+index, 'nb_comm.php', {method: 'get', parameters:"ajax=1&idpost="+index,evalScripts: true});
			}
			
		});
		
		//sauvegarde des cookies
		if (document.getElementById(form).elements['remember'].checked) {
			date_cookie=new Date;

			date_cookie.setTime(date_cookie.getTime() + 2 * 365 * 24 * 3600 * 1000);
			document.cookie="auteur="+$(form).elements['auteur'].value+";expires="+date_cookie.toUTCString();
			document.cookie="mail="+$(form).elements['email'].value+";expires="+date_cookie.toUTCString();
			document.cookie="siteweb="+$(form).elements['www'].value+";expires="+date_cookie.toUTCString();
		};
	};


}

function verif_input (name,form,index) {
	if ($(form).elements[name].value=="") {
		$("noti_"+name+"_"+index).style.display="inline";
		form_msg ("noti_"+name+"_"+index,entree_vide);
		valid=0;
	} else {
		$("noti_"+name+"_"+index).hide();
	}
}

function read_cookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function show_form (index) {
	$("coord_"+index).show();
}
