﻿var xmlhttp;
var discuss_xmlDoc;
var post_url;
var already_post_discuss=false;
var all_discuss=false;
function loadXMLDoc(url,ispost)
{
	xmlhttp=null;
	post_url=url;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		if (!ispost) {
		    xmlhttp.open("GET", url, true);
		    xmlhttp.send(null);
		}
		else {
		    url111 = url.substring(0, url.indexOf('?'));
		    url112 = url.substring(url.indexOf('?') + 1, url.length);
		    xmlhttp.open("POST", url111, false);
		    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		    xmlhttp.send(url112);
		}
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
    {
		if(get_action(post_url)=='post')
		{
			alert(xmlhttp.responseText);
			if(xmlhttp.responseText=='评论发布成功!')
			{
				
				already_post_discuss=true;
				if(!all_discuss)
					document.getElementById('discuss_root').innerHTML=document.getElementById('discuss_root').innerHTML.toLowerCase().replace('</th></tr>',
						'</th></tr><tr><td class="TableBody2" style="color:blue;"><b>'+document.getElementById('discuss_username').value+'</b>发表的评论：<font color="#777777">'
						+'由您刚发布</font></td></tr>'
						+'<tr><td>'+document.getElementById('discuss_text').value.replace('\n','<br>')+'</td></tr>');
				else
					document.getElementById('discuss_root').innerHTML=document.getElementById('discuss_root').innerHTML.toLowerCase().replace('</table>',
						'<tr><td class="TableBody2" style="color:blue;"><b>'+document.getElementById('discuss_username').value+'</b>发表的评论：<font color="#777777">'
						+'由您刚发布</font></td></tr>'
						+'<tr><td>'+document.getElementById('discuss_text').value.replace('\n','<br>')+'</td></tr></table>');
				
					
			}
				
		}
		else
			create_discuss();
    }
  else
    {
		alert("Problem retrieving XML data");
    }
  }
}

	function import_discuss(path)
{
    document.write('<div id="discuss_root"></div>');
	loadXMLDoc(path,false);
}

function create_discuss()
{

	if(xmlhttp==null)
		return;
	discuss_xmlDoc=xmlhttp.responseXML;
	d_count=discuss_xmlDoc.documentElement.tagName.replace('Data','');
	var x = discuss_xmlDoc.getElementsByTagName('discuss');
	var dicuss_content='';
	var discuss_info = new Array("id","discuss_txt","name","posttime");
	discuss_content='<hr><table width="100%"><tr><th><b>评　　论(共'+d_count+'条,显示'+x.length+'条)</b></th></tr>';
	if(x.length>0)
	{
		for(j=0;j<x[0].childNodes.length;j++)
		{
			if(x[0].childNodes[j].nodeType==1)
				discuss_info[x[0].childNodes[j].nodeName]=j;
		}
		for (i=0;i<x.length;i++)
		{
			discuss_content+='<tr><td class="TableBody2"><b>'+x[i].childNodes[discuss_info["name"]].firstChild.nodeValue+'</b>发表的评论：<font color="#777777">'
				+replace_date(x[i].childNodes[discuss_info["posttime"]].firstChild.nodeValue)+'</font></td></tr>'
				+'<tr><td>'+x[i].childNodes[discuss_info["discuss_txt"]].firstChild.nodeValue.replace('\n','<br>')+'</td></tr>';
		}
		if(!all_discuss&&x.length<d_count)
			discuss_content+='<tr><td align="right"><br><br><a href="/gb/discuss.aspx?action=more_'+get_action(post_url)+'&id='+get_ID(post_url)+'" target="_balnk"><b>更多评论</b></a></td></tr>';
		
	}
	else
	{
		discuss_content+='<tr><td>暂无评论。</td></tr>';
	}
	
	discuss_content+='</table>';
	
	document.getElementById('discuss_root').innerHTML=discuss_content;
}


function creat_discuss_post(action,id)
{
	document.write('<div id="discuss_post"></div>');
	document.getElementById('discuss_post').innerHTML='<br><br><table align="center">'
		+'<tr><td class="TableBody2"><a name=abc></a><form action=/gb/discuss.aspx method=post onSubmit="return check_discuss_post(this);">'
		+'<input type=hidden name=id value='+id+'><input type=hidden name=action value=post><input type=hidden name=kind value='+action+'>'
		+'您的称呼：<input type=text name=username id="discuss_username">　<input type=checkbox onclick="username.value=\'网友\'">匿名<br>'
		+'<textarea name="discuss" id="discuss_text" style="width:485px;height:100px;" onclick="if(this.value==\'请在这里写上你的评论（200字以下）\') this.value=\'\';">请在这里写上你的评论（200字以下）</textarea>'
		+'<div align="center">请输入右边的验证码：<input tpye=text name=code size=4>'
		+'<span style="font-size:9pt;background-color:DarkBlue;color:yellow;font-weight:bold;height:20;"><img src="/gb/send_pic.aspx?action=get_num1" align="absbottom"><img src="/gb/images/ver_pic/'
		+get_dateOfyear()%9+'.gif" align="absbottom"><img src="/gb/send_pic.aspx?action=get_num2" align="absbottom"></span>'
		+'<br><input type="submit" value="发表评论"></div>'
		+'</form></center></td></tr>'
		+'</table>';
}

function check_discuss_post(theform)
{
	if(already_post_discuss)
	{
		alert('你已经发表过评论了，如要再次发表，请刷新页面。');
		return false;
	}
	if(theform.username.value==''||theform.discuss.value=='')
	{
		alert('请先填写内容');
		return false;
	}
	loadXMLDoc('/gb/discuss.aspx?action=post&id='+theform.id.value+'&kind='
	+theform.kind.value+'&username='+escape(theform.username.value)+'&discuss='+escape(theform.discuss.value)+'&code='+theform.code.value,true);
	return false;

}

function get_dateOfyear()
{
	
	var day=new Date();
	var day_count=0;
	var month_num=new Array("1","2","3","4","5","6","7","8","9","10","11","12");
	month_num["1"]=31;
	if(day.getFullYear()%4!=0)
		month_num["2"]=28;
	else
		month_num["2"]=29;
	month_num["3"]=31;
	month_num["4"]=30;
	month_num["5"]=31;
	month_num["6"]=30;
	month_num["7"]=31;
	month_num["8"]=31;
	month_num["9"]=30;
	month_num["10"]=31;
	month_num["11"]=30;
	month_num["12"]=31;
	for(i=0;i<day.getMonth();i++)
	{
		day_count+=month_num[i+1];
	}
	day_count+=day.getDate();
	return day_count;
}


function replace_date(str)
{
	
	str=str.replace('T',' ');
	str=str.replace(':00.0000000+08:00','');
	return str;
}

function get_action(url1)
{
	loc=url1.indexOf('action=');
	if(loc==-1)
		return 'error';
	sub_url=url1.substring(loc+7);
	if(sub_url.indexOf('&')==-1)
		return sub_url;
	else
		return sub_url.substring(0,sub_url.indexOf('&'));
	
}

function get_ID(url1)
{
	loc=url1.indexOf('id=');
	if(loc==-1)
		return 'error';
	sub_url=url1.substring(loc+3);
	if(sub_url.indexOf('&')==-1)
		return sub_url;
	else
		return sub_url.substring(0,sub_url.indexOf('&'));
	
}		