/* vi:set nowrap ts=2: */
/* Cookie用の定義 */
dd = new Date();
ye = dd.getYear();      if (ye < 1900) ye += 1900;
mo = dd.getMonth() + 1; if (mo < 10) mo = "0" + mo;
da = dd.getDate();      if (da < 10) da = "0" + da;
ho = dd.getHours();     if (ho < 10) ho = "0" + ho;
mi = dd.getMinutes();   if (mi < 10) mi = "0" + mi;
se = dd.getSeconds();   if (se < 10) se = "0" + se;
date = ye + "/" + mo + "/" + da + " " + ho + ":" + mi + ":" + se;

var objtab = new Array(0);
var evtab  = new Array(0);
var ync_dialog_window = null;
var ync_dialog_value  = null;
var ync_dialog_y_act;
var ync_dialog_n_act;
var ync_dialog_c_act;
var url_to_move;
var booktype_for_go_book_detail;
var isbn_list;
var isbn_form;
var isbn_count;

if (navigator.appName == 'Netscape') {
  var noev = undefined;
} else {
  var noev = null;
}

function save_obj(o)
{
  objtab.length += 1;
  objtab[objtab.length - 1] = o;
}

function save_events()
{
  var i;
  evtab.length = objtab.length * 8;

  for (i = 0; i < objtab.length; i++) {
    evtab[i * 8 + 0] = objtab[i].onmousedown;
    evtab[i * 8 + 1] = objtab[i].onmouseup  ;
    evtab[i * 8 + 2] = objtab[i].onclick    ;
    evtab[i * 8 + 3] = objtab[i].ondblclick ;
    evtab[i * 8 + 4] = objtab[i].onkeydown  ;
    evtab[i * 8 + 5] = objtab[i].onkeypress ;
    evtab[i * 8 + 6] = objtab[i].onkeyup    ;
    evtab[i * 8 + 7] = objtab[i].onfocus    ;
  }
}

function load_events()
{
  var i;
  for (i = 0; i < objtab.length; i++) {
    objtab[i].onmousedown=evtab[i * 8 + 0];
    objtab[i].onmouseup  =evtab[i * 8 + 1];
    objtab[i].onclick    =evtab[i * 8 + 2];
    objtab[i].ondblclick =evtab[i * 8 + 3];
    objtab[i].onkeydown  =evtab[i * 8 + 4];
    objtab[i].onkeypress =evtab[i * 8 + 5];
    objtab[i].onkeyup    =evtab[i * 8 + 6];
    objtab[i].onfocus    =evtab[i * 8 + 7];
  }
}

function focus_ync()
{
  if (ync_dialog_window.closed) ync_dialog_value = -1;

  if (ync_dialog_value == null) {
    ync_dialog_window.focus();
    return false;
  }

  if (!ync_dialog_window.closed) ync_dialog_window.close();

  load_events();

  switch (ync_dialog_value) {
    case  1: ync_dialog_y_act()   ; break;
    case  0: ync_dialog_n_act()   ; break;
    case -1: ync_dialog_c_act()   ; break;
    defalut: alert('system error'); break;
  }

  return true;
}

function set_events()
{
  var i;
  for (i = 0; i < objtab.length; i++) {
    objtab[i].onmousedown=focus_ync;
    objtab[i].onmouseup  =focus_ync;
    objtab[i].onclick    =focus_ync;
    objtab[i].ondblclick =focus_ync;
    objtab[i].onkeydown  =focus_ync;
    objtab[i].onkeypress =focus_ync;
    objtab[i].onkeyup    =focus_ync;
    objtab[i].onfocus    =focus_ync;
  }
}

function onload_handler()
{
  var i;
  var k;
  save_obj(window);
  save_obj(document);

  for (k = 0; k < document.links.length; k++) 
    save_obj(document.links[k]);
  
  for (i = 0; i < document.forms.length; i++)
    for (k = 0; k < document.forms[i].elements.length; k++) 
      save_obj(document.forms[i].elements[k]);
  
  save_events();
}

