var tabdispo = new Array();
var tabpromo = new Array();
var tabpero  = new Array();
var tabpero2  = new Array();

var lstNombre = "0123456789";
var lstCarSpec = "&é'(-è_çà)=*ù!:;,?./§-+<>$£µ%"+'"';

function RunFoo(swf, hauteur, largeur, couleur, nom, remp){
	document.write('<object type="application/x-shockwave-flash" wmode=transparent data="'+swf+'" width="'+hauteur+'" height="'+largeur+'">'+"\n");
	document.write('<param name="movie" value="'+swf+'" />'+"\n");
	document.write('<param name="bgcolor" value="'+couleur+'" />'+"\n");
	document.write('<param name="quality" value="high" />'+"\n");
	document.write('<param name="wmode" value="transparent" />'+"\n");
	document.write('<param name="movie" value="'+swf+'" />'+"\n");
	document.write(remp+"\n");
	document.write("</object>\n");
}

function FrontJS(){
	
	$("#btnTest").click(function(){
		alert(_defaultsearch);
	});
	
	$("#mode_paiement li input").hide();
	$("#mode_paiement li input").each(function(){
		if($(this).attr('checked') == true){
			var par = $(this).parent();
			var img = $("#"+par.attr('id')+" img");
			img.css('border', '3px solid #FF7200');
		}
	});

	$("#mode_paiement li label img").click(function(){
		var lbl = $(this).parent();
		$("#mode_paiement li input").attr('checked', false);
		$("#"+lbl.attr('for')).attr('checked', true);
		$("#mode_paiement li label img").css('border', 'none');
		$(this).css('border', '3px solid #FF7200');
	});
	
	$(".datecal").datepicker($.extend({minDate: 0},$.datepicker.regional[_lang],{dateFormat:_dateformat}));
	
	$("#depart,#arrivee").datepicker($.extend({beforeShow: customRange4,minDate: 0},$.datepicker.regional[_lang],{dateFormat:_dateformat}));
	//console.log('test');
	function customRange4(input) {  
		//console.log();
	    return {minDate: (input.id == "depart" ? $("#arrivee").datepicker("getDate") : 0), maxDate: (input.id == "arrivee" ? $("#depart").datepicker("getDate") : null)};  
	}	

	$("#depart2,#arrivee2").datepicker($.extend({minDate: 0,
		onSelect: function(value, date) { 
			//alert('The chosen date is ' + value + '-'+$("#arrivee2").val()+'-'+$("#depart2").val()); 
			val1 = $("#arrivee2").val();
			val2 = $("#depart2").val();
			if(val1!='' && val2==''){
				$.get("/_includes/script-cal.inc.php?action=depart&id_bien="+$('#id_bien').val()+"&id_loueur="+$('#id_loueur').val()+"&date="+val1, function(data){
					$("#depart2").val(data);
				});
			}else if(val2!='' && val1==''){
				$.get("/_includes/script-cal.inc.php?action=arrivee&id_bien="+$('#id_bien').val()+"&id_loueur="+$('#id_loueur').val()+"&date="+val2, function(data){
					$("#arrivee2").val(data);
				});
			}
			setTimeout(dispo,1000);
		},
		onChangeMonthYear: function(year, month){
			//tabdispo2 = new Array();
			//tabpero2 = new Array();

			month2 = (month + 1);
			yeard = yearf = year;
			if (month < 10) 
				month = '0' + month;
			if (month2 < 10) 
				month2 = '0' + month2;
			
			if(month2>=13){
				month2='01';
				yearf+=1;
			}
			
			$.ajax({
				type: "POST",
				url: "/_includes/script-bien.inc.php",
				data: 'action=getperiode&id_bien=' + $('#id_bien').val() + '&id_loueur='+$('#id_loueur').val()+'&debut=' + yeard + '-' + month + '-01&fin=' + yearf + '-' + month2 + '-01',
				success: function(msg){
					tmp = msg.split('||');
					
					for (i = 0; i < tmp.length; i++) {
						tmp2 = tmp[i].split('|');
						tmp3 = tmp2[0].split('_');
						
						if(!tabpero2[tmp3[1]+'_'+tmp3[2]]){ tabpero2[tmp3[1]+'_'+tmp3[2]] = new Array(); }
						tabpero2[tmp3[1]+'_'+tmp3[2]][tmp2[0]] = new Array();
						tabpero2[tmp3[1]+'_'+tmp3[2]][tmp2[0]][0] = tmp2[1];
						tabpero2[tmp3[1]+'_'+tmp3[2]][tmp2[0]][1] = tmp2[2];
						
						//console.log('--'+tmp2[0]+' : '+tmp2[2]);
						$('#c' + tmp2[0]).addClass('bperi' + tmp2[1]);
						$('#c' + tmp2[0]+' a').attr('title',''+_prixnuit+' : '+tmp2[2]+' €');
					}
					//console.log(tabpero2);
					//debugger;
				}
			});	
			qte=$('#quantite').val();
			if(qte==0){
				$.ajax({
					type: "POST",
					url: "/_includes/script-bien.inc.php",
					data: 'action=getbloc&id_bien=' + $('#id_bien').val() + '&id_loueur='+$('#id_loueur').val()+'&debut=' + yeard + '-' + month + '-01&fin=' + yearf + '-' + month2 + '-01',
					success: function(msg){
						//alert('++');
						tmp = msg.split('||');
						
						//alert(msg+' '+tmp.length);
						for (i = 0; i < tmp.length; i++) {
							//alert(tmp[i]);
							tabdispo[tmp[i]] = 1;
							$('#c' + tmp[i]).addClass('dateblo');
						}
					}
				});
			}else{
				$.ajax({
					type: "POST",
					url: "/_includes/script-bien.inc.php",
					data: 'action=getbloceltfus&qte='+qte+'&id_bien=' + $('#id_bien').val() + '&id_loueur='+$('#id_loueur').val()+'&debut=' + yeard + '-' + month + '-01&fin=' + yearf + '-' + month2 + '-01',
					success: function(msg){
						//alert('++');
						tmp = msg.split('||');
						
						//alert(msg+' '+tmp.length);
						for (i = 0; i < tmp.length; i++) {
							//alert(tmp[i]);
							tabdispo[tmp[i]] = 1;
							$('#c' + tmp[i]).addClass('dateblo');
						}
					}
				});
			}
			
			$.ajax({
				type: "POST",
				url: "/_includes/script-bien.inc.php",
				data: 'action=getpromo&id_bien=' + $('#id_bien').val() + '&id_loueur='+$('#id_loueur').val()+'&debut=' + yeard + '-' + month + '-01&fin=' + yearf + '-' + month2 + '-01',
				success: function(msg){
					//alert('++');
					tmp = msg.split('||');
					
					//alert(msg+' '+tmp.length);
					for (i = 0; i < tmp.length; i++) {
						//alert(tmp[i]);
						tabpromo[tmp[i]] = 1;
						$('#c' + tmp[i]).addClass('datepromo');
					}
				}
			});
		}
	},$.datepicker.regional[_lang],{dateFormat:_dateformat}));
	
	
	mapacc();
	mapfiche();
	
	$("#recherche_email").val(_chpemail);
	
	$("#recherche_email").click(function(){
		
		if($("#recherche_email").val() == _chpemail){
			//alert(_chpemail);
			$("#recherche_email").val("");
		}
	});
	
	$(".verifpwd").keyup(function(){
		var mdp = $(this).val();
		
		//On vérifie l'indice de sécurité du mot de passe
		var csets = new Array(0,0,0,0);
		var i, c, len, _width;
		
		len = mdp.length;
		
	    for (i = 0; i < len; i++){
		    c = mdp.charAt(i);
			if (lstNombre.indexOf(c)>=0){
			// caractère numérique
				csets[0]++;
			}
			else if (lstCarSpec.indexOf(c)>=0){
			// caractère spécial
				csets[1]++;
			}
			else if (c.toUpperCase() ==c){
			// caractère en Majuscule
				csets[2]++;
			}
			else if (c.toLowerCase() ==c){
			// caractère en Minuscule
				csets[3]++;
			}
		}
		//On modifie les indicateurs
		var indice = 0;
		if(csets[0] >= 1){
			indice += 2;
		}
		if(csets[1] >= 1){
			indice += 2;
		}
		if(csets[2] >= 1){
			indice += 2;
		}
		if(csets[3] >= 1){
			indice += 2;
		}
		if(len >= 8){
			indice += 2;
		}
		else if(len >= 6){
			indice += 1;
		}
		
		if(indice > 5 && len < 6){
			indice = 4;
		}
		if(indice > 7 && (csets[0] == 0 || csets[1] == 0)){
			indice = 7;
		}
		else if(indice >= 5 && csets[0] == 0 && csets[1] == 0){
			indice = 4;
		}
		
		if(indice >=9){
			$("#indicetxt").html(_secu_pwd_5);
			$("#indicevisu span").css('background-color', '#00FF00');
		}
		else if(indice >=7){
			$("#indicetxt").html(_secu_pwd_4);
			$("#indicevisu span").css('background-color', 'green');
		}
		else if (indice >= 5) {
			$("#indicetxt").html(_secu_pwd_3);
			$("#indicevisu span").css('background-color', 'yellow');
		}
		else if (indice >= 3) {
			$("#indicetxt").html(_secu_pwd_2);
			$("#indicevisu span").css('background-color', 'orange');
		}
		else if (indice >= 1){
			$("#indicetxt").html(_secu_pwd_1);
			$("#indicevisu span").css('background-color', 'red');
		}
		else if (mdp == "") {
			$("#indicetxt").html(_secu_pwd_0);
			$("#indicevisu span").css('background-color', '#fff');
		}
		_width = indice * 10;
		$("#variavisu").css('width', _width+'%');
	});
	
	if($("#search").val() == ""){
		$("#search").val(_defaultsearch);
	}
	
	if($("#txt_recherche").val() == ""){
		$("#txt_recherche").val(_defaultsearch);
	}
	
	$("#search").bind("focus", function(e){
		if(this.value == _defaultsearch)
			this.value = '';
	});
	$("#search").bind("blur", function(e){
		if(this.value == '')
			this.value = _defaultsearch;
	});
	$("#txt_recherche").bind("focus", function(e){
		if(this.value == _defaultsearch)
			this.value = '';
	});
	$("#txt_recherche").bind("blur", function(e){
		if(this.value == '')
			this.value = _defaultsearch;
	});
	

	/*function customRange(input) {  
	    return {minDate: (input.id == "depart2" ? 
	        $("#arrivee2").datepick("getDate") : null),  
	        maxDate: (input.id == "arrivee2" ? 
	        $("#depart2").datepick("getDate") : null)};  
	}*/
	
	function customRange(input) { 
		alert(input.id); 
	    if(input.id == "depart2" && $("#arrivee2").val()==''){
			$("#arrivee2").val(input.value);
		}else if(input.id == "arrivee2" && $("#depart2").val()==''){
			$("#depart2").val(input.value);
		}
	}

	// fait fonctionner l'utilisation des images dans un label sous ie6
	if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1){
		var a = document.getElementsByTagName("label");
		for(var i=0,j=a.length;i<j;i++){
			if(a[i].hasChildNodes && a[i].childNodes.item(0).tagName == "IMG"){
				a[i].childNodes.item(0).forid = a[i].htmlFor;
				a[i].childNodes.item(0).onclick = function(){
					var e = document.getElementById(this.forid);
					switch(e.type){
						case "radio": e.checked|=1;break;
						case "checkbox": e.checked=!e.checked;break;
						case "text": case "password": case "textarea": e.focus(); break;
					}
				}
			}
		}
	}
	
	$('.limg').click(function(){
		_id = this.id;
		_id = _id.replace('se','');
		
		source = $('#im'+_id).attr('src');
		
		$('#principale img').attr('src',source.replace('small','normal'));
		
		return false;
	});
	
	
	$('#selingelit').change(function(){
		var _val = this.value;
		_id = this.id;
		
		if(_val!=0){
			temp = Math.floor((nbnuit-1)/_val);
		}else{
			temp = 0;
		}
		
		$('#'+_id.replace('se','fs')).html(temp);
	});
	
	
	$('#semenage').change(function(){
		var _val = this.value;
		_id = this.id;
		
		temp = Math.floor((nbnuit-1)/_val);
		
		$('#'+_id.replace('se','fs')).html(temp);
	});
}