var IeSixMask = new Class({
	initialize: function(zIndex){
		this._zIndex  = zIndex;
		this.elRoot = new Element('iframe');
		this.elRoot.setStyle('filter','mask()');
		this.elRoot.injectTop($(document.body));
		this.elRoot.setStyles({
			'position':'absolute',
			'top':'0px',
			'left':'0px',
			'display':'none',
			'z-index':this._zIndex
		});
	},
	turnOn:function(){
		this.elRoot.setStyle('width', window.getWidth() + 'px');
		this.elRoot.setStyle('height', window.getHeight() + 'px');
		this.elRoot.setStyle('display', 'block');
	},
	turnOff:function(){
		this.elRoot.setStyle('display', 'none');
	},
	resize:function() {
		if(this.elRoot.getStyle('display') == 'block') {
			this.elRoot.setStyle('width', window.getWidth() + 'px');
			this.elRoot.setStyle('height', window.getHeight() + 'px');
		}
	}
});

var BlackMate = new Class({
    initialize: function(baseZIndex){
		// 使用 BlackMate 時, body 以下 Style 一定要設定
		$(document.body).setStyle('padding', '0px');
		$(document.body).setStyle('margin', '0px');
		// 先判斷 Client Browser 是否為 IE6
		this._isIe6 = (Browser.Engine.trident4 == true)? true:false;
		// 如果是 IE6 的話要留一層給 <select> Object 的遮罩
		this._zIndex = (this._isIe6 == true)? baseZIndex + 1:baseZIndex;
		// 建立顯示實體
		this._ele = new Element('div');
		this._ele.setStyle('display', 'block');
		this._ele.setStyle('background-color', '#000000');
		this._ele.setStyle('opacity', '0.6');
		this._ele.setStyle('visibility', 'hidden');
		this._ele.setStyle('position', 'absolute');
		this._ele.setStyle('top', '0px');
		this._ele.setStyle('left', '0px');
		this._ele.setStyle('z-index', this._zIndex);
		this._ele.injectTop($(document.body));
		// 如果是 IE6 會把 ieSixMask 遮罩實體化為 IeSixMask
		this._ieSixMask = null;
		if(this._isIe6 == true) {
			this._ieSixMask = new IeSixMask(this._zIndex-1);
		}
		// 註冊 window 的 resize event
		window.addEvent('resize', (function() {
			this.resize();
		}).bind(this));
	},
	turnOn:function() {
		if (this._ele.getStyle('visibility') == 'hidden') {
			this._ele.setStyle('width', window.getWidth() + 'px');
			this._ele.setStyle('height', window.getHeight() + 'px');
			this._ele.setStyle('visibility', 'visible');
		}
		if(this._isIe6 == true) {
			this._ieSixMask.turnOn();
		}
	},
	turnOff:function() {
		this._ele.setStyle('visibility', 'hidden');
		if(this._isIe6 == true) {
			this._ieSixMask.turnOff();
		}
	},
	resize:function() {
		if(this._ele.getStyle('visibility') != 'hidden') {
			this._ele.setStyle('width', window.getWidth() + 'px');
			this._ele.setStyle('height', window.getHeight() + 'px');
		}
		if(this._isIe6 == true) {
			this._ieSixMask.resize();
		}
	},
	clickToTurnOff:function() {
		this._ele.addEvent('click',this.turnOff.pass(null, this));
	}
});

