/****************************** JS-COMPARATIS *********************************/
/****************************** 01/2009 ***************************************/

$(document).ready(function(){
	 //overlay images de patience
   $("#contentLoading").css({
			display: 'none',  
			position: 'absolute',  
			top :'0',  
			left : '0',  
			width : '100%',  
			height : '190%',  
			zIndex : 1, 
			'background-color': '#666666',
			'background-position':'center',
			'background-repeat':'no-repeat',
			opacity : 0.7
    }); 
		
		$("input[id*=hidblur]").each(function(){	  
			var val = ($(this).val()).split("|");
			var reg_ext = new RegExp("_ext","g");
			var tmp_ext = '';
			if(reg_ext.test(val[2]))tmp_ext = '_ext';
			$("#"+val[2]).blur(function(){		
				supprErreur(val[0],val[1],tmp_ext);
			});
		});
		
    $('.row').each(function () {
    		var size_left;    		
				if(bw.ie6)size_left = 223;
				else size_left = 210;

        var time = 50;
        var hideDelay = 50;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);
        $([trigger.get(0), info.get(0)]).click(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;
                info.css({
                    bottom: 0,
                    left: size_left,
                    display: 'block'
                }).animate({
                    bottom: 0,
                    opacity: 99.72
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }
            return false;
        }).blur(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    bottom: 0,
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);
            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    bottom: 0,
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);
            return false;
        });
    });		
	//alert($(":hidden", document.body).length);						      
});

var Comparatis = {
	geoLocalisation : function(cpostal,profil,champs,site,ext){
	var espace = /[ ]+/;
	var tmp = "";
    $('#geoloc').dialog('destroy').remove();
    if(site == "voneo")tmp="../";
    /*Contrôl exception du browser IE6 pour le formulaire sous flash*/
    /*if($("#sous-flash").val() == "yes" && bw.ie6){
    	$('.right').find('select').each(function(i){
    		$(this).hide();
    	});
    }*/
    while (espace.test(cpostal)) cpostal = cpostal.replace(/[ ]+/,"-");
    
    $('<div id="geoloc">').load(tmp+'geo-localisation.php?cpostal='+cpostal+'&profil='+profil+'&champs='+champs+'&site='+site+'&ext='+ext).dialog({    		
    		maximize: false,
        title: 'Comparatis vous propose :',
        width: 400,
        height: 220,
       	modal:true,
        overlay:{background:'#cc9866', opacity:0.3,zIndex:0,filter: 'alpha(opacity=30)'}
        /*
				,close: function(){alert("ici");
        	if(bw.ie6){
        		$('.right').find('select').each(function(i){
        			alert(i);
			    		$(this).show();
			    	});
        	}
        }//*/
    });
	}
}

function doProfil(form_name,ext){
	/*if(document.getElementById("rdprofil2").checked==true && document.getElementById("profilp").value == "v"){
		document.getElementById("profilp").value="b";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();
	}
	else if(document.getElementById("rdprofil1").checked==true && document.getElementById("profilp").value == "b"){
		document.getElementById("profilp").value="v";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();
	}	
	else if(document.getElementById("rdprofil3").checked==true){
		document.getElementById("profilp").value="a";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();
	}
	else if(document.getElementById("rdprofil1").checked==true && document.getElementById("profilp").value == "a"){
		document.getElementById("profilp").value="v";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();		
	}
	else if(document.getElementById("rdprofil2").checked==true && document.getElementById("profilp").value == "a"){
		document.getElementById("profilp").value="b";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();		
	}*/
	if(document.getElementById("rdprofil1"+ext).checked==true){
		document.getElementById("profilp"+ext).value="v";
	}
	else if(document.getElementById("rdprofil2"+ext).checked==true){
		document.getElementById("profilp"+ext).value="b";
	}
	else if(document.getElementById("rdprofil3"+ext).checked==true){
		document.getElementById("profilp"+ext).value="a";		
	}
	else if(document.getElementById("rdprofil4"+ext).checked==true){
		document.getElementById("profilp"+ext).value="r";
	}
	var ancre = '';
	var ville = '';
	var cpostal = '';
	var variables = '';
	if(ext != ''){
		ancre = '#frame';				
		ville = $("#geo_ville"+ext).val();
		cpostal = $("#cpostal_bien"+ext).val();
		variables = "?ville="+ville+"&cpostal="+cpostal;
	}
	document.getElementById(form_name).action=window.location.pathname+variables;
	document.getElementById(form_name).target="_self";
	document.getElementById(form_name).submit();	
}

