﻿String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
function setAd(ad)
{
	try
	{
		document.getElementById(ad).appendChild ( document.getElementById(ad+"s") );
		document.getElementById(ad+"s").style.display = "";
	}
	catch(e)
	{}
}
function setAd2(ad)
{
	try
	{
		document.getElementById(ad).innerHTML = document.getElementById(ad+"s").innerHTML;
	}
	catch(e)
	{}
}
function getCookie(namex)
{
	var cookies = document.cookie;
	var cookieHeader = namex + "=";
	var beginPosition = cookies.indexOf(cookieHeader);
	if (beginPosition != -1) 
	{
		var acookie = cookies.substring(beginPosition + cookieHeader.length);
		if (acookie.indexOf(";")>-1)
		{
			acookie = acookie.substring(0, acookie.indexOf(";"));
		}
		return acookie;
	}
	return "";
}
function useloggingname()
{
	var userid = decodeURI(getCookie("x_username"));
	if (userid=='')
	{
		alert("请先登陆");
		return;
	}
	document.getElementById("comment_author").value = userid;
}
function loadCookie()
{
	var author = getCookie("author");
	comment_form.author.value = author;
}
function submit_comment()
{
	if (locked==1)
	{
		alert("请刷新页面再继续评论");
		return false;
	}
	
	var comment_form = document.getElementById("comment_form");
	var author = comment_form.author.value.trim();
	var body = comment_form.body.value.trim();

	
	if (author=="")
	{
		alert("请填写名字");
		return false;
	}
	if (body=="")
	{
		alert("请填写内容");
		return false;
	}
	
	if (document.getElementById("rem").checked)
	{
		var exDate = new Date;
		exDate.setMonth(exDate.getMonth()+9);
		document.cookie = "author=" + author.replace(/;/ig,",") + ";path=/; domain="+self.location.host+"; expires=" + exDate.toGMTString();
	}


	var http;
	var text;
	var url = "/addcomment.jsp";
	try
	{
		http = new XMLHttpRequest();
	}
	catch(e)
	{
		http = new ActiveXObject( "Microsoft.XMLHTTP" );
	}
	try
	{
		http.open("POST", url, true);
		http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http.onreadystatechange = function () 
		{
			if (http.readyState == 4)
			{
				if(http.status==200)
				{
					text = http.responseText;
					if (text.trim()!="")
					{
						//alert(text.trim());
					}
					else
					{
						locked = 1;
						var comment_form = document.getElementById("comment_form");
						var author = comment_form.author.value.trim();
						var body = comment_form.body.value.trim();
						var ptime = document.getElementById("c_ptime").value;
						var template = document.getElementById("comment_template").innerHTML;
						template = template.replace(/\%5B/gi, "[");
						template = template.replace(/\%5D/gi, "]");
						template = template.replace("[author]", author);
						template = template.replace("[author]", author);
						template = template.replace("[ptime]", ptime);
						template = template.replace("[body]", body.replace(/\n/gi,"<br>"));

						document.getElementById("comment").innerHTML+=template;
						document.getElementById("commentbutton").disabled = true;
					}
				}
			}
		}
		//var out = jsonrpc.commentbs.insertComment(commentid, mp3id, commentuserid, body, author, homepage, hash);
		var data =  "picuserid="+encodeURIComponent(document.getElementById("c_picuserid").value);
		data     += "&imgid="+encodeURIComponent(document.getElementById("c_imgid").value);
		data     += "&pageuserid="+encodeURIComponent(document.getElementById("c_pageuserid").value);
		data     += "&body="+encodeURIComponent(body);
		data     += "&author="+encodeURIComponent(author);
		http.send(data);
	}
	catch(e)
	{
		//alert(e);
	}
	return false;
}
function uphits(pageuserid, imgid)
{
	var http;
	var text;
	var url = "/uphits.jsp";
	try
	{
		http = new XMLHttpRequest();
	}
	catch(e)
	{
		http = new ActiveXObject( "Microsoft.XMLHTTP" );
	}
	try
	{
		http.open("POST", url, true);
		http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http.onreadystatechange = function () 
		{
			if (http.readyState == 4)
			{
				if(http.status==200)
				{
					text = http.responseText;
				}
			}
		}
		var data =  "imgid="+encodeURIComponent(imgid);
		data     += "&userid="+encodeURIComponent(pageuserid);
		http.send(data);
	}
	catch(e)
	{
		//alert(e);
	}
}
function clogin()
{
	if (getCookie("x_username")!=""&&getCookie("x_password")!="")
	{
		return true;
	}
	return false;
}