// JavaScript Document

bSend=false;

function formSEND(){
	if(bSend){
	showDialog('Confirmation','現在、送信中です','prompt');
		return false; 
	}
	bSend=true;
	return true;
}

function ConfirmReset(){ 
	if(window.confirm('入力した内容を全てクリアーしてもよろしいですか？')){
		return true; 
	} else {
		return false;	
	} 
}

function searchzip() {
    //XMLHttpRequestオブジェクト生成
    var xmlhttp = createHttpRequest();
    if (xmlhttp == null) {
        return null;
    }

	if ( document.contact_form.ZIP.value == "" ) {
	    alert("郵便番号を入力してください。");
		document.contact_form.ZIP.focus();
		return false;
	}

    var data = "selL1=" + document.contact_form.ZIP.value;
    document.contact_form.PREF.selectedIndex = 0;

    sendRequest(xmlhttp, "POST", "optionL1.php", false, data, callBack1);
	
}
function searchB_zip() {
    //XMLHttpRequestオブジェクト生成
    var xmlhttp = createHttpRequest();
    if (xmlhttp == null) {
        return null;
    }

	if ( document.contact_form.B_ZIP.value == "" ) {
	    alert("郵便番号を入力してください。");
		document.contact_form.B_ZIP.focus();
		return false;
	}

    var data = "selL1=" + document.contact_form.B_ZIP.value;
    document.contact_form.B_PREF.selectedIndex = 0;

    sendRequest(xmlhttp, "POST", "optionL1.php", false, data, callBack2);
	
}
function callBack1(xmlhttp)
{
    var result = xmlhttp.responseText;

    if( result == "" )
    {
	    alert("住所が見つかりませんでした。");
		document.contact_form.ZIP.focus();
    }
    else
    {
	    var resArray = result.split(",");
		document.contact_form.ADDRESS1.value = resArray[1];
	    len = document.contact_form.PREF.options.length;
	    for (i=len-1; i>=0; i--){
	        if(document.contact_form.PREF.options[i].value == resArray[0] )
	        {
			    document.contact_form.PREF.selectedIndex = i;
	        }
	    }
		document.contact_form.ADDRESS2.focus();
    }
}
function callBack2(xmlhttp)
{
    var result = xmlhttp.responseText;

    if( result == "" )
    {
	    alert("住所が見つかりませんでした。");
		document.contact_form.B_ZIP.focus();
    }
    else
    {
	    var resArray = result.split(",");
		document.contact_form.B_ADDRESS1.value = resArray[1];
	    len = document.contact_form.B_PREF.options.length;
	    for (i=len-1; i>=0; i--){
	        if(document.contact_form.B_PREF.options[i].value == resArray[0] )
	        {
			    document.contact_form.B_PREF.selectedIndex = i;
	        }
	    }
		document.contact_form.B_ADDRESS2.focus();
    }
}
function createHttpRequest()
{
    var xmlhttp = null;

    if(window.ActiveXObject){
        try {
        // MSXML2以降用
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                // 旧MSXML用
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e2) {

            }
        }
    } else if(window.XMLHttpRequest){
        // Win Mac Linux m1,f1,o8 Mac s1 Linux k3用
        xmlhttp = new XMLHttpRequest();
    } else {

    }
    if (xmlhttp == null) {
        alert("Can not create an XMLHTTPRequest instance");
    }

    return xmlhttp;
}
// 送受信処理
function sendRequest (xmlhttp, method, url, async, data, callback)
{
    //ブラウザ判定
    var ua = navigator.userAgent;
    var safari  = ua.indexOf("Safari")!=-1;
    var konqueror = ua.indexOf("Konqueror")!=-1;
    var mozes = ((a = navigator.userAgent.split("Gecko/")[1] )
                   ? a.split(" ")[0] : 0) >= 20011128 ;

    // サーバーからの受信処理
    // opera       : onreadystatechange に多重レスバグがあるので onload が安全
    // Moz,FireFox : readyState==3 でも受信するので通常は onload が安全
    // Win ie      : onload が動作しない
    // Konqueror   : onload が不安定
    if (window.opera || safari || mozes) {
        xmlhttp.onload = function ()
        {
            callback(xmlhttp);
        }
    } else {
        xmlhttp.onreadystatechange = function ()
        {
            // サーバーからの応答判定
            //   0 : 初期化されていません
            //   1 : 読み込み中です
            //   2 : 読み込み完了
            //   3 : 双方向に扱えます
            //   4 : すべて完了しました
            if (xmlhttp.readyState == 4) {

                // サーバーの応答コード判定
                //   200 : OK
                if (xmlhttp.status == 200) {
                    callback(xmlhttp);
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
    }

    // サーバーからの送信処理
    xmlhttp.open(method, url, async);
    xmlhttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    xmlhttp.send(data);
}
function inputzip(azip)
{
    var fzip1 = zip_getElementByName(azip);
    if ( ! fzip1 ) return;

    // 郵便番号を数字のみ7桁取り出す
    var vzip = fzip1.value;
    if ( ! vzip ) return;
    var nzip = '';
    for( var i=0; i<vzip.length; i++ ) {
        var chr = vzip.charCodeAt(i);
        if ( chr < 48 ) continue;
        if ( chr > 57 ) continue;
        nzip += vzip.charAt(i);
    }
    if ( nzip.length < 7 ) return;
    searchzip();
}
function zip_getElementByName ( elem, sibling ) {
    if ( typeof(elem) == 'string' ) {
        var list = document.getElementsByName(elem);
        if ( ! list ) return null;
        if ( list.length > 1 && sibling && sibling.form ) {
            var form = sibling.form.elements;
            for( var i=0; i<form.length; i++ ) {
                if ( form[i].name == elem ) {
                    return form[i];
                }
            }
        } else {
            return list[0];
        }
    }
    return elem;
}
