//Area物件, 紀錄郵遞區號
function areaObject(area, zipcode) {
	this.zipcode = zipcode;
	this.area = area;
	this.getZipcode = function() {
		return this.zipcode;
	};
	this.getArea = function() {
		return this.area;
	};
}

//City物件
function cityObject(cityName, id) {
	this.areaArray = new Array();
	this.cityName = cityName;
	this.id = id;
	this.getCityName = function() {
		return this.cityName;
	};
	this.addArea = function(area, zipcode) {
		this.areaArray[this.areaArray.length] = new areaObject(area, zipcode);
	};
}

//城市
function CityActionEventConstruct() {
	this.item = new Array();
	this.actionEvent;
	this.parent;
	this.change = function() {
		for(var i=0;i<this.item.length;i++) {
			this.item[i].notify();
		}
	}	
	this.addActionListener = function(listener) {
		listener.parent = this;
		this.item[this.item.length] = listener;
	}

}
function CityActionEvent(bundleObject) {
	temp = new CityActionEventConstruct();
	bundleObject.actionEvent = temp;
	temp.actionEvent = bundleObject;
	return temp;
}
//地區
function AreaActionEventConstruct(bundleObject) {
	this.item = new Array();
	this.actionEvent;
	this.parent;
	this.addActionListener = function(listener) {
		listener.parent = this;
		this.item[this.item.length] = listener;
	}
	this.change = function() {
		for(var i=0;i<this.item.length;i++) {
			this.item[i].notify(this.actionEvent);
		}
	}
	this.notify = function() {
		ObCity = this.parent.actionEvent;
		//取得點選的索引
		var tempIndex = parseInt(ObCity.options[ObCity.selectedIndex].value);
		if(tempIndex == 0) return;
		//這邊要取出城市物件
		cityObj = zipcode.getCity(tempIndex);
		//列出城市資料
		for(var i=0;i<cityObj.areaArray.length;i++) {
			this.actionEvent.options[i] = new Option(cityObj.areaArray[i].getArea(),	cityObj.areaArray[i].getZipcode());
		}
		this.actionEvent.options.length = cityObj.areaArray.length;
		//預設第一個是被選擇
		this.actionEvent.options[0].selected = true;
		
		for(var i=0;i<this.item.length;i++) {
			this.item[i].notify(this.actionEvent);
		}
	}
}
function AreaActionEvent(bundleObject) {
	temp = new AreaActionEventConstruct();
	bundleObject.actionEvent = temp;
	temp.actionEvent = bundleObject;
	return temp;
}
//郵遞區號
function ZipCodeActionEventConstruct(bundleObject) {
	this.item = new Array();
	this.actionEvent;
	this.parent;
	this.addActionListener = function(listener) {
		listener.parent = this;
		this.item[this.item.length] = listener;		
	}
	this.notify = function() {
		this.actionEvent.value=this.parent.actionEvent.value;
	}
}
function ZipCodeActionEvent(bundleObject) {
	temp = new ZipCodeActionEventConstruct();
	bundleObject.actionEvent = temp;
	temp.actionEvent = bundleObject;
	return temp;
}
//地址
function AddressCodeActionEventConstruct(bundleObject) {
	this.item = new Array();
	this.actionEvent;
	this.parent;
	this.addActionListener = function(listener) {
		listener.parent = this;
		this.item[this.item.length] = listener;		
	}
	this.notify = function() {
		ObCity = this.parent.parent.actionEvent;
		ObArea = this.parent.actionEvent;
		this.actionEvent.value=ObCity.options[ObCity.selectedIndex].text + ObArea.options[ObArea.selectedIndex].text;
	}
}
function AddressCodeActionEvent(bundleObject) {
	temp = new AddressCodeActionEventConstruct();
	bundleObject.actionEvent = temp;
	temp.actionEvent = bundleObject;
	return temp;
}
	
	

function Zipcode() {
	this.item = new Array();
	this.wrapper;
	//加入
	this.add = function(id, country, area, zipcode) {
		if( this.item[id] == null) {
			this.item[id] = new cityObject(country, id);
		}
		tempObj = this.item[id];
		tempObj.addArea(area, zipcode);
	};
	//顯示, 傳遞ID值
	this.build = function(ob) {
		this.wrapper = ob;
		ob.options.length = 0;
		try {
			for(var i=0;i<this.item.length;i++) {
				if(this.item[i] != null) {
					ob.options[i] = new Option(this.item[i].cityName, this.item[i].id);
				}
			}
		} catch(e) { alert(e.description); }
	};
	this.getCity = function(id) {
		return this.item[id];
	};
	this.setSelected = function(id) {
		try {
			for(var i=0;i<this.item.length;i++) {
				if(this.item[i]!=null && this.item[i].id == id) {
					this.wrapper.options[i].selected=true;
					this.wrapper.actionEvent.change();
				}
			}
		} catch(e) { alert(e.description); }
	}
	//push
	this.push = function(id, country, area, zipcode) {
		tempObj = new cityObject(country, id);
		tempObj.addArea(area, zipcode);
		try {
			for(var i=this.item.length;i>0;i--) {
				ob.options[i] = ob.options[i-1];
			}
			ob.options[i] = tempObj;
		} catch(e) { alert(e.description); }
	};
}
zipcode= new Zipcode();
document.write("<script language=\"JavaScript1.2\" src=\"/include/lib/address/ZipcodeData.js\"></script>");


