$(document).ready(function(){
	function changeColor() {
        var that = $(this);
        var message_index = that.val();
        if (this.selectedIndex == 0) {
            if (that.hasClass('cpt_noir')) that.removeClass('cpt_noir');
            that.addClass('cpt_gris');
        }
        else {
            if (that.hasClass('cpt_gris')) that.removeClass('cpt_gris');
            that.addClass('cpt_noir');
        }
    }
    $("select").change(changeColor).change();
    
    var con_textArea = 'pour obtenir des devis précis, merci d\'indiquer le plus d\'informations possible.';
    $(":textarea[class=TEXTAREA]")
	.focus(function(){
		if(this.value==con_textArea) this.value='';
	})
	.blur(function(){
		if(this.value=='') this.value=con_textArea;
	})
	.val(con_textArea)
	;
	
	$(':text[name^=critere]').each(function(){
		var that = $(this);
		
		var parentShowHide = function (visible){
			//console.log(that,visible,that.parent());
			if (visible){
				if (!that.parent().is(':visible')){
				    that.parent().show('normal');
				}
			}else{
				that.parent().hide();
			}
		}
		var testIfToDisplay=function(text){
			var rtn;
			if (/^Si oui/.test(text)){
				rtn = true;
			}else if (/^Autre/.test(text)){
				rtn = true;
			}else if (/^Oui/.test(text)){
				rtn = true;
			}else{
				rtn = false;
			}
			return rtn;
		}
		
		var rgx1 = new RegExp('critere\\\[(.*)\\\]\\\[saisie\\\]');		
		
		if (match = that[0].name.match(rgx1)){
			//bloc select
			var slc = $('select[name=critere\\\['+match[1]+'\\\]\\\[valeur\\\]]');
			if (slc.length){
				slc.change(function(){					
					var maCombo = $(this)[0];
					var DisplayOrNot; 
					//console.log(that,maCombo);
					if (testIfToDisplay(maCombo.options[maCombo.selectedIndex].text)){
						DisplayOrNot = true;
					}
					parentShowHide(DisplayOrNot);
				});
				that.parent().hide();
			}

			//bloc checkbox

			var checks = $(':checkbox[name^=critere['+match[1]+'][valeur]]');
			if (checks.length){		
		                $(checks).click(function(){
					var libelle = "";
		                    	var DisplayOrNot = false;
					$(checks).each(function(x) {
						if ($(this).is(':checked')){
					       		libelle = $('#label_'+(/radio_id_(.*)__/.exec(this.className))[1])[0].innerHTML;
					                if (testIfToDisplay(libelle)){
					                	DisplayOrNot = true;
					                }
						}
		                    	});
		                    	parentShowHide(DisplayOrNot);                   
		                });
		                that.parent().hide();
			}

			//bloc radio
			var radios = $('input[type="radio"][name^=critere['+match[1]+'][valeur]]');
			if (radios.length){
				$(radios).click(function(){
					var libelle = $('#label_'+(/radio_id_(.*)__/.exec(this.className))[1])[0].innerHTML;
					DisplayOrNot = false;   
					if (testIfToDisplay(libelle)){
						DisplayOrNot = true;
						//}else{ DisplayOrNot = $(this).val()==radios.length-1;
					}
					 //= testIfToDisplay(maCombo.options[maCombo.selectedIndex].text)
					parentShowHide(DisplayOrNot);					
				});
				that.parent().hide();
			}
		}
    });  
});
function verif_me()
{
    //alert(this.nom);
    //console.log(this);
    if (!document.form1.elements[this.nom]) return true;
    switch(this.type)
    {
        case "radio":
        	
            for(i=0;i<document.form1.elements[this.nom].length;i++)
            {
                if(document.form1.elements[this.nom][i].checked == true) return true;
            }
            return false;
        break;
        case "checkbox":
            test = eval(this.nom);
            //alert(document.form1.elements[this.nom]);
            for(i=0;i<test.length;i++)
            {
                if(document.form1.elements[test[i]].checked == true) return true;
            }
            return false;
        break;
        case "select":
            if(document.form1.elements[this.nom].selectedIndex) return true;
            else return false;
        break;
        case "text":
            if(document.form1.elements[this.nom].value == "") return false;
            else return true;
        break;
        case "textarea":
		    var con_textArea = 'pour obtenir des devis précis, merci d\'indiquer le plus d\'informations possible.';
		    if(document.form1.elements[this.nom].value == con_textArea) {
		       document.form1.elements[this.nom].value = "";
		       return false;
		    }
		    else {
                if(document.form1.elements[this.nom].value == "" & document.form1.elements[this.nom].value.length < 1000) return false;
		    }
            return true;
        break;
    }
}

function critere_a_verif(id,nom,type,libelle)
{
    this.id = id;
    this.nom = nom;//peut etre un tableau notament pour les checkboxs
    this.type = type;
    this.libelle = libelle;
    this.verif_me = verif_me;
    this.message_critere = message_critere;
}

function message_critere()
{
	switch(this.type)
	{
		case "radio":
			return "Sélectionner une valeur pour '"+ this.libelle +"'";
		break;
		case "checkbox":
			return "Spécifier au moins une valeur pour '"+ this.libelle +"'";
		break;
		case "select":
			return "Sélectionner une valeur pour '"+ this.libelle +"'";
		break;
		case "text":
			return "Saisir une valeur pour '"+ this.libelle + "'";
		break;
		case "textarea":
			return "Saisir une valeur de 1000 caractères maximum pour '"+ this.libelle + "'";
		break;
	}
	return 

}


function verif()
{
	message="Vous devez :\n";
	if(document.form1.cp.value == "")  message +="- Spécifier le code postal du lieu des travaux\n";
	else
		{
			StrNewStr=""; 
        	for(i=0;i<=document.form1.cp.value.length;i++){ 
           		StrChar=document.form1.cp.value.substring(i,i+1); 
           		if(StrChar!=" " ){ 
              		 StrNewStr=StrNewStr+StrChar; 
           		} 
		 	} 
			if(StrNewStr.search(/^[0-9][0-9][0-9][0-9][0-9]$/)==0)
				document.form1.cp.value=StrNewStr;
			else
			 	message +="- Spécifier un code postal à 5 chiffres\n";
			
			
		}
	for(t=0; t<tab_element.length ;t++)
	{
		if(!tab_element[t].verif_me()) message +="- "+ tab_element[t].message_critere() +"\n";
	}
	if(message!="Vous devez :\n")
	{
		alert(message);
		return false;
	}
	else return true;
}



