// 修正：浅沼孝夫

// グローバル変数
var httpObj = null;
var timerID = null;
var timeOut = 1200;
var timeID  = null;

// イベント紐付け
function addListener(elem, eventType, func)
{
	if (elem == null) {
			return false;
	}
	else {
		if (elem.addEventListener) {
			elem.addEventListener(eventType, func, false);
		}
		else if (elem.attachEvent) {
			elem.attachEvent("on" + eventType, func);
		}
		else {
			return false;
		}
	}
}

// イベント解除
function removeListener(elem, eventType, func)
{
	if (elem.removeEventListener) {
		elem.removeEventListener(eventType, func, false);
	}
	else if (elem.detachEvent) {
		elem.detachEvent("on" + eventType, func);
	}
	else {
		return false;
	}
}

// XML request
function httpXmlRequest(targetUrl, data, okFunc, ngFunc)
{
	if (window.XMLHttpRequest) {
		try {
			httpObj = new XMLHttpRequest();
		}
		catch(e) {}
	}
	else if (window.ActiveXObject)
	{
		try {
			httpObj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				httpObj = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e2) {}
		}
	}

	if (httpObj == null) {
		return false;
	}
	
	timeOut = 1200;
	timerID = setInterval("timeoutCheck()", 1000);

	httpObj.open("POST", targetUrl, true);
	httpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpObj.onreadystatechange = function() {
		if (httpObj.readyState == 4) {
			clearInterval(timerID);
			if (httpObj.status == 200) {
				okFunc(httpObj.responseXML);
			}
			else {
				ngFunc("-1", "通信エラー");
			}
		}
	}

	// dspErr(0, data);
	httpObj.send(data);
	
	return;
}

// タイムアウトチェック
function timeoutCheck()
{
	timeOut--;
	if (timeOut <= 0) {
		clearInterval(timerID);
		httpObj.abort();
		
		return;
	}
}

// Error box
function dspErr(iSts, sMsg)
{
// 	var obj;
// 	var objItem;
// 	var iTop;
// 	
// 	obj = document.getElementById("WAIT_BOX");
// 	obj.style.visibility = "hidden";
// 	iTop = 200 + document.body.scrollTop;
// 	
// 	obj = document.getElementById("ERR_BOX");
// 	objItem = document.getElementById("TD_MESSAGE");
// 	if ((objItem != null) && (sMsg != null))
// 		objItem.innerHTML = sMsg;
// 	obj.style.top = iTop + "px";
// 	obj.style.left = "200px";
// 	obj.style.visibility = "visible";
// 	
// 	if (iSts == 1)
// 		document.ERR_ICO.src = icoAlert.src;
// 	else
// 		document.ERR_ICO.src = icoError.src;

//	obj = wnidow.createPopup();
//	obj.show(100,100, 160, 120);
//	obj.document.body.innerText(iSts + ":" + sMsg);

	alert(sMsg);
}

// XML: text形式の値取得
function getTextXML (items) {
	var value;

	if (items == null) {
		value = "";
	}
	else {
		if (items[0].text) {
			value = items[0].text;
		}
		else if (items[0].textContent) {
			value = items[0].textContent;
		}
		else {
			value = "";
		}
	}

	return value;
}

// XML: int形式の値取得
function getIntXML (items) {
	var value = "";

	if (items != null) {
		if (items.length > 0) {
			if (items[0].text) {
				value = parseInt(items[0].text);
			}
			else if (items[0].textContent) {
				value = parseInt(items[0].textContent);
			}
		}
	}

	return value;
}

function getTextData(obj) {
	var obj;
	var value = "";

	if (obj.text) {
		value = obj.text;
	}
	else if (obj.textContent) {
		value = obj.textContent;
	}

	if (value == "") {
		// value = "&nbsp;";
		value = "";
	}

	return value;
}

// Wait box
function dspWait(obj) {
	var sHtml;
	
	sHtml = "<span id=\"WAIT_BOX\" style=\"visibility:visible;\">&nbsp;&nbsp;&nbsp;<img src=\"img/loading.gif\" alt=\"loading animation\" style=\"vertical-align:middle;\" />　処理中...</span>";

	obj.innerHTML = sHtml;
}

