var ltie7 = ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent));
var winpopup = null ;
function isNumeric(sText){
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++){
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1){
			IsNumber = false;
		}
	}
	return IsNumber;
}
function open_popup() { 
	if ((!winpopup) || (winpopup.closed)) 
	{ 
	winpopup = window.open('zoda_im/index.php','CHAT','width=700,height=500,menubar=0,statusbar=0,scro­llbars=0,resizable=1,toolbar=0'); 
	} 
	else if(winpopup.closed) 
	{ 
	winpopup = null 
	} 
	else 
	{ 
	winpopup.focus();
	} 
} 
function suivi() {
	//if(opener) opener.truc = self.window;
}
function doTick(){
	$('#consulter_chat').load("feeds.php",function() { if($('#consulter_chat').html()=='0') $('#consulter_chat').hide(); else $('#consulter_chat').show();});
	}
var isIE =$.browser.msie;
//ltie7 : $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
// Nouvelle fonction jquery de retaillage de blocks
$.fn.adjustSize=function(options) {
	var settings = {
		periode: 4,
		mode: 'height',
		minHeight: 0,
		minWidth: 0
	};
	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if (options) {
		jQuery.extend(settings, options);
	}
	var totalSize = $(this).size();
	var nbPeriodes = Math.ceil(totalSize/settings.periode);
	var index = 0;
	if (settings.mode == 'height' || settings.mode == 'both') {
		var maxTotalHeightArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalHeightArray[blockId]) maxTotalHeightArray[blockId]=0;
			var blockTotalHeight = $(this).height();
			if (blockTotalHeight > maxTotalHeightArray[blockId]) {
				if (settings.minHeight && settings.minHeight > blockTotalHeight) blockTotalHeight = settings.minHeight;
				maxTotalHeightArray[blockId] = blockTotalHeight;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedHeight = maxTotalHeightArray[blockId];
			if (ltie7) $(this).height(blockComputedHeight+'px');
			else $(this).css('minHeight',blockComputedHeight+'px');
			index++;
		});
	}
	if (settings.mode == 'width' || settings.mode == 'both') {
		var maxTotalWidthArray = new Array();
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			if (!maxTotalWidthArray[blockId]) maxTotalWidthArray[blockId]=0;
			var blockTotalWidth = $(this).width() ? $(this).width() : $(this).attr('offsetWidth') ;
			if (blockTotalWidth > maxTotalWidthArray[blockId]) {
				if (settings.minWidth && settings.minWidth > blockTotalHeight) blockTotalWidth = settings.minWidth;
				maxTotalWidthArray[blockId] = blockTotalWidth;
			}
			index++;
		});
		var index = 0;
		$(this).each(function(){
			var blockId = Math.floor(index/settings.periode);
			var blockComputedWidth = maxTotalWidthArray[blockId];
			if (ltie7) $(this).width(blockComputedWidth+'px');
			else $(this).css('minWidth',blockComputedWidth+'px');
			index++;
		});
	}
	
	return this;
}