function doProfilVoneo(){
	if(document.getElementById("profil").value=="b" && document.getElementById("profilp").value == "v"){
		document.getElementById("profilp").value="b";
	}
	else if(document.getElementById("profil").value=="v" && document.getElementById("profilp").value == "b"){
		document.getElementById("profilp").value="v";
	}	
	else if(document.getElementById("profil").value=="a"){
		document.getElementById("profilp").value="a";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();
	}
	else if(document.getElementById("profil").value=="v" && document.getElementById("profilp").value == "a"){
		document.getElementById("profilp").value="v";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();		
	}
	else if(document.getElementById("profil").value=="b" && document.getElementById("profilp").value == "a"){
		document.getElementById("profilp").value="b";
		document.form.action=window.location.pathname;
		document.form.target="_self";
		document.form.submit();		
	}		
}

function displayDiv(val){
	var type = (val.value).toLowerCase();
	if(type == "maison"){
		document.getElementById("div_nombre_piece_"+type).style.display = "block";
		document.getElementById("div_nombre_piece_appartement").style.display = "none";
	}
	else if(type == "appartement"){
		document.getElementById("div_nombre_piece_"+type).style.display = "block";
		document.getElementById("div_nombre_piece_maison").style.display = "none";
	}
		
}

function verifierFormulaire(tab,type_form,site,form_name,ext){
	
	if(form_name == '')form_name = 'form';
	if(ext == '')ext = '';
	
	var bool = Array(tab.length);
	var profil = document.getElementById("profilp"+ext).value;
	var test="";	
	var prof;
	var tmp_url="";
	var exp_all_true = new RegExp("^1*$","g");

	if(profil == "v")prof = "1";
	else if(profil == "b")prof = "2";	 
	else if(profil == "a")prof = "3";
	else if(profil == "r")prof = "4";
		for(var i = 0; i < tab.length; i++){
		//alert(tab[i][0] +","+ tab[i][1] +","+ tab[i][2]);
		document.getElementById(tab[i][0]+ext).value = document.getElementById(tab[i][0]+ext).value.replace(/^[ ]{1,}|[ ]{1,}$/,"");
		switch(tab[i][1]){
			case "email":
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(tab[i][2] == "1"){
					if(filter.test(document.getElementById(tab[i][0]+ext).value)){
						//vrai
						$("#"+tab[i][0]+ext).css({border:"1px solid black"});
						bool[i] = 1;
					}
					else{
						//faux
						$("#"+tab[i][0]+ext).css({border:"2px solid red"});
						bool[i] = 0;						
					}
				}
				break;
			case "password":
				var filter  = /^([a-zA-Z0-9]{6,12})+$/;
				/*if(tab[i][2] == "1"){
					if(filter.test(document.getElementById(tab[i][0]).value)){
						//vrai
					}					
				}	*/			
				break;
			case "text(chiffre)":
				var filter  = /^([0-9])+$/;
				if(tab[i][0] == 'annee_construction'){
					//filter  = /^([0-9]{4})+$/;
					filter  = /^(19|20)[0-9]{2}$/;
				}
				if(tab[i][2] == "1"){//annee_construction
					if(filter.test(document.getElementById(tab[i][0]+ext).value)){						
						//faux
						$("#"+tab[i][0]+ext).css({border:"1px solid black"});
						bool[i] = 1;
					}	
					else{
						//vrai
						$("#"+tab[i][0]+ext).css({border:"2px solid red"});
						bool[i] = 0;						
					}				
				}	
				break;
			case "text(code postal)":
				if(tab[i][2] == "1"){
					if( document.getElementById(tab[i][0]+ext).value == "Code postal ou Ville" || document.getElementById(tab[i][0]+ext).value == ""){
						//faux
						$("#"+tab[i][0]+ext).css({border:"2px solid red"});
						bool[i] = 0;
					}
					else{
						//vrai
						$("#"+tab[i][0]+ext).css({border:"1px solid black"});
						bool[i] = 1;												
					}					
				}
				var cpostal = document.getElementById(tab[i][0]+ext).value;
				var champs = tab[i][0]+ext;
				break;
			case "text(téléphone)":
				if(tab[i][2] == "1"){
					exp = /[.()\s+]/g;
					expChiffre = /^([0-9])+$/;
					var tel = (document.getElementById(tab[i][0]+ext).value).replace(exp,'');
					tel = tel.replace('(+33)','0');
					tel = tel.replace('0033','0');

					if (tel == "" || !expChiffre.test(tel) || tel.length !=  10){
						//faux
						$("#"+tab[i][0]+ext).css({border:"2px solid red"});
						bool[i] = 0;
					}
					else {
						//vrai
						$("#"+tab[i][0]+ext).css({border:"1px solid black"});							
						bool[i] = 1;
					}									
				}			
				break;
			case "attachment":
				regExFile = /^(.+)\.(pdf|jpg|gif|avi)/gi;
				/*if(tab[i][2] == "1"){
					if(document.getElementById(tab[i][0]).value != "" || !regExFile.test(document.getElementById(tab[i][0]).value)){
						//faux
					}
				}	*/		
				break;
			case "radio":
				/*if(tab[i][2] == "1"){
					
				}	*/		
				break;				
			case "checkbox":
				/*if(tab[i][2] == "1"){
					
				}	*/		
				break;
			case "date(mm-dd-yyyy)":
				/*if(tab[i][2] == "1"){
					if (date_valide(document.getElementById(tab[i][0]).value)==false){
						//faux
					}
				}	*/		
				break;	
			default:
				if(tab[i][2] == "1"){
					if(document.getElementById(tab[i][0]+ext).value == ""){
						if(navigator.appName.indexOf('Microsoft')!=-1){
							//faux
							$('#div_erreur_'+tab[i][0]+ext).addClass('erreur-select-ie');
							//exception IE interne
							if(type_form == "contenu"){
								$('.right > select').removeClass();
								$('.right > select').addClass('erreur-right');
							}							
						}
						else{
							//faux
							$("#"+tab[i][0]+ext).css({border:"2px solid red"});
						}
						bool[i] = 0;
					}
					else{
						if(navigator.appName.indexOf('Microsoft')!=-1){
							//vrai
							$('#div_erreur_'+tab[i][0]+ext).removeClass();							
						}
						else{
							//vrai
							$("#"+tab[i][0]+ext).css({border:"thin solid black"});
						}
						bool[i] = 1;						
					} 
				}		
				break;			
		}		
	}
	var chaine = bool.join("");
	if(exp_all_true.test(chaine)){	
		
		document.getElementById("erreur"+ext).style.display = "none";		
		
		//ouverture de la géoloc + Ajax send value
		var exp=new RegExp("^[0-9]*$","g");
    
    if(site == "voneo"){
    	$("#contentLoading").css("height","120%");
    	tmp_url = "../";
    }
    var formulaire='';		
		var bool_ext = false;
		if(ext != ''){
			if($('#geo_ville_ext').val() == ''){
				bool_ext = true;
			}
		}
		else{
			bool_ext = true;
		}
		
		if(bool_ext){
	    //overlay
			$("#contentLoading").fadeIn(700);
			$("#texte").text("  Chargement en cours...");
			
			var test = $.ajax({
		    type: "POST",
		  	url: tmp_url+"ajax-cpostal.php",
		  	data: 'cpostal='+cpostal+'|'+profil, 
		  	async: false
			}).responseText;
			//fin overlay
			//---------------  ouverture de la géoloc ---------------//		
			if(!exp.test(cpostal) || cpostal.length!=parseInt(5,10) || test.toLowerCase() == "ok"){
	      $('#contentLoading').fadeOut(700);			
				Comparatis.geoLocalisation(cpostal,prof,champs,site,ext);
				return false;
			}
			else{				
				if(site == "voneo")
					formulaire = "document.getElementById(\""+form_name+"\").action=\"simulation-2-2-voneo.html\";document.getElementById(\""+form_name+"\").submit();";	
				else if(site == "comparatis"){
					if(ext != ''){
						formulaire = "top.iframe_form.document.getElementById(\""+form_name+"\").action=\"c-2-"+prof+"/determiner-diagnostic.html\";top.iframe_form.document.getElementById(\""+form_name+"\").target=\"_parent\";top.iframe_form.document.getElementById(\""+form_name+"\").submit();";
					}
					else{						
						formulaire = "document.getElementById(\""+form_name+"\").action=\"c-2-"+prof+"/determiner-diagnostic.html\";document.getElementById(\""+form_name+"\").submit();";	
					}
				}
			}
		}
		else{
			formulaire = "top.iframe_form.document.getElementById(\""+form_name+"\").action=\"c-2-"+prof+"/determiner-diagnostic.html\";top.iframe_form.document.getElementById(\""+form_name+"\").target=\"_parent\";top.iframe_form.document.getElementById(\""+form_name+"\").submit();";	
		}
	 	eval(formulaire);		
		return false;				
	}
	else{ 
		document.getElementById("erreur"+ext).style.display = "block";
		return false;		
	}
}


