//--------------変数の宣言------------------
var searchkey;                               //----現在の検索キーワード(tel/add)
var searchtype = 0;                          //----選択結果の分岐
//-------------------------------------------
var zipallnum  = 0;                            //----全該当Zipデータ数
var zippage  = 0;                            //----現在のZip表示ページ番号
var zipcount = 0;                            //----現ZipページのZipデータ数
var ziplists = new Array;                    //----Zipデータリスト(配列)
//-------------------------------------------
var addoffset;

//-------------------------------------------
$(function(){
	
	//------------〒→住所検索---------------
	$("#zip2add").click(function(){
		if (document.adrset.post1.value == '' || document.adrset.post2.value == '') {
			alert('郵便番号欄に空白があります...');
		} else {
			var post1 = numzen2han(document.adrset.post1.value);
			var post2 = numzen2han(document.adrset.post2.value);
			searchkey = post1 + '-' + post2;
			searchtype = 1;
			searchZip2Add(1);
		}
		return false;
    });
	
	//------------町名等カナ検索-------------
	$("#add2zip").click(function(){
		if (document.adrset.add3.value == '') {
			alert('町名・大字等欄が入力されていません...');
		} else {
			var add1 = document.adrset.add1.value;
			var add2 = document.adrset.add2.value;
			var add3 = document.adrset.add3.value;
			searchkey = add1 + "/" + add2 + "/" + add3;
			searchtype = 2;
			searchKana2Add(1);
		}
		return false;
    });

	//--------検索結果リストを閉じる---------
	$("#popupclose").click(function(){
		$("#popupdiv").css("visibility","hidden");
		return false;
    });
});

//----------------〒→住所検索---------------
function searchZip2Add(pnum) {
	zippage = pnum;
	//--------------Ajax通信-----------------
	$.ajax({
		//-------------送信部----------------
		dataType: "jsonp",
		data: {
			"zip": searchkey,
			"pnum": pnum
		},
		cache: false,
		url: "http://tools.digiban.co.jp/dbzip/zip2add_utf.cgi",
		//-------------受信部----------------
		success: function (data) {
			zipListSet(data);
		}
	});
}

//--------------町名等カナ検索---------------
function searchKana2Add(pnum) {
	zippage = pnum;
	//--------------Ajax通信-----------------
	$.ajax({
		//-------------送信部----------------
		dataType: "jsonp",
		data: {
			"add": searchkey,
			"pnum": pnum
		},
		cache: false,
		url: "http://pcenter.oita-press.co.jp/map/kana2add.cgi",
		//-------------受信部----------------
		success: function (data) {
			zipListSet(data);
		}
	});
}

//------------検索結果リスト表示-------------
function zipListSet(data) {

	//---------------検索結果----------------
	var statusx = parseInt(data.feed.status,10);
	if (statusx != 0) {
		alert("Zip DataBase Search Error!!...");
		return;
	}

	//---------------全該当数----------------
	zipallnum = parseInt(data.feed.allnum,10);
	if (zipallnum == 0) {
		alert("検索条件に該当する住所データが見つかりません...");
		return;
	}

	//--------------リストの取得-------------
	var ziptemplist = new Array;
	$.each(data.feed.entry, function(i,item){
		ziptemplist[i] = new Array(item.zipcode,item.add1,item.add2,item.add3,item.add4);
	});
	ziplists = ziptemplist;
	zipcount = ziplists.length;
	
	//------------検索結果タイトル-----------
	searchkey = data.feed.searchkey;
	var titlehtml = '[ 検索キー : '+searchkey+' ]';
	$("#searchtitle").html(titlehtml);
	
	//-------------検索結果リスト------------
	var listhtml = '';
	for (var i = 0; i < zipcount; i++) {
		var postnumber = '<font color=white>000-0000</font>';
		if (ziplists[i][0] != "") {
			postnumber = ziplists[i][0];
		}
		listhtml += '<a href="javascript:zipValueSet(' + i + ')" style="font-family:monospace;">' + postnumber + ' ' + ziplists[i][1] + ziplists[i][2] + ziplists[i][3];
		if (ziplists[i][4] != '') {
			listhtml += ' ( ' + ziplists[i][4] + ' )</a><br>';
		} else {
			listhtml += '</a><br>';
		}
	}
	$("#ziplist").html(listhtml);

	//-------------ページナビ----------------
	var allpage = 0;
	if (zipallnum < 10) {
		allpage = 1;
	} else if (zipallnum % 10 == 0) {
		allpage = zipallnum / 20;
	} else {
		allpage = (zipallnum - (zipallnum % 10)) / 10 + 1;
	}
	var navihtml = '';
	for (var p=1; p <= allpage; p++) {
		if (p == zippage) {
			navihtml += '<u><b><font color="red">' + p + '</font></b></u>　';
		} else {
			if (searchtype == 1) {
				navihtml += '<a href="javascript:searchZip2Add(' + p + ')">' + p + '</a>　';
			} else if (searchtype == 2) {
				navihtml += '<a href="javascript:searchKana2Add(' + p + ')">' + p + '</a>　';
			}
		}
	}
	$("#zipnavi").html(navihtml);
	
	//------------POPUP位置設定--------------
	addoffset = $("#zip2add").offset();
	addoffset.left = addoffset.left;
	addoffset.top  = addoffset.top;

	//------------ポップアップ指定-----------
	if (searchtype == 1) {
		var leftpx = addoffset.left - 90 + 'px';
		var toppx  = addoffset.top + 25 + 'px';
		$("#popupdiv").css("left", leftpx);
		$("#popupdiv").css("top", toppx);
	} else {
		var leftpx = addoffset.left - 90 + 'px';
		var toppx  = addoffset.top + 125 + 'px';
		$("#popupdiv").css("left", leftpx);
		$("#popupdiv").css("top", toppx);
	}
	$("#popupdiv").css("visibility","visible");
}

//--------------選択結果値設定---------------
function zipValueSet(num) {
	var post1 = ziplists[num][0].substr(0,3);
	var post2 = ziplists[num][0].substr(4,4);
	document.adrset.post1.value = post1;
	document.adrset.post2.value = post2;
	document.adrset.add1.value = ziplists[num][1];
	document.adrset.add2.value = ziplists[num][2];
	document.adrset.add3.value = ziplists[num][3];
	$("#popupdiv").css("visibility","hidden");
	//if (searchtype == 2) {
	//	searchPostArea();
	//}
}

//*************数字の全角半角変換************
var zennum = "０１２３４５６７８９．，－ー／（）";
var hannum = "0123456789.,--/()";
//-------------------------------------------
function numzen2han(mototext) {
	var str = "";
	for (var i = 0; i < mototext.length; i++) {
		var c = mototext.charAt(i);
		var n = zennum.indexOf(c,0);
		if (n >= 0) { c = hannum.charAt(n); }
		str += c;
	}
	return str;
}

