/**
	* Copyright (c) 2008, 计算机世界集团B2C互联网事业部
	* All rights reserved.
	* @js_article.js
	* @brief 文章最终页JS
	* @author 李蕴志(li_yunzhi@ccw.com.cn) 
	* * 本文件用于产品评论JS调用，验证
*/
var article_comment_str = '';
function Postcontent(num,paths)
{
	$result = comment_form_check();
	
	if( !$result )
	{
		return $result;
	}
	article_comment_str = {'path':paths};
	cookie = getUserCookie();
	if( !cookie || cookie === '')
	{
		if( ( paths.substring( 0, 2 ) == '16' ) || ( $("input[id^=hide]").attr('checked') === false ) )
		{
			ajaxLogin( '', function(){addPost(article_comment_str,post_callback);} );
			return false;
		}
		else
		{
			addPost(article_comment_str,post_callback);
		}
	}
	else
	{
		addPost(article_comment_str,post_callback);
	}
	return false;
}

function Postcontent_16(num,paths)
{
	$result = comment_form_check();
	if( !$result )
	{
		return $result;
	}
	article_comment_str = {'path':paths};
	cookie = getUserCookie();
	return ajaxLogin('comment_1',function(){
		addPost(article_comment_str,post_callback);
		}); 
}

function post_callback(comment_json,data)
{
	alert( data.message );
	if( parseInt( data.result ) > 0 )
	{
		$('#contentMessage').html('');
		submit_article(1,article_comment_str);
		$("form[id=comment_form_"+comment_json.path+"]").resetForm();
		$("textarea[id=comment_post_content]").val('请在此处填写评论内容');
		$("#comment_post_words_remain").text('0');
	}
	else
	{
		$('#contentMessage').html("<span style=\"color:red\">"+data.message+"</span>");
	}
	change();
}

function comment_form_check()
{
	if( $("textarea[id=comment_post_content]").val().length === 0 || $("textarea[id=comment_post_content]").val() == '请在此处填写评论内容' )
	{
		alert("请输入内容再提交");
		$("textarea[id=comment_post_content]").focus();
		return false;
	}
	if( $("textarea[id=comment_post_content]").val().length > 3000 )
	{
		alert("您最多可以输入3000个字");
		$("textarea[id=comment_post_content]").focus();
		return false;
	}
	if( $("#verfiycode").val().length != 5 )
	{
		alert("请仔细输入验证码");
		$("#verfiycode").val('');
		$("#verfiycode").focus();
		return false;
	}
	return true;
}

var comment_category_n;	//用于标示提交方式：1 直接登录；2 用户信息和内容一起提交；3 回复窗口登录；4 回复窗口用户信息和内容一起提交

function ajaxLoginPost(n,path)
{
	comment_category_n = n;
	if(path)
	{
		article_comment_str = {'path':path};
	}
	if(n == 1)
	{
		if(!checkLogin( showResult, $("#username").val(), $("#password]").val()))
		{
			showResult({result:-9}, 'show');
		}
	}
	else if(n == 2)
	{
		if(checkLogin( showResult, $("#username").val(), $("#password").val()))
		{
			showResult({result:-9} , 'submit_article');
		}
	}
	else if( (n == 3) || (n == 4))
	{
		if(checkLogin( showResult, $("#username_ajaxlogin").val(), $("#password_ajaxlogin").val()))
		{
			showResult({result:-9}, 'show');
		}
	}
	ajax_interface();
}

//用于一个页面多个窗口登录的情况
function ajaxLoginPost(n,username,password)
{
	comment_category_n = n;
	if(n == 1)
	{
		if(!checkLogin( showResult, username, $("#password]").val()))
		{
			showResult({result:-9}, 'show');
		}
	}
	else if(n == 2)
	{
		if(checkLogin( showResult, username, $("#password").val()))
		{
			showResult({result:-9} , 'submit_article');
		}
	}

	ajax_interface();
}

