///////////////////////////////////////
// e-kodate.js
 
function getJSONData(filename) {
  var xmlhttp = createXMLHttpRequest();
  var jsondata = "";
  if (xmlhttp) {
    xmlhttp.open("GET", filename, false);
    xmlhttp.send(null);
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      jsondata = eval("(" + xmlhttp.responseText + ")");
    }
  }
  return jsondata;
}

function getCookie(item) {
    var i, index, arr;
    arr = document.cookie.split(";");
    for(i = 0; i < arr.length; i++) {
        index = arr[i].indexOf("=");
        if(arr[i].substring(0, index) == item || 
                arr[i].substring(0, index) == " " + item)
            return arr[i].substring(index + 1);
    }
    return "";
}

function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}


function dispFileIntoElement(url,elm){
	var xmlhttp = createXMLHttpRequest();
	if (xmlhttp) {
		xmlhttp.open("GET", url, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById(elm).innerHTML = xmlhttp.responseText;
		}
	}
	return false;
}

function getFile() {
	var xmlhttp = createXMLHttpRequest();
	var filename = "/all_thread.txt";
	if (xmlhttp) {
		xmlhttp.open("GET", filename, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("all_thread").innerHTML = xmlhttp.responseText;
		}
	}
	return null;
}

function getIP(){
	var xmlhttp = createXMLHttpRequest();
	if (xmlhttp) {
		xmlhttp.open("GET", '/cgi-local/checkIP.cgi', false);
		xmlhttp.send(null);
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			return xmlhttp.responseText;
		}
	}
	return '';
}

function createXMLHttpRequest() {
   return this.XMLHttpRequest ? 
      new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}

function jump(obj) {
  var url = obj.options[obj.selectedIndex].value;
  location.href = url;
}

function submitRes(elm) {
  if(elm.name.value == '') {
    alert("名前を入力または匿名を選択してください。");
    return false;
  } else if(elm.body.value == '') {
    alert("本文を入力してください。");
    return false;
  } else {
//    var hostName = getIP();
//    var conFirm = confirm("投稿してもよろしいですか？\n\n※投稿データと共に以下のホスト情報がサーバーに記録されます。\n" + hostName);

    var conFirm = confirm("投稿します。誤字脱字・書き間違いはありませんか？");
    if(conFirm) {
      setCookie('name_local',encodeURIComponent(elm.name.value),90);
      elm.submit.disabled = true;
    }
    return conFirm;
  }
}


function confirmThread() {

  if( document.getElementById("confirm_sub").value == '') {
    alert("題名を入力してください。");
    return false;
  } else if( document.getElementById("confirm_name").value == '') {
    alert("名前を入力または匿名を選択してください。");
    return false;
  } else if( document.getElementById("confirm_com").value == '') {
    alert("本文を入力してください。");
    return false;
	} else {

	document.getElementById("confirm_thread").style.display = "block";
	document.getElementById("enter_thread").style.display = "none";

	var str = '' ;
//  var hostName = getIP();

	str = '<div style="font-size:20px;font-weight:bold;font-family:meiryo;">' + document.getElementById("confirm_sub").value + '</div>\n' ;
	str += '<div style="width:100%;font-size:12px;font-family:meiryo;background-color:#EFEFEF;">名前：' + document.getElementById("confirm_name").value + '</div>\n\n' ;
	str += '<div style="font-size:14px;font-family:meiryo;">' + document.getElementById("confirm_com").value + '</div>\n\n' ;
	str += '<input type="submit" value="新規スレッド作成" name="submit">\n\n' ;
	str += '<input type="button" value="入力画面に戻る" onClick="return backThread();">' ;

  document.getElementById("confirm_thread").innerHTML = '<pre style="white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;white-space: pre-wrap;word-wrap: break-word;">' + str + '</pre>' ;

//	if(conFirm) {
//		setCookie('name_local',encodeURIComponent(elm.name.value),90);
//		elm.submit.disabled = true;
//	}
//	return conFirm;
//	location.hash = "#";

  return true;
	}
}

function backThread() {
	document.getElementById("confirm_thread").style.display = "none";
	document.getElementById("enter_thread").style.display = "block";
	location.hash = "#newthread";
}


