jQuery(function(){
    $('#contactEchantillon').hide();
    $('#contactDevisPersonnalisation').hide();
    $('#contactDevisFabrication').hide();
	
    
	
    $('#reference1').change(function(){
        var reference = $('#reference1').val();
        var zoneAjax = "div.blocForm p.couleur1";
        var name = "couleur1"
        recherche_couleur(reference, zoneAjax, name);
    });
    $('#reference2').change(function(){
        var reference = $('#reference2').val();
        var zoneAjax = "div.blocForm p.couleur2";
        var name = "couleur2"
        recherche_couleur(reference, zoneAjax, name);
    });
    $('#reference3').change(function(){
        var reference = $('#reference3').val();
        var zoneAjax = "div.blocForm p.couleur3";
        var name = "couleur3"
        recherche_couleur(reference, zoneAjax, name);
    });
    $('#reference4').change(function(){
        var reference = $('#reference4').val();
        var zoneAjax = "div.blocForm p.couleur4";
        var name = "couleur4"
        recherche_couleur(reference, zoneAjax, name);
    });
    $('#reference5').change(function(){
        var reference = $('#reference5').val();
        var zoneAjax = "div.blocForm p.couleur5";
        var name = "couleur5"
        recherche_couleur(reference, zoneAjax, name);
    });

    /* ajout par Gaultier pour gérer l'arrivée sur la page à partir d'autres pages de contenu pour mettre à jour automatiquement la liste déroulante */
    typeRenseignement = $.url.param("renseignements");
    if(typeRenseignement) {
        $("#renseignements").val(typeRenseignement);
        if($('#blocContact div.nouscontactez #renseignements').val() == 0)
        {
            $('#contactMessage').show();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 1)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').show();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 2)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 3)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').show();
            $('#contactDevisFabrication').hide();
        }
        if($('#blocContact div.nouscontactez #renseignements').val() == 4)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').show();
        }
    }
	
    $('#blocContact div.nouscontactez #renseignements').change(function(){
        if($('#blocContact div.nouscontactez #renseignements').val() == 0)
        {
            $('#contactMessage').show();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }
		
        if($('#blocContact div.nouscontactez #renseignements').val() == 1)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').show();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }
		
        if($('#blocContact div.nouscontactez #renseignements').val() == 2)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').hide();
        }
		
        if($('#blocContact div.nouscontactez #renseignements').val() == 3)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').show();
            $('#contactDevisFabrication').hide();
        }
        if($('#blocContact div.nouscontactez #renseignements').val() == 4)
        {
            $('#contactMessage').hide();
            $('#contactEchantillon').hide();
            $('#contactDevisPersonnalisation').hide();
            $('#contactDevisFabrication').show();
        }
    });
	
    $("#formContact").bind("submit", function()
    {
        var bReturn = true;
		
        $("label[for=\"civilite\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(!$(".civilite1").attr('checked') && !$(".civilite2").attr('checked') && !$(".civilite3").attr('checked'))
        {
            $("label[for=\"civilite\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"nom\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(jQuery.trim($("#nom").val()).length == 0)
        {
            $("label[for=\"nom\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"prenom\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(jQuery.trim($("#prenom").val()).length == 0)
        {
            $("label[for=\"prenom\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"telephone\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        var regex_tel = new RegExp("^[\+?0-9\-\(\)\ ]+$");
        if(!regex_tel.test(jQuery.trim($("#tel").val())))
        {
            $("label[for=\"telephone\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }
		
        $("label[for=\"email\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        var regex_email = new RegExp("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z.]+$","i");
        if(!regex_email.test(jQuery.trim($("#email").val())))
        {
            $("label[for=\"email\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"adresse1\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(jQuery.trim($("#adresse1").val()).length == 0)
        {
            $("label[for=\"adresse1\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"cp\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        var regex_cp = new RegExp("^[0-9]{2,5}$","i");
        if(!regex_cp.test(jQuery.trim($("#cp").val())))
        {
            $("label[for=\"cp\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"ville\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(jQuery.trim($("#ville").val()).length == 0)
        {
            $("label[for=\"ville\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        $("label[for=\"pays\"]").css({
            'color': '#4A4A4A',
            'font-weight': 'normal'
        });
        if(jQuery.trim($("#pays").val()).length == 0)
        {
            $("label[for=\"pays\"]").css({
                'color':'red',
                'font-weight':'bold'
            });
            bReturn = false;
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 0)
        {
            $("label[for=\"demande\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#demande").val()).length == 0)
            {
                $("label[for=\"demande\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 1)
        {
            $("label[for=\"reference\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#reference1").val()).length == 0 && jQuery.trim($("#reference2").val()).length == 0
                && jQuery.trim($("#reference3").val()).length == 0 && jQuery.trim($("#reference4").val()).length == 0
                && jQuery.trim($("#reference5").val()).length == 0)
                {
                $("label[for=\"reference\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
	
            $("label[for=\"couleur\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#couleur1").val()).length == 0 && jQuery.trim($("#couleur2").val()).length == 0
                && jQuery.trim($("#couleur3").val()).length == 0 && jQuery.trim($("#couleur4").val()).length == 0
                && jQuery.trim($("#couleur5").val()).length == 0)
                {
                $("label[for=\"couleur\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 3)
        {
            $("label[for=\"reference_devis\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#reference_devis").val()).length == 0)
            {
                $("label[for=\"reference_devis\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"quantite\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#quantite").val()).length == 0)
            {
                $("label[for=\"quantite\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"recto\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(!$(".rectoOui").attr('checked') && !$(".rectoNon").attr('checked'))
            {
                $("label[for=\"recto\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"verso\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(!$(".versoOui").attr('checked') && !$(".versoNon").attr('checked'))
            {
                $("label[for=\"verso\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
			
            if($(".rectoOui").attr('checked') && $(".versoOui").attr('checked'))
            {
                $("p.devisErreurs").css({
                    'color':'red',
                    'font-weight':'bold',
                    'text-align':'center',
                    'margin':'5px'
                });
                $('p.devisErreurs').html('Veuillez n\'effectuer qu\'un seul choix d\'impression');
                bReturn = false;
            }
            else if($(".rectoNon").attr('checked') && $(".versoNon").attr('checked'))
            {
                $("p.devisErreurs").css({
                    'color':'red',
                    'font-weight':'bold',
                    'text-align':'center',
                    'margin':'5px'
                });
                $('p.devisErreurs').html('Veuillez effectuer un choix d\'impression');
                bReturn = false;
            }
            else
            {
                $('p.devisErreurs').html('');
            }
            $("label[for=\"couleur_devis\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#couleur_devis").val()).length == 0)
            {
                $("label[for=\"couleur_devis\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
        }

        if($('#blocContact div.nouscontactez #renseignements').val() == 4)
        {
            $("label[for=\"quantite_fab\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#quantite_fab").val()).length == 0)
            {
                $("label[for=\"quantite_fab\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"recto_fab\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(!$(".rectoOui_fab").attr('checked') && !$(".rectoNon_fab").attr('checked'))
            {
                $("label[for=\"recto_fab\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"verso_fab\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(!$(".versoOui_fab").attr('checked') && !$(".versoNon_fab").attr('checked'))
            {
                $("label[for=\"verso_fab\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }

            if($(".rectoOui_fab").attr('checked') && $(".versoOui_fab").attr('checked'))
            {
                $("p.devisErreurs_fab").css({
                    'color':'red',
                    'font-weight':'bold',
                    'text-align':'center',
                    'margin':'5px'
                });
                $('p.devisErreurs_fab').html('Veuillez n\'effectuer qu\'un seul choix d\'impression');
                bReturn = false;
            }
            else if($(".rectoNon_fab").attr('checked') && $(".versoNon_fab").attr('checked'))
            {
                $("p.devisErreurs_fab").css({
                    'color':'red',
                    'font-weight':'bold',
                    'text-align':'center',
                    'margin':'5px'
                });
                $('p.devisErreurs_fab').html('Veuillez effectuer un choix d\'impression');
                bReturn = false;
            }
            else
            {
                $('p.devisErreurs_fab').html('');
            }

//            alert(bReturn);

            $("label[for=\"couleur_devis\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#couleur_devis").val()).length == 0)
            {
                $("label[for=\"couleur_devis\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
            $("label[for=\"format\"]").css({
                'color': '#4A4A4A',
                'font-weight': 'normal'
            });
            if(jQuery.trim($("#format").val()).length == 0)
            {
                $("label[for=\"format\"]").css({
                    'color':'red',
                    'font-weight':'bold'
                });
                bReturn = false;
            }
        }
		
        if(!bReturn)
        {
            $("p.infosErreurs").css({
                'color':'red',
                'font-weight':'bold',
                'text-align':'center',
                'margin':'10px 0 -20px'
            });
            $('p.infosErreurs').html('Un ou plusieurs champs obligatoires n\'ont pas &eacute;t&eacute; correctement renseign&eacute;s');
        }
		
        return bReturn;
    });
});

function recherche_couleur(ref, zone, name){
    $.ajax({
        type:"GET",
        url:"client/plugins/demande/ajax/recherche_couleur.php",
        data : "ref="+ref+"&name="+name,
        success : function(html){
            //alert(html);
            $(zone).html('');
            $(zone).html(html);
//            var p_couleur_devis = $(zone).html();
//            label_couleur_devis = p_couleur_devis.split("<input ");
//            label_couleur_devis = label_couleur_devis[0].split("<select ");
//            label_couleur_devis = label_couleur_devis[0].split("<span ");
//            $(zone).html(label_couleur_devis[0] + html);
        }
    })
}
