
// ---------------------------------------------------------------
var cityAll = new Array();
var oSelectArea;
var oSelectCity;
var AreaUseAll = false; 

// 返回特定数组　region array -- or --- city array
function getAreaArray(areaId)
{
	var region_type = true;
	
	if(typeof(areaId) == "string"){
		if(/^\d+$/.test(areaId)){
			region_type = false;
		}
	}else if(typeof(areaId) == "number"){
		region_type = false;
	}
	
	objArray = new Array();
	
	if(region_type){
		// region array
		for(i = 0, j = area.length; i < j; i++){
			if(area[i].city_id == -1){
				objArray[area[i].region_id] = area[i].cn;
			}
		}
	}else{
		// city array
		for(i = 0, j = area.length; i < j; i++){
			if(area[i].region_id == areaId && area[i].city_id != -1){
				objArray[area[i].city_id] = area[i].cn;
			}
		}
	}
	
	return objArray;
}

// 输出默认的area select, select name: 'regionCity', 'regionCity_1'
function areaSelect(areaId, cityId, level)
{
	document.write('<select id="regionCity" name="regionCity" onChange="changeCity(this.value, -1)"></select>');
	document.write('<select id="regionCity_1" name="regionCity_1"></select>');
	
	areaInit("regionCity", "regionCity_1", areaId, cityId);
}

function areaInit(areaName, cityName, areaId, cityId, reander){
	
	if(typeof(areaId) == "undefined"){
		areaId = -1;
	}else if(typeof(areaId) == "string"){
		if(!(/^\d+$/.test(areaId))){
			areaId = -1;
		}
	}
	
	if(typeof(cityId) == "undefined" || areaId == -1){
		cityId = -1;
	}else if(typeof(cityId) == "string"){
		if(!(/^\d+$/.test(cityId))){
			cityId = -1;
		}
	}
	
	if(areaId == -1){
		defaultCity = -1;
	}else{
		defaultCity = 0;
	}
	
	if(reander){
		if(cityName == 0){
			document.write('<select id="' + areaName + '" name="' + areaName + '></select>');
		}else{
			document.write('<select id="' + areaName + '" name="' + areaName + '" onChange="changeCity(this.value, '+ defaultCity +')"></select>');
			document.write('<select id="' + cityName + '" name="' + cityName + '"></select>');
		}
	}
	
	// 获得select 对象
	getSelectObj(areaName, cityName, reander);
	
	if(typeof(oSelectArea) != "object"){
		alert("select id err!");
		return;
	}
	
	// 初始化select 对象
	//alert(areaId);
	addItems(oSelectArea, getAreaArray(), areaId);
	if(cityName!= 0){
		changeCity(areaId, cityId);
	}
	
}

// 添加 option 
function addItems(oSelect, values, selId)
{
	oSelect.options.length = 0; 
	//alert(selId);
	
	if(selId == -1){
		oOption = document.createElement("OPTION");
		oOption.text = "全部地区";
		oOption.value = "all";
		oSelect.options.add(oOption);
		selId = 0;
	}
	
	for(i = 0; i < values.length; i++){
		oOption = document.createElement("OPTION");
		oOption.text = values[i];
		oOption.value = i;
		oSelect.options.add(oOption);
	}
	
	oSelect.selectedIndex = selId;
}

// onchang Evt function
function changeCity(areaId, cityId){

	var display = false;
	var cityArray = new Array();
	
	if(typeof(areaId) == "string"){
		if(/^\d+$/.test(areaId)){
			display = true;
		}
	}else if(typeof(areaId) == "number"){
		display = true;
	}
	
	if(display){
		cityArray = getAreaArray(areaId);
	}
	
	addItems(oSelectCity, cityArray, cityId);
}

