
var _hospitalTable;
function setHospitalTable(obj){
    // obj = {"大橋医院":"1304000000077","OCEAN VIEW CLINIC":"03234200",,}
	_hospitalTable = obj;
}
function hospitalTable(){
	// obj[hospitalName] = hospitalId のようなオブジェクト
	return _hospitalTable;
}
function hospitalIdArray(){
    var array = new Array();
    for (hospName in _hospitalTable){
        array.push(_hospitalTable[hospName]);
    }
	return array;
}
function hospitalNameForId(id){
	var obj = hospitalTable();
	for (name in obj){
		if (id == obj[name])
			return name;
	}
	return "??";
}

var _hospId;
function setHospId(id){
	_hospId = id;
}
function hospId(){
	return _hospId;
}

///////////////////////////////////////////////////////////////
///// guest ユーザ用テンプレート /////////////////////////////////

function userTemplate(){
    var obj = new Object();
    obj.userCode = "write";
    obj.sectionCode = "00";
    obj.groupCode = "doctor";
    obj.loginName = "guest";
    obj.owner = "guest";
    obj.kanjiName = "野安　巧";
    obj.kanaName = "のあ　たくみ";
    obj.hospitalId = "012345";
    obj.hospitalName = "SEA SIDE CLINIC"; // hospitalTable 不要：直接登録
    return obj;
}

///// guest ユーザ用テンプレート /////////////////////////////////
///////////////////////////////////////////////////////////////


function logined(answer){
	// neron.js からの返答を実行
	if (answer.length){
		answer = decodeSTRING(answer);
		//alert("logined->"+answer); //##
		if (answer.indexOf("ERROR:") >= 0){
			document.getElementById("alert").innerHTML = answer;
			document.getElementById("userName").focus();
		} else {
            // カルテ画面を開く
            window.location.replace("./top.php");
        }
	} else {
		document.getElementById("passwd").value == ""; // うまく機能しない
		alert("passwd を設定してください");
		setPasswd();
	}
}
function login(){
	// ログイン処理を実行
	var name = document.getElementById("userName").value;
	var pwd = document.getElementById("passwd").value;
    
    // ここで施設名が決まるので hospitalObject() に記憶
    var obj = hospitalObject();
//    alert("hospitalObject->"+encodeObject(obj)); //return; //##

    obj.hospitalName = _hospitalName;
//    obj.hospitalName = document.getElementById("hospitalNamePop").value;
//    alert("obj.hospitalName->"+obj.hospitalName); //return; //##
    
    setHospitalObject(obj); // localStorage.js

//    alert("login->"+encodeObject(obj)); //return; //##

    if ((name == "guest") && (pwd == "guest")){
        window.location.replace("./top.php");
    } else {
        NRLogin(name, pwd, logined); // neuron.js
    }
}

var _pwd;
function reportPassword(answer){
	// パスワードが設定された
	if (answer.length && (answer.indexOf("ERROR") >= 0))
		alert("ERROR: パスワードの設定ができませんでした"); // ERROR
}
function setPasswd(){
	// パスワードを設定
	var pwd = prompt("パスワードを設定してください");
	if (pwd.length){
		_pwd = pwd;
		pwd = prompt("確認のためパスワードをもう一度入力してください");
		if (_pwd != pwd)
			alert("パスワードが違っています");
		else {
			var name = document.getElementById("userName").value;
			NRPutPassword(name, pwd, reportPassword); // neuron.js
		}
	}
}

