//	コンストラクタ
function Industry(query) {
	this.xml; // jQueryのオブジェクト化済みXMLのキャッシュ
	this.query = query; // 業種XMLのアドレス
	this.dfltTag = '<option id="" value="">--選択してください--</option>'; // 初期タグ
	this.tmplTag = '<option id="%id" value="%id">%value</option>'; // テンプレートタグ
	
}

Industry.prototype = {
	// 初期化
	init : function () {
		$.ajax({
		    url: this.query,
		    cache:true,
		    top:this,
		    type: 'GET',
		    async: false,
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				alert("業種リストの取得に失敗しました");
		    },
		    success: function(dom){
		    	var industry = this.top;
		    	
		    	// 取得したXMLのキャッシュ
		    	industry.xml = $(dom);
		    	
		    	// 第一階層の取得
		    	industry.setIndustryList();
		    	
		    	// 第二階層の選択不可
		    	industry.disableDetail();
		    }
		});
		
	},
	
	// 編集時
	load : function (id1, id2) {
		$.ajax({
		    url: this.query,
		    cache:true,
		    top:this,
		    type: 'GET',
		    async: false,
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				alert("業種リストの取得に失敗しました");
		    },
		    success: function(dom){
		    	var industry = this.top;
		    	
		    	// 取得したXMLのキャッシュ
		    	industry.xml = $(dom);
		    	
		    	// 第一階層の取得
		    	industry.setIndustryList();
		    	$('#industry1').val(id1);
	    		industry.setDetailList(id1);
		    	
		    	// 第二階層のIDがあれば選択
		    	if (id2!='') {
		    		$('#industry2').val(id2);
		    	}
		    }
		});
		
	},
	
	// 業種リスト(1階層目)
	setIndustryList : function() {
		var industry = this;
		
		var list = $('#industry1');
		list.append(industry.dfltTag);
		list.width(); // IE用HACK
		
    	// 業種IDから子のリストを抜き出し追加
		this.xml.find('response').children('item').each(function(){
			var id = $(this).attr('id');
		    var value = $(this).attr('value');

	    	var tag = industry.tmplTag;
	    	tag = tag.replace(/%id/g, id);
	    	tag = tag.replace(/%value/g, value);

	    	list.append(tag);
	    	list.width(); // IE用HACK
		});	
		
		// 業種リスト(2階層目)変更時トリガ
		list.change( function() {
			var id = this[this.selectedIndex].value;
			industry.setDetailList(id);
		});
		
	},

	// 業種リスト(2階層目)
	setDetailList : function(id) {
		var industry = this;
		var children = this.xml.find('#' + id).children('item');

		// 挿入先の取得
		var list = $('#industry2');
		list.empty();
		list.append(industry.dfltTag);
		list.width(); // IE用HACK

		// リストが空ならば選択不可にする
		if (children.size()==0) {
			list.attr('disabled', 'disabled');
			return;
		}
		list.removeAttr('disabled');
		
    	// 業種IDから子のリストを抜き出し追加
		children.each(function(){
			var id = $(this).attr('id');
		    var value = $(this).attr('value');

	    	var tag = industry.tmplTag;
	    	tag = tag.replace(/%id/g, id);
	    	tag = tag.replace(/%value/g, value);

	    	list.append(tag);
	    	list.width(); // IE用HACK
			
		});
		
	},
	
	// 二階層目を選択不可にする
	disableDetail : function() {
		var industry2 = $('#industry2');
		industry2.empty();
		industry2.append(this.dfltTag);
		industry2.attr('disabled', 'disabled');
	}
}