﻿//索引
//保存留言
//显示留言回复表单
//保存留言回复
//保存评论
//显示评论回复表单
//保存评论回复
//更新文章阅读次数
//验证码，显示问题
//验证码，校验答案
//删除留言、评论、回复
//保存移动回复



//////////////////////////////////////////////////////
//保存留言
//////////////////////////////////////////////////////
function save_friend_say(){
	var txt_author = get_input("txt_author");
	var txt_email = get_input("txt_email");
	var txt_blog = get_input("txt_blog");
	var txt_say = get_input("txt_say");

	//去除左右空格
	txt_author = txt_author.trim();
	txt_email = txt_email.trim();
	txt_blog = txt_blog.trim();
	txt_say = txt_say.trim();
	
	//编码
	//txt_say = txt_say.replace(new RegExp("\n", "g"), "\n\r");

	boolean_save = (txt_author && txt_say);
	if (!boolean_save){
		alert("大名 + 想说的话！");
	}
	else{
		//检验验证码
		var txt_security_code = get_element_by_id("txt_security_code");
		
		check_answer(txt_security_code);
		
		if (get_div("div_result", 1) != "真棒，智商非比寻常！"){
			alert ("请输入正确的验证码！");
		}
		else{
			display_tips_saving(2);
			set_cookies();
			
			//escape,encodeURI,encodeURIComponent
			txt_author = encodeURIComponent(txt_author);
			txt_email = encodeURIComponent(txt_email);
			txt_blog = encodeURIComponent(txt_blog);
			txt_say = encodeURIComponent(txt_say);
			//alert(txt_say);
			
			var strForm = "txt_author=" + txt_author + "&txt_email=" + txt_email + "&txt_blog=" + txt_blog + "&txt_say=" + txt_say;
			//strForm = encodeURI(strForm);
			//alert(decodeURIComponent(strForm));
			
			var url = "ajax_function.asp?action=SaveFriendSay";
			
			var async_mode = true;
			xmlHttp = createXMLHTTP();
			
			//异步模式（IE和Firefox都支持onreadystatechange）
			xmlHttp.onreadystatechange = function() {stateChanged(6)};
			
			xmlHttp.open("POST", url, async_mode);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(strForm);
			
			//同步模式（Firefox不支持onreadystatechange，直接调用xmlHttp.responseText）
//			if (!async_mode && navigator.userAgent.indexOf("Firefox")>0) {
//				display_tips_saved();
//				set_div("div_message", xmlHttp.responseText + get_div("div_message", 2), 2);
//				gen_question(2);
//			}
			
			//如果是第一条留言，则隐藏"暂时没有朋友留言！"
			if (get_element_by_id("div_no_friend_say")){
				hide_element_by_id("div_no_friend_say");
			}
		}
	}
}