function submitThread(elm) {
  if(elm.name.value == '') {
    alert("名前を入力または匿名を選択してください。");
    return false;
  } else if(elm.body.value == '') {
    alert("本文を入力してください。");
    return false;
  } else if(elm.title.value == '') {
    alert("題名を入力してください。");
    return false;
  } else {
//    var hostName = getIP();
//    var conFirm = confirm("投稿してもよろしいですか？\n\n※投稿データと共に以下のホスト情報がサーバーに記録されます。\n" + hostName);

    var conFirm = confirm("投稿します。誤字脱字・書き間違いはありませんか？");
    if(conFirm) {
      setCookie('name_local',encodeURIComponent(elm.name.value),90);
      elm.submit.disabled = true;
    }
    return conFirm;
  }
}

function checkQueryStr(form) {
	if(!form.q.value){
		alert('検索条件を入力してください。');
		form.q.focus();
		return false;
	} else {
		return true;
	}
}

function selectTokumei(elm) {
  if(elm.tokumei.options[elm.tokumei.selectedIndex].value != '#') {
    elm.name.value = elm.tokumei.options[elm.tokumei.selectedIndex].value;
  }
}

function openThreadMakingForm(){
  if (document.getElementById("bbsHeaderForm").style.display == "block") {
    document.getElementById("bbsHeaderForm").style.display = "none";
  } else {
    document.getElementById("bbsHeaderForm").style.display = "block";
  }
}

function openBlock(name){
  if (document.getElementById(name).style.display == "block") {
    document.getElementById(name).style.display = "none";
  } else {
    document.getElementById(name).style.display = "block";
  }
}




///////////////////////////////////////
// 投稿フォームポップアップ

function showHiddenForm(e){
 var x;
 var y;
 
 var mainTop = document.getElementById('main').offsetTop;

 if(window.event != null){
  
  x = window.event.x + document.body.scrollLeft;
  
  if(document.all && window.XMLHttpRequest && !window.opera){
   //IE7
   y = window.event.clientY + document.body.scrollTop - mainTop;
  }else{
   //Opera and Chrome
   y = window.event.y + document.body.scrollTop - mainTop;
  }
 }else{
  //FF
  x = e.pageX;
  y = e.pageY - mainTop;
 }
 
 var hiddenform = document.getElementById('hiddenform');
 hiddenform.style.display  = 'block';
 //hiddenform.style.position = 'absolute';
 hiddenform.style.left    = '639px' ;
 hiddenform.style.top      = y ;
 hiddenform.style.zIndex   = 999;
 
 return false;
}

function hideHiddenForm(e){
 var hiddenform = document.getElementById('hiddenform');
 hiddenform.style.display  = 'none';
 return false;
}


function stopPropagation(evt){
if(evt && evt.stopPropagation){
evt.stopPropagation();
}else if(window.event){
window.event.cancelBubble = true;
}
}


/* ---------------------------------------------------------
□ CookieClass
--------------------------------------------------------- */
/**
 * Cookie()コンストラクタ
 * 指定したcookie情報を設定する
 * @param cname cookie名
 */
function Cookie(cname) {
	//cookieの名前を記憶する
	this.$name = cname ;
	
	//このドキュメントに付随するcookieをすべて読み込む
	var cookieObj = document.cookie ;
	//cookieが存在しない場合は処理を終了
	if ( cookieObj == "" ) return ;
	
	var cookies = cookieObj.split(';') ;
	var cookie = null ;
	for ( var i = 0 ; i < cookies.length ; i++ ) {
		//引数で指定されたcookie名の場合
		cookies[i] = cookies[i].replace(/(^\s+)|(\s+$)/g, "") ;
		if (cookies[i].substring(0, cname.length+1) == (cname + "=")) {
			cookie = cookies[i] ;
			break ;
		}
	}
	//指定したcookieが存在しない場合は処理を終了
	if ( cookie == null ) return ;
	
	//cookieに格納されている値を取得する
	var cval = cookie.substring(cname.length+1) ;
	var wk = cval.split('&') ;
	for ( var i = 0 ; i < wk.length ; i++ ) {
		wk[i] = wk[i].split(':') ;
	}
	for ( var i = 0 ; i < wk.length ; i++ ) {
		//デコードした値を格納する
		this[wk[i][0]] = decodeURIComponent(wk[i][1]);
	}
}