function getCPostalExt(cpostal,champs,ville,profil,site,ext){
	var form_name = 'form';
	var tmp_ext = '';
	if(ext != ''){
		form_name = 'form_frame';
		tmp_ext = '_ext';
	}	

	if(ext == ''){
		//affichage ville	
		$("#ville").css({display:"block"});		
		$("#ville").text("(Ville : " + ville + ")");		
		$("#"+champs+tmp_ext).val(cpostal);
		$("#geo_ville").val(ville);	
	}
	else{
		window.iframe_form.document.getElementById("geo_ville"+tmp_ext).value = ville;		
		window.iframe_form.document.getElementById(champs).value = cpostal;		
	}
	
	var formulaire='';
	if(site == "voneo")
		formulaire = "document.getElementById(\""+form_name+"\").action=\"simulation-2-2-voneo.html\";document.getElementById(\""+form_name+"\").submit();";	
	else if(site == "comparatis"){
		if(ext != ''){
			formulaire = "top.iframe_form.document.getElementById(\""+form_name+"\").action=\"c-2-"+profil+"/determiner-diagnostic.html\";top.iframe_form.document.getElementById(\""+form_name+"\").target=\"_parent\";top.iframe_form.document.getElementById(\""+form_name+"\").submit();";
		}
		else{
			formulaire = "document.getElementById(\""+form_name+"\").action=\"c-2-"+profil+"/determiner-diagnostic.html\";document.getElementById(\""+form_name+"\").submit();";	
		}
	}				
	eval(formulaire);	
}

