var countries;
var countryregions;
var cities;
var functionsStores = {

    selectCountries: function(region, country){
        if(!country) var country= '';
        $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=getcountries&continentid=' + region +'&countryid='+country,
            beforeSend: function() {
                if(countries) {
                    countries = countries.remove();
                }
                if(cities) {
                    cities = cities.remove();
                }
                if(countryregions) {
                    countryregions = countryregions.remove();
                }
                $('#countriesSelector').empty();
                $('#citiesSelector').empty();
                functionsStores.displayCountryRegions(false);
			},
			success: function(msg) {
				countries = jQuery(msg).appendTo('#countriesSelector');
			}
		});
	},
	
	selectCities: function(country, countryregion, city){ //countryregion et city, paramètres non obligatoires
	   if(!city) var city = '';
	   if(!countryregion) var countryregion = '';
	   
	   if(country!=195) {
	       functionsStores.displayCountryRegions(false);    
       }
	   
	   if(country==195 && countryregion=='') {
	       functionsStores.submitForm('search');
           //functionsStores.selectCountryRegions(country);    
       } else {
            $.ajax({
    			type: 'POST',
    			url: '?ajax_stores',
    			data: 'action=getcities&countryid=' + country + '&countryregionid='+ countryregion +'&city='+city,
                beforeSend: function() {
                    if(cities) {
                        cities = cities.remove();
                    }
                    $('#citiesSelector').empty();
    			},
    			success: function(msg) {
    				cities = jQuery(msg).appendTo('#citiesSelector');
    			}
    		});
    	}
    	if(country!=195) {
    	    functionsStores.submitForm('search');
    	}
	},
	
	
	selectCountryRegions: function(country){
	   
       $.ajax({
			type: 'POST',
			url: '?ajax_stores',
			data: 'action=getcountryregion&countryid=' + country,
            beforeSend: function() {
                if(countryregions) {
                    countryregions = countryregions.remove();
                }
                $('#countryRegionsSelector').empty();
			},
			success: function(msg) {
				countryregions = jQuery(msg).appendTo('#countryRegionsSelector');
				$('#countryRegionsSelector').css("display","inline");
			}
		});
		
    },
	
	displayCountryRegions: function(display) {
	    if(display==true) {
	        $('#countryRegionsSelector').css("display","inline");
        } else {
            $('#countryRegionsSelector').css("display","none");
        }  
    },
	
	/*selectStores: function(city, country, region) {
	   $.ajax({
	       type: 'POST',
	       url: '?ajax_stores',
	       data: 'action=search&regions='+region+'&countries='+country+'&cities='+city,
	       success: function (msg) {
	           stores = jQuery(msg).appendTo('#results');
	       }
	    });
    },*/
	
	showStores: function() {
	   $("div#representatives").hide(); 
	   $("div#customerservice").hide();
	   $("div#stores").show();
	   $("#btn_stores").css({background: "#666", color: "#fff"});
	   $("#btn_repres").css({background: "#000", color: "#c6c7c8"});
	   $("#btn_customer").css({background: "#000", color: "#c6c7c8"});
	   $("#option").attr("value", "stores");
	},
	
	showRepresentatives: function() {
	    $("div#stores").hide();
        $("div#customerservice").hide(); 
		$("div#representatives").show();
        $("#btn_repres").css({background: "#666", color: "#fff"});
	    $("#btn_stores").css({background: "#000", color: "#c6c7c8"});
        $("#btn_customer").css({background: "#000", color: "#c6c7c8"});
        $("#option").attr("value", "repres");     
    },
    
    showCustomerService: function() {
	    $("div#stores").hide(); 
		$("div#representatives").hide();
		$("div#customerservice").show();
		$("#btn_customer").css({background: "#666", color: "#fff"});
        $("#btn_repres").css({background: "#000", color: "#c6c7c8"});
	    $("#btn_stores").css({background: "#000", color: "#c6c7c8"});
        $("#option").attr("value", "cs");     
    },
    
    submitForm: function(action) {
        if(action=="search") {
            document.form_stores.submit() ;
        }
    }

}; 