﻿var datainfo = function()
{
	this.zipcode = null;
}
var difo = new datainfo();
var zipcode =function (city,area,useZipNameValue,appendZipCode)
{
	this.city = document.getElementById(city);
	this.area = document.getElementById(area);
	this.allArea = ["台北市","基隆市","台北縣","宜蘭縣","新竹市","新竹縣","桃園縣","苗栗縣","台中市","台中縣","彰化縣","南投縣","嘉義市","嘉義縣","雲林縣","台南市","台南縣","高雄市","高雄縣","澎湖縣","屏東縣","台東縣","花蓮縣","金門縣","連江縣","南海諸島"];
	this.area_def = "\x2D\u5730\u5340\x2D";
	//是否要加入zipcode
	this.appendZipCode = appendZipCode;
	this.CityEmptyValue = "";
	this.useZipNameValue = useZipNameValue;
	this.WillappendZipCode = function (value)
	{
		this.appendZipCode = value;
	};
	//初始化
	this.initData = function ()
	{

		//設定city的值
		this.setAllArea();
		//var data = this;
		//this.city.prototype.myobj =data;
		//difo.zipcode = this;
		//this.city.onchange = function(){
			//difo.zipcode.citychange;
		//};
	};

	//city改變 function
	this.citychange = function()//city,area,resetSelZC,appendZipCode2,addSelectOption
	{
	//alert(this.city.selectedIndex);
		if (this.city.selectedIndex==0)
			this.resetSelZC();
		else
			this.getAreaInfo(this.city.selectedIndex-1);
	};
	//reset地區
	this.resetSelZC = function() {
		this.area.options.length = 0;
		this.addSelectOption(this.area, '', this.area_def);
	};

	//ajax 取得資料
	this.getAreaInfo = function()
	{
		//alert("ajax");
		difo.zipcode = this;
		new Ajax.Request("/getZipCodeList.jsp", {
	    method: 'get',
		parameters:{"area":difo.zipcode.city.selectedIndex-1},
	    onSuccess: function(transport) {
			//alert(transport.responseText);
			var xml = transport.responseText;
		    var zcList = xml.split("|");
		    difo.zipcode.area.options.length = 0;
			//alert(zcList.length);

		    for (var i=0; i<zcList.length; i++) {

			  var sV = zcList[i].split(",");
			  var sValue = sV[0];
			  if(difo.zipcode.useZipNameValue)//用名稱做value
			  {
				sValue = sValue<0 ? difo.zipcode.city.options[difo.zipcode.city.selectedIndex].text :difo.zipcode.city.options[difo.zipcode.city.selectedIndex].text+sV[1];

			  }
			  //alert(sValue);
		      var sName = sV[0]<0 ? difo.zipcode.CityEmptyValue : (((difo.zipcode.appendZipCode==true)?(sV[0] +" "):"")+ sV[1]);
		      difo.zipcode.addSelectOption(difo.zipcode.area, sValue.trim(), sName.trim());
			  //alert(sName);
		    }

	    },
		onCreate: function()
	    {
		//alert("create");
		  difo.zipcode.area.options.length = 0;
	      difo.zipcode.addSelectOption(difo.zipcode.area, '', 'Loding...');
		  //alert("2222");
	    },
	    onFailure: function(transport)
	    {
	      alert("更新失敗");
	    },
		onComplete:function()
	    {
			//alert("ok");
	    }
	  });

	};

	//加入option
	this.addSelectOption = function (sel, val, txt)	{
	  var opt= document.createElement('OPTION');
	  sel.options.add(opt);
	  opt.value= val;
	  opt.text= txt;
	};
	//設定city的值
	this.setAllArea = function () {
		for (var i=0;i<allArea.length;i++) {
                
			this.addSelectOption(this.city,null,allArea[i]);
		}
	};

	//開始初始化
	this.initData();
}

