var comment = {
	sid: 0,
	ref: window.location.href,
	tips: ['文明上网 理性发声', '您必须要先登录到金鹰网才能发表评论'],
	init: function () {
		this.getForm();
		this.vLogin();
	},
	getForm: function () {
		var _html = '<div class="comment-pub">';
			_html += '<h2 class="title"><span class="name">发表评论</span><span class="extra"><a href="http://comment.hunantv.com/comment/?type=news&sid='+this.sid+'" target="_blank">查看所有评论(<span id="commenttotal2">0</span>)</a></span></h2>';
			_html += '<div class="content">';
			_html += '<ol>';
			_html += '<li class="logininfo"><span id="comment-login" class="comment-login"></span></li>';
			_html += '<form name="cform">';
			_html += '<input type="hidden" name="type" id="type" value="news" />';
			_html += '<input type="hidden" name="sid" id="sid" value="'+this.sid+'" />';
			_html += '<li><textarea id="comment" name="comment" cols="40" rows="5" class="f-textarea-tips1" onclick="fOnCommentClick();">'+this.tips[0]+'</textarea></li>';
			_html += '<li class="op"><span id="comment-code"></span>';
			_html += '<button type="button" class="btn-submit" onclick="fOnCommentSubmit();">提交评论</button></li>';
			_html += '</form>';
			_html += '</ol>';
			_html += '</div>';
			_html += '</div>';
			document.write(_html);
	},
	vLogin: function () {
		if(passport.checkCookie())
		{
			var username = passport.getCookie('uid');
			var _html_1 = '<strong><a href="http://home.hunantv.com/home/home" title="'+username+'">'+username+'</a></strong> <a href="http://passport.hunantv.com/login/login_out.php?ref='+this.ref+'" title="退出">退出</a>';
			var _html_2 = '<label>验证码：</label><input type="text" name="icode" id="icode" size="4" maxlength="4" autocomplete="off" class="f-vcode" onclick="getCode();"/> <img id="imgcode" src="http://comment.hunantv.com/comment/images/space.gif"/> ';
		}else{
			var _html_1 = '<form name="xform">';
				_html_1 += '<label>金鹰网账号：</label><input type="text" name="user" id="email" value="E-mail" class="login-inline-input" onclick=\'this.value="";\'/> ';
				_html_1 += '<label>密码：</label><input type="password" name="password" id="pwd" class="login-inline-input"/> ';
				_html_1 += '<input type="button" value="登录" class="login-inline-btn" onclick="fOnLoginSubmit();"/> ';
				_html_1 += '<a href="http://passport.hunantv.com" target="_blank" title="新用户注册">新用户注册</a>';
				_html_1 += '<input type="hidden" name="ref" value="'+this.ref+'">';
				_html_1 += '<input type="hidden" name="ptype" value="">';
				_html_1 += '</form>';
			var _html_2 = '<label>'+this.tips[1]+'</label>';
		}
		document.getElementById('comment-login').innerHTML = _html_1;
		document.getElementById('comment-code').innerHTML = _html_2;
	}
};

function fOnLoginSubmit() {
	var _email = document.getElementById('email');
	var _pwd = document.getElementById('pwd');
	if(_email.value == '' || _email.value == 'E-mail')
	{
		alert('请输入您的金鹰网账号');
		_email.focus();
		return false;
	}
	if(_pwd.value == '')
	{
		alert('请输入您的密码');
		_pwd.focus();
		return false;
	}
	document.xform.action = 'http://passport.hunantv.com/login/login.php';
	document.xform.enctype = 'application/x-www-form-URLencoded';
	document.xform.method = 'post';
	document.xform.submit();
}

function fOnCommentClick() {
	if(passport.checkCookie())
	{
		document.getElementById('comment').innerHTML = '';
		document.getElementById('comment').className = 'f-textarea';
	}else{
		document.getElementById('comment').innerHTML = comment.tips[1];
		document.getElementById('comment').className = 'f-textarea-tips2';
	}
}

function fOnCommentSubmit() {
	if(passport.checkCookie())
	{
		var _comment = document.getElementById('comment');
		var _code = document.getElementById('icode');
		if(_comment.value == '' || _comment.value == comment.tips[0] || _comment.value == comment.tips[1])
		{
			alert('请输入评论内容');
			_comment.focus();
			return false;
		}
		if(_code.value == '')
		{
			alert('请输入验证码');
			_code.focus();
			return false;
		}
		if(_code.value.length < 4)
		{
			alert('验证码不能少于4位');
			_code.focus();
			return false;
		}
		document.cform.action = 'http://comment.hunantv.com/comment/create.php';
		document.cform.method = 'post';
		document.cform.submit();
	}else{
		document.getElementById('comment').innerHTML = comment.tips[1];
		document.getElementById('comment').className = 'f-textarea-tips2';
		return false;
	}
}

function getCode() {
	document.getElementById('imgcode').src='http://comment.hunantv.com/comment/icode.php?'+Math.random();
}

String.prototype.Trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.isNumber = function()
{  
	var s = this.Trim();
	return (s.search(/^[+-]?[0-9.]*$/) >= 0);
}