var _hospitalName;
function showHospitalName(label){
	// 施設名を表示し login ボタンを表示
	var obj = hospitalTable();
	var hid = "??";
	for (name in obj){
		if (name == label)
			hid = obj[name];
	}
	setHospId(hid); // hid を記憶
    _hospitalName = label;
    
	var tr = document.getElementById("hospitalNameArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "");
    td.setAttribute("colspan", "3");
    td.style.textAlign = "center";
    td.innerHTML = "施設 ID：" + hid;
	
    var sp = newSPAN(td, "");
    sp.style.paddingLeft = "20px";
	var bt = newBUTTON(td, "", "open NOA");
	bt.setAttribute("onclick", "login()");
	bt.focus(); // ログイン・ボタンに fucus
	
	var tr = document.getElementById("controlArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "");
    td.setAttribute("colspan", "3");
    td.style.textAlign = "center";
	td.style.fontSize = "10pt";
	var a = newA(td, "passwd 変更", "#", "");
	a.setAttribute("onclick", "setPasswd()");
}
function _showHospitalName(label){
	// 施設名を表示し login ボタンを表示
	var obj = hospitalTable();
	var hid = "??";
	for (name in obj){
		if (name == label)
			hid = obj[name];
	}
	setHospId(hid); // hid を記憶
    _hospitalName = label;
    
    //    alert(" showHospitalName->"+label+"->"+hospId()); //##
	
	var tr = document.getElementById("hospitalNameArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "　"); // left side
	var td = newTD(tr, "hospitalTD", "施設 ID ");
    td.style.textAlign = "right";
	var td = newTD(tr, "hospitalId", hid);
	td.style.fontSize = "10pt";
	td.setAttribute("colspan", "2");
	
    var sp = newSPAN(td, "");
    sp.style.paddingLeft = "20px";
	var bt = newBUTTON(td, "", "open NOA");
	bt.setAttribute("onclick", "login()");
	bt.focus(); // ログイン・ボタンに fucus
	
	var tr = document.getElementById("controlArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "　"); // left side
	var td = newTD(tr, "", "");
	var td = newTD(tr, "", "");
	td.style.fontSize = "10pt";
	var a = newA(td, "passwd 変更", "#", "");
	a.setAttribute("onclick", "setPasswd()");
}

function clickedHospital(elm){
	// 施設名ポップアップが選択された
	showHospitalName(elm.value);
}

function selectHospital(array){
	// ユーザの所属する施設名を選択
	// 所属施設が複数ある場合はポップアップメニューで選択
	var tr = document.getElementById("hospitalPopupArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "");
    td.setAttribute("colspan", "3");
    td.style.textAlign = "center";
    
	var obj = hospitalTable();
	if (array.length == 1){
		for (name in obj){
			var hid = obj[name];
			if (hid == array[0]){
                td.innerHTML = "施設名：" + name;
				showHospitalName(name);
			}
		}
	} else {
		// popup を表示
		var names = allKeys(obj);
        td.innerHTML = "施設名 ";
		var pu = makePopupMenu(td, "hospitalNamePop", names, "");
		pu.setAttribute("onchange", "clickedHospital(this)");
        
        showHospitalName(names[0]);
	}
}

function gotHospitalId(answer){
	// neron.js からの返答を実行
	if (answer.length){
		answer = decodeSTRING(answer);

		if (answer.indexOf("ERROR:") >= 0){
			document.getElementById("alert").innerHTML = answer;
			document.getElementById("userName").focus();
		} else {
			var obj = JSON.parse(answer);
            
            //_initDebug(false); //##
            //_debug("gotHospitalId->"+encodeObject(obj)); //##
            
            //var recs = obj['hospitalTable'];

			// hospitalName と hospitalId のテーブルを記憶
            setHospitalObject(obj); // localStorage.js
			setHospitalTable(obj.hospitalTable);
            
			var array = hospitalIdArray();
			selectHospital(array);
		}
	} else {
        // password がサーバに設定されていないので新たに設定させる
		document.getElementById("passwd").value == ""; // うまく機能しない
		alert("passwd を設定してください");
		setPasswd();
	}
}
function getHospitalId(){
	// ログイン処理を実行
	var name = document.getElementById("userName").value;
	var pwd = document.getElementById("passwd").value;
    if ((name == "guest") && (pwd == "guest")){
        var obj = userTemplate();

        //alert("getHospitalId->"+encodeObject(obj)); //##

        // hospitalName と hospitalId のテーブルを記憶
        setHospitalObject(obj); // localStorage.js
        
        var array = hospitalIdArray();
        selectHospital(array);
    } else {
        NRLogin(name, pwd, gotHospitalId); // neuron.js
    }
}

function makeLoginBody(elm){
	// LOGO
	var dv = newDIV(elm, "");
    var img = newIMAGE(dv, "", "./abdomen.png", "");
    img.style.marginLeft = "150px";
    img.style.marginTop = "80px";
    
    // MESSAGE
	var dv = newDIV(elm, "");
    dv.style.padding = "10px 0px";
    dv.style.textAlign = "center";
	var sp = newSPAN(dv, "");
    sp.innerHTML = "Welcome to the ";
	var sp = newSPAN(dv, "", "");
    sp.innerHTML = "NOA";
    sp.style.fontWeight = "bold";
    sp.style.fontFamily = "arial, Helvetica";
	var sp = newSPAN(dv, "");
    sp.innerHTML = " world.";

	// LOGIN AREA ///////////////
	var tbl2 = newTABLE(elm, "base-table");
    tbl2.style.width = "400px";
    tbl2.style.margin = "0 auto"; // センター揃えとする
    tbl2.style.marginTop = "20px";
    tbl2.style.color = "#666";
    var tr2 = newTR(tbl2, "", "");
    tr2.style.textAlign = "center";
    // USER NAME
    var td2 = newTD(tr2, "", "　"); // left side
    var td2 = newTD(tr2, "", "userName");
    td2.style.paddingRight = "3px";
    td2.style.textAlign = "right";
    var td2 = newTD(tr2, "", "");
    td2.style.width = "280px";
    td2.style.textAlign = "left"; //##
	var fd = newFIELD(td2, "userName", " ", 25, "");
	fd.focus();
    // PASSWORD
    var tr2 = newTR(tbl2, "", "");
    var td2 = newTD(tr2, "", "　"); // left side
    var td2 = newTD(tr2, "", "password");
    td2.style.paddingRight = "3px";
    td2.style.textAlign = "right";
    var td2 = newTD(tr2, "", "");
    td2.style.textAlign = "left";
	var fd = newFIELD(td2, "passwd", "", 25, "");
	fd.setAttribute("TYPE", "password");
	fd.setAttribute("onchange", "getHospitalId()");
	var bt = newBUTTON(td2, "", "login");
	bt.setAttribute("onclick", "getHospitalId()");
    newBR(td2);

    // TABLE AREA
	var tr2 = newTR(tbl2, "hospitalPopupArea");
	var tr2 = newTR(tbl2, "hospitalNameArea");
	var tr2 = newTR(tbl2, "controlArea");

    // MESSAGE
	var dv = newDIV(elm, "");
    dv.style.textAlign = "center";
    dv.style.paddingTop = "20px";
    dv.style.paddingBottom = "10px";
    dv.style.fontSize = "11pt";
	var sp = newSPAN(dv, "");
    sp.innerHTML = version();
}

function initLogin(){
	// login パネルを生成
	var elm = document.getElementById("base");
    var p = newP(elm, "");
    p.style.margin = "0 auto"; // センター揃えとする
    p.style.marginTop = "200px";
    p.style.border = "thin solid #aaa";
    p.style.width = "500px";
    p.style.backgroundColor = "#aec";
    p.style.color = "#666";
    p.style.fontSize = "16pt";
    p.style.fontFamily = "times, arial, Helvetica";

    makeLoginBody(p);

	///// MESSAGE TABLE /////
	var dv = newDIV(elm, "");
    dv.style.paddingTop = "10px";
    dv.style.textAlign = "center";
    dv.style.fontSize = "9pt";
    dv.style.color = "#888";
	dv.innerHTML = window.navigator.userAgent;	// Web Browser 種類とバージョン
	var dv = newDIV(elm, "alert");
    dv.style.textAlign = "center";
}


