if (typeof(QLife) == 'undefined') {
    var QLife = {};
}


QLife.Zipcode = {
    addListener : function(inputZipcode1, inputZipcode2, funcBefore, funcUpdated){
        // 指定したinput要素が存在しないとき、終了
        if ($(inputZipcode1) == null || $(inputZipcode2) == null) {
            alert('No input elements: inputZipcode1=' + inputZipcode1 + ' inputZipcode2=' + inputZipcode2
                + ' funcBefore=' + typeof(funcBefore) + ' funcUpdated=' + typeof(funcUpdated));
            return;
        }
        // 入力値変更時の処理
        var func = function(){
            var zipcode = $F(inputZipcode1)+$F(inputZipcode2);
            QLife.Zipcode.updated(zipcode, funcBefore, funcUpdated);
        };
        $(inputZipcode1).onkeyup = func;
        $(inputZipcode2).onkeyup = func;
    },
    /**
     * 直前に検索した郵便番号
     */
    lastZipcode : '',
    /**
     * 郵便番号から住所を検索し、処理を実行
     */
    updated : function(zipcode, funcBefore, funcUpdated){
        // 7桁の郵便番号でないか、直前に検索した値と同じ
        if (zipcode == null || String(zipcode).length < 7 || this.lastZipcode == zipcode) {
            return;
        }
        if (zipcode.match(/^[0-9]{7}$/)) {
        } else {
            return;
        }
        this.lastZipcode = zipcode;
        // 処理前実行関数を実行
        if (funcBefore != null) {
            funcBefore();
        }

        new Ajax.Request('/pet/zipcode.php', {
            method : 'get',
            parameters : {zipcode : zipcode},
            onSuccess : funcUpdated
        });
    }
}
