
var _hospitalTable;
function setHospitalTable(obj){
	_hospitalTable = obj;
}
function hospitalTable(){
	// obj[hospitalName] = hospitalId のようなオブジェクト
	return _hospitalTable;
}

// ## hospitalId は front.js で使われているので重複を避ける
var _hospName;
function setHospName(name){
	_hospName = name;
}
function hospName(){
	return _hospName;
}

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

function gotHospitalId(answer){
	// neron.js からの返答を実行
    console.log("gotHospitalId", answer); //##
    
	if (trim(answer).length > 0){
		answer = decodeSTRING(answer);
		if (answer.indexOf("ERROR") >= 0){
			document.getElementById("errorMessage").innerHTML = answer;
			var dom = document.getElementById("login").focus();
		} else {
			var obj = eval('(' + answer + ')');

			// hospitalName と hospitalId のテーブルを記憶
			setHospitalTable(obj.hospitalTable);
			var hospitalIds = obj.hospitalId;
			var array = hospitalIds.split(" ");
			
			document.getElementById("errorMessage").innerHTML = "";
			selectHospital(array);
		}
	} else {
		alert("passwd を設定してください");
		var name = document.getElementById("login").value;
		setPasswd(name);
	}
}
function getHospitalId(){
	// ログイン処理を実行
	var name = document.getElementById("login").value;
	var pwd = document.getElementById("passwd").value;
    
    var args = new Object();
    args["loginName"] = name;
    args["passwd"] = pwd;
    NRCall("LOGIN", args, gotHospitalId);
}

function openFront(url){
	// FRONT を開く
	window.location.replace(url);
}
function showOpenFrontButton(url){
	// Ajax の中からは別ウインドーを開けないので、ボタンクリックで開く
	var elm = document.getElementById("openButtonArea");
	elm.innerHTML = "";
	var tx = newTEXT(elm, "　");
	var bt = newBUTTON(elm, "", "FRONT を開く");
	bt.setAttribute("onclick", "openFront('" + url + "')");
    bt.focus();
}

function logined(answer){
	// neron.js からの返答を実行
	if (answer.length > 0){
		answer = decodeSTRING(answer);
		//alert("=== logined ===\n"+ answer); //##
		if (answer.indexOf("ERROR ") >= 0){
			document.getElementById("errorMessage").innerHTML = answer;
			var dom = document.getElementById("login").focus();
		} else {
			var obj = eval('(' + answer + ')');
            _debug("== logined ->"+ encodeObject(obj)); //##
			var hospTab = obj.hospitalTable; // "hospName":"hospId"
			
			// Ajax の中からは別ウインドーを開けないので、ボタンクリックで開く
			var array = new Array();
			array['owner'] = obj.loginName;
			array['hospitalName'] = hospName();
			array['hospitalId'] = hospTab[hospName()];
			var url = "./index.php?value="+encodeObject(array);
			//alert("--- logined\n"+url); //##
			showOpenFrontButton(url);
			document.getElementById("errorMessage").innerHTML = "";
		}
	} else {
		alert("passwd を設定してください");
		var name = document.getElementById("login").value;
		setPasswd(name);
	}
}
function login(){
	// ログイン処理を実行
	var name = document.getElementById("login").value;
	var pwd = document.getElementById("passwd").value;
	//NRLogin(name, pwd, logined); // neuron.js
    var args = new Object();
    args["loginName"] = name;
    args["passwd"] = pwd;
    NRCall("LOGIN", args, logined);
}

var _pwd;
function reportPassword(answer){
	// パスワードが設定された
	if (answer.length)
		alert(answer); // ERROR
}
function setPasswd(name){
	// パスワードを設定
	var pwd = prompt("パスワードを設定してください");
	if (pwd.length){
		_pwd = pwd;
		pwd = prompt("確認のためパスワードをもう一度入力してください");
		if (_pwd != pwd){
			alert("パスワードが違っています");
		} else {
            var args = new Object();
            args["loginName"] = name;
            args["passwd"] = pwd;
            alert("PUT_PASSWD"); //##
            
            NRCall("PUT_PASSWD", args, reportPassword);
        }
	}
}

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


