//画面表示時

window.onload = 
function(){
    districtsearch();
    districtsearch2();
    railwaysearch();
    railwaysearch2();
    property_all_num();
    property_new_num();
    news();
    newpropertylist();
    lastupdate();
};


//区域表示
function districtsearch(){
    new Ajax.Request("/property/index/districtsearch/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("id_district").innerHTML = res;
        }
    });
}

//区域表示2(footer)
function districtsearch2(){
    new Ajax.Request("/property/index/districtsearch2/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("id_district2").innerHTML = res;
        }
    });
}

//路線表示
function railwaysearch(rr_cd){
    if (!rr_cd) {
        rr_cd = 1;
    }

    new Ajax.Request("/property/index/railwaysearch/",
    {
        method : "post",
        asynchronous : true,
        parameters : { rr_cd : rr_cd },
        onSuccess : function(data) {
            var res = data.responseText;
            $("list").innerHTML = res;
        }
    });
}

//路線表示2(footer)
function railwaysearch2(){
    new Ajax.Request("/property/index/railwaysearch2/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("list2").innerHTML = res;
        }
    });
}

//現在の募集件数表示
function property_all_num(){
    new Ajax.Request("/property/index/propertyallnum/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("property_all_num").innerHTML = res;
        }
    });
}

//新着物件数表示
function property_new_num(){
    new Ajax.Request("/property/index/propertynewnum/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("property_new_num").innerHTML = res;
        }
    });
}

//最新ニュース表示
function news(){
    new Ajax.Request("/property/index/news/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            $("newslist").innerHTML = res;
        }
    });
}


var newOffset =0;

//新着物件表示
function newpropertylist(a){
    if(a == "prev"){
        newOffset--;
        if(newOffset < 0){
            newOffset = 0;
        }
    }else if(a == "next"){
        newOffset++;
    }
    
    new Ajax.Request("/property/index/newpropertylist/",
    {
        method : "post",
        asynchronous : true,
        parameters : {"offset": newOffset},
        onSuccess : function(data) {
            var res = data.responseText;
            if(res == ""){
                newOffset--;
            }else{
                $("newPropertyList").innerHTML = res;
            }
        }
    });
}

//最終更新日表示
function lastupdate(){
    new Ajax.Request("/property/index/lastupdate/",
    {
        method : "post",
        asynchronous : true,
        onSuccess : function(data) {
            var res = data.responseText;
            if($("lastupdate") != null){
                $("lastupdate").innerHTML = res;
            }
            if($("lastupdate2") != null){
                $("lastupdate2").innerHTML = res;
            }
        }
    });
}

//路線表示切替
function rrChange (imgid, rr_cd) {
    if (imgid == "metro") {
        document.getElementById("metro").src = "imgs/top/tab_metro_on.jpg";
    } else {
        document.getElementById("metro").src = "imgs/top/tab_metro.jpg";
    }

    if (imgid == "jr") {
        document.getElementById("jr").src = "imgs/top/tab_jr_on.jpg";
    } else {
        document.getElementById("jr").src = "imgs/top/tab_jr.jpg";
    }

    if (imgid == "toei") {
        document.getElementById("toei").src = "imgs/top/tab_toei_on.jpg";
    } else {
        document.getElementById("toei").src = "imgs/top/tab_toei.jpg";
    }

    if (imgid == "other") {
        document.getElementById("other").src = "imgs/top/tab_other_on.jpg";
    } else {
        document.getElementById("other").src = "imgs/top/tab_other.jpg";
    }

    railwaysearch(rr_cd);
}

//チェックボックスon/off
function BoxChecked(frmname, name, check){
    if (!document.forms[frmname].elements[name].length) {
        document.forms[frmname].elements[name].checked = check; //チェックボックスをON/OFFにする
        document.forms[frmname].elements[name].onchange();
    }

    for(count = 0; count < document.forms[frmname].elements[name].length; count++){
        document.forms[frmname].elements[name][count].checked = check;  //チェックボックスをON/OFFにする
        document.forms[frmname].elements[name][count].onchange();
    }
}

//オフィス移転の概算予算計算
function change_out(){
    var inVal = document.getElementById("in").value.replace(/,/g, '');
    if(inVal.match(/[^0-9]/) != null){
        if(inVal == "相談" || inVal == "未定"){
            document.getElementById("total").value = inVal;
            return false;
        }
        inVal = 0;
    }else{
        inVal = parseInt(inVal);
    }

    var moveVal = document.getElementById("move").value.replace(/,/g, '');
    if(moveVal.match(/[^0-9]/) != null){
        moveVal = 0;
    }else{
        moveVal = parseInt(moveVal);
    }

    var exitCostsVal = document.getElementById("exit_costs").value.replace(/,/g, '');
    if(exitCostsVal.match(/[^0-9]/) != null){
        exitCostsVal = 0;
    }else{
        exitCostsVal = parseInt(exitCostsVal);
    }
    var pyeongVal = document.getElementById("pyeong").value.replace(/,/g, '');
    var totalVal = 0;
    if(pyeongVal.match(/[^0-9\.]/) != null){
        alert("坪数は半角数字と小数点のみ入力して下さい。");
        return false;
    }else{
        pyeongVal = parseFloat(pyeongVal);
    }

    outVal = pyeongVal * exitCostsVal;
    document.getElementById("out").value = String(outVal).replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, "$1,");
    document.getElementById("total").value = String(inVal + moveVal + outVal).replace(/(\d{1,3})(?=(\d{3})+(?!\d))/g, "$1,");
}
