
function code(){
	return document.getElementById("_code").value;
}

function name(){
	return document.getElementById("_name").value;
}

var _records;
function setRecords(array){
	// 読込まれたレコードを記憶
	_records = array;
}
function records(){
	return _records;
}
function recordAtIndex(num){
	return _records[num];
}

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

function searched(){
	// サーバから返された処理を実行
	if ((xmlHttpObject.readyState == 4) && (xmlHttpObject.status == 200)){
		var value = xmlHttpObject.responseText;
		//alert("searched ===\n"+value);//##
		var array = value.split("<SEPARATOR>");
		if (array.length > 1){
			var objects = JSON.parse(array[1]);
			setRecords(objects);
			showResult(objects);
		}
		hideMessage("_message");
	} else 
		showMessage("_message", "サーバで処理中です ...");
}
function search(){
	// 検索キーによりデータを検索
	var code = document.getElementById("codeF").value;
	var name = document.getElementById("nameF").value;
	
	if (code.length + name.length == 0){
		alert("検索キーが指定されていません");
		return;
	}
	
    //xmlHttpObject = createXMLHttpRequest(NR_debug);
	xmlHttpObject = createXMLHttpRequest(searched);
	if (xmlHttpObject){ // GETパラメータ付きでリクエスト送信
		var array = new Array();
		array["code"] = code;
		array["name"] = name;
		
		var st = "sysServer.php?command=GET_GLOBAL_PRICE"
		+ "&value=" + encodeSTRING(encodeObject(array));
		//alert(st); //return; //===
		xmlHttpObject.open("GET", encodeURI(st), true);
		xmlHttpObject.send(null);
	}
}

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


function putPriceList(buff){
	// PriceList にレコードを返す
	window.close();
	window.opener.getGlobal(buff);
}

function clickedRow(row){
	// row 行のレコードを返送するかどうか確認パネル
	var obj = recordAtIndex(row);
	//alert(encodeObject(obj)); //##

	var elm = document.getElementById("_confirm");
	elm.innerHTML = "";
	var tbl = newTABLE(elm, "base-table");
	
	var tr = newTR(tbl, "", "コード");
	// obj.code はグローバル辞書では長いものなので検索に使われたものに変更
	obj.code = document.getElementById("codeF").value;
	var td = newTD(tr, "", obj.code);
	
	var tr = newTR(tbl, "", "請求コード");
	var td = newTD(tr, "", obj.requestCode);
	
	var tr = newTR(tbl, "", "名称");
	var td = newTD(tr, "", obj.name);
	
	var tr = newTR(tbl, "", "点数");
	var td = newTD(tr, "", obj.point);
	
	if (obj.unitName){
		var tr = newTR(tbl, "", "単位");
		var td = newTD(tr, "", obj.unitName);
	}
	
	if (obj.effectCode){
		var tr = newTR(tbl, "", "薬効コード");
		var td = newTD(tr, "", obj.effectCode);
	}
	
	if (obj.handanCode && obj.handanCode * 1){
		var tr = newTR(tbl, "", "判断料");
		// handanCode から handan を生成
		obj.handan = handan(obj.handanCode);
		var td = newTD(tr, "", obj.handan);
	}

	var tr = newTR(tbl, "", " ");
	var td = newTD(tr, "", "このレコードを PriceList へ ");
	td.style.textAlign = "right-side";
	td.style.color = "#930";
	var bt = newBUTTON(td, "", "転記");
	var action = "putPriceList('" + encodeObject(obj) + "')"
	bt.setAttribute("onclick", action);
	
	openFloatPanel(tbl, 30, 20, "確認パネル");
	
	function handan(code){
		// handanCode から判断料を返す
		switch (code * 1){
			case 1: return "尿";
			case 2: return "血";
			case 3: return "生1";
			case 4: return "生2";
			case 5: return "免";
			case 6: return "微";
			case 7: return "病";
			default: return "";
		}
	}
}

function showResult(array){
	var elm = document.getElementById("resultArea");
	elm.innerHTML = "";
    var tbl = newTABLE(elm, "base-table");
    tbl.style.fontSize = "10pt";
	
	// title
	var tr = newTR(tbl, "", "");
	tr.style.backgroundColor = "#6c9";
	var td = newTD(tr, "", "code");
	var td = newTD(tr, "", "requestCode");
	var td = newTD(tr, "", "name");
	var td = newTD(tr, "", "point");
	var td = newTD(tr, "", "unitName");
	//var td = newTD(tr, "", "handan");
    
    //alert("showResult->"+encodeObject(array)); //##
	
	// records
    for (i in array){
		var obj = array[i];
		var tr = newTR(tbl, "record", "");
		tr.setAttribute("onmouseout","resetColor(this,'#000','#fff')");
		tr.setAttribute("onmouseover","setColor(this,'#ff0','#00f')");
		var action = "clickedRow('"+i+"')";
		tr.setAttribute("onclick", action);
		
		var td = newTD(tr, "", obj.code);
		var td = newTD(tr, "", obj.requestCode);
		var td = newTD(tr, "", obj.name);
		var td = newTD(tr, "", obj.point);
		var td = newTD(tr, "", obj.unitName);
		//var td = newTD(tr, "", obj.handanCode);
	}
}

function initGlobal(){
	// PriceListGlobal からレコードを選択するパネルを表示
	var elm = document.getElementById("base");
    var div = newDIV(elm, "title-bar");
    div.style.borderBottom = "2px solid #fff";
	var tbl = newTABLE(div, "base-table");
	
	// 検索エリア
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "controlArea", "");
	var fd = newFIELD(td, "codeF", "code ", 10, code());
	var fd = newFIELD(td, "nameF", " name ", 20, name());
	fd.setAttribute("onchange","search()");
	var bt = newBUTTON(td, "", "検索");
	bt.setAttribute("onclick","search()");
	
	
	// レコード表示エリア
    var div = newDIV(elm, "resultArea");
	
	// footer
    var div = newDIV(elm, "title-bar");
	var tbl = newTABLE(div, "base-table");
	var tr = newTR(tbl, "", "");
	var td = newTD(tr,"right-side",version());
	
	// メッセージ・エリア
	var dv = newDIV(elm, "message");
}