$(document).ready(function(){

    jQuery.validator.addMethod("selectCriteria",
			function(val, el) {
				if($("#villeOuCodePostal").val()=="" && $("#ville").val()=="" && $("#region").val()=="") return false;
				return true;
			}, "Veuillez sélectionner une région ou indiquez la ville ou le code postal recherché"
		);
	
	// Validation du formulaire de login
	var validator = $("#storeSearch").validate({
		rules: {
			villeOuCodePostal: {selectCriteria: true}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			$("#villeOuCodePostal_tick").addClass("notvalid");
			$("#region_tick").addClass("notvalid");
			$("#villeOuCodePostal_message").removeAttr("style");
			$("#region_message").removeAttr("style");
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			label.parent().prev().removeClass("notvalid");
			label.remove();
		},
		
		highlight: function(element, errorClass) {
			$("#villeOuCodePostal_tick").addClass("notvalid");	
		},
		
		unhighlight: function(element, errorClass) {
			$("#villeOuCodePostal_tick").removeClass("notvalid");
			$("#region_tick").removeClass("notvalid");
			$("#villeOuCodePostal_message").attr("style","display: none;")
			$("#region_message").attr("style","display: none;")
		}
		
	});
	
	// Validation du formulaire de recherche de magasin
	$("#storeSearch").submit(function(){

		if($("#storeSearch").valid()) {

			var code_postal = $("#villeOuCodePostal").val();
			var ville =$("#ville").val();
				
			if(ville != null && ville != ""){
				return true;
			}
	
			if($("#coordsLat").val() == ""){
				var adresse = null;
				
				if(code_postal != null && code_postal != ""){
					adresse = code_postal+" FRANCE";
				}
				
				if(adresse != null){
					resolveAddress(adresse);
					return false;
				}
			}
		}
		else
		{
			return false;
		}
			
	});
});

$(function(){
	$("#tParfumeries").click(function(){
		checkToutesParfumeries()
	})

	$("#iBeaute").click(function(){
		checkCritere();
	})

	$("#sClarins").click(function(){
		checkCritere();
	})

	$("#ePara").click(function(){
		checkCritere();
	})

	$("#cBio").click(function(){
		checkCritere();
	})

	$("#gNov").click(function(){
		checkCritere();
	})

	$("#pCarteCadeau").click(function(){
		checkCritere();
	})
})

function checkCritere()
{
	if($("#tParfumeries").attr("checked") == true)
	{
		$("#tParfumeries").removeAttr("checked");
	}
	else if($("#storeSearch :checkbox:checked").length == 0)
	{
		$("#tParfumeries").attr("checked","checked");
	}
}

function checkToutesParfumeries()
{
	if($("#tParfumeries").attr("checked") == true)
	{
		$("#storeSearch :checkbox").removeAttr("checked");
	}
	$("#tParfumeries").attr("checked","checked");
}

$(function(){
	  $("#region").change(function(){
		  if($(this).val() == "")
		  {
			  var options = '<option value="">Choisissez une ville</option>';
		      $("select#ville").html(options);
		      $("select#region").val("");
		      $("select#ville").attr("disabled","disabled");
		  }
		  else
		  { 
		  	selectRegion($(this).val());
		  }
	  })
	})

$(function(){
	  $("#villeOuCodePostal").focus(function(){
		  var options = '<option value="">Choisissez une ville</option>';
	      $("select#ville").html(options);
	      $("select#region").val("");
	      $("select#ville").attr("disabled","disabled");
	  })
	})
	
function selectRegion(regionId)
{
	$.getJSON(PATHRACINE+"/AjaxSelectVille?"+paramsGlobal, {regionId: regionId, ajax: 'true'} , function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("select#ville").html(options);
      $("select#ville").removeAttr("disabled")
      $("#villeOuCodePostal").val("");
      $("#region").val(regionId);
    })
}