function setInfosSupp(num){
	var chaine = $("#telepro").val();
	var exp = new RegExp(""+num+"#","g");
		
	if(document.getElementById("telepro"+num).checked == false){
		//non checké
		chaine = chaine.replace(exp,'');
		document.getElementById("telepro").value = chaine;
	}
	else{
		//checké
		if(!exp.test(chaine))document.getElementById("telepro").value = chaine+num+"#";
	}		
}

function supprErreur(type_form,type_tab,ext){
		var tab = type_tab.split(",");	
		switch(tab[1]){
			case "email":
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(tab[2] == "1"){
					if(filter.test(document.getElementById(tab[0]+ext).value)){
						//vrai
						$("#"+tab[0]+ext).css({border:"1px solid black"});
					}
					else{
						//faux
						$("#"+tab[0]+ext).css({border:"2px solid red"});						
					}
				}				
				break;
			case "password":
				var filter  = /^([a-zA-Z0-9]{6,12})+$/;
				/*if(tab[2] == "1"){
					if(filter.test(document.getElementById(tab[0]).value)){
						//vrai
					}					
				}	*/			
				break;
			case "text(chiffre)":
				var filter  = /^([0-9])+$/;
				if(tab[2] == "1"){
					if(filter.test(document.getElementById(tab[0]+ext).value)){						
						//faux
						$("#"+tab[0]+ext).css({border:"1px solid black"});
					}	
					else{
						//vrai
						$("#"+tab[0]+ext).css({border:"2px solid red"});						
					}				
				}	
				break;
			case "text(code postal)":
				if(tab[2] == "1"){
					if( document.getElementById(tab[0]+ext).value == "Code postal ou Ville" || document.getElementById(tab[0]+ext).value == ""){
						//faux
						$("#"+tab[0]+ext).css({border:"2px solid red"});
					}
					else{
						//vrai
						$("#"+tab[0]+ext).css({border:"1px solid black"});											
					}					
				}
				var cpostal = document.getElementById(tab[0]+ext).value;
				var champs = tab[0]+ext;
				break;
			case "text(téléphone)":
				if(tab[2] == "1"){
					exp = /[.()\s+]/g;
					expChiffre = /^([0-9])+$/;
					var tel = (document.getElementById(tab[0]+ext).value).replace(exp,'');
					tel = tel.replace('(+33)','0');
					tel = tel.replace('0033','0');

					if (tel == "" || !expChiffre.test(tel) || tel.length !=  10){
						//faux
						$("#"+tab[0]+ext).css({border:"2px solid red"});
					}
					else {
						//vrai
						$("#"+tab[0]+ext).css({border:"1px solid black"});							
					}									
				}			
				break;
			case "attachment":
				regExFile = /^(.+)\.(pdf|jpg|gif|avi)/gi;
				/*if(tab[2] == "1"){
					if(document.getElementById(tab[0]).value != "" || !regExFile.test(document.getElementById(tab[0]).value)){
						//faux
					}
				}	*/		
				break;
			case "radio":
				/*if(tab[2] == "1"){
					
				}	*/		
				break;				
			case "checkbox":
				/*if(tab[2] == "1"){
					
				}	*/		
				break;
			case "date(mm-dd-yyyy)":
				/*if(tab[2] == "1"){
					if (date_valide(document.getElementById(tab[0]).value)==false){
						//faux
					}
				}	*/		
				break;	
			default:
				if(tab[2] == "1"){
					if(document.getElementById(tab[0]+ext).value == ""){
						if(navigator.appName.indexOf('Microsoft')!=-1){
							//faux
							$('#div_erreur_'+tab[0]+ext).addClass('erreur-select-ie');
							//exception IE interne
							if(type_form == "contenu"){
								$('.right > select').removeClass();
								$('.right > select').addClass('erreur-right');
							}							
						}
						else{
							//faux
							$("#"+tab[0]+ext).css({border:"2px solid red"});
						}
					}
					else{
						if(navigator.appName.indexOf('Microsoft')!=-1){
							//vrai
							$('#div_erreur_'+tab[0]+ext).removeClass();							
						}
						else{
							//vrai
							$("#"+tab[0]+ext).css({border:"thin solid black"});
						}					
					} 
				}		
				break;			
		}		
}

