var apiHandle = null;

function getAPIHandle() {
	if (apiHandle == null) {
		var _api = findAPIAdapter(window);
		if ((_api == null) &&
				(window.opener != null) &&
				(typeof(window.opener) != "undefined")) {
			_api = findAPIAdapter(window.opener);
		}
		apiHandle = _api;
	}
	return apiHandle;
}

function findAPIAdapter(win) {
	var tries = 0;
	while ((win.API == null)
			&& (win.parent != null)
			&& (win.parent != win)) {
		if (tries++ > 10) {
			alert("SCORM API NOT FOUND!!");
			return null;
		}
		win = win.parent;
	}
	return win.API;
}

function startTest(userCourseId, itemId) {
	var testWindowStatus = "width=800,height=500,resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars=yes";
	var w;
	if (userCourseId == undefined) {
		var api = getAPIHandle();
		courseId = api._GetValue("$userCourseId");
		itemId = api._GetValue("$itemId");
		w = window.open("/lms/test/Explanation.do?userCourseId=" + userCourseId + "&itemId=" + itemId, "test", testWindowStatus);
	} else if (itemId == undefined) {
		w = window.open("/lms/test/Explanation.do?userCourseId=" + userCourseId, "test", testWindowStatus);
	} else {
		w = window.open("/lms/test/Explanation.do?userCourseId=" + userCourseId + "&itemId=" + itemId, "test", testWindowStatus);
	}
	w.focus();
}

function goToTop() {
	var api = getAPIHandle();
	courseId = api._GetValue("$userCourseId");
	parent.location.replace("/lms/learning/Top.do?userCourseId=" + userCourseId);
}

function showQA() {
	var testWindowStatus = "width=800,height=500,resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars=yes";
	var w = window.open("/lms/qa/Main.do", "qa", testWindowStatus);
	w.focus();
}

function showCourseQA() {
	var testWindowStatus = "width=800,height=500,resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars=yes";
	var api = getAPIHandle();
	var qaurl = api._GetValue("$qaURL");
	if (qaurl != "") {
		var w = window.open(qaurl, "qa", testWindowStatus);
		w.focus();
	} else {
		showQA();
	}
}

function showManual(role) {
	var windowStatus = "width=800,height=500,resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars=yes";
	if (role=="admin") {
		var w = window.open("/manual/kanrisha/", "manual", windowStatus);
		w.focus();
	} else {
		var w = window.open("/manual/jukosha/", "manual", windowStatus);
		w.focus();
	}
}
function disableButtons(form) {
	var elements = form.elements;
	var count = elements.length;
	for (i = 0; i < count; i++) {
		if (elements[i].type == "button" || elements[i].type == "submit") {
			elements[i].disabled = true;
		}
	}
}
function checkAll(form) {
	var elements = form.elements;
	var count = elements.length;
	for (i = 0; i < count; i++) {
		if (elements[i].type == "checkbox") {
			elements[i].checked = true;
		}
	}
}

function uncheckAll(form) {
	var elements = form.elements;
	var count = elements.length;
	for (i = 0; i < count; i++) {
		if (elements[i].type == "checkbox") {
			elements[i].checked = false;
		}
	}
}

function getCheckedCount(form) {
	var elements = form.elements;
	var count = elements.length;
	var n = 0;
	for (i = 0; i < count; i++) {
		if (elements[i].type == "checkbox") {
			if (elements[i].checked) n++;
		}
	}
	return n;
}
function trim(s) {
	return s.replace(/(^ *)([^ ]*)( *$)/,'$2');
}