$(function(){
	
	//suivi();
		   
	$('img[src$=.png]').ifixpng();
	$('input[type=image][src$=.png]').ifixpng();
	$('p.question').adjustSize();
	$('p.question_spec').adjustSize();

	$('#compose_message').hide();
	$('#compose_message.show').show();
	
	$('#zoding_content').hide();
	$('#headline_content').hide();
	
	
	if (ltie7) {
		$('img[@usemap]').css('border','none');
	}
	$('#f_enligne1').click(function(){
			$.post("_handlers/ajax/checked.php",{ onligne: "1"}, function(data){
			alert("Votre Status est : en ligne");
			});
	});
	
	$('#f_enligne0').click(function(){
			$.post("_handlers/ajax/checked.php",{ onligne: "0" }, function(data){
     alert("Votre Status est : hors ligne");
   	});
	});

	
	$('#update_message').click(function(){
			data=$('textarea#f_message').val();			
			$.post("_handlers/ajax/message.php",{ message: data });
			messag=data.substring(0,56);
			alert('Votre Message à été enregistré');
			$('#message_value').html(messag);
			$('#headline_content').toggle();
			//$('textarea#f_message').val(data);
	});
	$('#update_zoding').click(function(){
			data=$('input#f_zoding').val();
			$.post("_handlers/ajax/zoding.php",{ lieux: data });
			zoodin=data.substring(0,56);
			$('#zooding_value').html(zoodin);
			alert('Vos lieux zoding préférés ont été enregistré');
			$('#zoding_content').toggle();
			//$('input#f_zoding').val(data);
	});
	$('a#envoyer_un_message').click(function(){
			$('#compose_message').toggle();
			$('#member_status').hide();
			return false;
	});
	
	/*Repondre a un message*/
	$('#repondre_message').hide();
	
	$('a#aff_repondre').click(function(){
			$('#repondre_message').toggle();
			return false;
	});
	
	$('a#add_zoding').click(function(){
			$('#zoding_content').toggle();
			return false;
	});
	$('a#add_headline').click(function(){
			$('#headline_content').toggle();
			return false;
	});
	
	$('div.container_form').hide();
	$("div.container_form:eq(0)").show();
	$('p.questionnaire_').toggle(function(){ $('div.container_form').hide();   $(this).next("div.container_form").show(); },function(){   $(this).next("div.container_form").hide(); });

	$('#proposer_chat').click(function(){
		open_popup();
		return false;	
	});
	$('#consulter_chat').click(function(){
		open_popup();
		return false;	
	});
	
	$('#f_commander').submit(function(){
		var checkTab="produit[]";
		var formName='f_commander';
		
		for (i=0; i<document.forms[formName].elements[checkTab].length; i++) {
	      if(document.forms[formName].elements[checkTab][i].checked) {
	      	return true; 
	    	}
		}
	      alert('Vous devez choisir un ou plusieurs produits avant de commander !');
	     return false;
	});
	
	$("#spot_pays").change(function(){
			$("#spot_villes").load("ville.php?id="+this.value);
	});
	
	
	$("#party_pays").change(function(){
			$("#party_villes").load("ville_party.php?id="+this.value);
			$("#party_date").load("date_party.php?id="+this.value);
			$("#party_organisateur").load("organisateur_party.php?id="+this.value);
	});
	
	/// affich contacts
	$('#affiche_contacts').click(
	function(){
		$('#f_affich').val('all');
		$('#f_tri_contacts').submit();
		return false;
	});
	$("#im_vip").toggle(function(){$(this).attr('src','_media/img/radio_off.gif'); $('#tp_vip').removeAttr("checked"); },function(){$(this).attr('src','_media/img/vip.gif'); $('#tp_vip').attr("checked", "checked");});
	$("#im_regular").toggle(function(){$(this).attr('src','_media/img/radio_off.gif'); $('#tp_regular').removeAttr("checked");},function(){$(this).attr('src','_media/img/regular.gif'); $('#tp_regular').attr("checked", "checked");});
	$("#im_block").toggle(function(){$(this).attr('src','_media/img/radio_off.gif'); $('#tp_block').removeAttr("checked");},function(){$(this).attr('src','_media/img/blacklist.gif'); $('#tp_block').attr("checked", "checked");});
	
	/// gestion login 
	
	$('#connect').find('#email').focus(
	function (){
		if($(this).val()=='Email'){
		$(this).val('');
		}
	  }
	  
	);
	$('#connect').find('#email').blur(
	function (){
		if($(this).val()=='')
		$(this).val('Email');
	  }
	);
	$('#connect').find('#password').focus(
	function (){
		if($(this).val()=='Mot de passe'){
		$(this).val('');
		}
	  }
	  
	);
	$('#connect').find('#password').blur(
	function (){
		if($(this).val()=='')
		$(this).val('Mot de passe');
	  }
	);
	
	$("#check_liv").click(function(){
		$("#form_livraison").toggle();
	});
	
	$('.form_inscr #date_naiss').focus(function(){
		$(this).parent().prepend("<span id='inscription_bulle'>Il faut avoir 18 ans ou plus pour s'inscrire sur myzoda.com</span>");
	}).blur(function(){$('#inscription_bulle').remove();});
	
	$('.form_inscr #pseudo').focus(function(){
		$(this).parent().prepend("<span id='inscription_bulle'>Pseudo Zoda : Les membres de myzoda vous connaîtront sous ce nom, il doit contenir entre 4 et 16 caractères</span>");
	}).blur(function(){$('#inscription_bulle').remove();});
	
	$('.form_inscr #pwd, #pwd_confirm').focus(function(){
		$(this).parent().prepend("<span id='inscription_bulle'>Mot de passe : doit contenir au moins 5 caractères</span>");
	}).blur(function(){$('#inscription_bulle').remove();});
	
	$('.form_inscr #email').focus(function(){
		$(this).parent().prepend("<span id='inscription_bulle' class='inscription_bulle'>Cette adresse est strictement confidentielle.</span>");
	}).blur(function(){$('#inscription_bulle').remove();});
	
	/// verification age > 18 ans dans le formulaire mon compte
	$("form[@name='questionnaire1']").submit(function(){
		if($("input[@name='fourchette_3']").size()==1){
			//alert(parseInt($("input[@name='fourchette_3']").val()));
			//$('p.titre').html($("input[@name='fourchette_3']").val());
			if(isNumeric($("input[@name='fourchette_3']").val()) ){ 
				if(parseInt($("input[@name='fourchette_3']").val())<18) {
					erreur_age ='Vous devez choisir un age min supérieur ou égal à 18';
					alert(erreur_age);
					$("input[@name='fourchette_3']").focus();
					return false;
				}
			}
			else{erreur_age='Vous devez choisir un age min valide supérieur ou égal à 18'; alert(erreur_age); return false;}
			if(isNumeric($("input[@name='fourchette_4']").val()) ){ 
				if(parseInt($("input[@name='fourchette_4']").val())<18) {
					erreur_age ='Vous devez choisir un age max supérieur ou égal à 18';
					alert(erreur_age);
					$("input[@name='fourchette_4']").focus();
					return false; 
				}
			}
			else{ erreur_age='Vous devez choisir un age max valide supérieur ou égal à 18'; alert(erreur_age); return false; }
		}
		return true;
	});

});





