
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;
}


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;
    
    //    alert(" showHospitalName->"+label+"->"+hospId()); //##
	
	var tr = document.getElementById("hospitalNameArea");
	tr.innerHTML = "";
    var td = newTD(tr, "", "施設 ID：");
    td.setAttribute("colspan", "3");
    td.style.textAlign = "center";
    var sp = newSPAN(td, "hospitalIdArea");
    sp.innerHTML = hid;
	
    var sp = newSPAN(td, "");
    sp.style.paddingLeft = "20px";
	var bt = newBUTTON(td, "", "open FRONT");
	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 変更", "#", "");
    var userName = document.getElementById("userName").value;
	a.setAttribute("onclick", "setPasswd('" + userName + "')");
}

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

/*var _hospitalName;
function setHospitalName(name){
    _hospitalName = name;
}
function hospitalName(){
    return _hospitalName;
}*/

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;
                setHospitalName(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]);
	}
}

////////////////////////////////
/// neuron.js による処理 /////////

function gotHospitalId(answer){
	// neron.js からの返答を実行
    //alert("gotHospitalId->"+answer); //##
	if (answer.length){
		answer = decodeSTRING(answer);
        //alert("answer->"+answer); //##
        
		if (answer.indexOf("ERROR") >= 0){
			document.getElementById("alert").innerHTML = answer;
			document.getElementById("userName").focus();
		} else {
			var obj = eval('(' + answer + ')');
            //alert("gotHospitalId->"+encodeObject(obj)); //##
            
			// hospitalName と hospitalId のテーブルを記憶
			setHospitalTable(obj.hospitalTable);
            
			var array = hospitalIdArray();
			selectHospital(array);
			document.getElementById("alert").innerHTML = "";
		}
	} else {
        // password がサーバに設定されていないので新たに設定させる
		document.getElementById("passwd").value == ""; // うまく機能しない
        var userName = document.getElementById("userName").value;
        setPasswd(userName);
	}
}
function getHospitalId(){
	// ログイン処理を実行
	var name = document.getElementById("userName").value;
	var pwd = document.getElementById("passwd").value;

	NRLogin(name, pwd, gotHospitalId); // neuron.js
}

function logined(answer){
	// neron.js からの返答を実行
    _initDebug(false); //##
    _debug("logined->"+answer); //##
    
	if (answer.length){
		if (answer.indexOf("ERROR") >= 0){
            // answer は ERROR: で始まる文字列
			document.getElementById("alert").innerHTML = answer;
			document.getElementById("userName").focus();
		} else {
            // answer は JSON 型式のオブジェクト
            // FRONT を開く
			var obj = eval('(' + answer + ')');
            _debug("logined->"+encodeObject(obj)); //##
            
			// Ajax の中からは別ウインドーを開けないので、ボタンクリックで開く
			var array = new Array();
			array['owner'] = obj.loginName;
            var pop = document.getElementById("hospitalNamePop");
            if (pop)
                array['hospitalName'] = pop.value;
            else
                array['hospitalName'] = hospitalName();
			array['hospitalId'] = document.getElementById("hospitalIdArea").innerHTML;
            var st = encodeObject(array);
			var url = "./index.php?value="+encodeSTRING(st);
            
            _debug("url->"+url); //##

            window.open(url, "_self","width=1000,height=700");
        }
	} else {
		document.getElementById("passwd").value == ""; // うまく機能しない
		//alert("passwd を設定してください");
        var userName = document.getElementById("userName").value;
        setPasswd(userName);
	}
}
function login(){
	// ログイン処理を実行
	var name = document.getElementById("userName").value;
	var pwd = document.getElementById("passwd").value;

    NRLogin(name, pwd, logined); // neuron.js
}

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

/// neuron.js による処理 /////////
////////////////////////////////

function makeLoginBody(elm){
	// LOGO
	var dv = newDIV(elm, "");
    var img = newIMAGE(dv, "", "./front.jpg", "");
    img.style.margin = "0 auto"; // センター揃えとする
    img.style.marginTop = "50px";
    
	// LOGIN AREA ///////////////
	var tbl2 = newTABLE(elm, "");
    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()");
    // LOGIN BUTTON
	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.fontSize = "10pt";
    dv.style.textAlign = "center";
    dv.style.color = "#f30";
}