function onunload_handler()
{
  if (ync_dialog_window != null && !ync_dialog_window.closed)
    ync_dialog_window.close();
}

function open_ync_dialog(y, n, c)
{
  set_events();
  ync_dialog_value = null;

  ync_dialog_y_act = y;
  ync_dialog_n_act = n;
  ync_dialog_c_act = c;

  ync_dialog_window =
    window.open(
      '/html/ync_dialog.html?c=' + isbn_count, 'ync_dialog',
      'DIRECTORIES=0,LOCATION=0,MENUBAR=0,' +
      'RESIZABLE=0,SCROLLBARS=0,TOOLBARS=0,STATUS=0,' +
      'WIDTH=400,HEIGHT=100'
    );

  ync_dialog_window.moveTo(
    screen.width  / 2 - 200,
    screen.height / 2 -  50
  );
}

function on_ync_cancel() { 
	return;
}

function on_ync_yes() {
	clear_selection(isbn_form);
	location.href = url;
	/* location.href = getCookie("sendcarturl") +"?" + isbn_list; */
}

function on_ync_no() {
	location.href = url_to_move;
}

function sendcart(isbn)
{
	var form = document.right_main;
	isbn = unescape(isbn);
/*	isbn = isbn.replace(/-/g, ""); */

	var sendcarturl = getCookie("sendcarturl");

	if (sendcarturl == "") {
		alert(
			"クッキーを有効にして、情報を更新しなければ\n"
			+ "カートに入れられません。"
		);
		return;
	}

	if (typeof(form) == "undefined") {

location.href = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") + "&b=" + escape(location.href) + "&i=" + isbn +"&d=d";

		return;
	}

	if ((isbn_list = scan_single(form)) == "") {
location.href = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") +"&b="+escape(location.href)+"&i=" + isbn +"&d=d";
		return;
	}

	if (isbn_list == 'i=' + isbn) {
location.href = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") +"&b="+escape(location.href)+"&i=" + isbn +"&d=d";
		return;
	}

	isbn_form  = form;
	isbn_count = counts_parm(isbn_list);
	if (isbn_list.indexOf(isbn) < 0)  {
		isbn_list = isbn_list + '&i=' + isbn;
	}
wholecartchk();	
url_to_move = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") +"&b="+escape(location.href)+"&i=" + isbn +"&d=d";
	open_ync_dialog(on_ync_yes, on_ync_no, on_ync_cancel);
	return;
}

function narrowing(qid) 
{
	url_to_move = 'd3olp112cg?qid=' + qid;
	check_isbn_checkbox(document.right_main);
}

function redisplay(qid) 
{
  var imgdsp = document.right_main.imgdsp.options;
  var sortby = document.right_main.sortby.options;

  url_to_move =
    'd3olp113cg?' +
    'qid=' + qid + '&' +
    'imgdsp=' + imgdsp[imgdsp.selectedIndex].value + '&' +
    'sortby=' + sortby[sortby.selectedIndex].value
  ;
	check_isbn_checkbox(document.right_main);
}

function paging(pageno, qstr, dspword) 
{
	if (
		location.pathname == '/cgi-bin/d3olp123cg'
		|| location.pathname == '/cgi-bin/d3olp126cg'
	) {
		document.syshidden.pageno.value  = pageno;
		document.syshidden.keyword.value = qstr;
		document.syshidden.dspword.value = dspword;
		url_to_move = 'javascript:document.syshidden.submit();';
	} else {
		url_to_move = location.pathname + '?' + 'pageno=' + pageno + '&' + qstr;
	}
	check_isbn_checkbox(document.right_main);
}

function check_isbn_checkbox(form)
{
	if ((isbn_list = scan_single(form)) == "") {
		on_ync_no();
		return;
	}

	isbn_form  = form;
	isbn_count = counts_parm(isbn_list);

wholecartchk();	
	open_ync_dialog(on_ync_yes, on_ync_no, on_ync_cancel);
	return;
}

