/*******************************************************/
/*****    処理名称    :システム使用しているjs      *****/
/*******************************************************/
/*****                                             *****/
/*****    ファイル名    :kakeibo.js                *****/
/*****    機能概要      :システム使用しているjs    *****/
/*****    作成者        :                          *****/
/*****    作成日        :                          *****/
/*****    バージョン    :0.1.0                     *****/
/*****                                             *****/
/*******************************************************/

/*
*    概要    ：収入データを保存する
*    引数    ：なし
*    戻り値  ：true    保存可能；false　保存不可能
*/
function savesyunyudata(){
    return savesisyutudata();
}

/*
*    概要    ：支出データを保存する
*    引数    ：なし
*    戻り値  ：true    保存可能；false　保存不可能
*/
function savesisyutudata(){
    
    document.theform.save.disabled=true;
    
    var checkresult = checknum(document.theform.money);
    if(checkresult == false){
        document.theform.save.disabled=false;
        return false;
    }
    
    if(document.theform.memo.value.length>50){
        document.theform.memo.select();
        document.theform.save.disabled=false;
        return false;
    }
    
    if(document.theform.inexid == null){
        document.theform.command.value="save";
    }
    else{
        document.theform.command.value="update";
    }
    
    document.theform.submit();
    
    return true;

}

/*
*    概要    ：年月によって、selectboxのoptionsを自動的に変更する
*    引数    ：なし
*    戻り値  ：なし
*/
function rewriteDate(){
    var year,monthh;
    year = theform.year.value;    
    month = theform.month.value;
    var elDay = theform.day;
    var days = getDays(year,month);
    if(elDay.length < days){
        for(var i=elDay.length;i<days;i++){
            var oOption = document.createElement("OPTION");
            oOption.text=elDay.length+1;
            oOption.value=elDay.length+1;
            elDay.add(oOption);
        }
    }
    else if(elDay.length > days){
        var selectedIndex = elDay.selectedIndex;
        if(elDay.selectedIndex+1 >=days) selectedIndex = days-1;
        elDay.selectedIndex = selectedIndex;
        var deleteCount = elDay.length-days;
        for(var i=0;i<deleteCount;i++){
            elDay.remove(elDay.length-1);
        }
    }
}

/*
*    概要    ：親項目をクリックすると、formをsubmitされる。
*    引数    ：なし
*    戻り値  ：なし
*/
function refreshSubItems(){
    document.theform.save.disabled=true;
    if(document.theform.can_submit.value == 0) {
        return;
    }
    document.theform.can_submit.value = 0;
    
    document.theform.command.value="refresh_subitem";
    document.theform.submit();
    document.theform.disabled=false;
    return true;
}

/*
*    概要    ：変更使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function modify(id){
    document.theform.save.disabled=true;
    document.theform.id.value = id;
    document.theform.command.value="modify";
    document.theform.submit();
}

/*
*    概要    ：詳細を表示する使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function detail(id){
    document.theform.save.disabled=true;
    document.theform.id.value = id;
    document.theform.command.value="detail";
    document.theform.submit();
}

/*
*    概要    ：項目を削除する使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function remove(id){
    if(confirm("項目を削除、よろしいでしょうか？")==true){
        document.theform.save.disabled=true;
        document.theform.id.value = id;
        document.theform.command.value="delete";
        document.theform.submit();
    }
    else{
        return;
    }
}
/*
*    概要    ：項目を追加する使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function add(){
    document.theform.save.disabled=false;
    document.theform.id.value = "";
    document.theform.name.value="";
    document.theform.date_type.selectedIndex=0;
    document.theform.money.value=0;
    document.theform.name.focus();
    document.theform.command.value="add";
}

/*
*    概要    ：支出情報を追加する使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function addSisyutu(){
    document.theform.save.disabled=false;
    document.theform.id.value = "";
    document.theform.pName.selectedIndex=0;
    document.theform.name.value="";
    document.theform.date_type.selectedIndex=0;
    document.theform.money.value=0;
    document.theform.name.focus();
    document.theform.command.value="add";
}

/*
*    概要    ：項目を削除する使用js
*    引数    ：なし
*    戻り値  ：なし
*/
function saveitem(){
    document.theform.save.disabled=true;
    if(document.theform.name.value==""){
        document.theform.name.select();
        document.theform.save.disabled=false;
        return false;
    }

    if(document.theform.money.value!=""){
        var checkresult = checknum(document.theform.money);
        if(checkresult == false){
            document.theform.save.disabled=false;
            return false;
        }
    }
    
    document.theform.command.value="save";
    document.theform.submit();
    
    return true;
}