function $(id) {
	return $x(document.getElementById(id));
}
function $e(tagName, className, id) {
	var e = document.createElement(tagName);
	if (className) e.className = className;
	if (id) e.id = id;
	return $x(e);
}
function $t(str) {
	return document.createTextNode(str);
}
function $h(s) {
	var div = $e("div");
	div.innerHTML = s;
	return div.firstChild;
}
function $x(e) {
	e.show = function () { e.style.display = "block"; }
	e.hide = function () { e.style.display = "none"; }
	e.clear = function () {
		var n = this.childNodes.length;
		for (var i = 0; i < n; i++) {
			this.removeChild(this.firstChild);
		}
		return this;
	}
	e.append = function (child) {
		if (typeof(child) == "object") {
			if (child instanceof Array) {
				for (var i = 0; i < child.length; i++) {
					this.append(child[i]);
				}
			} else {
				this.appendChild(child);
			}
		} else {
			this.appendChild($t(String(child)));
		}
		return this;
	}
	e.replace = function (n) {
		return this.clear().append(n);
	}
	return e;
}
function dateValueOf(val) {
	var y, m, d;
	var error = false;
	var a = val.match(/^([0-9]{1,4})\/([0-9]{1,2})\/([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})\/([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})([0-9]{2})$/)
		|| val.match(/^([0-9]{1,4})([0-9]{2})([0-9]{2})$/);
	if (a) {
		if (a.length == 4) {
			y = Number(a[1]);
			m = Number(a[2]);
			d = Number(a[3]);
			if (y < 100) {
				y = (y < 50 ? y + 2000 : y + 1900);
			}
		} else {
			m = Number(a[1]);
			d = Number(a[2]);
			var today = new Date();
			y = today.getFullYear();
		}
	} else {
		error = true;
	}
	error = error || (y < 1900 || y > 2099 || m < 1 || m > 12 || d < 1 || d > 31);
	return error ? null : new Date(y, m - 1, d);
}
function timeValueOf(val) {
	var h, m, s;
	var error = false;
	var a = val.match(/^([0-9]{1,2}):([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})([0-9]{2})$/)
		|| val.match(/^([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})([0-9]{2})([0-9]{2})$/);
	if (a) {
		if (a.length == 2) {
			h = Number(a[1]);
			m = 0;
			s = 0;
		} else if (a.length == 3) {
			h = Number(a[1]);
			m = Number(a[2]);
			s = 0;
		} else {
			h = Number(a[1]);
			m = Number(a[2]);
			s = Number(a[3]);
		}
	} else {
		error = true;
	}
	error = error || (h >= 24) || (m >= 60) || (s >= 60);
	return error ? null : new Date(0, 0, 0, h, m, s);
}
function onBlurDate(e) {
	if (!e) e = event;
	var elem = (e.target ? e.target : e.srcElement);
	var val = trim(elem.value);
	if (val == "") {
		return;
	}
	var dt = dateValueOf(val);
	if (dt != null) {
		y = dt.getFullYear();
		m = dt.getMonth() + 1;
		d = dt.getDate();
		elem.value = y + "/" + (m < 10 ? "0" : "") + m + "/" + (d < 10 ? "0" : "") + d;
	}
}
function onBlurTime(e) {
	if (!e) e = event;
	var elem = (e.target ? e.target : e.srcElement);
	var val = trim(elem.value);
	if (val == "") {
		return;
	}
	var tm = timeValueOf(val);
	if (tm != null) {
		h = tm.getHours();
		m = tm.getMinutes();
		elem.value = (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m;
	}
}

function createCalendar(cont, date) {
	var curdate = new Date();
	var cy = curdate.getFullYear();
	var year, month;
	var dt = dateValueOf(date);
	if (!dt) {
		dt = curdate;
	}
	year = dt.getFullYear();
	month = dt.getMonth() + 1;
	var cal = $h('<table><thead><tr><td><img src="img/calendar/prev.gif"/><img src="img/calendar/prev.gif"/></td><td><img src="img/calendar/prev.gif"/></td><td colspan="3"></td><td><img src="img/calendar/next.gif"/></td><td><img src="img/calendar/next.gif"/><img src="img/calendar/next.gif"/></td></tr><tr><td class="w1">日</td><td class="w2">月</td><td class="w2">火</td><td class="w2">水</td><td class="w2">木</td><td class="w2">金</td><td class="w3">土</td></tr></thead><tbody><tr><td></td></tr></tbody></table>');
	cal.setMonth = function (y, m) {
		y = (m < 1 ? y - 1 : (m > 12 ? y + 1 : y));
		m = (m < 1 ? m + 12 : (m > 12 ? m - 12 : m));
		var dt = new Date(y, m - 1, 1);
		var dow = dt.getDay();
		var tm = dt.getTime() - dow * 86400000;
		var dstyle = [1, 2, 2, 2, 2, 2, 3];
		var tbody = $x(this.lastChild).clear();
		dt.setTime(tm);
		while (dt.getMonth() + 1 == m || dt.getDate() > 7) {
			var row = $e("tr");
			for (var dow = 0; dow < 7; dow++) {
				var col = $e("td",
							(dt.getMonth() + 1 == m ? "w" : "x") + dstyle[dow]
							).append(dt.getDate());
				col.date = (dt.getFullYear() + "/"
					+ (dt.getMonth() < 9 ? "0" : "") + (dt.getMonth() + 1) + "/"
					+ (dt.getDate() < 10 ? "0" : "") + dt.getDate());
				col.onmouseover = function () {
					this.style.backgroundColor = "#ddd";
				}
				col.onmouseout = function () {
					this.style.backgroundColor = "#fff";
				}
				col.onclick = function () {
					var c = this.parentNode.parentNode.parentNode;
					c.parentNode.previousSibling.previousSibling.value = this.date;
					c.parentNode.previousSibling.click();
				}
				row.append(col);
				tm += 86400000;
				dt.setTime(tm);
			}
			tbody.append(row);
		}
		var head = this.firstChild.firstChild.firstChild;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y - 1, m);
		}
		head = head.nextSibling;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y, m - 1);
		}
		head = $x(head.nextSibling).clear();
		var ydisp = $e("span").append(y);
		var yselect = $e("select");
		yselect.size = 10;
		yselect.style.display = "none";
		ydisp.onclick = function() {
			var i;
			for (i = -40; i <= 40; i++) {
				if (y + i > cy + 10) {
					break;
				}
				yselect.append($e("option").append(y + i));
			}
			yselect.selectedIndex = 40;
			yselect.style.display
				= (yselect.style.display == "block" ? "none" : "block");
		}
		var mdisp = $e("span").append(m);
		var mselect = $e("select");
		mselect.size = 12;
		mselect.style.display = "none";
		mdisp.onclick = function() {
			for (var i = 1; i <= 12; i++) {
				mselect.append($e("option").append(i));
			}
			mselect.selectedIndex = m - 1;
			mselect.style.display
				= (mselect.style.display == "block" ? "none" : "block");
		}
		head.append(ydisp.append(yselect)).append("／").append(mdisp.append(mselect));
		yselect.onchange = function() {
			this.parentNode.parentNode.parentNode.parentNode.parentNode.setMonth(y + this.selectedIndex - 40, m);
		}
		mselect.onchange = function() {
			this.parentNode.parentNode.parentNode.parentNode.parentNode.setMonth(y, this.selectedIndex + 1);
		}
		head = head.nextSibling;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y, m + 1);
		}
		head = head.nextSibling;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y + 1, m);
		}
	}
	cont.append(cal);
	cal.setMonth(year, month);
	return cal;
}

