﻿var clsSearchcoordinator = Class.create();
var searchcoordinatorCls = Array();

clsSearchcoordinator.prototype = {
	initialize: function(id) {
		this.id = id;
	}
}
function createSearchCoordinator(id){
	removeMessage(id);
	setMessageLink(id);
	if(self['SearchCoordinatorRoot']){
		var statuscode = SearchCoordinatorRoot.header.code;
		if(statuscode != "420" && statuscode != "423"){
			addTextTag(id,'MessageArea',sc_disp_inputform);
			for(key in SearchCoordinatorRoot.body.SearchCondition){
				if(key != "cattp"){
					addInputTag(id,key);
					addTextTag(id,'InputColumn'," ");
				}
			}

			addList(id);
			for(group in SearchCoordinatorRoot.body.SearchSite.group){
				if(SearchCoordinatorRoot.body.SearchSite.group.length > group){
					addOption(id,SearchCoordinatorRoot.body.SearchSite.group[group].site,reSanitize(SearchCoordinatorRoot.body.SearchSite.group[group].grpname));
				}
			}
			addTextTag(id,'InputColumn'," ");

			setSite(id);
			addSearchButton(id);
		}
		if(statuscode == "420"){
			addBRTag('MessageArea'+id);
			addTextTag(id,'MessageArea',reSanitize(SearchCoordinatorRoot.header.message));
		}else if(statuscode != "220" && statuscode != "221"){
			alert(sc_error_module + "\n" + reSanitize(SearchCoordinatorRoot.header.message));
		}else if(statuscode == "220"){
			setUserLink(id);
			if(SearchCoordinatorRoot.header.rirekiflg == "true"){
				setRirekiLink(id);
			}
		}
		if(sc_adminflg == "true" && SearchCoordinatorRoot.header.adminflg == "true"){
			setAdminLink(id);
		}
	}else{
		addBRTag('MessageArea'+id);
		addTextTag(id,'MessageArea',sc_error_message);
	}
}

function addSearchButton(id){
	var info = document.getElementById('InputColumn'+id);
	var element = document.createElement('input');
	element.type = "button";
	element.value = "検索";
<!--	element.value = "Search";-->
	if(element.attachEvent){
		element.attachEvent("onclick",function(){keywdCheck(id)});
	}else{
		element.addEventListener("click",function(){keywdCheck(id)},true);
	}
	info.appendChild(element);
}

function addTextTag(id,column,text){
	var info = document.getElementById(column+id);
	var element = document.createTextNode(text);
	info.appendChild(element);
}

function addInputTag(id,elementName){
	var info = document.getElementById('InputColumn'+id);
	var element = document.createElement('input');
	element.id = elementName+id;
	element.type = "text";
	element.name = elementName;
	info.appendChild(element);

    onFocus = setFocus(element);
}

function setFocus(element){

   element.onfocus = function() {
       if( element.value == 'free keyword' ) {
               element.value = '';
       }
   }
   element.onblur = function() {
       if( element.value == '' ) {
               element.value = 'free keyword';
       }
   }
}

function addBRTag(column){
	var info = document.getElementById(column);
	var element = document.createElement('BR');
	info.appendChild(element);
}

function addList(id){
	var info = document.getElementById('InputColumn'+id);
	var element = document.createElement('select');
	element.id = "SiteList"+id;
	if(element.attachEvent){
		element.attachEvent("onchange",function(){setSite(id)});
	}else{
		element.addEventListener("change",function(){setSite(id)},true);
	}
	info.appendChild(element);
}

function addOption(id,value,disp){
	var info = document.getElementById('SiteList'+id);
	var element = document.createElement('option');
	element.value = value;
	var dispname = disp;
	if(dispname == "defaultsite"){
		dispname = sc_disp_default;
	}else if(dispname == "usersite"){
		dispname = sc_disp_usersite;
	}
	var text = document.createTextNode(dispname);
	element.appendChild(text);
	info.appendChild(element);
}

function setMessageLink(id){
	addLink(id,"usrshe",sc_usrshe,"FUSRSRL");
	addTextTag(id,'LinkColumn'," | ");
	addLink(id,"usrshd",sc_usrshd,"FUSRSRL");
}

function setUserLink(id){
	addTextTag(id,'LinkColumn'," | ");
	addLink(id,"myssts",sc_myssts,"FUSRSRL");
}

function setRirekiLink(id){
	addTextTag(id,'LinkColumn'," | ");
	addLink(id,"mysshh",sc_mysshh,"FUSRSRL");
}

function setAdminLink(id){
	addTextTag(id,'LinkColumn'," | ");
	addLink(id,"admmnu",sc_admmnu,"admmnu");
}

function addLink(id,type,mes,target){
	var info = document.getElementById('LinkColumn'+id);
	var element = document.createElement('a');
	element.href = sc_address + "/" + type + ".do?tenantId=" + tenant_id;
	element.target = target;
	var text = document.createTextNode(mes);
	element.appendChild(text);
	info.appendChild(element);
}

function removeMessage(id){
	var info = document.getElementById('WaitMessage'+id);
	info.parentNode.removeChild(info);
}

function setSite(id){
	removeHidden(id);
	var sites = document.getElementById('SiteList'+id).value.split(",");
	for(index in sites){
		if(sites.length > index){
			addHidden(id,sites[index]);
		}
	}
}

function addHidden(id,value){
	var info = document.getElementById('HiddenColumn'+id);
	var element = document.createElement('input');
	element.type = "hidden";
	element.name = "site";
	element.value = value;
	info.appendChild(element);
}

function removeHidden(id){
	var info = document.getElementById('HiddenColumn'+id);
	while(info.hasChildNodes()){
		info.removeChild(info.firstChild);
	}
}

function keywdCheck(id) {
	if( document.getElementById('keywd' + id).value == "" ){
		alert(sc_keywd_check);
	}else{
		document.getElementById('scform' + id).submit();
	}
}

function reSanitize(arg1){
	arg1 = arg1.replace("&amp;","&");
	arg1 = arg1.replace("&lt;","<");
	arg1 = arg1.replace("&gt;",">");
	arg1 = arg1.replace("&quot;","\"");
	arg1 = arg1.replace("&#39;","\'");
	return arg1;
}

if(!searchcoordinatorCls["_0"]) {
		searchcoordinatorCls["_0"] = new clsSearchcoordinator("_0");
	}

	if(window.name == "FUSRSRL" || window.name == "admmnu"){
		window.name = "SearchCoordinator";
	}

	var tenant_id         = "default";
	<!-- URLは変更して下さい -->
	var sc_address        = "http://glim-els.glim.gakushuin.ac.jp/cass/";
	var sc_adminflg       = "";
	var sc_disp_inputform = "◆学術情報検索キーワード入力欄";
	var sc_disp_default   = "デフォルト検索サイト";
	var sc_disp_usersite  = "お気に入りサイト";
	var sc_usrshe         = "検索先の個別選択";
	var sc_usrshd         = "詳細検索画面へ";
	var sc_myssts         = "お気に入りサイト設定";
	var sc_mysshh         = "検索履歴参照";
	var sc_admmnu         = "管理者画面へ";
	var sc_error_module   = "横断検索モジュール";
	var sc_keywd_check    = "検索条件が未入力です";
	var sc_error_message  = "正しく処理が行えませんでした。システム管理者に連絡してください。";
	createSearchCoordinator("_0");
