// ================================================================
//  ajaxzip2.js ---- AjaxZip 2 驛オ萓ソ逡ェ蜿キ竊剃ス乗園螟画鋤繝ゥ繧、繝悶Λ繝ェ
//  Copyright 2006-2007 Kawasaki Yusuke <u-suke [at] kawa.net>
//  http://www.kawa.net/works/ajax/ajaxzip2/ajaxzip2.html
// ================================================================
//  -- JSONP蛹� --
//  Copyrigh (c)t 2007 @WEB
//  http://www.atmarkweb.jp/

AjaxZip2 = function () {};
AjaxZip2.VERSION = '2.06';
AjaxZip2.JSONDATA = 'ajaxzip2/data/';
AjaxZip2.CACHE = [];
AjaxZip2.prev = '';
AjaxZip2.PREFMAP = [
    null,       '蛹玲オキ驕�',   '髱呈」ョ逵�',   '蟯ゥ謇狗恁',   '螳ョ蝓守恁',
    '遘狗伐逵�',   '螻ア蠖「逵�',   '遖丞ウカ逵�',   '闌ィ蝓守恁',   '譬�惠逵�',
    '鄒、鬥ャ逵�',   '蝓シ邇臥恁',   '蜊�痩逵�',   '譚ア莠ャ驛ス',   '逾槫・亥キ晉恁',
    '譁ー貎溽恁',   '蟇悟アア逵�',   '遏ウ蟾晉恁',   '遖丈コ慕恁',   '螻ア譴ィ逵�',
    '髟キ驥守恁',   '蟯宣�逵�',   '髱吝イ。逵�',   '諢帷衍逵�',   '荳蛾㍾逵�',
    '貊玖ウ€逵�',   '莠ャ驛ス蠎�',   '螟ァ髦ェ蠎�',   '蜈オ蠎ォ逵�',   '螂郁憶逵�',
    '蜥梧ュ悟アア逵�', '魑・蜿也恁',   '蟲カ譬ケ逵�',   '蟯。螻ア逵�',   '蠎�ウカ逵�',
    '螻ア蜿」逵�',   '蠕ウ蟲カ逵�',   '鬥吝キ晉恁',   '諢帛ェ帷恁',   '鬮倡衍逵�',
    '遖丞イ。逵�',   '菴占ウ€逵�',   '髟キ蟠守恁',   '辭頑悽逵�',   '螟ァ蛻�恁',
    '螳ョ蟠守恁',   '鮖ソ蜈仙ウカ逵�', '豐也ク�恁'
];

var AjaxZipMyCallback  = function () {};
var AjaxZipMyCallback2 = function () {};
var AjaxZipMyCallback3 = function () {};

AjaxZip2.message = new Array();
AjaxZip2.message[0] = '�玲。√�驛オ萓ソ逡ェ蜿キ繧貞濠隗偵〒蜈・蜉帙@縺ヲ縺上□縺輔>';
AjaxZip2.message[1] = '縲€---縲€讀懃エ「荳ュ縲€---';
AjaxZip2.message[2] = '隧イ蠖薙�菴乗園縺ッ隕九▽縺九j縺セ縺帙s縺ァ縺励◆';


