
function kinds(){
    // 筋腫の配列を返す
    return [1, 5, 10, 50, 100, 500, 1000, 5000, 10000];
}

function beginningId(){
    return "0";
}
function endingId(){
    return "1";
}

var _history;
function setHistory(array){
    _history = array;
}
function history(){
    return _history;
}

var _staffList;
function setStaffList(array){
    // 職員リストをセット
    //alert("setStaffList->"+encodeObject(array)); //##
    var staffs = new Array();
    // チェックのため、リストから空の氏名を取り除く
    for (num in array){
        var name = trim(array[num]);
        if (name.length) staffs.push(name);
    }
    if (staffs.length == 0){
        alert("職員名が登録されていませんので登録が必要です");
        openPreference();
        return;
    }
    
    staffs.splice(0,0,""); // 最初に空アイテムを挿入
    _staffList = staffs;
}
function staffList(){
    return _staffList;
}

var _record;
function setRecord(obj){
 //   _initDebug(true); //##
    _record = obj;
    
//    _debug("setRecord->"+encodeObject(obj)); //##
//    _debug("payment===>"+obj.payment); //##
//    _debug("income===>"+obj.income); //##
    
    setPayArray(obj.payment);
    setIncomeArray(obj.income);
}
function record(){
    return _record;
}

var _currentDate;
function setCurrentDate(date){
    _currentDate = date;
}
function currentDate(){
    // 始業点検を行った時刻を返す
    if (!_currentDate) alert("currentDate がセットされていません"); //##
    return _currentDate;
}

/////////////////////////////////////////////////////////
///// Ajax //////////////////////////////////////////////

function checkedDB(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
		//alert("checkedDB ===\n"+value);//###
        
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
            var st = trim(array[1]);
            if (st.length > 0){
                var msg = "--- MoneyTable が生成されました ---<br>" + st;
                showFadeoutInfo("_message", msg, 1000); // dom.js
            }
		}
        getHistory();
	}
}
function checkDB(){
	// データベースをチェック
	xmlHttpObject = createXMLHttpRequest(checkedDB);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
		var st = "./monyServer.php?command=CREATE_TABLE_IF_NEEDED";
		
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
	}
}

function gotHistory(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
		//alert("gotHistory ===\n"+value);//###
        
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
            var obj = eval('(' + array[1] + ')');
            setHistory(obj.history);
            setStaffList( obj.staffs);
            showHistory();
		}
	}
}
function getHistory(){
	// データベースをチェック
	xmlHttpObject = createXMLHttpRequest(gotHistory);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
        var args = new Array();
        args['entryDate'] = "2011-07-01";
        
		var st = "./monyServer.php?command=GET_HISTORY";
		
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
	}
}

function saved(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
		//alert("saved ===\n"+value); //###
        
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
            //document.getElementById("_message").innerHTML = array[1];
            showFadeoutInfo("_message", array[1], 1000); // dom.js
        }
	}
}
function save(id){
    // id のペーンを保存
	xmlHttpObject = createXMLHttpRequest(saved);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
        var obj = new Object();
        var array = kinds();
        for (num in array){
            var kind = array[num];
            var cid = kind + "." + id;
            var val = document.getElementById(cid).value;
            if ((val.length > 0) && (val * 1 > 0)){
                // 枚数があるなら obj に金種ごとの枚数を記憶
                obj[kind] = val;
            }
        }
        var args = new Array();
        var staff1 = trim(document.getElementById("user." + beginningId()).value);
        var staff2 = trim(document.getElementById("user." + endingId()).value);
        if (id == beginningId()){
            args['val1'] = encodeObject(obj);
            args['staff1'] = staff1;
            if (staff1.length == 0){
                alert("始業点検の取扱者氏名を入力してください");
                return;
            }
        } else {
            args['val2'] = encodeObject(obj);
            args['staff2'] = staff2;
            if (staff1.length == 0){
                alert("始業点検の取扱者氏名を入力してください");
                return;
            }
            if (staff2.length == 0){
                alert("終業点検の取扱者氏名を入力してください");
                return;
            }
        }
        args['memo'] = document.getElementById("memoF").value;
        args['entryDate'] = currentDate();
        
        args['income'] = encodeObject(incomeArray());
        args['payment'] = encodeObject(payArray());
        //        args['income'] = object2json(incomeArray());
        //        args['payment'] = object2json(payArray());

		var st = "./monyServer.php?command=PUT_MONY";
        st += "&value=" + encodeObject(args);

//        _initDebug(true); //##
//        _debug(st); //return; //##
		
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
    }
}