function open_window_for(qstr, mode, seqno)
{
  var q = "";                 /* query用文字列変数 */

  if (mode  != "") q = "&contributor=" + mode;
  if (seqno != "") q = "&seqno=" + seqno;
  window.open(qstr + q, 'comment', 'width=640,height=700,top=0,scrollbars=1')
}

function open_html(qstr, name, attr)
{
	if (typeof(attr) == "undefined" || attr == null) 
		attr = 'width=640,height=700,top=0,scrollbars=1';
	window.open(qstr, name, attr);
}

/* 書籍詳細へ */
function go_book_detail(act, isbn)
{
	if (booktype_for_go_book_detail == 'globe') {
		url_to_move = act + '?booktype=globe&isbn=' + isbn;
	} else {
		url_to_move = act + '?isbn=' + isbn;
	}
	check_isbn_checkbox(document.right_main);
}

/* カートの閲覧 */
function viewcart()
{
	var viewcarturl = getCookie("viewcarturl");

	if (viewcarturl == "") {
		alert(
			"クッキーを有効にして、情報を更新しなければ\n"
			+ "カートに入れられません。"
		);
		return;
	}

  location.href = viewcarturl;
}

/* チェック済み書籍のカート投入 */
function wholecart()
{
  var f = document.right_main;
  var q = "";
  var wb;
  var result;
  var m1 = "[ まとめてカート ]は、\n書籍を選択してからご利用ください。";
  var m2 = "このページ内に、選択済みの書籍が%s件あります。\n" +
           "この書籍をまとめてカートへ入れますか？";
  var m3 = "ページを移動します。よろしいですか？";

  q  = scan_single(f);
  wb = scan_whole(f);
  if(q == "") {
    alert(m1);
    return;
  } else if(q != "" && !wb) {
    result = confirm(m2.replace("%s", String(counts_parm(q))));
    if(result==false) {
      result = confirm(m3);
      if(result==false) return;
    }
  }

  clear_selection(f);
location.href = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") +"&" + q +"&d=d" + "&b="+ escape(location.href);
 /* location.href = getCookie("sendcarturl") +"?" + q;*/
  return;
}
/* 複数選択して通常のカート選択を押した際に使用するURL編集*/
function wholecartchk()
{
  var f = document.right_main;
  var q = "";
  var wb;
  var result;
  var m1 = "[ まとめてカート ]は、\n書籍を選択してからご利用ください。";
  var m2 = "このページ内に、選択済みの書籍が%s件あります。\n" +
           "この書籍をまとめてカートへ入れますか？";
  var m3 = "ページを移動します。よろしいですか？";

  q  = scan_single(f);
  wb = scan_whole(f);
  if(q == "") {
    alert(m1);
    return;
  } else if(q != "" && !wb) {
    result = confirm(m2.replace("%s", String(counts_parm(q))));
    if(result==false) {
      result = confirm(m3);
      if(result==false) return;
    }
  }

  clear_selection(f);
url = "/cgi-bin/d3olp171cg?u="+getCookie("sendcarturl") +"&" + q +"&d=d" + "&b="+ escape(location.href);
 /* location.href = getCookie("sendcarturl") +"?" + q;*/
  return;
}

/* まとめてカートの選択チェックBOXのカウント */
function scan_whole(f)
{
  for(var i = 0; i<f.elements.length; i++) {
    if (f.elements[i].type=="checkbox" && f.elements[i].checked) {
			return(true);
    }
  }
  return(false);
}

/* ISBN個々の選択チェックBOXのカウント */
function scan_single(f) {
  var q = "";

  for (var i = 0; i < f.elements.length; i++) {
    if (f.elements[i].type == "checkbox") {
      if (f.elements[i].checked) {
        if (q != "") { q += "&"; }
       /* q += "i=" + unescape(f.elements[i].value).replace(/-/g, "");*/
					q += "i=" + f.elements[i].value;
      }
    }
  }
  return q;
}