// 获取地区名字
function getAreaName(areaId, cityId, outHtml, displayRegion)
{
	var areaName;
	
	if(typeof(areaId) == "string"){
		if(!(/^\d+$/.test(areaId))){
			return "未知地区";
		}
	}
	
	if(areaId == 100){
		return "全国";
	}
	
	// find region
	for(i = 0, j = area.length; i < j; i++){
		if(area[i].region_id == areaId && area[i].city_id == -1){
			areaName = area[i].cn;
			break;
		}
	}
	useCity = false;
	if(typeof(cityId) == "string"){
		if(/^\d+$/.test(cityId)){
			useCity = true;
		}
	}else if(typeof(cityId) == "number"){
		useCity = true
	}
	
	if(useCity){
		// find city
		for(i = 0, j = area.length; i < j; i++){
			if(area[i].region_id == areaId && area[i].city_id == cityId){
				if(typeof(displayRegion) == "undefined"){
					areaName = areaName + "." + area[i].cn;
				}else{
					areaName = area[i].cn;
				}
				break;
			}
		}
	}
	
	if(outHtml != false){
		document.write(areaName);
	}
	
	return areaName;
}

// 获取 document select 对象
function getSelectObj(areaName, cityName)
{
	// init select
	if(typeof(areaName) == "string"){
		oSelectArea = document.getElementById(areaName);
	}else if(typeof(areaName) == "object"){
		oSelectArea = areaName;
	}
	
	if(typeof(cityName) == "string"){
		oSelectCity = document.getElementById(cityName);
	}else if(typeof(cityName) == "object"){
		oSelectCity = cityName;
	}
}

function getAreaEn(areaId, cityId)
{
	var areaName;
	
	if(typeof(areaId) == "string"){
		if(!(/^\d+$/.test(areaId))){
			return "";
		}
	}
	
	if(areaId == 100){
		return '';
	}
	
	// find region
	for(i = 0, j = area.length; i < j; i++){
		if(area[i].region_id == areaId && area[i].city_id == -1){
			areaName = area[i].en;
		}
	}
	
	useCity = false;
	if(typeof(cityId) == "string"){
		if(/^\d+$/.test(cityId)){
			useCity = true;
		}
	}else if(typeof(cityId) == "number"){
		useCity = true
	}
	
	if(useCity){
		// find city
		for(i = 0, j = area.length; i < j; i++){
			if(area[i].region_id == areaId && area[i].city_id == cityId){
				areaName = area[i].en;
			}
		}
	}

	return areaName;
}

//////////////////////////////////////////////////////////
//
// 列表显示 region 
function listArea(patten, out)
{
	if(typeof(patten) != "string"){
		alert("Patten Err");
		return false;
	}
	
	outHtml = "";
	reId = /\[id\]/g;
	reName = /\[name\]/g;
	reEncodeName = /\[encodeName\]/g;
	reFun = /\[fun_(.+)(\(.+\))\]/g
	
	region_array = getAreaArray();
	
	for(i = 0; i < region_array.length; i++)
	{
		itemHtml = patten.replace(reId, i);
		itemHtml = itemHtml.replace(reName, region_array[i]);
		//itemHtml = itemHtml.replace(reEncodeName, encodeURI(gdClassArray[i].name));
		itemHtml = itemHtml.replace(reEncodeName, region_array[i]);

		// 执行函数替换
		func = itemHtml.match(reFun);
		if(func != null){
			for(y=0; y < func.length; y++){
				
				funcName = func[y].replace(/\[fun_(.+)\]/g, "$1");
				oldFuncName = funcName; // add code later...
				
				if(funcName.indexOf("cycle") != -1){
					funcName = funcName.replace(/\((.+)\)/g, "($1," + i + ")");
				}
				
				funcName = "render_" + funcName;
				funResult = eval(funcName);
				
				// 未完成全部功能
				itemHtml = itemHtml.replace(reFun, funResult);
				break;
			}
		}
		outHtml += itemHtml + "\n";
	}
	
	if(out != false){
		document.write(outHtml);
	}else{
		return outHtml;
	}

}

function render_cycle()
{
	return arguments[arguments[arguments.length-1] % (arguments.length-1)];
}