//////////////////////////////////////////////////////
//显示留言回复表单
//////////////////////////////////////////////////////
function display_friend_say_reply_form(article_id){
	var div_friend_message_reply_form = "div_friend_message_reply_form_" + article_id;
	
	if (!get_div(div_friend_message_reply_form, 2)){
		set_div(div_friend_message_reply_form, "正在载入表单，请稍后……", 1);
		
		var url = "ajax_function.asp?action=DisplayFriendSayReplyForm&ArticleID=" + article_id;
		xmlHttp = createXMLHTTP();
		xmlHttp.onreadystatechange = function() {stateChanged_2(1, article_id)};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

//////////////////////////////////////////////////////
//保存留言回复
//////////////////////////////////////////////////////
function save_friend_say_reply(article_id){
	var txt_reply = "txt_reply_" + article_id;
	var txt_reply = get_input(txt_reply);
	
	//去除左右空格
	txt_reply = txt_reply.trim();
	boolean_save = (txt_reply);
	
	if (!boolean_save){
		alert("总得说点什么吧！");
	}
	else {
		txt_reply = encodeURIComponent(txt_reply);
		
		var strForm = "txt_reply=" + txt_reply;

		var url = "ajax_function.asp?action=SaveFriendSayReplyABC&ArticleID=" + article_id;

		xmlHttp = createXMLHTTP();

		switch(browser_type){
			case 1:xmlHttp.onreadystatechange = function() {stateChanged_2(2, article_id)};break;
		}

		xmlHttp.open("POST", url, false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(strForm);
		
		switch(browser_type){
			case 2:
				var div_friend_say_reply = "div_friend_say_reply_" + article_id;
				var div_friend_say_reply_form = "div_friend_say_reply_form_" + article_id;
				set_div(div_friend_say_reply, get_div(div_friend_say_reply, 2) + xmlHttp.responseText, 2);
				set_div(div_friend_say_reply_form, "", 2);
				break;
		}
	}
}



//////////////////////////////////////////////////////
//保存评论
//////////////////////////////////////////////////////
var TBP_blog_name, TBP_excerpt, TBP_url, TBP_title;
function save_comment(article_id){
	var txt_author = get_input("txt_author");
	var txt_email = get_input("txt_email");
	var txt_blog = get_input("txt_blog");
	var txt_say = get_input("txt_say");
	
	txt_author = txt_author.trim();
	txt_email = txt_email.trim();
	txt_blog = txt_blog.trim();
	txt_say = txt_say.trim();
	
	boolean_save = (txt_author && txt_say);
	if (!boolean_save){
		alert("大名 + 想说的话！");
	}
	else {
		//检验验证码
		var txt_security_code = get_element_by_id("txt_security_code");
		check_answer(txt_security_code);
		
		if (get_div("div_result", 1) != "真棒，智商非比寻常！"){
			alert ("请输入正确的验证码！");
		}
		else {
			//显示正在保存提示
			display_tips_saving(1);
			//设置cookies
			set_cookies();
			
			//设置Trackblog的全局变量
			TBP_blog_name = txt_author;
			TBP_excerpt = txt_say;
			//静态还是动态页面
			TBP_url = article_id + ".html";
			//TBP_url = "read.asp?ArticleID=" + article_id;
			TBP_title = TBP_excerpt.substr(0, 10);
			
			txt_author = encodeURIComponent(txt_author);
			txt_email = encodeURIComponent(txt_email);
			txt_blog = encodeURIComponent(txt_blog);
			txt_say = encodeURIComponent(txt_say);
			
			var strForm = "txt_author=" + txt_author + "&txt_email=" + txt_email + "&txt_blog=" + txt_blog + "&txt_say=" + txt_say + "&article_id=" + article_id;
			
			var url = "ajax_function.asp?action=SaveComment";
			
			var async_mode = true;
			xmlHttp = createXMLHTTP();

			//异步模式（IE和Firefox都支持onreadystatechange）
			xmlHttp.onreadystatechange = function() {stateChanged_2(5, article_id)};
			
			xmlHttp.open("POST", url, async_mode);
			//设置Header，post方法必须设置，且必须在open之后
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(strForm);
		}
	}
}

//////////////////////////////////////////////////////
//显示评论回复表单
//////////////////////////////////////////////////////
function display_message_reply_form(article_id, article_id_comment){
	var div_message_reply_form = "div_message_reply_form_" + article_id_comment;
	
	if (!get_div(div_message_reply_form, 2)){
		set_div(div_message_reply_form, "正在载入表单，请稍后……", 1);
		
		var url = "ajax_function.asp?action=DisplayCommentReplyForm&ArticleID=" + article_id + "&ArticleIDComment=" + article_id_comment;
		xmlHttp = createXMLHTTP();
		xmlHttp.onreadystatechange = function() {stateChanged_2(3, article_id_comment)};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

//////////////////////////////////////////////////////
//保存评论回复
//////////////////////////////////////////////////////
function save_message_reply(article_id, article_id_comment){
	var txt_reply = "txt_reply_" + article_id_comment;
	var txt_reply = get_input(txt_reply);
	
	//去除左右空格
	txt_reply = txt_reply.trim();
	boolean_save = (txt_reply);
	
	if (!boolean_save){
		alert("总得说点什么吧！");
	}
	else {
		txt_reply = encodeURIComponent(txt_reply);
		
		var strForm = "txt_reply=" + txt_reply;

		var url = "ajax_function.asp?action=SaveCommentReplyABC&ArticleIDComment=" + article_id_comment;

		xmlHttp = createXMLHTTP();

		switch(browser_type){
			case 1:xmlHttp.onreadystatechange = function() {stateChanged_2(4, article_id_comment)};break;
		}

		xmlHttp.open("POST", url, false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(strForm);
		
		switch(browser_type){
			case 2:
				var div_comment_reply = "div_comment_reply_" + article_id_comment;
				var div_message_reply_form_ = "div_message_reply_form_" + article_id_comment;
				set_div(div_comment_reply, get_div(div_comment_reply, 2) + xmlHttp.responseText, 2);
				set_div(div_message_reply_form, "", 2);
				break;
		}
		
		url = "g_html.asp?ArticleID=" + article_id;
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
	}
}






//////////////////////////////////////////////////////
//更新文章阅读次数
//////////////////////////////////////////////////////
function add_article_click(article_id){
	var url = "ajax_function.asp?action=AddArticleClick&ArticleID=" + article_id;
	xmlHttp = createXMLHTTP();
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

//////////////////////////////////////////////////////
//验证码，显示问题
//////////////////////////////////////////////////////
function gen_question(QType){
	//初始化问题（产生第一个问题）
	if (QType == 1){
		if (get_div("div_question", 1) == ""){
			var url = "SecurityCode.asp?action=init_question";
			//创建xmlHttp对象
			xmlHttp = createXMLHTTP();
			//在调用open方法之前注册回调函数stateChanged()
			xmlHttp.onreadystatechange = function() {stateChanged(1)};
			xmlHttp.open("GET", url, true);
			xmlHttp.send(null);
			set_div("div_question", "正在载入验证码问题，请等待……", 1);
		}
	}

	//改变随机问题
	if (QType == 2){
		set_div("div_question", "正在载入新的验证码问题，请等待……", 1);
		set_div("div_result", "（必）", 1);
		var url = "SecurityCode.asp?action=change_question";
		xmlHttp = createXMLHTTP();
		xmlHttp.onreadystatechange = function() {stateChanged(1)};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

//////////////////////////////////////////////////////
//验证码，改变问题，且生成静态页面
//////////////////////////////////////////////////////
function gen_Q_html(QType, article_id){
	if (QType == 1){
		set_div("div_question", "正在载入新的验证码问题，请等待……", 1);
		set_div("div_result", "（必）", 1);
		var url = "SecurityCode.asp?action=change_question";
		xmlHttp = createXMLHTTP();
		xmlHttp.onreadystatechange = function() {stateChanged_2(6, article_id)};
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

//////////////////////////////////////////////////////
//验证码，校验答案
//////////////////////////////////////////////////////
function check_answer(ElementName){
	var check_answer_client = ElementName.value;
	check_answer_client = check_answer_client.trim();
	
	if (check_answer_client != ""){
		//set_div("div_result", "正在验证，请等待……", 1);
		
		var url = "SecurityCode.asp?action=check_answer&check_answer_client=" + encodeURIComponent(check_answer_client);
		
		xmlHttp = createXMLHTTP();

		switch(browser_type){
			case 1:xmlHttp.onreadystatechange = function() {stateChanged(2)};break;
		}

		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		
		switch(browser_type){
			case 2:
				set_div("div_result", xmlHttp.responseText, 1);break;
				break;
		}
	}
}

//////////////////////////////////////////////////////
//删除留言、评论、回复
//////////////////////////////////////////////////////
function del_message(article_id_g_html, article_id_del, type){
	if (window.confirm("真的要删除吗？")){
		var url = "ajax_function.asp?action=DelMessage" + "&ArticleID=" + article_id_del + "&Type=" + type;
		
		xmlHttp = createXMLHTTP();
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		
		var div_message = "div_message_" + article_id_del;
		div_message = get_element_by_id(div_message);
		div_message.parentNode.removeChild(div_message);
				
		//article_id_g_html等于-1表示不需要生成静态页面
		if (article_id_g_html != -1) {
			url = "g_html.asp?ArticleID=" + article_id_g_html;
			xmlHttp.open("GET", url, false);
			xmlHttp.send(null);
		}
	}
}

//////////////////////////////////////////////////////
//保存移动回复
//////////////////////////////////////////////////////
function save_mobile_message_reply(article_id, article_id_reply, ttype){
	var txt_reply = get_input("txt_reply");
	//var txt_validate = get_input("txt_validate");
	
	//去除左右空格
	txt_reply = txt_reply.trim();
	//txt_validate = txt_validate.trim();
	boolean_save = (txt_reply);
	
	if (!boolean_save){
		alert("总得说点什么吧！");
	}
	else {
		if (window.confirm("真的要发送吗？")){
			set_div("div_tips", "正在发送回复……", 1);
			var strForm = "TXTReply=" + txt_reply;
			strForm = encodeURI(strForm);
	
			var url = "ajax_function.asp?action=SaveMobileReplyABC&ArticleIDReply=" + article_id_reply + "&Type=" + ttype;
	
			xmlHttp = createXMLHTTP();
			xmlHttp.onreadystatechange = function() {stateChanged_2(7, article_id)};
	
			xmlHttp.open("POST", url, true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(strForm);
		}
	}
}


//////////////////////////////////////////////////////
//生成新静态页面
//////////////////////////////////////////////////////
function g_html(article_id){
	var url = "g_html.asp?ArticleID=" + article_id;
	xmlHttp = createXMLHTTP();
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

//////////////////////////////////////////////////////
//审核评论
//////////////////////////////////////////////////////
function manager_comment(article_id, comment_id, state_num){
	var url = "ajax_function.asp?action=ManagerComment&CommentID=" + comment_id + "&StateNUM=" + state_num;
	xmlHttp = createXMLHTTP();
	xmlHttp.onreadystatechange = function() {stateChanged_3(2, article_id, comment_id)};
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

//////////////////////////////////////////////////////
//审核留言
//////////////////////////////////////////////////////
function manager_friend_say(article_id, state_num){
	var url = "ajax_function.asp?action=ManagerFriendSay&ArticleID=" + article_id + "&StateNUM=" + state_num;
	xmlHttp = createXMLHTTP();
	xmlHttp.onreadystatechange = function() {stateChanged_2(11, article_id)};
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}