// Wait box type2
function dspWaitMessage(obj, msg) {
	var sHtml;

	sHtml = msg;
	sHtml += "<br/>";
	
	sHtml += "<span id=\"WAIT_BOX\" style=\"visibility:visible;\">&nbsp;&nbsp;&nbsp;<img src=\"img/loading.gif\" alt=\"loading animation\" style=\"vertical-align:middle;\" />　処理中...</span>";

	obj.innerHTML = sHtml;
}

function hidemenuConcept(x, y) {
	var x0;
	var y0;
	var sTmp;
	var w = 120;
	var h = 60;
	var margin = 10;

	sTmp = document.getElementById("MENU_BOX").style.top;
	y0 = parseInt(sTmp.replace(/[a-z]/i, ""));
	sTmp = document.getElementById("MENU_BOX").style.left;
	x0 = parseInt(sTmp.replace(/[a-z]/i, ""));

	x0 -= margin;
	y0 -= margin;
	w += margin;
	h += margin;

	if ((x > x0 && x < (x0 + w)) && (y > y0 || y < (y0 + h))) {
	}
	else {
		document.getElementById("MENU_BOX").style.visibility = "hidden";
	}
}

function popupmenuConcept(id, cid, x, y) {
	var obj
	var top;
	var left;
	var sHtml = "";

	left = x + "px";
	top  = y + "px";

	sHtml += "<div id=\"MENU_R_0\" class=\"menu_r_off\" onmousemove=\"this.className='menu_r_on'\" onmouseout=\"this.className='menu_r_off'\" onclick=\"clickConceptMenu(0, '" + id + "', '" + cid + "')\">別　名</div>";
	sHtml += "<div id=\"MENU_R_1\" class=\"menu_r_off\" onmousemove=\"this.className='menu_r_on'\" onmouseout=\"this.className='menu_r_off'\" onclick=\"clickConceptMenu(1, '" + id + "', '" + cid + "')\">出　典</div>";
	sHtml += "<div id=\"MENU_R_2\" class=\"menu_r_off\" onmousemove=\"this.className='menu_r_on'\" onmouseout=\"this.className='menu_r_off'\" onclick=\"clickConceptMenu(2, '" + id + "', '" + cid + "')\">GO / MeSH Term</div>";

	document.getElementById("MENU_BOX").innerHTML = sHtml;

	document.getElementById("MENU_BOX").style.top = top;
	document.getElementById("MENU_BOX").style.left = left;
	document.getElementById("MENU_BOX").style.visibility = "visible";
}

function isYear(year) {
	var iYear;

	if (year.match(/^20[0-5][0-9]$/)) {
		return true;
	}
	if (year.match(/^19[0-9][0-9]$/)) {
		return true;
	}
	if (year.match(/^20[0-5][0-9]-$/)) {
		return true;
	}
	if (year.match(/^19[0-9][0-9]-$/)) {
		return true;
	}
	if (year.match(/^[12][09][0-9][0-9]-[12][09][0-9][0-9]$/)) {
		return true;
	}

	return false;
}

function checkNGfileName(fname) {
	if((fname.indexOf("\\") != -1)
	|| (fname.indexOf("/") != -1)
	|| (fname.indexOf(":") != -1)
	|| (fname.indexOf(",") != -1)
	|| (fname.indexOf(";") != -1)
	|| (fname.indexOf("*") != -1)
	|| (fname.indexOf("?") != -1)
	|| (fname.indexOf("\"") != -1)
	|| (fname.indexOf("<") != -1)
	|| (fname.indexOf(">") != -1)
	|| (fname.indexOf("|") != -1)) {
		return true;
	}

	return false;
}

function noExistsInArray(arr, str) {
	var i;
	
	for (i = 0; i < arr.length; i++) {
		if (arr[i] == str) return false;
	}
	
	return true;
}

