base_url = 'http://hostels.bootsnall.com/services/';
$(document).ready(function()
{ 
  
  var GET = getUrlVars();  
  $.getJSON(base_url + "fetch_countries_json.php?callback=?",function(data){
         $.each(data, function(key, value) {
            if(GET['country'] == value.country_name){
                selected = 'selected="selected" ';                
            }else{
                 selected = '';  
            } 
            $("#country_location_id").append('<option ' + selected + 'value="' + value.country_id + '">' + value.country_name + '</option>');
         });
         
         if(GET['country']){
            $("#country_location_id").trigger('change');    
         }
         
  });
  
  $("#country_location_id").live('change', function(){
        var value = $(this).val();
        if(value!=''){
            $.getJSON(base_url + "fetch_cities_json.php?country_id=" + value + "&callback=?",function(data){
             $.each(data, function(key, value) {
                if(GET['city'] == value.city_name) selected = 'selected="selected" '; else selected = ''; 
                $("#city_location_id").append('<option '+ selected + 'value="' + value.city_id + '">' + value.city_name + '</option>');
             });
            });    
        }
  }); 
  
});


function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
