var regionList = null;
var locationList = null;
var regionCode = null;
var locationCode = null;
function init(in_regionCode,in_regionList,in_locationCode,in_locationList) {
	regionCode=in_regionCode;
	regionList=in_regionList;
	locationCode=in_locationCode;
	locationList=in_locationList;
}

function getRegionList(){
	PackageService.getRegionList(getRegionListCallback);
}
function getRegionListCallback(data) {
	if (data != null && regionList != null) {
		clearDropDown(regionList);
		addOption(regionList, "", "\u6240\u6709\u5730\u5340");
		var locationListLoaded=false;
		for (var i=0;i<data.length;i++) {
			addOption(regionList, data[i].value, data[i].label);
			if(locationListLoaded){continue;}
			if(regionCode!=null && regionCode!=""){
				if (data[i].value==regionCode){
					regionList.selectedIndex=regionList.options.length-1;
					getLocationList(data[i].value);
					locationListLoaded=true;
				}
			}else{
				getLocationList("");
				locationListLoaded=true;
			}
		}
	}
}

function getLocationList(regionCd){
	PackageService.getLocationList(regionCd,getLocationListCallback);
}
function getLocationListCallback(data){
	if(data!=null && locationList!=null){
		clearDropDown(locationList);
		var lastLocationCode=null;
		addOption(locationList,"","");
		for(var i=0;i<data.length;i++){
			addOption(locationList,data[i].value,data[i].label);
		}
		locationList.selectedIndex=0;
		for(var i=0;i<locationList.options.length;i++){
			if(locationList.options[i].value==locationCode){
				locationList.options[i].selected=true;
				locationCode=null;
			}
		}
	}
}

function getCruiseRegionList(regionCd){
	PackageService.getCruiseRegionList(getCruiseRegionListCallback);
}
function getCruiseRegionListCallback(data){
	if (data != null && regionList != null) {
		clearDropDown(regionList);
		addOption(regionList, "", "\u6240\u6709\u5730\u5340");
		var locationListLoaded=false;
		for (var i=0;i<data.length;i++) {
			addOption(regionList, data[i].value, data[i].label);
			//if(locationListLoaded){continue;}
			if(regionCode!=null && regionCode!=""){
				if (data[i].value==regionCode){
					regionList.selectedIndex=regionList.options.length-1;
					getLocationList(data[i].value);
					locationListLoaded=true;
				}
			}
		}
	}
}

//util method for selector option
function addOption(s,v,l) {
	var o=document.createElement("option");
	o.text=l;o.value=v;
	s.options.add(o);
}
function clearDropDown(s) {
	if (s!=null) {
		for(z=s.options.length;z>=0;z--) {
			s.remove(z);
			if(z==0){break;}
		}
	}
}