var initializer = {
	functions : [],
	execute : function () {
		for (var i = 0; i < this.functions.length; i++) {
			this.functions[i].call();
		}
	},
	add : function (f) { this.functions.push(f); }
};
initializer.add(function() {
	var inputs = document.getElementsByTagName("input");
	if (inputs == null) {
		return;
	}
	for (var i = 0; i < inputs.length; i++) {
		var elem = inputs[i];
		var cl = elem.className;
		if (cl == "date") {
			elem.onblur = onBlurDate;
		} else if (cl == "time") {
			elem.onblur = onBlurTime;
		}
	}
});
onload = function () { initializer.execute() };


function requireText(elem, name,errmsg) {
	if (trim(elem.value) == "") {
		if(errmsg==undefined){
			errmsg=name + "を入力してください";
		}
		alert(errmsg);
		if (elem.type != "hidden") {
			elem.focus();
		}
		return false;
	} else {
		return true;
	}
}

function requireSelect(elem, name) {
	if (elem.selectedIndex == 0) {
		alert(name + "を選択してください");
		elem.focus();
		return false;
	} else {
		return true;
	}
}

function requireRadio(elem, name) {
	var selected = false;
	if (elem.length) {
		for (var i = 0; i < elem.length; i++) {
			if (elem[i].checked) {
				selected = true;
				break;
			}
		}
	} else {
		if (elem.checked) {
			selected = true;
		}
	}
	if (selected) {
		return true;
	} else {
		alert(name + "を選択してください");
		if (elem.length) {
			elem[0].focus();
		}
		return false;
	}
}

function isDate(elem, name) {
	var val = trim(elem.value);
	if (val == "") {
		return true;
	}
	if (dateValueOf(val) == null) {
		alert(name + "が正しくありません。");
		elem.focus();
		return false;
	}
	return true;
}

