
function dateAndId(){
	return decodeSTRING(elmFor("_dateAndId").value);
}

///////////////////////////
/// Ajax による処理 /////////

function closeEditor(answer){
    //alert("closeEditor->"+answer); //##
    
    window.close();
}
function saveRecord(){
	// 編集内容をサーバへ保存
	var patientId = document.getElementById("ed_patientId").value;
	if (patientId.length == 0) return;

    var args = new Object();
    args.owner = document.getElementById("ed_owner").value;
    args.patientId = document.getElementById("ed_patientId").value;
    args.kanjiName = document.getElementById("ed_patientKanjiName").value;
    args.entryDate = document.getElementById("ed_entryDate").value;
    args.reservedDate = document.getElementById("ed_reservedDate").value;
    args.userCode = document.getElementById("ed_userCode").value;
    args.groupCode = document.getElementById("ed_groupCode").value;
    args.inOut = (document.getElementById("ed_inOut").checked) ? "1" : "";
    args.newPatient = (document.getElementById("ed_newPatient").checked) ? "1" : "";
    args.point = document.getElementById("ed_point").value;
    args.insFee = document.getElementById("ed_insFee").value;
    args.ownFee = document.getElementById("ed_ownFee").value;
    args.receive = document.getElementById("ed_receive").value;
    args.mishuu = document.getElementById("ed_mishuu").value;
    args.duplicate = ""; // not regist_mode

    _callServer("PUT_PATIENT", "frontServer.php", args, closeEditor);
}

var _record;
function record(){
	return _record;
}
function gotRecords(answer){
    //_record = JSON.parse(answer);
    _record = eval('(' + answer + ')');

    //alert("_record->"+encodeObject(_record)); //##
    
    setEditorValues();
}
function getRecords(){
    // dateAndId に相当するレコードをサーバへリクエスト
    var array = dateAndId().split("^");
    var args = new Object();
    args.entryDate = array[0];
    args.patientId = array[1];

    _callServer("GET_PATIENT", "frontServer.php", args, gotRecords);
}

function removedOldPatient(){
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
        //_debug("gotRecords->"+value); //##
        var array = value.split("<SEPARATOR>");
        if (array.length > 1){
            alert(array[1]);
        }
	}
}
function removeOldPatient(){
	// entryDate のレコード削除をリクエスト
    var array = dateAndId().split("^");
    var args = new Object();
    args.entryDate = array[0];
    args.patientId = array[1];
    
    if (!confirm(args.patientId + " を削除していいですか")) return;
    
    window.close();
    
    _callServer("REMOVE_PATIENT", "frontServer.php", args, gotRecords);
}

/// Ajax による処理 /////////
///////////////////////////


function setEditorValues(){
	// 各フィールドに値をセット
	var rec = record();
    //_initDebug(true); //##
    _debug("->"+encodeObject(rec)); //##
    
	document.getElementById("ed_owner").value = rec.owner;
	document.getElementById("ed_entryDate").value = rec.entryDate;
	document.getElementById("ed_reservedDate").value = rec.reservedDate;
	document.getElementById("ed_patientId").value = rec.patientId;
	document.getElementById("ed_patientKanjiName").value = rec.kanjiName;
	document.getElementById("ed_groupCode").value = rec.groupCode;
	document.getElementById("ed_point").value = rec.point;
	document.getElementById("ed_insFee").value = rec.insFee;
	document.getElementById("ed_ownFee").value = rec.ownFee;
	document.getElementById("ed_userCode").value = rec.userCode;
	document.getElementById("ed_mishuu").value = rec.mishuu;
	document.getElementById("ed_receive").value = rec.receive;
    // 入院/外来チェックボックス
    var elm = elmFor("inOutArea");
    elm.innerHTML = "";
	var status = (rec.inOut && rec.inOut.length) ? 1 : 0;
    var cb = newCHECKBOX(elm, "ed_inOut", "入院", status);
    // 新患/再来チェックボックス
    var elm = elmFor("newPatientArea");
    elm.innerHTML = "";
	var status = (rec.newPatient && rec.newPatient.length) ? 1 : 0;
    var cb = newCHECKBOX(elm, "ed_newPatient", "新患", status);
    
    // 削除ボタン
	if (rec){
        var elm = elmFor("removeButtonArea");
        elm.innerHTML = "";
        var bt = newDIV(elm, "/whiteButton");
        bt.innerHTML = "削除";
		bt.setAttribute("onclick", "removeOldPatient()");
	}
}

function initPatientEditor(){
	// front から該当レコード属性を取得
//	setRecord();
	
	var base = document.getElementById("base");
    
    // HEADER ==========================================
    var div = newDIV(base, "/yellow-header");
    div.innerHTML = "編集パネル";
    div.style.textAlign = "center";
    div.style.fontSize = "9pt";
    
    // CONTENTS ========================================
	var tbl = newTABLE(base, "/base-table");
    tbl.style.border = "thin solid #aaa";
    tbl.style.fontSize = "9pt";
    tbl.style.backgroundColor = "#ec9";
    
    editorField(tbl, "ed_entryDate", "受付時刻", "", false);
	editorField(tbl, "ed_reservedDate", "予約日時", "", false);
	editorField(tbl, "ed_owner", "取扱者", "", true);
	editorField(tbl, "ed_userCode", "ユーザコード", "", true);
	editorField(tbl, "ed_patientId", "カルテID", "", true);
	editorField(tbl, "ed_patientKanjiName", "氏名", "", false);
	editorField(tbl, "ed_groupCode", "保険種別", "", false);
    var tr = newTR(tbl,"","");
    var td = newTD(tr, "", "入院/外来");
    td.style.paddingLeft = "5px";
    var td = newTD(tr, "inOutArea", "");
    var tr = newTR(tbl,"","");
    var td = newTD(tr, "", "新患/再来");
    td.style.paddingLeft = "5px";
    var td = newTD(tr, "newPatientArea", "");
	editorField(tbl, "ed_point", "点数", "", false);
	editorField(tbl, "ed_insFee", "一部負担金", "", false);
	editorField(tbl, "ed_ownFee", "自費", "", false);
	editorField(tbl, "ed_mishuu", "未収", "", false);
	editorField(tbl, "ed_receive", "受取額", "", false);

    // FOOTER ==========================================
    var div = newDIV(base, "/yellow-footer");
    div.style.fontSize = "9pt";
    var dv = newDIV(div, "/right-side");
    dv.style.paddingRight = "5px";
    // とりやめボタン
    var bt = newDIV(dv, "/whiteButton");
    bt.innerHTML = "とりやめ";
	bt.setAttribute("onclick", "window.close()");
    // 削除ボタン・エリア
    var sp = newSPAN(dv, "removeButtonArea");
    // 確定ボタン
    var bt = newDIV(dv, "/fixButton");
    bt.innerHTML = "確定";
	bt.setAttribute("onclick", "saveRecord()");
    
    getRecords();
    
	
	function editorField(tbl, id, label, val, disabled){
		var tr = newTR(tbl,"","");
		var td = newTD(tr, "", label);
        td.style.paddingLeft = "5px";
		var td = newTD(tr, "", "");
		var ip = newFIELD(td, id, "", 23, val);
		if (disabled) ip.setAttribute("disabled", true);
	}
	
	function makeTRCheckBox(tbl, cellId, label, buttonLabel, status, width){
		var tr = newTR(tbl,"ed_field",label);
		var td = newTD(tr,"","");
		var ip = newCHECKBOX(td, cellId, buttonLabel, status);
		return ip;
	}
}