var DatePickerPanel2 = new Class({
	Implements:[Events],
	/**
	* td 的 class 包含 :
	* tdDayName:顯示禮拜幾的 td class
	* tdEmpty:不能選擇, 沒有註冊日期.
	* tdSelected:註冊日期被選擇.
	* tdNormal:一般顯示.
	* tdHover:滑鼠移入時.
	*/
	initialize: function(now,ymaxto,yminto){
		this._now = (now != undefined && now != null)? now:new Date();
		this._selectedDate = null;
		this._yearMaxTo = (isNaN(ymaxto))? this._now.getFullYear():ymaxto;//可選年從現在開始到以後幾年
		this._yearMinTo = (isNaN(yminto))? this._now.getFullYear() - 50:yminto;//可選年從現在開始到以前幾年
		
		// 選擇年的下拉式選單
		this._yearSelector = new Element('select');
		this._yearSelector.addEvent('change', this.onYearSelectorChange.bind(this));
		// 選擇月的下拉式選單
		this._monthSelector = new Element('select');
		this._monthSelector.addEvent('change', this.onMonthSelectorChange.bind(this));

		// 這個 DatePicker 會顯示在那個 Element 裡面(div)
		this._baseBox = null;
		
		// 單純存放日期 table 的 div 主要用來執行 empty
		this._dayBox = new Element('div');
		
		//
		this._kpanel = null;
		
		this._viewer = null;
		
		this._buildBaseFrame();
		this._start();
	},
	launch:function(el) {
		var ds = el.get('value');
		this._viewer = el;
		if(ds != '') {
			this._selectedDate = this.string2Date(ds);
		}
		this._makingYearSelector();
		this._makingMonthSelector();
		this._buildingCells();
		this._kpanel.setPosition(el,'leftbottom',{'x':-182,'y':-60});
		this._kpanel.show();
	},
	hide:function(){
		this._kpanel.hide();
	},
	_buildBaseFrame:function() {
		this._baseBox = new Element('div',{
			'class':'DatePickerPanel2',
			'styles':{'width':'99%','display':'none'}
		});
		this._baseBox.inject(document.body,'bottom');
		
		// 建立 KPanel
		this._kpanel = new KPanel(185);
		this._kpanel.setTitle('請選取日期');
		this._kpanel.addEvent('hide',
			function(){

			}.bind(this)
		);
		this._kpanel.addEvent('show',
			function(){

			}.bind(this)
		);
		
		// 因為此框架不變, 可以直接注入
		this._kpanel.container.grab(this._baseBox);
		this._baseBox.setStyle('display','block');
	},
	_start:function() {
		this._makingYearSelector();
		this._makingMonthSelector();
		// 建立年, 月的選擇器位置
		var div = new Element('div');
		div.setStyles({
			'text-align':'center'
		});
		div.set('html', '民國&nbsp;');
		this._yearSelector.inject(div,'bottom');
		this._monthSelector.inject(div,'bottom');
		div.inject(this._baseBox, 'top');
		// 建立週末表, attributes 跟 style 要分開設定;不知為何用物件'style':{...}一起設定會無效.
		table = new Element('table',
			{
				'border':'1',
				'bordercolor':'#F0F0F0',
				'cellpadding':'0',
				'cellspacing':'0'
			}
		);
		table.setStyles({
			'border-collpase':'collapse',
			'width':'100%'
		});
		var tbody = new Element('tbody');
		var aTr;
		var aTd;
		// 建立 head 部分
		aTr = new Element('tr');
		var dNames = this._getDayNames();
		for(var j=0;j<dNames.length;j++) {
			aTd = new Element('td');
			aTd.set('text', dNames[j]);
			aTd.setProperty('class','tdDayName');
			if(j== 0 || j==6) {//0 = 週日 6 = 週六
				aTd.setStyle('width','15%');
			} else {
				aTd.setStyle('width','14%');
			}
			aTd.inject(aTr, 'bottom');
		}
		aTr.inject(tbody, 'bottom');
		tbody.inject(table,'bottom');
		table.inject(this._baseBox, 'bottom');
		
		// 再放一個 div 進去準備等一下用
		this._dayBox.inject(this._baseBox, 'bottom');
		
		// 建立 cell 部分
		this._buildingCells();
	},
	_buildingCells:function() {
		// 裡面有東西就先清掉
		this._dayBox.empty();
		
		var table,tbody,aTr,aTd;

		var _sy = this._yearSelector.options[this._yearSelector.selectedIndex].value;
		var _sm = this._monthSelector.options[this._monthSelector.selectedIndex].value;
		table = new Element('table',
			{
				'border':'1',
				'bordercolor':'#F0F0F0',
				'cellpadding':'0',
				'cellspacing':'0'
			}
		);
		table.setStyles({
			'border-collpase':'collapse',
			'width':'100%'
		});
		tbody = new Element('tbody');
		var a = this._runDays(_sy,_sm);
		var l = a.length;
		
		for(var j=0;j<l;j++) {
			if(j%7 == 0) {aTr = new Element('tr');}
			aTd = new Element('td');
			aTd.set('html', (a[j]==0)? '&nbsp;':a[j]);
			aTd.setProperty('class',(a[j]==0)? 'tdEmpty':'tdNormal');
			if(j%7 == 0 || j%7 == 6) {//0 = 週日 6 = 週六
				aTd.setStyle('width','15%');
			} else {
				aTd.setStyle('width','14%');
			}
			aTd.inject(aTr, 'bottom');
			if(j%7 == 6) {aTr.inject(tbody, 'bottom');}
		}
		tbody.inject(table,'bottom');
		table.inject(this._dayBox, 'bottom');
		
		var eles;
		// 如果有選取日期需重新標記
		if((this._selectedDate != undefined && this._selectedDate != null) && _sy == this._selectedDate.getFullYear() && _sm == this._selectedDate.getMonth()){
			eles = tbody.getElements('td');
			for(var j=0;j<eles.length;j++) {
				if(eles[j].get('text') == this._selectedDate.getDate()) {
					eles[j].setProperty('class','tdSelected');
					break;// 因為只有選定一天，所以就直接 break
				}
			}
		}
		// 附加事件
		// 不知為何用兩個 selector 選回來是奇怪的東西
		//alert(printR(tbody.getElements('.tdSelected','.tdNormal')));
		
		// 注意 each 的用法, 如何把 Element 傳入以及 bind(this)
		eles = tbody.getElements('.tdNormal');
		eles.each(function(el) {
			el.addEvent('mouseover', function(){el.setProperty('class','tdHover');});
			el.addEvent('mouseout', function(){el.setProperty('class','tdNormal');});
			el.addEvent('click',this.onSelect.bind(this,[el.get('text')]));
		}.bind(this));
	},
	
	/**
	* 把該月的格子跑出來, 基本上就是一個 [0,0,0,1,2,3,4,5,6,7,...,30,31,0,0] 這樣的陣列
	*/
	_runDays:function(y,m) {
		// 本月一共有幾天
		var _td = this._getMonthDays(y,m);
		// 取得月份的第一天, 並判斷是禮拜幾
		var d1 = new Date(y, m, 1);
		var _wd = d1.getDay();//等於前面要補幾天, 0(日)不補, 6(六)要補六天
		// 後面要補幾天
		var dmax = Math.ceil((_wd + _td)/7) * 7;
		var _pd = dmax - (_wd + _td)
		var a = new Array();
		for(var j=0;j<dmax;j++) {// 最多就是 42 格
			if(j<_wd || j>=(_wd+_td)) {
				a.push(0);
			} else {
				a.push(j-_wd+1);
			}
		}
		//alert(printR(a));
		return a;
	},
	_makingYearSelector:function() {
		//var _sy = (this._selectedDate != undefined && this._selectedDate != null)? this._selectedDate.getFullYear():this._now.getFullYear();
		var _sy = (this._selectedDate != undefined && this._selectedDate != null)? this._selectedDate.getFullYear():this._yearMinTo + Math.floor((this._yearMaxTo-this._yearMinTo)*2/3);
		//this._yearSelector.options.length = 0;
		this._yearSelector.empty();
		var j;
		for(j = this._yearMinTo;j<=this._yearMaxTo;j++) {
			this._yearSelector.options[j-this._yearMinTo] = new Option(j-1911, j);
			if(_sy == j) {this._yearSelector.options[j-this._yearMinTo].selected = true;}
		}
	},
	_makingMonthSelector:function(){
		//var _sm = (this._selectedDate != undefined && this._selectedDate != null)? this._selectedDate.getMonth():this._now.getMonth();
		var _sm = (this._selectedDate != undefined && this._selectedDate != null)? this._selectedDate.getMonth():0;
		this._monthSelector.empty();
		var j;
		var _monthNames = this._getMonthNames();
		for(j = 0;j<12;j++) {
			this._monthSelector.options[j] = new Option(_monthNames[j], j);
			if(_sm == j) {this._monthSelector.options[j].selected = true;}
		}
	},
	_getMonthDays:function(y,m) {
		var a;
		if(((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
			a = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
		} else {
			a = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		}
		return a[m];
	},
	_getMonthNames:function() {
		return new Array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
	},
	_getDayNames:function() {
		return new Array('日', '一','二','三','四','五','六');
	},
	onYearSelectorChange:function() {
		this._buildingCells();
	},
	onMonthSelectorChange:function() {
		this._buildingCells();
	},
	onSelect:function(dNum) {
		var _sy = this._yearSelector.options[this._yearSelector.selectedIndex].value;
		var _sm = this._monthSelector.options[this._monthSelector.selectedIndex].value;
		this._selectedDate = new Date(_sy, _sm, dNum);
		//this._buildingCells();
		this._viewer.set('value', this.date2String(this._selectedDate));
		this._kpanel.hide();
		this._viewer = null;
		this._selectedDate = null;
		//this.fireEvent('onSelect',[_sy + '-' + (new Number(_sm) + 1) + '-' + dNum]);
	},
	string2Date:function(s) {
		var a = s.split('-');
		if(a.length == 3) {
			return new Date(a[0],(new Number(a[1])-1),a[2]);
		} else {
			return new Date(this._yearMaxTo);
		}
	},
	date2String:function(d) {
		return (d.getFullYear() + '-' + (new Number(d.getMonth()) + 1) + '-' + d.getDate());
	}
});

var TaiwanAddrSet = new Class({

	/**
	* 使用 injectToElement 會將四個欄位灌入指定元素  :
	* APPFORM_TW_COUNTY
	* APPFORM_TW_AREA
	* APPFORM_TW_DETAIL
	* APPFORM_TW_ZIP
	* 如果需要讀取則需建置三個對應的隱藏欄位 : 
	* APPFORM_TW_COUNTY_D
	* APPFORM_TW_AREA_D
	* APPFORM_TW_DETAIL_D
	* 另外使用 setDbInputType 可以指定資料庫溝通時是直接存入文字或是存入對應的索引鍵(int)
	* TEXT : 存入文字, 預設
	* NUM : 存入索引
	*/
    initialize: function(preFix){
		this.preFix = (typeof(preFix) == 'undefined')? '':preFix;
		this.countySelectorId = this.preFix + 'APPFORM_TW_COUNTY';
		this.areaSelectorId = this.preFix + 'APPFORM_TW_AREA';
		this.detailInputId = this.preFix + 'APPFORM_TW_DETAIL';
		this.zipSelectorId = this.preFix + 'APPFORM_TW_ZIP';
		this.defaultZipValue = '';//尚未選擇
		this.DB_INPUT_TYPE = 'TEXT';//使用 'TEXT' 則 option.value = '中文字或值', 使用 'NUM' 則存入陣列索引
		
		this.countySelector = new Element('select');
		this.countySelector.setProperties({'id':this.countySelectorId, 'name':this.countySelectorId});
		this.areaSelector = new Element('select');
		this.areaSelector.setProperties({'id':this.areaSelectorId, 'name':this.areaSelectorId});
		this.detailInput = new Element('input',{'type':'text','maxlength':'255'});
		this.detailInput.setStyles({
			'border-width':'1px',
			'border-style':'solid',
			'border-color':'#333',
			'width':'225px'
		});
		this.detailInput.setProperties({'id':this.detailInputId, 'name':this.detailInputId, 'type':'text'});
		this.zipHolder = new Element('input');
		this.zipHolder.setProperties({'id':this.zipSelectorId, 'name':this.zipSelectorId, 'type':'hidden'});
		
		this.counties = new Array('請選擇','基隆市','台北市','台北縣','桃園縣','新竹市','新竹縣','苗栗縣','台中市','台中縣','南投縣','彰化縣','雲林縣','嘉義市','嘉義縣','台南市','台南縣','高雄市','高雄縣','屏東縣','台東縣','花蓮縣','宜蘭縣','澎湖縣','金門縣','連江縣');
		this.a0 = new Array('請選擇');
		this.a1 = new Array('請選擇','仁愛區','信義區','中正區','中山區','安樂區','暖暖區','七堵區');
		this.a2 = new Array('請選擇','中正區','大同區','中山區','松山區','大安區','萬華區','信義區','士林區','北投區','內湖區','南港區','文山區');
		this.a3 = new Array('請選擇','萬里鄉','金山鄉','板橋市','汐止市','深坑鄉','石碇鄉','瑞芳鎮','平溪鄉','雙溪鄉','貢寮鄉','新店市','坪林鄉','烏來鄉','永和市','中和市','土城市','三峽鎮','樹林鎮','鶯歌鎮','三重市','新莊市','泰山鄉','林口鄉','蘆洲市','五股鄉','八里鄉','淡水鎮','三芝鄉','石門鄉');
		this.a4 = new Array('請選擇','中壢市','平鎮市','龍潭鄉','楊梅鎮','新屋鄉','觀音鄉','桃園市','龜山鄉','八德市','大溪鎮','復興鄉','大園鄉','蘆竹鄉');
		this.a5 = new Array('請選擇','東區','南區','北區','香山區');
		this.a6 = new Array('請選擇','竹北市','湖口鄉','新豐鄉','新埔鎮','關西鎮','芎林鄉','寶山鄉','竹東鎮','五峰鄉','橫山鄉','尖石鄉','北埔鄉','峨眉鄉');
		this.a7 = new Array('請選擇','竹南鎮','頭份鎮','三灣鄉','南庄鄉','獅潭鄉','後龍鎮','通霄鎮','苑裡鎮','苗栗市','造橋鄉','頭屋鄉','公館鄉','大湖鄉','泰安鄉','銅鑼鄉','三義鄉','西湖鄉','卓蘭鎮');
		this.a8 = new Array('請選擇','中區','東區','南區','西區','北區','北屯區','西屯區','南屯區');
		this.a9 = new Array('請選擇','太平市','大里市','霧峰鄉','烏日鄉','豐原市','后里鄉','石岡鄉','東勢鎮','和平鄉','新社鄉','潭子鄉','大雅鄉','神岡鄉','大肚鄉','沙鹿鎮','龍井鄉','梧棲鎮','清水鎮','大甲鎮','外埔鄉','大安鄉');
		this.a10 = new Array('請選擇','南投市','中寮鄉','草屯鎮','國姓鄉','埔里鎮','仁愛鄉','名間鄉','集集鎮','水里鄉','魚池鄉','竹山鎮','鹿谷鄉');
		this.a11 = new Array('請選擇','彰化市','芬園鄉','花壇鄉','秀水鄉','鹿港鎮','福興鄉','線西鄉','和美鎮','伸港鄉','員林鎮','社頭鄉','永靖鄉','埔心鄉','溪湖鎮','大村鄉','埔鹽鄉','田中鎮','北斗鎮','田尾鄉','埤頭鄉','溪州鄉','竹塘鄉','二林鎮','大城鄉','芳苑鄉','二水鄉');
		this.a12 = new Array('請選擇','斗南鎮','大埤鄉','虎尾鎮','土庫鎮','褒忠鄉','東勢鄉','臺西鄉','崙背鄉','麥寮鄉','斗六市','林內鄉','古坑鄉','莿桐鄉','西螺鎮','二崙鄉','北港鎮','水林鄉','口湖鄉','四湖鄉','元長鄉');
		this.a13 = new Array('請選擇','東區','西區');
		this.a14 = new Array('請選擇','番路鄉','梅山鄉','竹崎鄉','阿里山','中埔鄉','大埔鄉','水上鄉','鹿草鄉','太保市','朴子市','東石鄉','六腳鄉','新港鄉','民雄鄉','大林鎮','溪口鄉','義竹鄉','布袋鎮');
		this.a15 = new Array('請選擇','中區','東區','南區','西區','北區','安平區','安南區');
		this.a16 = new Array('請選擇','永康市','歸仁鄉','新化鎮','左鎮鄉','玉井鄉','楠西鄉','南化鄉','仁德鄉','關廟鄉','龍崎鄉','官田鄉','麻豆鎮','佳里鎮','西港鄉','七股鄉','將軍鄉','學甲鎮','北門鄉','新營市','後壁鄉','白河鎮','東山鄉','六甲鄉','下營鄉','柳營鄉','鹽水鎮','善化鎮','大內鄉','山上鄉','新市市','安定鄉');
		this.a17 = new Array('請選擇','新興區','前金區','苓雅區','鹽埕區','鼓山區','旗津區','前鎮區','三民區','楠梓區','小港區','左營區');
		this.a18 = new Array('請選擇','仁武鄉','大社鄉','岡山鎮','路竹鄉','阿蓮鄉','田寮鄉','燕巢鄉','橋頭鄉','梓官鄉','彌陀鄉','永安鄉','湖內鄉','鳳山市','大寮鄉','林園鄉','鳥松鄉','大樹鄉','旗山鎮','美濃鎮','六龜鄉','內門鄉','杉林鄉','甲仙鄉','桃源鄉','三民鄉','茂林鄉','茄萣鄉');
		this.a19 = new Array('請選擇','屏東市','三地鄉','霧臺鄉','瑪家鄉','九如鄉','里港鄉','高樹鄉','鹽埔鄉','長治鄉','麟洛鄉','竹田鄉','內埔鄉','萬丹鄉','潮州鎮','泰武鄉','來義鄉','萬巒鄉','崁頂鄉','新埤鄉','南州鄉','林邊鄉','東港鎮','琉球鄉','佳冬鄉','新園鄉','枋寮鄉','枋山鄉','春日鄉','獅子鄉','車城鄉','牡丹鄉','恆春鎮','滿州鄉');
		this.a20 = new Array('請選擇','臺東市','綠島鄉','蘭嶼鄉','延平鄉','卑南鄉','鹿野鄉','關山鎮','海端鄉','池上鄉','東河鄉','成功鎮','長濱鄉','太麻里鄉','金峰鄉','大武鄉','達仁鄉');
		this.a21 = new Array('請選擇','花蓮市','新城鄉','秀林鄉','吉安鄉','壽豐鄉','鳳林鎮','光復鄉','豐濱鄉','瑞穗鄉','萬榮鄉','玉里鎮','卓溪鄉','富里鄉');
		this.a22 = new Array('請選擇','宜蘭市','頭城鎮','礁溪鄉','壯圍鄉','員山鄉','羅東鎮','三星鄉','大同鄉','五結鄉','冬山鄉','蘇澳鎮','南澳鄉');
		this.a23 = new Array('請選擇','馬公市','西嶼鄉','望安鄉','七美鄉','白沙鄉','湖西鄉');
		this.a24 = new Array('請選擇','金沙鎮','金湖鎮','金寧鄉','金城鎮','烈嶼鄉','烏坵鄉');
		this.a25 = new Array('請選擇','南竿鄉','北竿鄉','莒光鄉','東引鄉');
		
		this.z0 = new Array(this.defaultZipValue);
		this.z1 = new Array(this.defaultZipValue,'200','201','202','203','204','205','206');
		this.z2 = new Array(this.defaultZipValue,'100','103','104','105','106','108','110','111','112','114','115','116');
		this.z3 = new Array(this.defaultZipValue,'207','208','220','221','222','223','224','226','227','228','231','232','233','234','235','236','237','238','239','241','242','243','244','247','248','249','251','252','253');
		this.z4 = new Array(this.defaultZipValue,'320','324','325','326','327','328','330','333','334','335','336','337','338');
		this.z5 = new Array(this.defaultZipValue,'300','300','300','300');
		this.z6 = new Array(this.defaultZipValue,'302','303','304','305','306','307','308','310','311','312','313','314','315');
		this.z7 = new Array(this.defaultZipValue,'350','351','352','353','354','356','357','358','360','361','362','363','364','365','366','367','368','369');
		this.z8 = new Array(this.defaultZipValue,'400','401','402','403','404','406','407','408');
		this.z9 = new Array(this.defaultZipValue,'411','412','413','414','420','421','422','423','424','426','427','428','429','432','433','434','435','436','437','438','439');
		this.z10 = new Array(this.defaultZipValue,'540','541','542','544','545','546','551','552','553','555','557','558');
		this.z11 = new Array(this.defaultZipValue,'500','502','503','504','505','506','507','508','509','510','511','512','513','514','515','516','520','521','522','523','524','525','526','527','528','530');
		this.z12 = new Array(this.defaultZipValue,'630','631','632','633','634','635','636','637','638','640','643','646','647','648','649','651','652','653','654','655');
		this.z13 = new Array(this.defaultZipValue,'600','600');
		this.z14 = new Array(this.defaultZipValue,'602','603','604','605','606','607','608','611','612','613','614','615','616','621','622','623','624','625');
		this.z15 = new Array(this.defaultZipValue,'700','701','702','703','704','708','709');
		this.z16 = new Array(this.defaultZipValue,'710','711','712','713','714','715','716','717','718','719','720','721','722','723','724','725','726','727','730','731','732','733','734','735','736','737','741','742','743','744','745');
		this.z17 = new Array(this.defaultZipValue,'800','801','802','803','804','805','806','807','811','812','813');
		this.z18 = new Array(this.defaultZipValue,'814','815','820','821','822','823','824','825','826','827','828','829','830','831','832','833','840','842','843','844','845','846','847','848','849','851','852');
		this.z19 = new Array(this.defaultZipValue,'900','901','902','903','904','905','906','907','908','909','911','912','913','920','921','922','923','924','925','926','927','928','929','931','932','940','941','942','943','944','945','946','947');
		this.z20 = new Array(this.defaultZipValue,'950','951','952','953','954','955','956','957','958','959','961','962','963','964','965','966');
		this.z21 = new Array(this.defaultZipValue,'970','971','972','973','974','975','976','977','978','979','981','982','983');
		this.z22 = new Array(this.defaultZipValue,'260','261','262','263','264','265','266','267','268','269','270','272');
		this.z23 = new Array(this.defaultZipValue,'880','881','882','883','884','885');
		this.z24 = new Array(this.defaultZipValue,'890','891','892','893','894','896');
		this.z25 = new Array(this.defaultZipValue,'209','210','211','212');
		this.areas = new Array(this.a0,this.a1,this.a2,this.a3,this.a4,this.a5,this.a6,this.a7,this.a8,this.a9,this.a10,this.a11,this.a12,this.a13,this.a14,this.a15,this.a16,this.a17,this.a18,this.a19,this.a20,this.a21,this.a22,this.a23,this.a24,this.a25);
		this.zips = new Array(this.z0,this.z1,this.z2,this.z3,this.z4,this.z5,this.z6,this.z7,this.z8,this.z9,this.z10,this.z11,this.z12,this.z13,this.z14,this.z15,this.z16,this.z17,this.z18,this.z19,this.z20,this.z21,this.z22,this.z23,this.z24,this.z25);
		
		// ### 080501 ###
		this.firstLoad = true;
    },
	
	injectToElement:function(ele) {
		if($type(ele)) {
			var usedE = $(ele);
			usedE.set('text','縣市 : ');
			this.countySelector.inject(usedE);
			//usedE.adopt(new Element('br'));
			usedE.appendText(' 鄉鎮 : ');
			this.areaSelector.inject(usedE);
			usedE.adopt(new Element('br'));
			usedE.appendText(' 詳細 : ');
			this.detailInput.inject(usedE);
			
			this.zipHolder.inject(usedE);
		
			var imax = this.counties.length;
			if(this.DB_INPUT_TYPE == 'TEXT') {
				for(i=0;i<imax;i++) {
					if (i == 0) {
						this.countySelector.options[i] = new Option(this.counties[i], '');
					} else {
						this.countySelector.options[i] = new Option(this.counties[i], this.counties[i]);
					}
				}
			} else {
				for(i=0;i<imax;i++) {
					this.countySelector.options[i] = new Option(this.counties[i], i);
				}
			}
			this.countySelector.selectedIndex = 0;
			this.countySelector.addEvent('change', this.countyUpdate.bind(this));
			
			this.areaSelector.options[0] = new Option(this.a0[0], '');
			this.areaSelector.selectedIndex = 0;
			this.areaSelector.disabled = true;
			this.areaSelector.addEvent('change', this.areaUpdate.bind(this));

		} else {
			alert('Can Not Bind Taiwan Address Input Because Target Element Does Not Exist!');
		}

	},
	
	countyUpdate:function() {
		var idx = this.countySelector.selectedIndex;
		this.areaSelector.options.length = 0;
		var imax = this.areas[idx].length;
		if(this.DB_INPUT_TYPE == 'TEXT') {
			for(i=0;i<imax;i++) {
				this.areaSelector.options[i] = new Option(this.areas[idx][i], this.areas[idx][i]);
			}
		} else {
			for(i=0;i<imax;i++) {
				this.areaSelector.options[i] = new Option(this.areas[idx][i], i);
			}
		}
		this.areaSelector.selectedIndex = 0;
		if(idx == 0) {
			this.areaSelector.disabled = true;
		} else {
			this.areaSelector.disabled = false;
		}
	},
	
	areaUpdate:function() {
		var idx1 = this.countySelector.selectedIndex;
		var idx2 = this.areaSelector.selectedIndex;
		this.zipHolder.value = this.zips[idx1][idx2];
	},
	
	checkComplete:function() {
		if(this.countySelector.selectedIndex > 0 && this.areaSelector.selectedIndex > 0 && this.detailInput.get('value').trim() != '') {
			return true;
		} else {
			return false;
		}
	},
	loadStatus:function() {
		if($defined((this.preFix + 'APPFORM_TW_COUNTY_D')) && $defined((this.preFix + 'APPFORM_TW_AREA_D')) && $defined((this.preFix + 'APPFORM_TW_DETAIL_D'))) {
			var i,imax;
			if(this.DB_INPUT_TYPE = 'TEXT') {
				imax = this.counties.length;
				for(i=0;i<imax;i++) {
					if(this.counties[i]==$(this.preFix + 'APPFORM_TW_COUNTY_D').get('value')) {
						this.countySelector.selectedIndex = i;
						this.countyUpdate();
						break;
					}
				}
				imax = this.areas[this.countySelector.selectedIndex].length;
				for(i=0;i<imax;i++) {
					if(this.areas[this.countySelector.selectedIndex][i]==$(this.preFix + 'APPFORM_TW_AREA_D').get('value')) {
						this.areaSelector.selectedIndex = i;
						this.areaUpdate();
						break;
					}
				}
			} else {
				imax = this.counties.length;
				for(i=0;i<imax;i++) {
					if(i==$(this.preFix + 'APPFORM_TW_COUNTY_D').get('value')) {
						this.countySelector.selectedIndex = i;
						this.countyUpdate();
						break;
					}
				}
				imax = this.areas[this.countySelector.selectedIndex].length;
				for(i=0;i<imax;i++) {
					if(i==$(this.preFix + 'APPFORM_TW_AREA_D').get('value')) {
						this.areaSelector.selectedIndex = i;
						this.areaUpdate();
						break;
					}
				}
			}
			this.detailInput.value = $(this.preFix + 'APPFORM_TW_DETAIL_D').get('value');
		}
	},
	
	load:function(v1,v2,v3){//v4 應該是 zip 會自動設定
			var i,imax;
			imax = this.counties.length;
			for(i=0;i<imax;i++) {
				if(this.counties[i]==v1) {
					this.countySelector.selectedIndex = i;
					this.countyUpdate();
					break;
				}
			}
			imax = this.areas[this.countySelector.selectedIndex].length;
			for(i=0;i<imax;i++) {
				if(this.areas[this.countySelector.selectedIndex][i]==v2) {
					this.areaSelector.selectedIndex = i;
					this.areaUpdate();
					break;
				}
			}
			this.detailInput.value = v3;
	},
	
	getData:function() {
		var o = {
			'addrCounty':'',
			'addrArea':'',
			'addrDetail':'',
			'addrZip':''
		}
		if(this.checkComplete()) {
			o.addrCounty = this.countySelector.options[this.countySelector.selectedIndex].value;
			o.addrArea = this.areaSelector.options[this.areaSelector.selectedIndex].value;
			o.addrDetail = this.detailInput.get('value').trim();
			o.addrZip = this.zipHolder.get('value');
		}
		return o;
	},
	
	setDbInputType:function(t) {
		t = (t == 'TEXT')? t:'NUM';
		this.DB_INPUT_TYPE = t;
	}
});

var KPanel = new Class({
	Implements:[Events],
	initialize: function(width){
		var _thisStickyWinOptions = {
			
		};
		this._panel = null;
		this.container = null;
		this._panelWidth = ($defined(width) || isNaN(width) == false)? width:300;
		this._buildBasePanel();
	},
	_buildBasePanel:function() {
		this._panel = new Element('div',
			{
				'class':'KPanel',
				'styles':{
					'width':this._panelWidth + 'px',
					'display':'none'
				}
			}
		);
		this._panel.inject(document.body);
		
		// 開始建立工具列
		var table,tbody,tr,td
		table = new Element('table',
			{
				'border':'0',
				'cellpadding':'0',
				'cellspacing':'0',
				'styles':{'width':'100%'}
			}
		);
		tbody = new Element('tbody');
		tr = new Element('tr',{'class':'headerTr'});
		td = new Element('td',{'class':'headerTitle'});
		td.set('text',this._title);
		td.inject(tr);

		td = new Element('td',{'class':'closer'});
		td.grab(new Element('div'));
		td.inject(tr);
		tr.inject(tbody);
		tbody.inject(table);
		table.inject(this._panel,'top');
		// 建立浮動視窗
		this._swin = new StickyWinFx({
			content: this._panel,
			closeClassName:'closer',
			draggable: true,
			dragHandleSelector: '.headerTitle',
			width: false,
			height:false,
			showNow:false,
			allowMultiple:true
		});
		this._swin.addEvent('display',
			function(){
				this.fireEvent('onShow');
			}.bind(this)
		);
		this._swin.addEvent('close',
			function(){
				this.fireEvent('onHide');
			}.bind(this)
		);
		
		// 完成內容
		this._buildContainer();
		
		// 可以顯示
		this._panel.setStyle('display','block');
	},
	_buildContainer:function(){
		this.container = new Element('div',{'styles':{'width':this._panelWidth + 'px'}});
		this.container.inject(this._panel);
	},
	
	/**
	* 設定標題文字
	*/
	setTitle:function(ttxt) {
		var el = this._panel.getElement('.headerTitle');
		el.set('text', ttxt);
	},
	
	/**
	* 設定 StickyWin 的相對位置
	*/
	setPosition:function(el,pos_str,offset) {
		this._swin.options.relativeTo = el;
		this._swin.options.position = ($defined(pos_str))? pos_str:'center';
		if($defined(offset)) {
			this._swin.options.offset = offset;
		}
	},
	
	show:function(){
		this._swin.show();
		// 顯示以後再把位置調回初始值!
		this._swin.options.relativeTo = $(document.body);
		this._swin.options.position = 'center';
		this._swin.options.offset = {'x':0,'y':0};
	},
	hide:function(){
		this._swin.hide();
	},
	/**
	* 使用請注意, args 一定要是陣列格式
	*/
	selected:function(args){
		this.fireEvent('onSelected',[$splat(args)]);
	}
});

