				/********************************/
				/*          Fonctions JavaScript School		*/
				/*                Requetes AJAX 	 	*/
				/********************************/
				

var AjaxFile = "/AJAX_School.php";
var selRegion = 0;

$(function() {        
//    $("#inpCity").autocomplete("/AJAX_School.php",{extraParams:{action:'getCity'}, onItemSelect:ExecuteSearch});
    
    $('.map').load(function() { $(this).maphilight({fillColor: 'FFFFBB',strokeColor: 'C2C2C2',fillOpacity: 0.4, fade: true}); });
    $('.mapeurope').load(function() { $(this).maphilight(); });
    
    $('#btnSearch').click(function(){
        ExecuteSearch();
    });
    
    $("#inpCountry").change(function(){ 
        ExecuteSearch();
    });

    $("#inpLevel").change(function(){
        ExecuteSearch();
    });

    $("#inpPriceMax").change(function(){
        ExecuteSearch();
    });

 	 $('#inpPriceMax').bind("keyup", function(e){
 	 	  checkNumber('inpPriceMax');
      ExecuteSearch();
    });

   $.ajaxSetup( {
   	url: AjaxFile,
   	dataType: 'text',
   	type: "GET"
 	 } );
    
//    $('#inpCity').bind("keyup", function(e){
//      ExecuteSearch();
//    }); 

    // execute query if BACK button used as query already executed
//    if ($('#inpCity').val() != '' || $('#inpLevel').val() != '')
//      ExecuteSearch();

  $('#divTraining').jqm({
      //onShow: function(h) {
        /* callback executed when a trigger click. Show notice */
        //h.w.slideDown("slow"); 
        //},
      onHide: function(h) {
        /* callback executed on window hide. Hide notice, overlay. */
        h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); }  
      } 
  );

  
  $('#hrefSelCountry').click(function(){
      DisplayMap();
  });

  $('.jqmWindow').height(screen.availHeight - 200 ) ;

  $('#divMap').jqm({
      //onShow: function(h) {
        /* callback executed when a trigger click. Show notice */
        //h.w.slideDown("slow"); 
        //},
      onHide: function(h) {
        /* callback executed on window hide. Hide notice, overlay. */
        h.w.slideUp("slow",function() { if(h.o) h.o.remove(); }); }  
      } 
  );
	
});


  function SetCountry(country_id) {
    selRegion = 0;
    $('#inpRegion').val(country_id);
    $('#inpCountry').val(country_id);
    razRegion();
    // hide modal div
    $('#divMap').jqmHide();
  }


  function razRegion(){
    selRegion = 0;
    $('#spnRegion').html('');
    ExecuteSearch();
  }

  function SetRegion(selected_region_id, selected_region_name) {
      $("#inpCountry").val(1);
      selRegion = selected_region_id;
      var spnregionhtml = selected_region_name + '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="razRegion" class="mini" onClick="javascript:razRegion();">Supprimer</a>';
      $('#spnRegion').html(spnregionhtml);
      ExecuteSearch();
  }
  
  
  function ExecuteSearch(){
    
    //$('#divSchoolSearchResult').hide(); //html('<img src="/images/ajax_loader.gif">');
    $('#divSchoolSearchResult').html('<img src="/images/ajax_loader.gif">');
    
		var param = 'action=getSchools';
//		param += '&city=' + $('#inpCity').val() ;
		param += '&pricemax=' + $('#inpPriceMax').val() ;
		param += '&level=' + $('#inpLevel').val() ;
    param += '&country=' + $("#inpCountry").val(); ;
		
		if (selRegion != 0) 
		  param += '&region=' + selRegion;
	
  	$.ajax({
      data: param,
      error: function(req,str,e){
          errmsg = 'Error start Execute Query' + str;
      },
      success: function(msg){
          // do something 
       	   handleSearchResult(msg);
  //     	   selRegion = "";
      }
  	});
  }


  function handleSearchResult(msg) {
    $('#divSchoolSearchResult').html(msg).slideDown('slow');
    
    $('html,body').animate({ scrollTop: $('#School_search').offset().top }, 1000);
  }

	function checkNumber(id)
		{
			var Nbr_Erreur = 0;
			var Obj = null;
			var Reg;


				Obj = document.getElementById(id);
				Reg = new RegExp('[^0-9]+', 'g'); //  initialisation of Reg
			
				if( Reg.test( Obj.value))
				{
					Nbr_Erreur++;
					Obj.value = Obj.value.replace(/[^0-9]+/, '');
				}

		return( Nbr_Erreur);
		}		

function LoadTrain(TrainId) {
		var param = 'action=getTrain';
		param += '&train_id=' + TrainId ;
		
  	$.ajax({
      data: param,
      error: function(req,str,e){
          errmsg = 'Error start Execute Query' + str;
      },
      success: function(msg){
          // do something 
       	  $('#divTraining').html(msg);
      }
  	});
}	

function DisplayTrain(TrainId) {
	LoadTrain(TrainId);
	$('#divTraining').jqmShow();  
}

function CloseTrain() {
	$('#divTraining').jqmHide();
	$('#divTraining').html('');
}		

function DisplayMap() {
  $('#divMap').jqmShow();
}