function showResult(msg, action)
{
	if(msg.result == -1){
		$("#loginError").html("<span class=\"mistake\">你已被锁定</span>");
	}
	else if(msg.result == 1)
	{
		$('#login').hide();
		$("#hide").attr('checked','');
		$("#hidebox").hide();
		$('#login_ajaxlogin').hide();
		$("#hide_ajaxlogin").attr('checked','');
		$("#hidebox_ajaxlogin").hide();
		cookie = getUserCookie();
		var username = cookie.username;
		$("#loginSuccess").html("<span class=\"mistake02\">"+username+" 说:</span>");
		$("#loginSuccess_ajaxlogin").html("<span class=\"mistake02\">"+username+" 说:</span>");
		if( action == 'submit' )
		{
			addPost(article_comment_str,function(comment_json,data){
				if( data.result > 0 )
				{
					submit_article(1,article_comment_str);
					$("form[id=comment_form_"+comment_json.path+"]").resetForm();
					$("textarea[id=comment_post_content]").val('请在此处填写评论内容');
					change();
				}
			});
		}
		else
		{
			if(comment_category_n == 4)
			{
					$('#reply_sc').submit_article();	
			}
		}
	}
	else {
		if(comment_category_n < 3)
		{
			$("#loginError").html("<span class=\"mistake\">请输入合法用户名和密码</span>");
		}
		else
		{
			$("#loginError_ajaxlogin").html("<span class=\"mistake\">请输入合法用户名和密码</span>");
		}
	}
}

function ajax_interface(comment_json,data)
{
	cookie = getUserCookie();
	if( cookie != ''){
		var username = cookie.username;
		$('#login').hide();
		$("#hide").attr('checked','');
		$("#hidebox").hide();
		$("#loginSuccess").html("<span class=\"mistake02\">"+username+" 说:</span>");
	}
}	
function submit_article(num,paths)
{
	var parm = {page : num,path : article_comment_str.path };
	getPostList(parm,function(comment_json,data){
		if (!data.list || !data.list.length)
		{
			return false;
		}
		//检查返回数据是否包含HTML模板
		if (data && data.html && data.html!='')
		{
			var myTemplateObj = TrimPath.parseTemplate(data.html);
		}
		else
		{
			var myTemplateObj = TrimPath.parseTemplate("");
		}
		if(!data.path || data.path=='')
		{
			data.path = comment_json.path;
		}
		//处理数据
		var result  = myTemplateObj.process(data);
		//输出
		$("#comment_post_list_"+comment_json.path).replaceWith(result);
		//判断如果是问答提交的话，显示列表
		if( comment_json.path.substring( 0,2 ) == '16' )
		{
			$("#best_answer_comments").show();
			$(".qaExpand").show();
			$(".qaExpandA").hide();
		}
	});
}
	
//更换验证码
function change()
{
	$('#codeimg').attr('src','http://comment.pcw.com.cn/comment_codeimg.php?'+Math.random());
	$("#verfiycode").val('');
	$('#verfiycode').focus();
}

//文本域初始化
function comment_form_init( obj,defaultvalue )
{
	if( $(obj).val() == defaultvalue )
	{
		$(obj).val('');
		$(obj).css({ color: "#000000"}); 
	}
	else if ($(obj).val()=='')
	{
		//$(obj).val(defaultvalue);
		//$(obj).css({ color: "gray"}); 
	}
}