AjaxZip2.zip2addr = function ( azip1, apref, aaddr, azip2, astrt, aarea ) {
    var fzip1 = AjaxZip2.getElementByName(azip1);
    var fzip2 = AjaxZip2.getElementByName(azip2,fzip1);
    var fpref = AjaxZip2.getElementByName(apref,fzip1);
    var faddr = AjaxZip2.getElementByName(aaddr,fzip1);
    var fstrt = AjaxZip2.getElementByName(astrt,fzip1);
    var farea = AjaxZip2.getElementByName(aarea,fzip1);
    //if ( ! fzip1 ) return;
    //if ( ! fpref ) return;
    //if ( ! faddr ) return;

    // 驛オ萓ソ逡ェ蜿キ繧呈焚蟄励�縺ソ7譯∝叙繧雁�縺�
    var vzip = fzip1.value;
    if ( fzip2 && fzip2.value ) vzip += fzip2.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 ){
		if ( fpref.type == 'select-one' || fpref.type == 'select-multiple' ) {
			faddr.value = AjaxZip2.message[0];
		}else{
			fpref.value = AjaxZip2.message[0];
		}
		return;
	}
	/*
    // 蜑榊屓縺ィ蜷後§蛟、��ヵ繧ゥ繝シ繝�縺ェ繧峨く繝」繝ウ繧サ繝ォ
    var uniq = nzip+fzip1.name+fpref.name+faddr.name;
    if ( fzip1.form ) uniq += fzip1.form.id+fzip1.form.name+fzip1.form.action;
    if ( fzip2 ) uniq += fzip2.name;
    if ( fstrt ) uniq += fstrt.name;
    if ( uniq == AjaxZip2.prev ) return;
    AjaxZip2.prev = uniq;
    */
	
    // JSON蜿門セ怜セ後�繧ウ繝シ繝ォ繝舌ャ繧ッ髢「謨ー
    AjaxZipMyCallback2 = function ( data ) {
        var array = data[nzip];
		
		if( ! array ){
			if ( fpref.type == 'select-one' || fpref.type == 'select-multiple' ) {
				faddr.value = AjaxZip2.message[2];
			}else{
				fpref.value = AjaxZip2.message[2];
			}
			return;
		}
		
        // Opera 繝舌げ蟇セ遲厄シ�0x00800000 繧定カ�∴繧区キサ蟄励� +0xff000000 縺輔l縺ヲ縺励∪縺�
        var opera = (nzip-0+0xff000000)+"";
        if ( ! array && data[opera] ) array = data[opera];
        if ( !array ) return;
        var pref_id = array[0];                 // 驛ス驕灘コ懃恁ID
        if ( ! pref_id ) return;
        var jpref = AjaxZip2.PREFMAP[pref_id];  // 驛ス驕灘コ懃恁蜷�
        if ( ! jpref ) return;
        var jcity = array[1];
        if ( ! jcity ) jcity = '';              // 蟶ょ玄逕コ譚大錐
        var jarea = array[2];
        if ( ! jarea ) jarea = '';              // 逕コ蝓溷錐
        var jstrt = array[3];
        if ( ! jstrt ) jstrt = '';              // 逡ェ蝨ー

        var cursor = faddr;
        var jaddr = jcity;                      // 蟶ょ玄逕コ譚大錐

        if ( fpref.type == 'select-one' || fpref.type == 'select-multiple' ) {
            // 驛ス驕灘コ懃恁繝励Ν繝€繧ヲ繝ウ縺ョ蝣エ蜷�
            var opts = fpref.options;
            for( var i=0; i<opts.length; i++ ) {
                var vpref = opts[i].value;
                var tpref = opts[i].text;
                opts[i].selected = ( vpref == pref_id || vpref == jpref || tpref == jpref );
            }
        } else {
            if ( fpref.name == faddr.name ) {
                // 驛ス驕灘コ懃恁蜷搾シ句クょ玄逕コ譚大錐�狗伴蝓溷錐蜷井ス薙�蝣エ蜷�
                jaddr = jpref + jaddr;
            } else {
                // 驛ス驕灘コ懃恁蜷阪ユ繧ュ繧ケ繝亥�蜉帙�蝣エ蜷�
                fpref.value = jpref;
            }
        }
        if ( farea ) {
            cursor = farea;
            farea.value = jarea;
        } else {
            jaddr += jarea;
        }
        if ( fstrt ) {
            cursor = fstrt;
            if ( faddr.name == fstrt.name ) {
                // 蟶ょ玄逕コ譚大錐�狗伴蝓溷錐�狗分蝨ー蜷井ス薙�蝣エ蜷�
                jaddr = jaddr + jstrt;
            } else if ( jstrt ) {
                // 逡ェ蝨ー繝�く繧ケ繝亥�蜉帶ャ�′縺ゅk蝣エ蜷�
                fstrt.value = jstrt;
            }
        }
		
		faddr.value = jaddr;
		
        // patch from http://iwa-ya.sakura.ne.jp/blog/2006/10/20/050037
        // update http://www.kawa.net/works/ajax/ajaxzip2/ajaxzip2.html#com-2006-12-15T04:41:22Z
		if ( ! cursor ) return;
		if ( ! cursor.value ) return;
        var len = cursor.value.length;
        cursor.focus();
        if ( cursor.createTextRange ) {
            var range = cursor.createTextRange();
            range.move('character', len);
            range.select();
        } else if (cursor.setSelectionRange) {
            cursor.setSelectionRange(len,len);
        }
    };

    // 驛オ萓ソ逡ェ蜿キ荳贋ス�3譯√〒繧ュ繝」繝�す繝・繝��繧ソ繧堤「コ隱�
    var zip3 = nzip.substr(0,3);
    var data = AjaxZip2.CACHE[zip3];
    if ( data ) return AjaxZipMyCallback2( data );
	
	fpref.value = AjaxZip2.message[1];
	
    // JSON繝輔ぃ繧、繝ォ蜿嶺ソ。蠕後�繧ウ繝シ繝ォ繝舌ャ繧ッ髢「謨ー
    AjaxZipMyCallback3 = function (req) {
        if ( ! req ){
			fpref.value = AjaxZip2.message[2];
			return;
		}
        //if ( ! req.responseText ) return;
        //var json = AjaxZip2.getResponseText( req );
        //data = eval('('+json+')');
        AjaxZip2.CACHE[zip3] = req;
        AjaxZipMyCallback2( req );
    };

	// JSONP 繧ウ繝シ繝ォ繝舌ャ繧ッ髢「謨ー
	AjaxZipMyCallback = function( req ){
		AjaxZipMyCallback3( req );
	};
	
    // JSON繝輔ぃ繧、繝ォ繧貞女菫。縺吶k
    var url = AjaxZip2.JSONDATA+'zip-'+zip3+'.json';
    //var opt = {
    //    method: 'GET',
    //    asynchronous: true,
    //    onComplete: func2
    //};
    //var conn = new Ajax.Request( url, opt );
	AjaxZipMyConn(url);
};

// Safari 譁�ュ怜喧縺大ッセ蠢�
// http://kawa.at.webry.info/200511/article_9.html
AjaxZip2.getResponseText = function ( req ) {
    var text = req.responseText;alert(text);
    //var text = req;
    if ( navigator.appVersion.indexOf('KHTML') > -1 ) {
        var esc = escape( text );
        if ( esc.indexOf('%u') < 0 && esc.indexOf('%') > -1 ) {
            text = decodeURIComponent( esc );
        }
    }
    return text;
}

// 繝輔か繝シ繝�name縺九i隕∫エ�繧貞叙繧雁�縺�
AjaxZip2.getElementByName = function ( 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;
}

function AjaxZipMyConn(url){
    var script = document.createElement("script");
    script.charset = "utf-8";
    script.src = url;
	script.type= "text/javascript";
	document.body.appendChild(script);
}