// **** F U N C T I O N S ****
  
  // validateEmail
  function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email) == false) {
      //alert('Adresse E-mail invalide');
      return true;
    }
  }
  
	//ValidateCP
	function validateCP(cp){
		if((cp=="")||(cp.length<5)||(cp.length>5)) return false;
		if(isNaN(cp)==true)  return false;
		else return true;
	}

  // validateFormProject
  function validateFormProject() {
    var error = false;
    var status = "";

    if ($("input[name='civilite']:checked").length == 0) { error = true; status += "- Civilité\n" }
    if ($("#nom").attr("value") == "") { error = true; status += "- Nom\n" }
    if ($("#prenom").attr("value") == "") { error = true; status += "- Prénom\n" }
    if ($("#adresse").attr("value") == "") { error = true; status += "- Adresse\n" }
    if ($("#codepostal").attr("value") == "") { error = true; status += "- Code postal\n" }
    if ($("#email").attr("value") == "") { error = true; status += "- E-mail\n" }
    if ($("#telephone").attr("value") == "") { error = true; status += "- Téléphone\n" }

    if  (!error) {
      if (!validateEmail($("#email").attr("value"))) return true;
      else alert("Merci de bien vouloir saisir un email valide.");
    } else alert("Merci de bien vouloir remplir les champs : \n"+status);
    return false;
  }
  
  // validateFormSponsorship
  function validateFormSponsorship() {
    var error = false;
    var status = "";
    var statusS = "";
    var statusG = "";
    var statusSponsor = "PARRAIN\n";
    var statusGodson = "FILLEUL\n";

    // Sponsor
    if ($("input[name='civilite_p']:checked").length == 0) { error = true; statusS += "- Civilité\n" }
    if ($("#nom_p").attr("value") == "") { error = true; statusS += "- Nom\n" }
    if ($("#prenom_p").attr("value") == "") { error = true; statusS += "- Prénom\n" }
    if ($("#adresse_p").attr("value") == "") { error = true; statusS += "- Adresse\n" }
    if ($("#codepostal_p").attr("value") == "") { error = true; statusS += "- Code postal\n" }
    if ($("#ville_p").attr("value") == "") { error = true; statusS += "- Ville\n" }
    if ($("#telephone_p").attr("value") == "") { error = true; statusS += "- Téléphone\n" }
    if ($("#email_p").attr("value") == "") { error = true; statusS += "- E-mail\n" }
    // Godson
    if ($("input[name='civilite_f']:checked").length == 0) { error = true; statusG += "- Civilité\n" }
    if ($("#nom_f").attr("value") == "") { error = true; statusG += "- Nom\n" }
    if ($("#prenom_f").attr("value") == "") { error = true; statusG += "- Prénom\n" }
    if ($("#adresse_f").attr("value") == "") { error = true; statusG += "- Adresse\n" }
    if ($("#codepostal_f").attr("value") == "") { error = true; statusG += "- Code postal\n" }
    if ($("#ville_f").attr("value") == "") { error = true; statusG += "- Ville\n" }
    if ($("#telephone_f").attr("value") == "") { error = true; statusG += "- Téléphone\n" }
    if ($("#email_f").attr("value") == "") { error = true; statusG += "- E-mail\n" }

    if  (!error) {
      if (!validateEmail($("#email_p").attr("value"))) return true;
      else alert("Merci de bien vouloir saisir un email valide.");
      if (!validateEmail($("#email_f").attr("value"))) return true;
      else alert("Merci de bien vouloir saisir un email valide.");
    } else {
      if (statusS != "") status += statusSponsor + statusS;
      if (statusG != "") status += statusGodson + statusG;
      alert("Merci de bien vouloir remplir les champs : \n"+status);
    }
    return false;
  }
  
  // subscribeNewsletter
  function subscribeNewsletter() {
    if (!validateEmail($("#mailnewsletter").attr("value"))) {
      $.fancybox.showActivity();
      var mail = $("#mailnewsletter").attr("value");
      $.ajax({
        url: 'newsletter.php?mail='+mail,
        success: function(dataNewsletter) {
          $("#newsletter_accept").fancybox({
            'padding'         : 30,
            'transitionIn'		: 'fade', // 'none'
        		'transitionOut'		: 'fade', // 'none'
        		'content'         : " ",
            'overlayColor'		: '#000',
            'overlayOpacity'	: 0.5,
            'centerOnScroll'  : 'true',
            'content' : dataNewsletter
        	});
          $('#newsletter_accept').trigger('click');
        }
      });
    } else alert("Veuillez entrer une adresse e-mail valide !");
  }
  
  // goToAgency
  function goToAgency(pIdInput){
	if(validateCP($("#"+pIdInput).val())==true){
		var cp = $("#"+pIdInput).val();
		$.ajax({
			url: 'cp.php?cp='+cp,
        	success: function(data) {
				if (data != "") setAgence(data);
				else alert("Code postal inconnu");
        	}
		});
	}else alert("Merci de saisir un code postal valide.");
  }
  
  // setAgence
  function setAgence(pName) {
    window.location.href = 'credit-immobilier-'+pName+'.php';
  }

$(document).ready(function($) {
	/*liste des agences */
	$("#liste_agences").css({"text-indent": "0" });
	$("#liste_agences").jDropDown({selected: 0});
	$("#info").css("visibility", "visible");
	/* Bouton OK Newsletter */
	$("#boutok").click(function() {
		subscribeNewsletter();
	});
	$(".boutok2").click(function() {
		var idInput = $(this).prev("input").attr("id");
		goToAgency(idInput);
	});
	/**********/

  
  /* Flash Player */
  var version = deconcept.SWFObjectUtil.getPlayerVersion();
	if (document.getElementById && version["major"] > 0) {
		$("#flashversion").css("display", "none");
	} else {
    $("#flashversion").css("display", "block");
    $("#flashversion").html("Vous ne semblez pas avoir Flash Player, ce qui vous permettrait de lire les vidéos du CIFNORD.<br /> Vous trouverez Flash Player <a href=\"http://get.adobe.com/fr/flashplayer/\">ici</a> !");
  }
	/**********/

	$("#ciftv_textevideo").hide();
	$('#ciftv_opentext').click(function() {
  		$('#ciftv_textevideo').slideToggle('slow', function() {
			if ($('#ciftv_opentext').css("background-image").search( 'down' )!= -1) {
				$('#ciftv_opentext').css('background-image', 'url(images/arrow-up.png)');
				$('#ciftv_opentext').css('border-bottom', 'none');
				$('#ciftv_opentext').css('background-color', 'transparent');
			}else{
				$('#ciftv_opentext').css('background-image', 'url(images/arrow-down.png)');
				$('#ciftv_opentext').css('border-bottom', '1px solid black');
				$('#ciftv_opentext').css('background-color', '#FFF');
			}
  		});
	});

});

