var mendatory_error = "Champ obligatoire.";
var invalidEmail_error = "Le format de votre courriel est incorrect (adresse@courriel.fr).";
var cp_error = "Votre code postal doit &ecirc;tre compos&eacute de 5 chiffres.";
$(document).ready(function () {
	var ie6 = false;
	if(jQuery.browser.version == "6.0" && jQuery.browser.msie){
		ie6 = true;
	}
	if(ie6) {
		initNavigation();
		initRollInput();
	}

	initDeleteFormValues();
	initCheckForms();
	initPageActions();

});

function initPageActions(){
$("#action_print").click(function () {
		window.print();
		return false;
	});
}


function initNavigation(){
	var menu = $("#menu");
	menu.addClass("js");
	$("#menu li").hover(
	function () {
			$(this).find("ul").css("display","block");
			$(this).find(".section").addClass("active");
		},
		function () {
			$(this).find("ul").css("display","none");
			$(this).find(".section").removeClass("active");
		}
	);
}

function initRollInput(){
	$(".btn_envoyer input,.btn_annuler input,.btn_afficher input").hover(
		function () {
			$(this).css("background-position","0 -19px");
		},
		function () {
			$(this).css("background-position","0 0");
		}
	);
}

function initDeleteFormValues(){
	$(".c_text input").click(function () {
		this.value = "";
	});
	$(".c_textarea textarea").click(function () {
		this.value = "";
	});
}



function initCheckForms(){
	$("form").each(function(){
		var required = $(this).find(".required");
		var cp = $(this).find(".cp");
		var email = $(this).find(".email");
		var form = $(this);

		var radios = $(this).find("input:radio");
		var groupe_radios = new Array();
		var name_radio = "";

		radios.each(function(i){

			if((name_radio != this.name) || (i == radios.length-1)){
				groupe_radios.push(name_radio);
			}
			name_radio = this.name;
		});



		$(this).find("input[type = submit]").click(function () {


			var submitUsed = $(this);
	        var relSubmitUsed = submitUsed.attr("rel");

			var hasError = false;
			var div_callBack = $(this).find("callBack");
			form.find(".message").remove();
			// champs requis
			required.each(function(){
				var div_label = $(this).parent().prev();
				var label = $(this).parent().prev().text();
				//errreur detectee
				if(this.value == ""){
					hasError = true;
					$("<span class='message'>"+mendatory_error+"</span>").insertBefore(div_label);

				}
			});

			var fonction2 = $("#c_fonction2");
			var media2   = $("#c_type_media2");

			if($("#c_fonction2").length) {
    			if($("#c_fonction2").val()=="") {
    			    $(".message2").show();
    			} else {
                    $(".message2").hide();
    			}
			}

			if($("#c_type_media2").length) {
    			if($("#c_type_media2").val()=="") {
    			    $(".message3").show();
    			} else {
                    $(".message3").hide();
    			}
			}

			cp.each(function(){/* Rajout� par Manuelle GUSTAVE le 030509*/

				var div_label = $(this).parent().prev();
				var label = $(this).parent().prev().text();
				//errreur detectee
				if(this.value.length < 5){
					hasError = true;
					$("<span class='message'>"+cp_error+"</span>").insertBefore(div_label);
				}
			});
			//champs emails
			email.each(function(){
				var div_label = $(this).parent().prev();
				var label = $(this).parent().prev().text();
				var reg_email = RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$");
				//erreur detectee
				if(!reg_email.test(this.value)){
					hasError = true;

					$("<span class='message'>"+invalidEmail_error+"</span>").insertBefore(div_label);
				}
			});




			for(var i=0; i< groupe_radios.length;i++){
				if(groupe_radios[i] != "")
				{

						var check = false;
						var groupe = document.getElementsByName(groupe_radios[i]);
						var div_label = $(document.getElementById(groupe_radios[i])).parent().prev();

						for (j=0;j < groupe.length; j++){
							if(groupe[j].checked) check = true;
						}
						if(check == false){
							hasError = true;

							$("<span class='message'>"+mendatory_error+"</span>").insertBefore(div_label);


						}
				}
			}



			// si pas d'erreurs
			if(hasError == false) {
				if(form.hasClass("ajaxForm")){
					// traitement specifique ajax pour les reactions

					dataPost = 'c_nom=' + encodeURIComponent(document.getElementById(relSubmitUsed + '_c_nom').value)
					           + '&c_courriel=' + encodeURIComponent(document.getElementById(relSubmitUsed + '_c_courriel').value)
					           + '&c_commentaire=' + encodeURIComponent(document.getElementById(relSubmitUsed + '_c_commentaire').value)
					           + '&idActu=' + encodeURIComponent(document.getElementById(relSubmitUsed + '_idActu').value);

					$.ajax({
                        type: "POST",
                        url: "/FRONT/OPEN_EDF_NATATION/classes/actus/addcomments.php",
                        data: dataPost,
                        success: function() {
                           document.getElementById(relSubmitUsed + '_errorLog').style.display = 'block';
                           document.getElementById(relSubmitUsed + '_commentForm').style.display = 'none';
                           document.getElementById(relSubmitUsed + '_actionsFormComment').style.display = 'none';
                        }
                      });



					return false;
				}
				else{
					return true;
				}
			}
			else {
				return false;
			}
		});

	});
}