//表单生成 路径，引用评论ID，引用评论用户名
function comment_form_create( path, rid, uname, memo )
{
	var parmaters = '';	//隐藏参数
	var title = '';		//表单的提示文字
	var action = '';	//表单的action链接
	if( $("#"+rid).text() != '' )
	var rid_num = $("#"+rid).text().length;
	if( rid_num > 50)
	{
		var str = $("#"+rid).text().substring(0,50);
		str += '...';
	}
	else
	{
		var str = $("#"+rid).text();
	}
	parmaters = '<input type=hidden name=path value="'+path+'">';
	if( parseInt( rid ) > 0 )
	{
		title = '<div class="blank18"></div><h2><span class="commentoff"><img src="http://img.pcw.com.cn/images/off.gif" width="14" height="14" onclick="javascript:tailFormDisplay( path )"/></span>回复该评论</h2><br>引用：“'+str+'”';
	}
	else
	{
		title = '<h2>我也来发表一下评论</h2>';
		parmaters += '<input type="hidden" name="memo" id="memo" value="'+memo+'" />';
	}
	return title+'<p>'+parmaters+'<textarea name="comment_post_content" id="comment_post_content" cols="75" rows="8" onfocus="javascript:comment_form_init(this,\'请在此处填写评论内容\')" onkeyup="javascript:textCounter( \'comment_post_content\', 3000, \'comment_post_words_remain\' )">请在此处填写评论内容</textarea>		</p>		<div class="txt">			字数在3000字以内，您还可以输入<input id="comment_post_words_remain" type="text" size="1" maxlength="1" readonly="true" value="3000" class="orange en"/>个字 。			<div class="blank8"></div>			<span id="hidebox"><input name="hide" id="hide" checked="checked" type="checkbox">&nbsp;匿名提交(无需注册) </span>			<span id="contentMessage"></span><br>			请输入验证码：			<input name="verfiycode" id="verfiycode" class="input90" size="5" maxlength="5" type="text">			<img src="http://comment.pcw.com.cn/comment_codeimg.php?id='+path+'&s='+Math.random()+'" name="codeimg" id="codeimg"  width="90" height="18" style="border:1px solid #808080;margin:0 10px;">			<a href="javascript:change()" class="BlueElink">换一张图片</a>			<img src="/image/comments/comm_Submit.gif" width="46" height="23" onclick="comment_tail()">			<!--<input src="http://img.pcw.com.cn/images/comm_Submit.gif" type="image">-->		</div>';
}

//文本域计数
function textCounter( field, maxlimit, countfield )
{
	if ($('#'+field).val().length > maxlimit) {
		$('#'+field).val(
			$('#'+field).val().substring(0, maxlimit)
		);
		return false;
	}
	$('#'+countfield).text($('#'+field).val().length);
}

//提交评论内容
function comment_tail()
{
	if( $("input[id^=comment_title]").length )	//产品点评的标题验证
	{
		if( $("input[id^=comment_title]").val().length == 0 )
		{
			alert("请输入标题再提交");
			$("#comment_title").focus();
			return false;
		}
	}
	if( ( $("#comment_post_content").val().length == 0 ) || ( $("#comment_post_content").val() == '请在此处填写评论内容' ) )
	{
		alert("请输入内容再提交");
		$("#comment_post_content").focus();
		return false;
	}
	if( $("#comment_post_content").val().length > 3000 )
	{
		//alert($("#comment_post_content").val().length);
		alert("您最多可以输入3000个字");
		$("#comment_post_content").focus();
		return false;
	}
	if( $("input[id^=dianping_vote]").length )	//产品点评的投票验证
	{
		var flag=0;
        for(i=0;i<$(":radio:checked").length;i++)
        if($(":radio:checked").get(0).checked=true){
            flag=1;
            break;
        }
        if(!flag){
            alert("请评个分再提交吧")
            return false
        }

	}
	if( $("#verfiycode").val().length != 5 )
	{
		alert("请仔细输入验证码");
		$("#verfiycode").val('');
		$("#verfiycode").focus();
		return false;
	}

	if( $("input[id^=hide]").attr('checked')==true)
	{
		return submitComments();
	}
	else 
	{
		return ajaxLogin('',submitComments);
	}
}
function submitComments()
{
	var mylocation = window.location.href;
	$.ajax({url:"http://comment.pcw.com.cn/comment.php?jsoncallback=?",		//指明访问哪个服务器端文件
		type:"post",                //发送方式
		dataType:"jsonp",			//参数类型
		data:{
			action:'comment',
			path:$("#comment_form_path").val(),
			title:$("#comment_form_title").val(),
			content:$("#comment_post_content").val(),
			verfiycode:$("#verfiycode").val(),
			hide:$("#hide").attr('checked'),
			memo:$("#comment_form_memo").val(),
			url:$("#comment_form_url").val(),
			vote:$("input[@type=radio][@checked]").val()
		},
		success: function(json){//如果调用php成功
			if( parseInt(json.result) == 1 )
			{
				alert( json.message );
				$('#comment_title').val('');
				$('#comment_post_content').val('请在此处填写评论内容');
				location.reload();
				window.location.href = mylocation;
			}
			else
			{
				$('#comment_post_content').val(json.content);
				$('#contentMessage').html("<span style=\"color:red\">"+json.message+"</span>");
			}
			change();
		}  
	});
}