		
//the id of regionId and countryId selector
var regionSelectorId = "regionId";
var countrySelectorId = "countryId";
var placeCodeSelectorId = "";
var json ;
var cs;
var rs;
var ps;
//the regionIds list,[id=businessCategory,name=region name]
//var regionIds = [[3, "日本"], [1, "東南亞"], [2, "中國"], [5, "長線"], [6, "EGL 向健康出發"], [7, "EGL 跳出校園計劃"]];

function initRegionDropDown(regionIdList, regionId, regionCode)
{
	
	regionIds = regionIdList;
	regionSelectorId = regionId
	rs = document.getElementById(regionSelectorId);

	
	//add by sam 
	buildRegionId();
	restoreOldRegionId(rs,regionCode);
	
}

function initCountryDropDown(countryId, oldCountryId, json_in)
{
	json = json_in;
	countrySelectorId = countryId;
	cs = document.getElementById(countrySelectorId);
	//store the old countryName
	regionIdChange(rs);
	restoreOldCountryId(cs,oldCountryId);

	
}

function initPlaceName(placeNameId,oldPlaceName,json_in){
	json = json_in;
	placeCodeSelectorId =placeNameId;
	ps=document.getElementById(placeNameId);
	
	countryChange(cs);
	restoreOldPlaceName(ps,oldPlaceName);
}


//build the regionCd select from regionIds
function buildRegionId() {
	var s = document.getElementById(regionSelectorId);
	var tOption = null;
	for (i = 0; i < regionIds.length; i++) {
		tOption = document.createElement("option");
		tOption.text = regionIds[i][1];
		tOption.value = regionIds[i][0];
		s.options.add(tOption);
	}
}

function refreshCountryIds(cids) {
	var cs = document.getElementById(countrySelectorId);
	var tOption = null;
	
	//remove all existing countryId first
	for (i = cs.options.length - 1; i >= 0; i--) {
		cs.remove(i);
	}
					
	//add the new countryId
	for (i = 0; i < cids.length; i++) {
		tOption = document.createElement("option");
		tOption.text = cids[i].name;
		tOption.value = cids[i].code;
		cs.options.add(tOption);
	}
	
}

//refresh the placeCodes list by given placeCodes
function refreshPlaceName(places){
	ps = document.getElementById(placeCodeSelectorId);
	var tOption = null;
	
	//the placeCodeSelector is not ready
	if(ps==null){
		return;
	}
	
	//remove all existing placeCodes first
	for (i = ps.options.length - 1; i >= 0; i--) {
		ps.remove(i);
	}
	
	//show when there are >=1 place coes
	if(places.length<=1){
		ps.style.display='none';
	}else{
		ps.style.display='';
	}
					
	//add the new placesCodes
	for (i = 0; i < places.length; i++) {
		tOption = document.createElement("option");
		tOption.text = places[i][1];
		tOption.value = places[i][0];
		ps.options.add(tOption);
	}
}

function getNewDefaultRegionCountryOption(){
	var tOption = document.createElement("option");
	tOption.text="---請選擇---";
	tOption.value="";
	return tOption;
}

function regionIdChange(s){

	//get the array from json
	var cids = json[s.value];
	
	//user select a regionId
	if(cids!=null){
		//if the array contains nothing, doesn't display the countryId
		if(cids.length<=1){
			cs.style.display='none'
		}else{
			cs.style.display='';
		}
		
		refreshCountryIds(cids);
		//store the new default option
		var tOption=getNewDefaultRegionCountryOption();
		tOption.text="所有地區";
		cs.options.add(tOption,0);
		//restoreOldCountryId(cs);
		
	}else{
		//user doesn't select a regionId
		if(s.selectedIndex == 0){
			//enable the first countryId
			//cs.options[0].disabled='';
			var tmp = new Array();
			for(i=0;i<regionIds.length;i++){
				cids = json[regionIds[i][0]];
				if(cids!=null){
					for(j=0;j<cids.length;j++){
						tmp.push(cids[j]);
					}
				}
			}
			refreshCountryIds(tmp);
			//store the new default option
			var tOption=getNewDefaultRegionCountryOption();
			tOption.text="所有國家";
			cs.options.add(tOption,0);
			//restoreOldCountryId(cs);
		}else{
			refreshCountryIds(new Array());
			cs.style.display='none'
		}
	}
	
	//select "所有國家" as default option
	cs.selectedIndex=0;
	//refresh the place codes selector
	countryChange(cs);
}

//change placeCode after the countrySelector changes
function countryChange(el){

	//all country
	var countrys = new Array();

	for(i=0;i<regionIds.length;i++){
		countrys=countrys.concat(json[regionIds[i][0]]);
	}
	
	//the place codes under the country
	var placeCodes = new Array();
	for(i=0;i<countrys.length;i++){
		if(countrys[i]!=null){
			if(countrys[i].code==el.value){
				placeCodes = placeCodes.concat(countrys[i].places);
			}
		}
	}
	refreshPlaceName(placeCodes);
	
	//store the new default option
	var tOption=getNewDefaultRegionCountryOption();
	tOption.text="所有地方";
	if(ps!=null){
		ps.options.add(tOption,0);
		ps.selectedIndex=0;
	}
	
}	
		
//common function for restoring regionId and countryId

function restoreOldRegionId(s,oldRegionId){
	for(i=0;i<s.options.length;i++){
		if(s.options[i].value==oldRegionId){
			s.selectedIndex=i;
			break;
		}	
	}
}

function restoreOldCountryId(s,oldCountryId){
	for(i=0;i<s.options.length;i++){
		if(s.options[i].value==oldCountryId){
			s.selectedIndex=i;
			break;
		}	
	}
}

function restoreOldPlaceName(s,oldPlaceName){
	for(i=0;i<s.options.length;i++){
		if(s.options[i].value==oldPlaceName){
			s.selectedIndex=i;
			break;
		}	
	}
}
 