/*!
 * GesWeb jquery Library for carc using jQuery v1.3.2
 * http://www.obtic.ch/
 *
 * Copyright (c) 2010 Obtic Sarl
 *
 * Date: 2010-07-08 (Fri, 9 July 2010)
 */
  $(document).ready(function(){

    // Category Validation

    if ($('#InpSex').val() != '' && $('#InpAnnee').val() != '') {
      filterCategories( 
        $('#InpSex').val(), 
        $('#InpAnnee').val(), 
        true
      );
    }

//    $('#InpSex').bind($.browser.msie? "propertychange": "change", function(){
    $('#InpSex').change( function() {
			filterCategories(	
        $(this).val(),
        $('#InpAnnee').val(),
        false
      );
    });
//    $('#InpSex').change( function() {
//      filterCategories(	
//        $(this).val(),
//        $('#InpAnnee').val(),
//        false
//      );
//    });

    $('#InpAnnee').blur( function() {
      filterCategories(	
        $('#InpSex').val(),
        $(this).val(),
        false
      );
    });
//    $('#InpAnnee').blur( function() {
//      filterCategories(	
//        $('#InpSex').val(),
//        $(this).val(),
//        false
//      );
//    });
		
  });// Document Ready

  // Helper 
  function filterCategories(sex,birthYear,fromInit) {
//alert(sex);
//alert(birthYear);
//alert(fromInit);
    var age = new Date().getFullYear() - birthYear ;//calculateAge(birthYear);
//alert(age)
    $('#InpCategories').find('option').hide();

    if (!fromInit)
      $('#InpCategories').val('');
					
    $('#InpCategories').find('option').filter( function (index) {

        if ( age >= 1 && age <= 100 )
        {
          //Walking
          if ( age >= 14 && $(this).val() == 'WW' ) return true;

          //Populaire	
          if ( age >= 10 ) {			
            if ( (sex == "H") && ($(this).val() == 'MP') ) return true;
						if ( (sex == "F") && ($(this).val() == 'FP') ) return true;
          }

          //Poussin
          if ( (age >= 1) && (age <= 9) ) {
            if ( (sex == "H") && ($(this).val() == 'MPO') ) return true;
            if ( (sex == "F") && ($(this).val() == 'FPO') ) return true;
							
            //Ecolier C
            if ( (age >= 8) && (age <= 9) ) {
              if ( (sex == "H") && ($(this).val() == 'MEC') ) return true;
              if ( (sex == "F") && ($(this).val() == 'FEC') ) return true;
            }
          }

          //Ecolier B
          if ( (age >= 10) && (age <= 11)  ) {
            if ( (sex == "H") && ($(this).val() == 'MEB') ) return true;
            if ( (sex == "F") && ($(this).val() == 'FEB') ) return true;
          }

          //Ecolier C
          if ( (age >= 12) && (age <= 13)  ) {
            if ( (sex == "H") && ($(this).val() == 'MEA') ) return true;
            if ( (sex == "F") && ($(this).val() == 'FEA') ) return true;
          }

          //Cadet B
          if ( (age >= 14) && (age <= 15)  ) {
            if ( (sex == "H") && ($(this).val() == 'MCB') ) return true;
            if ( (sex == "F") && ($(this).val() == 'FCB') ) return true;
          }	
						
          //Cadet A
          if ( (age >= 16) && (age <= 17)  ) {
            if ( (sex == "H") && ($(this).val() == 'MCA') ) return true;
            if ( (sex == "F") && ($(this).val() == 'FCA') ) return true;
          }
						
          //18 ans
          if ( (age >= 18) && (age <= 19)  ) {
            if ( (sex == "H") && ($(this).val() == 'M18') ) return true;
            if ( (sex == "F") && ($(this).val() == 'F18') ) return true;
          }

					//20 ans
          if ( (age >= 20) && (age <= 39)  ) {
            if ( (sex == "H") && ($(this).val() == 'M20') ) return true;
            if ( (sex == "F") && ($(this).val() == 'F20') ) return true;
          }

          //40 ans
          if ( (age >= 40) && (age <= 49)  ) {
            if ( (sex == "H") && ($(this).val() == 'M40') ) return true;
            if ( (sex == "F") && ($(this).val() == 'F40') ) return true;
          }

          //50 ans hommes
          if ( (age >= 50) && (age <= 59)  ) {
            if ( (sex == "H") && ($(this).val() == 'M50') ) return true;
            if ( (sex == "F") && ($(this).val() == 'F50') ) return true;
          }
	
          //50 ans femme
          if ( (age >= 50) && (age <= 100)  ) {
            if ( (sex == "F") && ($(this).val() == 'F50') ) return true;
          }
	
          //60 ans
          if ( (age >= 60) && (age <= 100)  ) {
            if ( (sex == "H") && ($(this).val() == 'M60') ) return true;
          }
					
          //50 ans old
//          if ( (age >= 50) && (age <= 59)  ) {
//            if ( (sex == "H") && ($(this).val() == 'M50') ) return true;
//            if ( (sex == "F") && ($(this).val() == 'F50') ) return true;
//          }
	
          //60 ans old
//          if ( (age >= 60) && (age <= 100)  ) {
//            if ( (sex == "H") && ($(this).val() == 'M60') ) return true;
//            if ( (sex == "F") && ($(this).val() == 'F60') ) return true;
//          }
        }
        else
        {
          if ( $(this).hasClass('type_'+sex)  || $(this).hasClass('type_U'))
            return true 
        }

      }).show();

	};

  function calculateAge(birthYear)
  {
    return new Date().getFullYear() - birthYear ;
  };
	
