/*
 * charset: utf-8
 */

function searchAddress(obj, zip1, zip2, address) {
	
	if($defined($('zipBox'))) $('zipBox').dispose();

	if (zip1 == '') return false;
	if (zip2 == '') return false;
	if (address == '') return false;

	var element = $(obj);
	var pos = element.getPosition();

	var width;
	if (Browser.Engine.trident && !Browser.Engine.trident5) {
		//IE7以外のIEの場合
		width = '200px';
		height = '40px';
	} else {
		//IE以外の場合
		width = 'auto';
		height = 'auto';
	}

	//郵便番号検索ボックス
	var zipBox = new Element('div', {
		 'id': 'zipBox'
		,'styles': {
			 'height': 'auto'
			,'width': width
			,'min-width': '200px'
			,'background-color' : '#eee'
			,'border' : '1px solid #000'
			,'position' : 'absolute'
			,'left': pos.x+40//(window.getScrollLeft() + (window.getWidth() - 56) / 2) + 'px'
			,'top': pos.y+20//(window.getScrollTop() + ((window.getHeight() - 20) / 2)) + 'px'
			,'padding' : '0'
			,'margin' : '0'
			//,'font-size' : '0.8em'
			,'font-size' : '12px'
		}
	});

	var buf = '';
	buf += '〒';
	buf += '<input type="text" name="condZip1" id="condZip1" size="4" maxlength="3" value="'+$(zip1).value+'">';
	buf += '-';
	buf += '<input type="text" name="condZip2" id="condZip2" size="5" maxlength="4" value="'+$(zip2).value+'">';
	buf += '&nbsp;';
	buf += '<input type="button" value="検索" onclick="_search(\''+zip1+'\', \''+zip2+'\', \''+address+'\');">';

	var zipCondition = new Element('div', {
		 'id': 'zipCondition'
		,'styles': {
			 'height': '30px'
			,'width': '100%'
			//,'background-color' : '#aaa'
			//,'border' : '1px dotted #333'
			,'padding' : '5px'
			,'margin' : '0'
		}
	}).set({
		html: buf
	});

	var zipResult = new Element('div', {
		 'id': 'zipResult'
		,'styles': {
			 'height': height
			,'min-height': '40px'
			,'width': '100%'
			//,'background-color' : '#aaa'
			//,'border' : '1px dotted #333'
			,'border-top' : '1px solid #000'
			,'padding' : '10px 0 5px  0'
			,'margin' : '0'
		}
	});


	var btnClose = new Element('input', {
		 'type': 'button'
		,'value': '閉じる'
		,'events': {
			'click': function(){
				_close();
			}
		}
	});

	var zipButton = new Element('div', {
		 'id': 'zipButton'
		,'styles': {
			 'height': '30px'
			,'width': '100%'
			//,'background-color' : '#aaa'
			//,'border' : '1px dotted #333'
			,'text-align' : 'center'
			,'padding' : '5px'
			,'margin' : '0'
		}
	});
	btnClose.inject(zipButton);

	zipCondition.inject(zipBox);
	zipResult.inject(zipBox);
	zipButton.inject(zipBox);
	zipBox.inject(document.body);

}

function _search(zip1, zip2, address) {
	//alert('_search');


	var msg = new Element('div', {
		'styles': {
			 'height': 'auto'
			,'width': '100%'
			,'padding' : '0'
			,'margin' : '0'
			,'color' : '#61a2ef'
			,'text-align' : 'center'
		}
	}).set({text: '検索中…'});

	$('zipResult').empty();
	msg.inject('zipResult');

	var btnClose = new Element('input', {
		 'type': 'button'
		,'value': '閉じる'
		,'events': {
			'click': function(){
				_close();
			}
		}
	});

	$('zipButton').empty();
	btnClose.inject('zipButton');




	//PHPファイルのURL
	var url = 'dummy_zip2address.php';

	new Request({
		 url: url
		,method: 'post'
		,onComplete: function(res) {
			//alert(res);

			var objRes = JSON.decode(res);

			$('zipResult').empty();

			if ($defined(objRes.list)) {
				//設定対象プルダウンメニューに値を設定する。
				var slc = new Element('select', {
					 'name': 'resAddress'
					,'id': 'resAddress'
					,'styles': {
						//'width': '100%'
					}
				});
				$each(objRes.list, function(item){
					var option = new Element('option', {
						//'value': item.zip1+','+item.zip2+','+item.pref+','+item.add1+','+item.add2
						'value': JSON.encode(item)
					}).set({text: item.zip1+''+item.zip2+'|'+item.pref+''+item.add1+''+item.add2});
					option.inject(slc);
				});


				var btnSelect = new Element('input', {
					 'type': 'button'
					,'value': '選択'
					,'events': {
						'click': function(){
							_select(zip1, zip2, address);
						}
					}
				});
				var btnClose = new Element('input', {
					 'type': 'button'
					,'value': '閉じる'
					,'events': {
						'click': function(){
							_close();
						}
					}
				});

				
				slc.inject('zipResult');
				
				$('zipButton').empty();
				btnSelect.inject('zipButton');
				btnClose.inject('zipButton');
				
			}


			//メッセージがある場合、表示する。
			if ($defined(objRes.message)) {
				if (objRes.message != '') {
					var msg = new Element('div', {
						'styles': {
							 'height': 'auto'
							,'width': '100%'
							,'padding' : '0'
							,'margin' : '0'
							,'color' : '#61a2ef'
							,'text-align' : 'center'
						}
					}).set({text: objRes.message});
					msg.inject('zipResult');
				}
			}
			//エラーメッセージがある場合、表示する。
			if ($defined(objRes.message_error)) {
				if (objRes.message_error != '') {
					var msg = new Element('div', {
						'styles': {
							 'height': 'auto'
							,'width': '100%'
							,'padding' : '0'
							,'margin' : '0'
							,'color' : '#f00'
							,'text-align' : 'center'
						}
					}).set({text: objRes.message_error});
					msg.inject('zipResult');
				}
			}
		}
	}).send('zip1='+$('condZip1').value+'&zip2='+$('condZip2').value);

}

function _select(zip1, zip2, address){
	var objZip = JSON.decode($('resAddress').value);
	$(zip1).value = objZip.zip1;
	$(zip2).value = objZip.zip2;
	$(address).value = objZip.pref + objZip.add1 + objZip.add2;

	_close();
}

function _close() {
	if($defined($('zipBox'))) $('zipBox').dispose();
}

