/*
author:胡江林
email:joneshu@gmail.com.tw
date:2007/01/05
*/

function Listener(arg) {
    {
		
	}
	this.listener = new Array();
	this.monitoredObject;
	this.parent;
    //加入傾聽器
	this.addListener = function(listener) {
		listener.parent = this;
		this.listener[this.listener.length] = listener;
	}
	//設定觀察者
	this.setMonitoredObject = function(targetObject) {
       var obj = this;
	   targetObject.onchange = function() { 
			for(var i=0;i<obj.listener.length;i++) {
			     obj.listener[i].notify();
			}
		}
		targetObject.actionReceiver = this;
		this.monitoredObject = targetObject;
	}
	//廣播通知觀察者
	this.notify = function(command) {
	}
    this.getCurrentOption = function() { 
	   return this.monitoredObject.options[this.monitoredObject.selectedIndex];
    }
}

var AddressProperties = {
    updateAddress: true,
    containCityInfo: true
};

function AddressCollection() {
    {
		this.newMethod = Listener;
		this.newMethod();
		delete this.newMethod;
	}
	this.item = new Array();
	this.containCityInfo = false;
	this.add = function(id, zipcode) {
		var temp = new Object();
		temp.id = id;
		temp.zipcode = zipcode;
		this.item[this.item.length] = temp;

	}
	this.notify = function(command) {
		if(AddressProperties.updateAddress && AddressProperties.containCityInfo) {
			this.monitoredObject.value=this.parent.parent.getCurrentOption().text + this.parent.getCurrentOption().text;
	    }
	}
}

function ZipcodeCollection() {
    {
		this.newMethod = Listener;
		this.newMethod();
		delete this.newMethod;
	}
	this.item = new Array();
	this.add = function(id, zipcode) {
		var temp = new Object();
		temp.id = id;
		temp.zipcode = zipcode;
		this.item[this.item.length] = temp;
	}
	this.notify = function() {
		this.monitoredObject.value=this.parent.getCurrentOption().value;
	}
}

function AreaCollection() {
    {
		this.newMethod = Listener;
		this.newMethod();
		delete this.newMethod;
	}
	this.item = new Array();
	this.name = "area";
	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() {
	   
        var parentValue = parseInt(this.parent.getCurrentOption().value, 10);
        
		if(parentValue == 0) return;
		this.monitoredObject.length = 0;
		 
		for(var i=0;i<this.item.length;i++) {
			if(this.item[i].id==parentValue) {
				this.monitoredObject.options[this.monitoredObject.length] = new Option(this.item[i].name,this.item[i].zipcode);
			}
		}
		this.change();
	}
	this.selectZipcode = function(zipcode) {
	   this.monitoredObject.value = zipcode;
	   this.change();
	}
	this.change = function() {
		for(var i=0;i<this.listener.length;i++) {
			this.listener[i].notify();
		}
	}
	
}

function CityCollection() {
    {
        
		this.newMethod = Listener;
		this.newMethod();
		delete this.newMethod;
	}
	this.name = "city ";
	this.item = new Array();
	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(cityID) {
	   
		this.monitoredObject.value=cityID;
		
		for(var i=0;i<this.listener.length;i++) {
		
			this.listener[i].notify();
		}
	}
	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); }
		this.change( parseInt(this.monitoredObject.options[0].value) );
	}
	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;
document.write("<script language=\"JavaScript1.2\" src=\"/include/lib/address/ZipcodeData.js\"></script>");