function gotMoney(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
//		alert("gotMoney ===\n"+value); //###
        
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
//            alert("buff ->"+array[1]); //###

            var obj = eval("(" + array[1] + ")");
//            alert("gotMoney ===\n"+encodeObject(obj)); //###

            setRecord(obj);
            makePanes(record());
		}
	}
}
function getMoney(entryDate){
    // entryDate の点検レコードをサーバへリクエスト
	xmlHttpObject = createXMLHttpRequest(gotMoney);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
        // スタッフリストから空行を削除
        var args = new Array();
        args['entryDate'] = entryDate;
        
		var st = "./monyServer.php?command=GET_MONY";
        st += "&value=" + encodeObject(args);
        
        //alert(st); //return; //##
		
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
	}
}

function savedStaffs(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
		//alert("savedStaffs ===\n"+value); //###
        
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
            var st = array[1];
            var array = st.split(",");
            setStaffList(array);
            closeFloatPanel();
		}
	}
}
function saveStaffs(){
    // 職員リストをサーバへ保存
	xmlHttpObject = createXMLHttpRequest(savedStaffs);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
        // スタッフリストから空行を削除
        var staffs = new Array();
        var buff = document.getElementById("staffF").value;
        var array = buff.split("\n");
        for (num in array){
            var name = trim(array[num]);
            if (name.length) staffs.push(name);
        }
        
        var args = new Array();
        args['staffs'] = staffs.join(",");
        
		var st = "./monyServer.php?command=PUT_STAFFS";
        st += "&value=" + encodeObject(args);
        
        //alert(st); //return; //##
		
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
	}
}

///// Ajax //////////////////////////////////////////////
/////////////////////////////////////////////////////////

function puttedWindowSize(){
    var json = window.localStorage["moneyCheckSize"];
    if (!json) return;
    
    var array = JSON.parse(json);

    if (array.length > 3){
        var x = array[0];
        var y = array[1];
        var w = array[2];
        var h = array[3];
        window.resizeTo(w, h); // resizeTo を先に実行すること
        window.moveTo(x, y);
    }
}
function putWindowSize(){
	// DocMaker のウインドー位置とサイズをサーバへ記憶
    // ウインドー・タイトルの高さ 22 分を補正
    // ## FireFox では screenLeft, screenTop が機能しない ##
    var w = window;
    var array = [w.screenLeft, w.screenTop-22, w.outerWidth, w.outerHeight];
    var json = JSON.stringify(array);
    window.localStorage["moneyCheckSize"] = json;
    
    // フェードアウト文字列で確認メッセージ
    showFadeoutInfo("_message", "ウインドーのサイズと位置を記憶しました", 1000);
}

function selectedHistory(elm){
    // ログのポップアップが選択された
    var dateTime = trim(elm.value);
    
    if (dateTime.length > 0){
        setCurrentDate(dateTime);
        getMoney(dateTime);
    } else {
        firstMessage();
    }
}
function showHistory(){
    // 過去ログを表示
    var array = history();
    array.splice(0,0,"");
    
    var elm = document.getElementById("datePopArea");
    elm.innerHTML = "";
	var pm = makePopupMenu(elm, "", array, "");
    pm.setAttribute("onchange", "selectedHistory(this)");
    pm.style.fontSize = "14pt";
}

function endingCashTotal(){
    // 終業時現金計を返す
    var total = 0;
    var id = "1";
    
    // 元金を計算
    var array = kinds();
    for (num in array){
        var kind = array[num];
        var cid = kind + "." + id;
        var val = document.getElementById(cid).value;
        if ((val.length > 0) && (val * 1 > 0)){
            total += val * kind;
        }
    }
    return total;
}