/**
 * cookieの発行処理
 * @param cduration cookieの有効期限（0:Cookie削除、null:セッションクッキー（ブラウザ終了時削除））
 * @param cpath cookieのパス
 * @param cdomain cookieのドメイン
 * @param csecure cookieのsecure属性 （true:secureを設定する、false:secureを設定しない）
 */
Cookie.prototype.publish = function ( cduration , cpath , cdomain , csecure ) {
	var cval = "" ;
	for ( var prop in this ) {
		//$で始まるプロパティ、メソッドを除く
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue ;
		if (cval != "") cval += '&' ;
		cval += prop + ':' + encodeURIComponent(this[prop]) ;
	}
	
	//cookieに保存する文字列を編集する
	var cookie = this.$name + '=' + cval ;
	if ( cduration || cduration == 0 ) {
		var expi = new Date();
		expi.setTime(expi.getTime() + cduration*24*60*60*1000);
		
		cookie += "; expires=" + expi.toGMTString() ;
		//cookie += "; max-age=" + (cduration*24*60*60) ;
	}
	if ( cpath ) cookie += "; path=" + cpath ;
	if ( cdomain ) cookie += "; domain=" + cdomain ;
	if ( csecure ) cookie += "; secure" ;

	document.cookie = cookie ;
}

/**
 * cookieの削除処理
 * @param cpath cookieのパス
 * @param cdomain cookieのドメイン
 * @param csecure cookieのsecure属性 （true:secureを設定する、false:secureを設定しない）
 */
Cookie.prototype.remove = function( cpath , cdomain , csecure ) {
	//取得しているcookie情報を削除する
	for(var prop in this) {
		if (prop.charAt(0) != '$' && typeof this[prop] != 'function') 
			delete this[prop] ;
	}
	//有効期限0でcookieを発行する
	this.publish( 0, cpath , cdomain , csecure ) ;
}

/**
 * cookieの有効判定
 * 有効な場合はtrue、無効な場合はfalseを返す
 * @return （true:有効、false:無効）
 */
Cookie.enabled = function() {
	//使用中のブラウザが、navigator.cookieEnabledが使用できる場合には、
	//navigator.cookieEnabledにて判定を行う
	if ( navigator.cookieEnabled != undefined ) return navigator.cookieEnabled ;
	
	//すでに判定済みであれば、判定結果をキャッシュから返す
	if ( Cookie.enabled.cache != undefined ) return Cookie.enabled.cache ;
	
	//とりあえずcookieを発行してみる
	document.cookie = "checkcookie=test; max-age=10000" ;
	var cookies = document.cookie ;
	if ( cookies.indexOf("checkcookie=test") == -1 ) {
		//発行できなかったので、結果をキャッシュに保存してfalseを返す
		return Cookie.enabled.cache = false ;
	} else {
		//発行したcookieを削除し、
		document.cookie = "checkcookie=test; max-age=0" ;
		//結果をキャッシュに保存してtrueを返す
		return Cookie.enabled.cache = true ;
	}
}

/* ---------------------------------------------------------
□  changeLetterSize
--------------------------------------------------------- */
var cname = "ab_hr_sel_takabbs" ;//Cookie名
var cterm = 365 ;//Cookieの保存日数
var cpath = "/" ;//Cookieのパス

//文字サイズ変更関数
function changeLetterSize(letterSize) {
	if(letterSize=="big") {
		document.getElementsByTagName("body")[0].style.fontSize = "16px";
	} else if(letterSize=="middle") {
		document.getElementsByTagName("body")[0].style.fontSize = "14px";
	} else {
		document.getElementsByTagName("body")[0].style.fontSize = "12px";
	}
	var cookie = new Cookie(cname) ;
	cookie.fsize = letterSize ;
	cookie.publish(cterm , cpath) ;
}

//文字サイズCookie判定
var cookie = new Cookie(cname) ;
if (cookie.fsize) {
	if(cookie.fsize=="big") {
		document.write('<style type="text/css">body{ font-size: 16px;}</style>');
	} else if (cookie.fsize=="middle") {
		document.write('<style type="text/css">body{ font-size: 14px;}</style>');
	} else {
		document.write('<style type="text/css">body{ font-size: 12px;}</style>');
	} 
}