function notDelimiter(c) {
	if ((c == ' ')
	 || (c == ',')
	 || (c == '.')
	 || (c == '?')
	 || (c == '-')
	 || (c == '/')
	 || (c == '(')
	 || (c == ')')) {
		return false;
	}
	return true;
}

function setSpanAll(sOrg, sKey, typeCase) {
	// mode: 0 = 大文字小文字を区別する

	var sRet;
	var iCurIndex;
	var iIndex;
	var cTmp;
	var sOrgL;
	var sKeyL;
	
	sRet = "";
	iCurIndex = 0;

	if (typeCase != 0) {
		sOrgL = sOrg.toLowerCase();
		sKeyL = sKey.toLowerCase();
	}
	else {
		sOrgL = sOrg;
		sKeyL = sKey;
	}

	if ((sKey.length == 0) && (sKey.length > sOrg.length)) {
		sRet = sOrg;
	}
	else if (sKeyL == sOrgL) {
		sRet = "<span class=\"term\">" + sKey + "</span>";
	}
	else {
		while(true) {
			iIndex = sOrgL.indexOf(sKeyL, iCurIndex);
			if (iIndex == -1) {
				sRet += sOrg.substring(iCurIndex);
				break;
			}
			sRet += sOrg.substring(iCurIndex, iIndex);
			sRet += "<span class=\"term\">" + sKey + "</span>";
			iCurIndex = iIndex + sKey.length;
		}
	}

	return sRet;
}

function setSpan(sOrg, sKey, typeCase, typeMatch) {
	// typeCase: 大文字小文字を区別する: 0=yes, 1=no
	// typeMatch: "FORW"
	// typeMatch: "EXACT"
	// typeMatch: "PART"
	// typeMatch: "BACK"

	var sRet;
	var iIndex;
	var sTmp;
	var sOrgL;
	var sKeyL;
	
	sRet = "";

	if (typeCase != 0) {
		sOrgL = sOrg.toLowerCase();
		sKeyL = sKey.toLowerCase();
	}
	else {
		sOrgL = sOrg;
		sKeyL = sKey;
	}

	if ((sOrg.length == 0) || (sKey.length == 0) || (sKey.length > sOrg.length)) {
		sRet = sOrg;
	}
	else if (sKeyL == sOrgL) {
		sRet = "<span class=\"term\">" + sOrg + "</span>";
	}
	else {
		iIndex = sOrgL.indexOf(sKeyL, 0);
		if (iIndex >= 0) {
			sTmp = sOrg.substr(iIndex, sKeyL.length);
			sRet = sOrg.substring(0, iIndex);
			if (iIndex == 0) {
				if (typeMatch == "BACK") {
					sRet += sTmp;
				}
				else {
					sRet += "<span class=\"term\">" + sTmp + "</span>";
				}
			}
			else {
				if (typeMatch == "FORW") {
					sRet += sTmp;
				}
				else if (typeMatch == "BACK") {
					if ((iIndex + sKeyL.length) == sOrgL.length) {
						sRet += "<span class=\"term\">" + sTmp + "</span>";
					}
					else {
						sRet += sTmp;
					}
				}
				else {
					sRet += "<span class=\"term\">" + sTmp + "</span>";
				}
			}
			sRet += sOrg.substring(iIndex + sKey.length);
		}
		else {
			sRet = sOrg;
		}
	}

	return sRet;
}

function confirmCustomer(mode) {
	var value = document.getElementById("logging_userkind").value;
	if (value != "0") {
		if (mode) {
			if (confirm("この機能は有料です。有料版の購入について問い合わせを行いますか？")) {
				location.href = "http://www.nalapro.com/inquiry/indexj.html";
			}
		}
		return false;
	}
	else {
		return true;
	}
}

function confirmRegUser(mode) {
	var value = document.getElementById("logging_userkind").value;
	if (value != "0" && value != "1") {
		if (mode) {
			if (confirm("この機能を使うためにユーザ登録が必要です。ユーザ登録を行いますか？")) {
				location.href = "./login.html";
			}
		}
		return false;
	}
	else {
		return true;
	}
}