function calc(id, doSave){
    // 合計を算出
	var user = document.getElementById("user." + id).value;
    if (doSave && (user.length == 0)){
        alert("取扱者の氏名を入力してください ");
        return;
    }

    if (id == endingId()){
        // 収支差額を算出
        calcPane(beginningId());
        calcPane(endingId());
        showTotalDatail();
        
    } else {
        calcPane(beginningId());
        var firstCost = document.getElementById("total.0").innerHTML * 1;
        var elm = document.getElementById("firstCost");
        if (elm) elm.innerHTML = firstCost;
    }
    if (doSave) save(id);
    
    function calcPane(id){
        // id の元金を計算
        var total = 0;
        var array = kinds();
        for (num in array){
            var kind = array[num];
            var cid = kind + "." + id;
            var val = document.getElementById(cid).value;
            if ((val.length > 0) && (val * 1 > 0)){
                total += val * kind;
                var mid = kind + "_" + id;
                document.getElementById(mid).innerHTML = "= "+ val * kind + " 円";
            }
        }
        var elm = document.getElementById("total."+id);
        if (elm) elm.innerHTML = total;
        
        // 収支計算エリアが存在すれば収支計算を実行
        var elm = document.getElementById("totalDetailArea");
        if (elm) showTotalDatail();
    }
    
    function showTotalDatail(){
        // 総計明細を表示
        var elm = document.getElementById("totalDetailArea");
        elm.innerHTML = "";
        var tbl2 = newTABLE(elm, "baseTable");
        
        // 臨時収入明細 ////////////////////////
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.style.padding = "0px 10px";
        td.style.textAlign = "right";
        var bt = newBUTTON(td, "", "収入欄を追加");
        bt.setAttribute("onclick", "addNewIncome()");
        // 収入適用・金額エリア
        var td = newTD(tr, "incomeArea", "");
        td.setAttribute("colspan","2");
        var income = showIncome() * 1;
        
        // セパレータ
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.setAttribute("colspan","2");
        var hr = newHR(td);
        
       // 支出明細 /////////////////////////////
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.style.padding = "0px 10px";
        td.style.textAlign = "right";
        var bt = newBUTTON(td, "", "支出欄を追加");
        bt.setAttribute("onclick", "addNewPayment()");
        // 支出適用・金額エリア
        var td = newTD(tr, "payArea", "");
        td.setAttribute("colspan","2");
        var payment = showPayments() * 1;

        // セパレータ
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.setAttribute("colspan","2");
        var hr = newHR(td);
        
        // 収支計算ボタン ///////////////////////
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "label", "");
        td.style.padding = "0px 5px";
        var bt = newBUTTON(td, "", "収支計算");
        bt.setAttribute("onclick", "calc('"+id+"',true)");
        var td = newTD(tr, "", "");
        var tbl3 = newTABLE(td, "baseTable");
        
        var firstCost = document.getElementById("total.0").innerHTML * 1;
        var lastCost = endingCashTotal() * 1;
        
        // 始業点検
        var tr = newTR(tbl3, "", "");
        var td = newTD(tr, "sumLabel", "始業時現金");
        var td = newTD(tr, "cost", "");
        var sp = newSPAN(td, "firstCost");
        sp.innerHTML = firstCost;
        var sp = newSPAN(td, "");
        sp.innerHTML = " 円";
        var td = newTD(tr, "mark", "+");
        
        // 外来収入
        var tr = newTR(tbl3, "", "");
        var td = newTD(tr, "sumLabel", "外来収入");
        var medicalIncome = lastCost + payment - firstCost - income;
        var td = newTD(tr, "cost", medicalIncome + " 円");
        var td = newTD(tr, "mark", "+");
        
        // 臨時収入
        var tr = newTR(tbl3, "", "");
        var td = newTD(tr, "sumLabel", "臨時収入");
        var td = newTD(tr, "cost", income + " 円");
        var td = newTD(tr, "mark", "+");
       
        // 支出
        var tr = newTR(tbl3, "", "");
        var td = newTD(tr, "sumLabel", "支出");
        var td = newTD(tr, "cost", "");
        var sp = newSPAN(td, "payment");
        sp.innerHTML = payment;
        var sp = newSPAN(td, "");
        sp.innerHTML = " 円";
        var td = newTD(tr, "mark", "-");
        
        // 終業
        var tr = newTR(tbl3, "", "");
        var td = newTD(tr, "sumLabel", "終業時現金");
        var td = newTD(tr, "cost", "");
        var sp = newSPAN(td, "lastCost");
        sp.innerHTML = lastCost;
        var sp = newSPAN(td, "");
        sp.innerHTML = " 円";
        var td = newTD(tr, "mark", "");
    }
}

function userSelected(elm, id){
    // 選択されたユーザ名をフィールドへ転記
    var name = elm.value;
    document.getElementById("user." + id).value = name;
}

//////////////////////////////////////////////
// 収入 ///////////////////////////////////////

var _incomeObj;
function setIncomeArray(json){
    // load された JSON から payArray を抽出
    _incomeObj = new Array();
    if (json.length == 0) return;

    var array = decodeObject(json);
    
    // 直接 _incomeObj = eval('(' + json + ')') 形式だと、どういう訳か
    // 後で addIncomeArray(title, cost) がうまくいかないので _incomeObj へ追加する
    for (num in array){
        var obj = array[num];
        addIncomeArray(obj.title, obj.cost);
    }
}

