citylist = Array('CHN', 'HK', 'TWN', 'home');
statelist = Array('CA','CHN', 'HK', 'TWN','JP','US', 'home');

function emptySelect(sel) {
	if (sel.length > 0) {
		for (var i = sel.length - 1; i >= 0; i--) {
			sel.options[i] = null;
		}
	}
}

function updateCitylist(prSel,listName) {
	var other = '其它';
	thisform = prSel.form;	
	province_split = prSel.options[prSel.selectedIndex].value.split("-"); 
	province = province_split[1]; 
	cityList = thisform.elements[listName]; 
	origList = thisform.elements[listName+"_ORIG"]; 
	emptySelect(cityList);
	cityList[cityList.length] = new Option(origList.options[0].text,"");
	
	var selIndex = 0;
	for (i = 0; i < origList.length; i++) { 
		parts = origList.options[i].value.split("-");
		if (parts[2] == province) {
			cityList[cityList.length] = new Option(origList.options[i].text,origList.options[i].value,origList.options[i].selected);
			if (origList.options[i].selected) 
				selIndex = cityList.length -1;
			}
	}
	cityList[cityList.length] = new Option(other,'other',thisform.home_city.value == 'other');  
	cityList.selectedIndex = selIndex;
	toggleCity();
}


function toggleCity() {
	var frm = document.forms.join;
	if( (frm.elements.home_country.value == 'TWN' && frm.elements.TWN_city.value=="other") || 
		(frm.elements.home_country.value == 'CHN' && frm.elements.CHN_city.value=="other")) {
		document.getElementById('city_other').style.display = ""; 
	} else {
		document.getElementById('city_other').style.display = "none"; 
	}
}

function update_country() {
	var frm = document.forms.join;
	country = frm.elements.home_country.value;
	
	cdone = false;
	for(x in citylist) {
		if (citylist[x] == country ||
		   (cdone == false && citylist[x] == 'home')	) {
				document.getElementById(citylist[x] + '_city_div').style.display = "block"; 
				cdone = true;
		} else {
			document.getElementById(citylist[x] + '_city_div').style.display = "none";
		}
	}
	
	sdone = false;
	for(x in statelist) {
		if (statelist[x] == country ||
		   (sdone == false && statelist[x] == 'home')	) {
				document.getElementById(statelist[x] + '_state_div').style.display = "block"; 
				sdone = true;
		} else {
			document.getElementById(statelist[x] + '_state_div').style.display = "none";
		}
	}
	toggleCity();
}

function validate_stateitems(list, itype, country) {
// checks if Province/State or City field is empty (Province/State or City defined by itype)
	if (country == 'HK') {
		return true;
	}

	if (itype == 'state' && country == 'TWN') {
		return true;
	}

	// checks to make sure menu Province/State menu for selected country is not empty
	var frm = document.forms.join;	
 	for(x in list) {
		if(list[x] == country) {
			if (document.getElementById(list[x] + '_' + itype).value == "") {
				return false;
			} else {	
				return true;
			}
		}
	}
	
	// checks to make sure Province/State field is not empty
	if (document.getElementById('home_' + itype).value == "") {
		return false;
	} else {	
		return true;
	}
}