/* ISBNパラメータの数をカウント */
function counts_parm(q)
{
  var c = 0;
  var i = 0;
  var result;

  if(q.length == 0) return(c);
  while (i != -1) {
    i = q.indexOf("i=", i); /* 文字列検索 */
    if(i != -1) {
      i += 2;
      c++;
    }
  }
  return(c);
}

/* ページ内チェックBOXのクリア */
function clear_selection(f)
{
  for(var i=0; i<f.elements.length; i++) {
    if (f.elements[i].type=="checkbox") {
      if (f.elements[i].checked) {
        f.elements[i].checked = false;
      }
    }
  }
  return;
}

function getCookie(key,  tmp1, tmp2, xx1, xx2, xx3) {
    tmp1 = " " + document.cookie + ";";
    xx1 = xx2 = 0;
    len = tmp1.length;
    while (xx1 < len) {
        xx2 = tmp1.indexOf(";", xx1);
        tmp2 = tmp1.substring(xx1 + 1, xx2);
        xx3 = tmp2.indexOf("=");
        if (tmp2.substring(0, xx3) == key) {
            return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
        }
        xx1 = xx2 + 1;
    }
    return("");
}

function setCookie(key, val, tmp) {
    tmp = key + "=" + escape(val) + "; ";
    tmp += "path=" + "/" + "; ";
    document.cookie = tmp;
}

function clearCookie(key) {
    document.cookie = key + "=" + "xx; expires=1-Jan-1997 00:00:00;";
}

function get_argv(name) {
	var argstr = location.search;
	var head;
	var tail;

	if ((head = argstr.indexOf(name + '=')) < 0) return null;

	if ((tail = argstr.indexOf('&', head)) < 0) 
		return unescape(argstr.substring(head + name.length + 1, argstr.length()));
	
	return unescape(argstr.substring(head + name.length + 1, tail));
}

function get_path_dir() {
	var path = location.pathname;
	var pos;

	if ((pos = path.lastIndexOf('/')) == 0) return '';
	return path.substring(0, pos);
}

function get_path_file() {
	var path = location.pathname;
	var pos;

	if ((pos = path.lastIndexOf('/')) == 0) 
		return path.substring(0, path.length);

	return path.substring(pos + 1, path.length);
}

function get_icgi() {
	var icgi;
	if ((icgi = get_argv('icgi')) != null) return icgi;
	return get_path_file();
}

function get_bcgi() {
	var bcgi;
	if ((bcgi = get_argv('bcgi')) != null) return bcgi;
	return get_path_file();
}

function is_issue_page() {
	return (get_argv('bcgi') != null);
}

function get_rest_search(namelist) {
	var argstr = location.search;
	var names  = namelist.split(/,/);
	var pos;
	var i;

	for (i = 0; i < names.length; i++)
		if ((pos = argstr.indexOf(names[i] + '=')) >= 0) 
			break;

	if ((pos = argstr.indexOf('&', pos)) < 0)
		return '';
	
	return argstr.substring(pos + 1, argstr.length);
}

function gobkno(code) {
	location.href = 
		get_path_dir() + '/' + get_bcgi()
		+ '?code=' + code + '&icgi=' + get_icgi()
		+ '&' + get_rest_search('bcgi,icgi');
}

function goissue(code, anch) {
	var my_anch = '';

	if (anch != '') my_anch = '#' + anch;

	location.href = 
		get_path_dir() + '/' + get_icgi()
		+ '?code=' + code + '&bcgi=' + get_bcgi() 
		+ '&' + get_rest_search('bcgi,icgi')
		+ my_anch;
}

function golate(code) {
	goissue(code, '');
}

function ymdpaging(code) {
	if (is_issue_page())
		goissue(code, '');
	else
		gobkno(code);
}

window.onload   = onload_handler;
window.onunload = onunload_handler;