function addIncomeArray(title, cost){
    if (!_incomeObj) _incomeObj = new Array();
    var obj = new Object();
    obj.title = title;
    obj.cost = cost;
    _incomeObj.push(obj);
}

function incomeArray(){
    // 収入オブジェクトを返す
    return _incomeObj;
}

function addNewIncome(){
    // 新しい収入入力欄を追加
    addIncomeArray("", "");
    showIncome();
}

function incomeTitleChanged(num){
    // num 番目のタイトルが更新された
    var obj = incomeArray()[num];
    obj.title = document.getElementById("inTitle." + num).value;
}

function incomeChanged(num){
    // num 番目の金額が更新された
    var obj = incomeArray()[num];
    obj.cost = document.getElementById("inCost." + num).value;
    
    // 収入合計を再計算し支出ペーンを再表示
    var income = showIncome();
    document.getElementById("incomeTotal").innerHTML = income;
}

function removeIncome(num){
    // 収入欄の num 行目を削除
	var array = incomeArray();
    delete array[num];
    showIncome();
}

function showIncome(){
    // 収入欄を表示
    var income = 0;
	var elm = document.getElementById("incomeArea");
    elm.innerHTML = "";
	var tbl = newTABLE(elm, "baseTable");
    
	var array = incomeArray(); //---
    for (num in array){
        var obj = array[num];
        if (!obj) continue; 
        
        var tr = newTR(tbl, "", "");
        tr.style.fontSize = "10pt";
        var td = newTD(tr, "", "");
        var fd = newFIELD(td, "inTitle." + num, "摘要", 25, obj.title);
        fd.setAttribute("onchange", "incomeTitleChanged('" + num + "')");
        fd.focus();
        var fd = newFIELD(td, "inCost." + num, "", 6, obj.cost);
        fd.setAttribute("onchange", "incomeChanged('" + num + "')");
        var tx = newTEXT(td, "円 ");
        var img = newIMAGE(td, "", "./close.png", "remove");
        img.style.height = "12px";
        img.setAttribute("onclick", "removeIncome('" + num + "')");

        income += obj.cost * 1;
    }

    // 収入合計
    var tr = newTR(tbl, "", "");
    var td = newTD(tr, "incomeTotal", "臨時収入合計 " + income + " 円");
    td.style.fontSize = "14pt";
    
    return income;
}

// 収入 ///////////////////////////////////////
//////////////////////////////////////////////

//////////////////////////////////////////////
// 支出 ///////////////////////////////////////

var _payObj;
function setPayArray(json){
    // load された JSON から payArray を抽出
    _payObj = new Array();
    if (json.length == 0) return;

    var array = decodeObject(json);
    
    // 直接 _payObj = eval('(' + json + ')') 形式だと、どういう訳か
    // 後で addPayArray(title, cost) がうまくいかないので _payObj へ追加する
    for (num in array){
        var obj = array[num];
        addPayArray(obj.title, obj.cost);
    }
}
function addPayArray(title, cost){
    if (!_payObj) _payObj = new Array();
    var obj = new Object();
    obj.title = title;
    obj.cost = cost;
    _payObj.push(obj);
}
function payArray(){
    // 支出オブジェクトを返す
    return _payObj;
}

function addNewPayment(){
    // 新しい支出入力欄を追加
    addPayArray("", "");
    showPayments();
}

function titleChanged(num){
    // num 番目のタイトルが更新された
    var obj = payArray()[num];
    obj.title = document.getElementById("title." + num).value;
}

function costChanged(num){
    // num 番目の金額が更新された
    var obj = payArray()[num];
    obj.cost = document.getElementById("cost." + num).value;
    
    // 支出合計を再計算し支出ペーンを再表示
    var payment = showPayments();
    document.getElementById("payment").innerHTML = payment;
}

function removePay(num){
    // 支出欄の num 行目を削除
	var array = payArray();
    delete array[num];
    showPayments();
}

