$(document).ready(function(){
    initApp();
	jQuery("#country").change(function(){
	   showChilds();
	});

	jQuery("#province").change(function(){
	   showChildc();
	});
 });

function initApp(){
  //Create Country Options
  var countryOptions = document.getElementById('country');
  countryOptions.options[0] = new Option('loading.....','');
  getRPCJSON('getCountry',0);
}


function getRPCJSON(method,id){
	 var host = window.location.host.replace ( /:.+$/, '' );
	 //alert(method+"/"+id);
	 jQuery.getJSON("http://"+host+"/xmlrpc/client.php?task="+method+"&id="+id,
        function(data){
		  var task = data["task"];
		  task = task.toLowerCase();
		  if(parseInt(data['error']) == 1){
		    if(task == 'getcountry'){
		        var countryOptions = document.getElementById('country');
               
				countryOptions.options.length = 0;

			    countryOptions.options[0] = new Option('-- other --','other');
			}else if(task == 'getchild'){
				var province = document.getElementById('province');
				 province.options.length = 0;
				 province.options[0] = new Option('-- other --','other');
			}else if(task == 'getcity'){
			    var city = document.getElementById('city');
                 city.options.length = 0;
				 city.options[0] = new Option('-- other --','other');
			}
			return false;
		  }

		  if(task == 'getcountry'){
		    createCountry(data);
		  }else if(task == 'getchild'){
		    showProvince(data);
		  }else if(task == 'getcity'){
			showCity(data);
		  }else{
		    alert('Error Method..');
		  }
        });
}

function createCountry(json){
  var countryOptions = document.getElementById('country');
  countryOptions.options[0] = new Option('--select--','');
  for(var i in json){
	// alert(json[i].title);
	if(json[i].title == "" || json[i].title == undefined){
	  continue;
	}
     countryOptions.options[countryOptions.options.length] = new Option(json[i].title,json[i].alias);
  }
}

function showProvince(json){
   var province = document.getElementById("province");
   province.options.selectedIndex=0;
   province.options.length = 0;
   province.options[0] = new Option('--select--','');
   try{
   var n=1;
   for(var i in json){
	 //alert(json[i].title);
	if(json[i].title == "" || json[i].title == undefined){
	  continue;
	}
     province.options[n] = new Option(json[i].title,json[i].alias);
	 n++;
  }

   }catch(e){}
}

function showCity(json){
   var city = document.getElementById("city");
   city.options.selectedIndex=0;
   city.options.length=0;
   city.options[0] = new Option('--select--','');
   try{
   var n=1;
   for(var i in json){
	// alert(json[i].title);
	if(json[i].title == "" || json[i].title == undefined){
	  continue;
	}
     city.options[n] = new Option(json[i].title,json[i].alias);
	 n++;
  }

   }catch(e){}
}

function showChilds(){
   var country = document.getElementById('country');
   var province = document.getElementById("province");
   var mid = country.options[country.options.selectedIndex].value;
   province.options[0] = new Option('loading.....','');
   getRPCJSON('getChild',mid);
   return true;
}

function showChildc(){
   var city = document.getElementById('city');
   var province = document.getElementById("province");
   var mid = province.options[province.options.selectedIndex].value;
   city.options[0] = new Option('loading.....','');
   getRPCJSON('getCity',mid);

   return false;
}