function MailTo(aAddr, aDomain, aSubject){
	var t="mailto:"+aAddr+"@"+aDomain;
	if(aSubject.length>0) t=t+"?subject="+escape(aSubject)
	location.href=t;
}
function verifLength(){
	return false;
}
function limitMe(e) {
	var expChiffre = /^[\d]+$/;
	var code = e.keyCode;
	if (!code) code = e.charCode;
	switch (code) {case 9: case 8: case 46: case 37: case 39: case 36: case 35: return true; }
	if (code == 8 || code == 107) { return true; }
	if (!expChiffre.test(this.value)) return true;
	return this.value.length < $(this).attr("my_maxlength");
}
function limitMeCh(e){
	var code = e.keyCode;
	if (!code) code = e.charCode;
	switch (code) {case 9: case 8: case 46: case 37: case 39: case 36: case 35: return true; }
	return (code>=48 && code<=57);
}
function limite_chiffre(id, lim){
	$('#'+id).attr('my_maxlength',lim).keypress(limitMe);			
}
function limite_chiffre_seul(id, lim){
	$('#'+id).attr('maxlength',lim).keypress(limitMeCh);			
}
function numerique(id){
	var valeur = id.value;
	var expChiffre = /^\d+$/;
	var exp = /[^\d]/g;
	if(valeur != ""){
		if(!expChiffre.test(valeur)){
			id.value = valeur.replace(exp,'');
			id.focus();						
		}
	}			
}