function showPayments(){
    // 支出欄を表示
    var payment = 0;
	var elm = document.getElementById("payArea");
    elm.innerHTML = "";
	var tbl = newTABLE(elm, "baseTable");
    
//    _initDebug(true); //##
    
	var array = payArray();
    for (num in array){
        var obj = array[num];
        if (!obj) continue; 
        
//        _debug(num+"->"+obj.title); //##
        
        var tr = newTR(tbl, "", "");
        tr.style.fontSize = "10pt";
        var td = newTD(tr, "", "");
        var fd = newFIELD(td, "title." + num, "摘要", 25, obj.title);
        fd.setAttribute("onchange", "titleChanged('" + num + "')");
        fd.focus();
        var fd = newFIELD(td, "cost." + num, "", 6, obj.cost);
        fd.setAttribute("onchange", "costChanged('" + num + "')");
        var tx = newTEXT(td, "円 ");
        var img = newIMAGE(td, "", "./close.png", "remove");
        img.style.height = "12px";
        img.setAttribute("onclick", "removePay('" + num + "')");
        
        payment += obj.cost * 1;
    }
    
    // 支出合計
    var tr = newTR(tbl, "", "");
    var td = newTD(tr, "payTotal", "支出合計 " + payment + " 円");
    td.style.fontSize = "14pt";
    
    return payment;
}

// 支出 ///////////////////////////////////////
//////////////////////////////////////////////

function makePane(id, record){
    // 始業点検または就業点検ペーンを生成
    var isBegin = (id == beginningId()) ? true : false;
	var elm = document.getElementById(id);
    elm.innerHTML = "";
    
	var tbl = newTABLE(elm, "baseTable");
	
	// header
    var date = (currentDate()) ? currentDate() : "";
    var title = (isBegin) ? date + "　始業点検" : "終業点検";
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "paneHeader", title);
	
	// contents
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "", "");
	var tbl2 = newTABLE(td, "baseTable");
   
    // 取扱者
    var tr = newTR(tbl2, "", "");
    var td = newTD(tr, "label", "取扱者");
    var td = newTD(tr, "", "");
    var staffs = staffList();
	var pm = makePopupMenu(td, "", staffs, "");
    pm.setAttribute("onchange", "userSelected(this, '" + id + "')");
    var staff1 = (record) ? record.staff1 : "";
    var staff2 = (record) ? record.staff2 : "";
    var staff = (isBegin) ? staff1 : staff2;
    var fd = newFIELD(td, "user." + id, "", 20, staff);
    
    //alert(encodeObject(record)); //##
    
    // 金種ごとの枚数
    var st = "";
    var kindObj = null;
    if (record){
        st = (isBegin) ? record.value1 : record.value2;
        kindObj = decodeObject(st);
    }
    
    var array = kinds();
    for (num in array){
        var kind = array[num];
        makeLine(tbl2, kindObj, kind, id);
    }

    // 収支差額
    if (id != endingId()){ // 始業点検        
        // 合計
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "label", "");
        var bt = newBUTTON(td, "", "現金合計");
        var action = (isBegin) ? "calc('"+id+"',true)" : "calc('"+id+"',true)";
        bt.setAttribute("onclick", action);
        var td = newTD(tr, "", "");
        td.style.padding = "0px 10px";
        var sp = newSPAN(td, "total." + id); 
        var tx = newTEXT(td, "円");
        
        // セパレータ
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.setAttribute("colspan","2");
        var hr = newHR(td);
        
        // メモ
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "label", "メモ");
        var td = newTD(tr, "", "");
        var memo = (record) ? record.memo : "";
        var fd = newTEXTAREA(td, "memoF", 30, 5, memo);
    } else { // 終業点検
        // 合計
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "label", "現金合計");
        var td = newTD(tr, "", "");
        td.style.padding = "0px 10px";
        var sp = newSPAN(td, "total." + id); 
        var tx = newTEXT(td, "円");
        
        // セパレータ
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "", "");
        td.setAttribute("colspan","2");
        var hr = newHR(td);
        
        // 収入欄・支出欄の表示は calc() で行う

        // 総計明細表示エリア
        var tr = newTR(tbl2, "", "");
        var td = newTD(tr, "totalDetailArea", "");
        td.setAttribute("colspan","2");
    }

    // もしレコードがあるなら合計を計算
    if (record) calc(id);

    function makeLine(tbl, obj, kind, id){
        var val = "";
        if (obj && obj[kind]) val = obj[kind];

        var tr = newTR(tbl, "", "");
        var td = newTD(tr, "label", kind + "円");
        var td = newTD(tr, "", "");
        var fd = newFIELD(td, kind+"."+id, "", 2, val);
        fd.style.fontSize = "14pt";
        var tx = newTEXT(td, "枚 ");
        var sp = newSPAN(td, kind+"_"+id);
    }
}

