/*
author:­J¦¿ªL
email:joneshu@gmail.com.tw
date:2007/01/05
*/
function AddressCollection() {
	this.item = new Array();
	this.listener = new Array();
	this.monitoredObject;
	this.parent;
	this.containCityInfo = false;
	this.addListener = function(listener) {
		listener.parent = this;
		this.listener[this.listener.length] = listener;
	}
	this.add = function(id, zipcode) {
		var temp = new Object();
		temp.id = id;
		temp.zipcode = zipcode;
		this.item[this.item.length] = temp;

	}
	this.setMonitoredObject = function(targetObject) {
		targetObject.actionReceiver = this;
		this.monitoredObject = targetObject;
	}
	this.notify = function(command) {
		if(this.containCityInfo) {
			this.monitoredObject.value=command.cityName+command.areaName;
		}
	}
}

function ZipcodeCollection() {
	this.item = new Array();
	this.listener = new Array();
	this.monitoredObject;
	this.parent;
	this.addListener = function(listener) {
		listener.parent = this;
		this.listener[this.listener.length] = listener;
	}
	this.add = function(id, zipcode) {
		var temp = new Object();
		temp.id = id;
		temp.zipcode = zipcode;
		this.item[this.item.length] = temp;

	}
	this.setMonitoredObject = function(targetObject) {
		targetObject.actionReceiver = this;
		this.monitoredObject = targetObject;
	}
	this.notify = function(command) {
		this.monitoredObject.value=command.zipcode;
	}
}

function AreaCollection() {
	this.item = new Array();
	this.listener = new Array();
	this.monitoredObject;
	this.parent;
	this.setMonitoredObject = function(targetObject) {
		targetObject.onchange = function() { 
			selectIndex = parseInt(this.options[this.selectedIndex].value);
			command = new Object();
			command.zipcode = selectIndex;
			areaCollection.change( command ); 
		}
		targetObject.actionReceiver = this;
		this.monitoredObject = targetObject;
	}
	this.addListener = function(listener) {
		listener.parent = this;
		this.listener[this.listener.length] = listener;
	}
	this.add = function(id, areaName, zipcode) {
		var temp = new Object();
		temp.id = id;
		temp.name = areaName;
		temp.zipcode = zipcode;
		this.item[this.item.length] = temp;
	}
	this.notify = function(command) {
		if(command.id == 0) return;
		this.monitoredObject.length = 0;
		for(var i=0;i<this.item.length;i++) {
			if(this.item[i].id==command.id) {
				this.monitoredObject.options[this.monitoredObject.length] = new Option(this.item[i].name,this.item[i].zipcode);
			}
		}
		selectIndex = parseInt(this.monitoredObject.options[this.monitoredObject.selectedIndex].value);
		if(command.zipcode == undefined) {
			command.zipcode = selectIndex;
		} else {
			for(var i=0;i<this.monitoredObject.options.length;i++) {
				if(this.monitoredObject.options[i].value==command.zipcode) {
					this.monitoredObject.options[i].selected=true;
				}
			}
		}
		this.change(command);
	}
	this.selectZipcode = function(zipcode) {
		var command = new Object();
		command.zipcode = zipcode;
		for(var i=0;i<this.item.length;i++) {
				var item = this.item[i];
				if(item.zipcode==command.zipcode) {
					command.id = this.item[i].id;
					command.zipcode = this.item[i].zipcode;
					command.areaName = this.item[i].name;
					var city = cityCollection.getCityByID(command.id);
					command.cityName = city.name;
					cityCollection.change(command);
				}
		}
	}
	this.change = function(command) {
		for(var i=0;i<this.item.length;i++) {
				var item = this.item[i];
				if(item.zipcode==command.zipcode) {
					command.id = this.item[i].id;
					command.zipcode = this.item[i].zipcode;
					command.areaName = this.item[i].name;
					var city = cityCollection.getCityByID(command.id);
					command.cityName = city.name;
				}
		}
		for(var i=0;i<this.listener.length;i++) {
			this.listener[i].notify(command);
		}
	}
}

function CityCollection() {
	this.item = new Array();
	this.listener = new Array();
	this.monitoredObject;
	this.parent;
	this.setMonitoredObject = function(targetObject) {
		targetObject.onchange = function() { 
			selectIndex = parseInt(this.options[this.selectedIndex].value);
			command = new Object();
			command.id = selectIndex;
			cityCollection.change( command ); 
		}
		targetObject.actionReceiver = this;
		this.monitoredObject = targetObject;
	}
	this.addListener = function(listener) {
		listener.parent = this;
		this.listener[this.listener.length] = listener;
	}
	this.contain = function(id) {
		for(var i=0;i<this.item.length;i++) {
				var item = this.item[i];
				if(item.id==id) {
					return true;
				}
		}
		return false;
	}
	this.add = function(id, cityName) {
		if(!this.contain(id)) {
			var temp = new Object();
			temp.id = id;
			temp.name = cityName;
			this.item[this.item.length] = temp;
		}
	}
	this.notify = function(command) { }
	this.change = function(command) {
		for(var i=0;i<this.item.length;i++) {
				var item = this.item[i];
				if(item.id==command.id) {
					command.cityName = this.item[i].name;
				}
		}
		for(var i=0;i<this.monitoredObject.options.length;i++) {
			if(this.monitoredObject.options[i].value==command.id) {
				this.monitoredObject.options[i].selected=true;
			}
		}
		for(var i=0;i<this.listener.length;i++) {
			this.listener[i].notify(command);
		}
	}
	this.buildData = function() {
		try {
			for(var i=0;i<this.item.length;i++) {
				this.monitoredObject.options[this.monitoredObject.length] = new Option(this.item[i].name, this.item[i].id);
			}
		} catch(e) { alert(e.description); }
		selectIndex = parseInt(this.monitoredObject.options[0].value);
		command = new Object();
		command.id = selectIndex;
		this.change(command );
	}
	this.getCityByID = function(id) { 
		for(var i=0;i<this.item.length;i++) {
			var item = this.item[i];
			if(item.id==id) {
				return item;
			}
		}
	}
}

function ZipCodeManager() {
	this.cityCollection= new CityCollection();
	this.areaCollection = new AreaCollection();
	this.zipcodeCollection = new ZipcodeCollection();
	this.addressCollection = new AddressCollection();
	this.add = function(id, cityName, areaName, zipcode) {
		this.cityCollection.add(id, cityName);
		this.areaCollection.add(id, areaName, zipcode);
		this.zipcodeCollection.add(id, zipcode);
	}
}

zipCodeManager = new ZipCodeManager();
var cityCollection = zipCodeManager.cityCollection;
var areaCollection = zipCodeManager.areaCollection;
var zipcodeCollection = zipCodeManager.zipcodeCollection;
var addressCollection = zipCodeManager.addressCollection;
