/*
・var doc = document.getElementById; →×firefox は省略用変数が使えない
・html編集の際には、「getElementById」で示しているように
　「id」もつけること。（IEは「name」だけでも動く）
*/

// データ読み込み
function loadPost(){
	var fpost = document.getElementById('post1').value + document.getElementById('post2').value;
	if(fpost.match(/^[0-9]{7}$/)){
		document.getElementById('result').value = "";
		fName = 'postcode_numsearch.php?num=' + fpost;
		httpObj = createXMLHttpRequest(displayData);
		if (httpObj){
			httpObj.open("GET",fName,true);
			httpObj.send(null);
		}
	}else{
		alert('3桁＋4桁の半角数字を入力してください。');
		if(document.getElementById('postcode').value != ''){
			poststr = document.getElementById('postcode').value;
			document.getElementById('post1').value = poststr.substring(0,3);
			document.getElementById('post2').value = poststr.substring(3,7);
		}
	}
}

function displayData(){
	if ((httpObj.readyState == 4) && (httpObj.status == 200)){
		document.getElementById('result').value = httpObj.responseText;
		splitData(document.getElementById('result').value);
	}
}

function splitData(str){
	switch (str) {
		case 'err1':
		    alert('3桁＋4桁の半角数字を入力してください。');
		    break;
		case 'err2':
		    alert('該当する情報はありません。');
		    break;
		default:
			tmp = str.split("|");
			document.getElementById('postcode').value = tmp[0];
			document.getElementById('area_cd').value  = tmp[1];
			document.getElementById('address1').value = tmp[2];
			document.getElementById('address2').value = tmp[3];
			//if (typeof(tmp[4]) != "undefined"){
				document.getElementById('address3').value = tmp[4];
			//}
		    break;
	}
}

function setFocus(fn){
	clearTimeout(TimeID);
	document.getElementById(fn).focus();
}

// focus 後ろ４桁
function autefucus(str){
	if(str.value.length==3){
		TimeID=setTimeout("setFocus('post2')",200);
		/*
		doc('post2').focus();
		//firefoxは× イベント中に別イベントが発生するのがよろしくないらしい
		*/
	}
}

// focus 「検索ボタン」
function autefucus2(str){
	if(str.value.length==4){
		TimeID=setTimeout("setFocus('zipbtn')",200);
	}
}
