function ajaxRequest(){
    var zipcode = $("#zip_1").val() + "-" + $("#zip_2").val();
    if ( zipcode.match(/[^\d-]/) ) {
        alert("郵便番号を半角数字で入力してください。");
    }else{
        if (zipcode.length >= 4) {
            var msec = (new Date()).getTime();
            var url = '/request/zip_api/'+zipcode;
            var is_unique = false;
            
            $.get(url, function(data){
                    $('#zip_search').html(data);
                    if( $("#address_list_len").val() == 1 ){
                        value = $("#ajax_addr").val();
                        setAddr(value);
                    }else{
                        popup(url);
                    }
                });
        }else{
            alert("郵便番号を3桁以上入力してください。");
        }
    }
}


function popup(url){
    settings = {
        height:600, // sets the height in pixels of the window.
        width:600, // sets the width in pixels of the window.
        toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
        scrollbars:1, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
        status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
        resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
        left:0, // left position when the window appears.
        top:0, // top position when the window appears.
        center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
        createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
        location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
        menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
        onUnload:null // function to call when the window is closed
    };


    parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
    var name = settings.createnew ? "PopUpWindow" : "PopUpWindow";

    winObj = window.open(url, name, parameters);
    if (settings.onUnload) {
        // Incremental check for window status
        // Attaching directly to window.onunlaod event causes invoke when document within window is reloaded
        // (i.e. an inner refresh)
        unloadInterval = setInterval(function() {
                if (!winObj || winObj.closed) {
                    clearInterval(unloadInterval);	
                    settings.onUnload.call($(this));
                }
            },500);
    }
	
    winObj.focus();

}

function setAddr(value){
    if(window.opener && window.opener.$("#zip_1")){
        reflected_window = window.opener;
    }else{
        reflected_window = window;
    }
    addrs = value.split(":") ;
    zip_1 =  reflected_window.$("#zip_1");
    zip_1.val(addrs[1].slice(0,3));
    zip_2 =  reflected_window.$("#zip_2");
    zip_2.val(addrs[1].slice(3,7));
    pref_ = reflected_window.$("#prefecture");
    pref_.val(parseInt(addrs[0])+1);
    address1_ = reflected_window.$("#address1");
    address1_.val(addrs[3]+" "+addrs[4]);
    area = reflected_window.$("#zip_ajax_area");
    area = area.attr("style","display:none");

    if(window.opener && window.opener.$("#zip_1")){
        window.close();
    }
}