function makePanes(record){
    // 新規始業点検ページを生成
//    alert("makePanes:"); //##
	var elm = document.getElementById("panesArea");
    elm.innerHTML = "";

//    alert("makePanes:"+encodeObject(record)); //##

    var tbl = newTABLE(elm, "frameTable");
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "pane", "");
    var dv = newDIV(td, beginningId());
	var td = newTD(tr, "pane", "");
    var dv = newDIV(td, endingId());
    
    if (!record) setCurrentDate(todayAndTime());
    
//    alert("beginningId:"+beginningId()); //##
    
    makePane(beginningId(), record);
    makePane(endingId(), record);
}

function openPreference(){
    // 初期設定パネルを開く
	var elm = document.getElementById("flaotArea");
	elm.innerHTML = "";
	var tbl = newTABLE(elm, "");
	tbl.style.fontSize = "9pt";
	tbl.style.textAlign = "right";
    
    // スタッフリストから空行を削除
    var staffs = new Array();
    var array = staffList();
    for (num in array){
        var name = trim(array[num]);
        if (name.length) staffs.push(name);
    }
	
	// staff 編集エリア
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "", "");
    var fd = newTEXTAREA(td, "staffF", 20, 10, staffs.join("\n"));
 	var td = newTD(tr, "", "");
    td.style.textAlign = "left";
    td.style.verticalAlign = "top";
    td.style.color = "#a63"; // brown
    td.innerHTML = "職員リストを編集してください<br>職員名は一行ずつ登録します";
   
    // 全ページ印刷ボタン
    var tr = newTR(tbl, "", "");
	var td = newTD(tr, "", "");
	var td = newTD(tr, "", "");
	var bt = newBUTTON(td, "", "登録");
    bt.setAttribute("onclick", "saveStaffs()");
    
	openFloatPanel(tbl, 0, 0, "職員リスト編集パネル");
}

function help(){
	var win = window.open("./help.html","Help"
						  ,"width=450,height=700,scrollbars=yes,resizable=yes");
	win.focus();
}

function firstMessage(){
    // 初期のメッセージ画面を生成
	var elm = document.getElementById("panesArea");
    elm.innerHTML = "";
    var dv = newDIV(elm, "");
    dv.style.width = "350px";
    dv.style.color = "#a63";
    dv.innerHTML = "<p>　午前・午後・夜間などの診療時間帯ごとに始業・終業点検を行う場合は、診療時間帯が異なるごとに、新規の始業点検から開始します</p> ";
    var dv = newDIV(elm, "");
    dv.style.width = "350px";
    dv.innerHTML = "<p>　すでに今回の始業点検の記録がある場合は、上のポップアップメニューから始業点検を行った年月日を選択してください。</p>";
    var dv = newDIV(elm, "");
    dv.style.width = "350px";
    dv.style.marginBottom = "10px";
    dv.innerHTML = "　始業点検記録がない場合は新規に ";
    var bt = newBUTTON(dv, "", "始業点検");
    bt.setAttribute("onclick", "makePanes()");
}

function initMoneyCheck(){
	var elm = document.getElementById("base");
	var tbl = newTABLE(elm, "baseTable");
	
	// header
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "header", "");
	var tbl1 = newTABLE(td, "baseTable");
	var tr = newTR(tbl1, "", "");
	var td = newTD(tr, "", "点検履歴 ");
    var sp = newSPAN(td, "datePopArea");
    // help icon
	var td = newTD(tr, "", "");
    td.style.width = "10px";
    var img = newIMAGE(td, "", "./help.png", "map");
    img.style.height = "20px";
    img.setAttribute("onclick", "help()");
    // ウインドー・サイズ記憶
    var td = newTD(tr, "", "");
    td.style.width = "10px";
    var img = newIMAGE(td, "", "zoomfit.png", "zoom");
    img.style.width = "18px";
    img.setAttribute("onclick","putWindowSize()");
    // preference icon
	var td = newTD(tr, "", "");
    td.style.width = "10px";
    var img = newIMAGE(td, "", "./hammer.png", "map");
    img.style.height = "15px";
    img.setAttribute("onclick", "openPreference()");

    // floating panel area
    var tr = newTR(tbl, "", "");
	var td = newTD(tr, "flaotArea", "");

	// contents
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "panesArea", "");
    firstMessage();
	
	// footer
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "footer", version());
	
	var dv = newDIV(elm, "message");
	var dv = newDIV(elm, "_debug");
    
    checkDB();
    puttedWindowSize();
}

function version(){
    return "Ver.121209";
}