 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("");
}

// RATING //

var rt_submitted = 0;
var rt_flag;

function submit_rating(flag,vid,zid){

	rt_flag = flag;

	if(!rt_submitted++){
		new Ajax.Request("/rating" , 
										 {method: "get" , 
											parameters: 'vid=' + vid + '&zid=' + zid + '&flag=' + flag + '&salt=' + Math.random(),
											onComplete: ratingResult});

	} else {
		set_rating_comment("投票は１人１回までしか受け付けていません。");
	}
	void(0);
}

function ratingResult(response){
	var res = response.responseText;
	if(res == "already"){
		set_rating_comment("<font color=#AA7777>過去に既に投票をしているようです。投票は1人1回までです。</font>");
	} else if(res == "done"){
		set_rating_comment("<font color=#7777AA>投票を受け付けました。</font>");
		
		$(rt_flag + "_html").innerHTML =	parseInt($(rt_flag + "_html").innerHTML) + 1;
		
	} else {
		set_rating_comment("エラー。何らかの原因で投票できませんでした。後ほど試してみてください。");
		rt_submitted = 0;
	}
}

function set_rating_comment(message){
	$("rating_comment").innerHTML = message;
}

// BBS //

var submit_form_view_flag = 1;

function submit_form_view(flag){
	$('bbs_submit_form').style.display=(flag ? "block" : "none");
	submit_form_view_flag = (submit_form_view_flag ? 0 : 1);
}

function bbs_submit(vid,zid,n){

	if($("comment").value){

		if($("rating:good").checked || $("rating:bad").checked) { 
			var rating_flag = ($("rating:good").checked ? "good" : "bad");
			submit_rating(rating_flag,vid,zid);
		}



		$('commentSubmitButton').disabled=1;
		$('bbs_submit_message').innerHTML = "投稿中";
		
		new Ajax.Request("/bbs" , 
										 {method: "get" , 
											parameters: Form.serialize("bbs_submit_form"),
											onComplete: bbs_submit_result
										 }
										);

	} else {
		$("bbs_submit_message").innerHTML = "コメントが空白です。";
	}
//	return false;
}

function bbs_submit_result(httpObj){
	$("bbs_comments").innerHTML = httpObj.responseText;
	$('comment').value = "";
	$('bbs_submit_message').innerHTML = "投稿完了しました。";
	$("rating:bad").checked = false;
	$("rating:good").checked = false;	
	
	if($("refresh")){
		location.href = "/w/" + $("v").value + "/" + $("z").value + "?mode=comment";
		return;
	}
	
	setTimeout(function(){ $('commentSubmitButton').disabled = 0 ;	$('bbs_submit_message').innerHTML = "";} , 10000);
}


// TOP //
var first_flag = 0;

function hoge(){
	
	if(!first_flag){
		if($("comment_text")){
			$("comment_text").value = "";
			$("comment_text").style.color = "black";
			first_flag = 1;
		}
	}
	
}

function enable_comment(flag){
//	alert(flag);
	if(flag){
		first_flag = 0;
		$("comment").innerHTML = "名前：<input size=6 name=name>&nbsp;" + 
															"一言：<input size=40 id=comment_text name=comment value='字幕をつけてくれる人へのメッセージを一言！' style='color:#999999' onClick='hoge()' onFocus='hoge()'><br>";
	} else {
		$("comment").innerHTML = "";
	}
}

function set_comment(comment){
	$("comment").value = comment;
	hoge();
}