//-----------------------------------------------------------------------------------
//家族管理画面
//-----------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------
//機  能：家族行追加。
//戻り値：
//------------------------------------------------------------------------------------
function addrow()
{
    if(document.getElementById("timerow9").style.display!="none"){
//        alert(msg264);
        alert("最大10人！");
        return;
    }

    temptab= document.getElementById("timetable");

    for(var i=0;i<temptab.rows.length;i++)
    {
        temprow=temptab.rows[i];
        if(temprow.style.display=="none")
        {
            temprow.style.display="";
            break;
        }
    }
}

//-----------------------------------------------------------------------------------
//機  能：家族管理行削除。
//戻り値：
//------------------------------------------------------------------------------------
function delrow()
{
    var temprow_p=0;
    var tempselected=false;

    for(var i=0;i<document.getElementsByName("deletebox[]").length;i++)
    {
        if(document.getElementById("timerow"+i).style.display!="none"){
            if(document.getElementsByName("deletebox[]")[i].checked==false){
                if(temprow_p!=i)
                {
                    CopyData(i,temprow_p);
                }
                temprow_p=temprow_p+1;
            }else{
                tempselected=true;
            }
            document.getElementsByName("deletebox[]")[i].checked=false;
        }else{
            break;
        }
    }

    if(tempselected==false){
          alert("You do not select the checkbox!");
//        alert(msg265);
        return;
    }

    for(var j=temprow_p;j<10;j++)
    {
        if(document.getElementById("timerow"+j).style.display!="none"){
            InitRowData(j);
        }else{
            break;
        }
    }

    //後ろのボタン欄表示がとうか判定する。
    for(i=0;i<document.getElementsByName("deletebox[]").length;i++)
    {
        if(document.getElementById("timerow"+(i+1)).style.display=="none"){
            if(i==0){
                document.getElementById("timerow1").style.display="";
            }
            break;
        }
    }
}


//-----------------------------------------------------------------------------------
//機  能：家族データのコピーと移動。
//戻り値：
//------------------------------------------------------------------------------------
function CopyData(fromindex,toindex)
{
    document.getElementsByName("rela"+toindex)[0].selectedIndex=document.getElementsByName("rela"+fromindex)[0].selectedIndex;
    document.getElementsByName("mname"+toindex)[0].value=document.getElementsByName("mname"+fromindex)[0].value;
}

//-----------------------------------------------------------------------------------
//機  能：家族データの初期化。
//戻り値：
//------------------------------------------------------------------------------------
function InitRowData(startindex)
{
    document.getElementsByName("rela"+startindex)[0].selectedIndex=0;
    document.getElementsByName("mname"+startindex)[0].value="";
    document.getElementById("timerow"+startindex).style.display="none";

}

//-----------------------------------------------------------------------------------
//戻り値：成功の場合はtrueを、失敗の場合はfalseを返す。
//------------------------------------------------------------------------------------
function DataCheck(intmode)
{
    if(window.confirm("update?"))
    {
        document.theform.action.value=intmode;
        document.theform.submit();
    }
}

//-----------------------------------------------------------------------------------
//共通Ｓｕｂｍｉｔ
//------------------------------------------------------------------------------------
function sendForm(intmode, intitemid) {
        document.theform.action.value=intmode;
        document.theform.itemid.value=intitemid;
        document.theform.submit();
}

//-----------------------------------------------------------------------------------
//login
//-----------------------------------------------------------------------------------
function setfocus(){
    document.theform.userid.focus();
}

function sendForm1() {
    document.theform.submit();
}

function downEnter(oName) {
    var vnm =oName.name;    
    switch (vnm) {
        case "userid":
            document.theform.password.focus();
            break;
        case "password":
            if(checkpwd()==true){
                sendForm1();
            }
            break;
        default:
            return;
    }
}