function showHospitalName(label){
	// 施設名を表示し login ボタンを表示
	var obj = hospitalTable();
	var hospitalId = "??";
	for (name in obj)
		if (name == label){
			hospitalId = obj[name];
	}
	setHospName(label); // hospitalName を記憶
	
	var tr = document.getElementById("hospitalNameArea");
	tr.innerHTML = "";
	var td = newTD(tr, "hospitalTD", "施設 ID ");
	var td = newTD(tr, "hospitalId", hospitalId);
	td.style.fontSize = "10pt";
	var tx = newTEXT(td, "　");
	var bt = newBUTTON(td, "", "login");
	bt.setAttribute("onclick", "login()");
	bt.focus(); // ログイン・ボタンに fucus
	
	var tr = document.getElementById("controlArea");
	tr.innerHTML = "";
	var td = newTD(tr, "", "");
	var td = newTD(tr, "", "");
	td.style.fontSize = "10pt";
	/*
	var a = newA(td, "passwd 変更", "#", "");
	var loginName = document.getElementById("login").value;
	var action = "setPasswd('" + loginName + "')";
	a.setAttribute("onclick", action);
	 */
	var js = newSPAN(td, "openButtonArea");
}

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

function selectHospital(array){
	// ユーザの所属する施設名を選択
	// 所属施設が複数ある場合はポップアップメニューで選択
	var tr = document.getElementById("hospitalPopupArea");
	tr.innerHTML = "";
	var obj = hospitalTable();
	if (array.length == 1){
		for (name in obj){
			var hid = obj[name];
			if (hid == array[0]){
				var td = newTD(tr, "hospitalTD", "施設名 ");
				var td = newTD(tr, "", name);
				showHospitalName(name);
			}
		}
	} else {
		// popup を表示
		var names = new Array();
		for (var i=0,ct=array.length; i < ct; i++){
			var hid = array[i];
			for (name in obj){
				if (hid == obj[name])
					names.push(name);
			}
		}
		var td = newTD(tr, "hospitalTD", "施設名 ");
		var td = newTD(tr, "", "");
		td.style.fontSize = "10pt";
		var pu = newPopupMenu(td, "", names, "");
		pu.setAttribute("onchange", "clickedHospital(this)");
		var tx = newTEXT(td, "　を選択してください");
		document.getElementById("hospitalNameArea").innerHTML = "";
	}
}

function initLogin(){
	// login パネルを生成
    //_initDebug(false); //##
    
	window.document.title = "FRONT LOGIN";

	var elm = document.getElementById("base");
	elm.innerHTML = "";
	var dv = newDIV(elm, "baseDIV");
	
	// LOGO
	var tbl = newTABLE(dv, "imageTable");
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "", "　");
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "imageTD", "");
	var img = newIMAGE(td, "", "./front.jpg", "");
	var tr = newTR(tbl, "", "");
	var td = newTD(tr, "loginTitle", "FRONT");

	// LOGIN PANEL
	var tr = newTR(tbl, "", "");
	var tbl2 = newTABLE(tr, "loginTable");
	
	var tr = newTR(tbl2, "", "");
	var td = newTD(tr, "", "　");
	td.setAttribute("colspan", "2");
	
	// login
	var tr = newTR(tbl2, "", "");
	var td = newTD(tr, "loginTD", "login ");
	var td = newTD(tr, "", "");
	var loginF = newFIELD(td, "login", "", 25, "");
	
	// password
	var tr = newTR(tbl2, "", "");
	var td = newTD(tr, "pwdTD", "password ");
	var td = newTD(tr, "", "");
	var fd = newFIELD(td, "passwd", "", 25, "");
	fd.setAttribute("TYPE", "password");
	fd.setAttribute("onchange", "getHospitalId()");
    var bt = newBUTTON(td, "", "確定");
    bt.setAttribute("onclick", "getHospitalId()");
	
	// hospital
	var tr = newTR(tbl2, "hospitalPopupArea", "");
	tr.style.fontSize = "10pt";
	var tr = newTR(tbl2, "hospitalNameArea", "");
	tr.style.fontSize = "10pt";
	var tr = newTR(tbl2, "controlArea", "");
	tr.style.fontSize = "10pt";
	
	// login 入力フィールドにフォーカスをセット
	loginF.focus();
	
	var tr = newTR(tbl2, "", "");
	var td = newTD(tr, "", version());
	td.setAttribute("colspan", "2");
    td.style.fontSize = "10pt";
    td.style.color = "#888";    
    td.style.textAlign = "right";    
	
	///// MESSAGE TABLE /////
	var tbl2 = newTABLE(elm, "messageTable");
	tbl2.setAttribute("ALIGN", "center");
	tbl2.style.width = 400;
	var tr = newTR(tbl2, "", "");
	var td = newTD(tr, "", "");
	td.style.fontSize = "10pt";
	td.style.color = "#f03"; //red
	var dv = newDIV(td, "message");
	dv.innerHTML = window.navigator.userAgent; // Web Browser 種類とバージョン
	var dv = newDIV(td, "errorMessage");
}