function compareDate(elem1, elem2, name) {
	var val1 = trim(elem1.value);
	var val2 = trim(elem2.value);
	if (val1 == "" || val2 == "") {
		return true;
	}
	if (val1 > val2) {
		alert(name + "が不正です。");
		elem1.focus();
		return false;
	}
	return true;
}
function isInteger(elem, name, min, max) {
	var n = trim(elem.value);
	if (n == "") {
		return true;
	} else if (!n.match(/^-?[0-9]+$/)) {
		alert(name + "は半角数字で入力してください");
		elem.focus();
		return false;
	} else if (min != undefined && max != undefined) {
		var m = parseInt(n, 10);
		if (m < min || m > max) {
			alert(name + "は" + min + "～" + max + "の範囲の値を入力してください");
			elem.focus();
			return false;
		}
	} else if (min != undefined) {
		var m = parseInt(n, 10);
		if (m < min) {
			alert(name + "は" + min + "以上の値を入力してください");
			elem.focus();
			return false;
		}
	}
	return true;
}
function isHankaku(elem, name, keta){
  var str=elem.value; 
  for(var i=0 ; i<str.length; i++){
    var code=str.charCodeAt(i);
    if ((48<=code && code<=57) || 
      (65<=code && code<=90) || (97<=code && code<=122) ||
       str.substr(i,1)==' ') {
    }else{
      alert(name + 'は半角英数字で入力してください');
      elem.focus(); 
      return false;
    }
  }
  if(keta != undefined && str.length != keta){
      alert(name + 'は半角英数字' + keta + '桁で入力してください');
      elem.focus(); 
      return false; 
  }
  return true;
}
function isHankaku2(elem, name, keta){
  var str=elem.value; 
  for(var i=0 ; i<str.length; i++){
    var code=str.charCodeAt(i);
    if ((48<=code && code<=57) || 
      (65<=code && code<=90) || (97<=code && code<=122) ||
       str.substr(i,1)==' ') {
    }else{
      alert(name + 'は半角英数字で入力してください');
      elem.focus(); 
      return false;
    }
  }
  if(keta != undefined && str.length < keta){
      alert(name + 'は半角英数字' + keta + '桁以上で入力してください');
      elem.focus(); 
      return false; 
  }
  return true;
}
function isEmail(elem, name) {
	var val = trim(elem.value);
	if (val == "") {
		return true;
	}
	if (val.match(/^[a-zA-Z0-9!$&*.=^`|~#%'+\/?_{}-]+@([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,4}$/) == null) {
		alert(name + "������������������������������");
		elem.focus();
		return false;
	} else {
		return true;
	}
}



function setSelectOptions(select, list, property, labelProperty, labelDefault,keep) {
	if (!select.append) {
		select = $x(select);
	}
	var value = select.value;
	var len = (list != null ? list.length + 1 : 1);
	select.options.length = len;
	select.options[0] = new Option(labelDefault == undefined ? "－" : labelDefault, "");
	if (len == 1) {
		return;
	}
	var firstItem = list[0];
	var firstLabel = firstItem[labelProperty];
	var firstProperty = firstItem[property];
	if (firstLabel == undefined) {
		labelProperty = labelProperty.toUpperCase();
	}
	if (firstProperty == undefined) {
		property = property.toUpperCase();
	}
	for (var i = 1; i < len; i++) {
		var item = list[i - 1];
		select.options[i] = new Option(item[labelProperty], item[property]);
		if(keep == 'keep'){
			if(item[property] == value){
				select.options[i].selected = true;
			}
		}
	}
}
function $x(e) {
	e.show = function () { e.style.display = "block"; }
	e.hide = function () { e.style.display = "none"; }
	e.clear = function () {
		var n = this.childNodes.length;
		for (var i = 0; i < n; i++) {
			this.removeChild(this.firstChild);
		}
		return this;
	}
	e.append = function (child) {
		if (typeof(child) == "object") {
			if (child instanceof Array) {
				for (var i = 0; i < child.length; i++) {
					this.append(child[i]);
				}
			} else {
				this.appendChild(child);
			}
		} else {
			this.appendChild($t(String(child)));
		}
		return this;
	}
	e.replace = function (n) {
		return this.clear().append(n);
	}
	return e;
}