function checkpwd() 
{
    var Id = "";
    var strPW = "";
    Id =document.theform.userid.value ;
    strPW =document.theform.password.value ;

    if (Id ==""){
        alert(msg100);
        document.theform.userid.focus();
        return false;
    }

    if (strPW ==""){
        alert(msg101);
        document.theform.password.focus();
        return false;
    }

    return true;
}

//-----------------------------------------------------------------------------------
//検索画面に
//------------------------------------------------------------------------------------
function showsisyutuwindow(intmode, intitemid) {
    var ret;
    window.name = "parent"; 
    ret = showModalDialog("sisyutu_changedialog.php?id=" + intitemid,window,"status:false;dialogWidth:500px;dialogHeight:440px");
    if(ret!=undefined){
        document.theform.action.value=intmode;
        document.theform.submit();
    }
}

//-----------------------------------------------------------------------------------
//頁Ｓｕｂｍｉｔ
//------------------------------------------------------------------------------------
function sendPageForm(intmode, intpage) {
    document.theform.action.value=intmode;
    document.theform.page.value=intpage;
    document.theform.submit();
}

//-----------------------------------------------------------------------------------
//項目詳細行表示・隠す
//------------------------------------------------------------------------------------
function showdetailrow(intmode) {
    var select_all =document.getElementsByName("sisyutuitem[]");

    if(intmode==0){
        document.getElementById("expendetailrow").style.display = "none";
        document.getElementById("csyunyurow").style.display = "";
        document.getElementById("csisyuturow").style.display = "none";
        for (var i=0 ;i<select_all.length; i++)
        {
            document.getElementById("tdsisyutuitemsub"+select_all[i].value).style.display = "none";
        }
    }else{
        document.getElementById("expendetailrow").style.display = "";
        document.getElementById("csyunyurow").style.display = "none";
        document.getElementById("csisyuturow").style.display = "";
        for (var i=0 ;i<select_all.length; i++)
        {
            if(document.getElementsByName("rowshowflg"+select_all[i].value)[0].value==1){
                document.getElementById("tdsisyutuitemsub"+select_all[i].value).style.display = "";
            }
        }
    }
}

//-----------------------------------------------------------------------------------
//支出項目詳細行表示・隠す
//------------------------------------------------------------------------------------
function showsisyutuitemsub(intitem) {
    var tmprow = document.getElementsByName("rowshowflg"+intitem)[0];
    if(document.getElementById("tdsisyutuitemsub"+intitem).style.display!="none"){
        tmprow.value = 0;
        document.getElementById("tdsisyutuitemsub"+intitem).style.display = "none";
		document.getElementById("btnplus" + intitem).src = "../image/b_plus.gif";
    }else{
        tmprow.value = 1;
        document.getElementById("tdsisyutuitemsub"+intitem).style.display = "";
		document.getElementById("btnplus" + intitem).src = "../image/b_minus.gif";
    }
}
//-----------------------------------------------------------------------------------
//支出項目行ALLチェック
//------------------------------------------------------------------------------------
function selectall(intpid) {
    var select_all =document.getElementsByName("sisyutuitemsub"+intpid+"[]");
    if(document.getElementById("sisyutuitem"+intpid).checked==false) {
        for (var i=0 ;i<select_all.length; i++)
        {
            select_all[i].checked=false;
        }
    }else{
        for (var i=0 ;i<select_all.length; i++)
        {
            select_all[i].checked=true;
        }
    }
}
//-----------------------------------------------------------------------------------
//支出項目詳細行チェック
//------------------------------------------------------------------------------------
function setselectall(intpid) {
    var select_all = document.getElementsByName("sisyutuitemsub"+intpid+"[]");
    var blntmp = true;

    for (var i=0 ;i<select_all.length; i++)
    {
        if(select_all[i].checked==false) {
            blntmp = false;
            break;
        }
    }

    if(blntmp == false){
        document.getElementById("sisyutuitem"+intpid).checked=false;
    }else{
        document.getElementById("sisyutuitem"+intpid).checked=true;
    }
}


