
window.onload = function() {
	var nav = document.getElementById('nav');
	var sub = nav.getElementsByTagName('DIV');
	for(var c=0;c<sub.length;c++) {
		if( c%2==0 ) {
			sub[c].className = 'sp';
			sub[c].innerHTML = "<img src='/css/nav_sp.jpg' style='width:3px;height:19px;'/>";
		}
		else {
			sub[c].onmouseover = function() { this.className = 'ho'; }
			sub[c].onmouseout = function() { this.className = ''; }
		}
		
	}
}

var loadComments = function(postId) {
	var loadComments = document.getElementById('loadComments');
	if(loadComments) {
		ajaxRequest('comments.php?type=load&post_id='+postId, {
				onComplete: function(originalRequest) {
					var r = originalRequest.responseText;
					loadComments.innerHTML = r;
				}
		   });
	}
}

var trimString = function(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

var newComment = function() {
	var frm = document.getElementById('commentFrm');
	if(frm) {
		if( trimString(frm.comment_user.value)=='') {
			alert('请输入你的姓名！');
			return false;
		}
		else if( trimString(frm.comment_data.value)=='') {
			alert('请输入你的评论内容！');
			return false;
		}
		else if( trimString(frm.comment_code.value)=='') {
			alert('请输入验证码！');
			return false;
		}
		var params = 'type=new&post_id='+frm.post_id.value + '&comment_user='+frm.comment_user.value + '&comment_email='+frm.comment_email.value + '&comment_data='+frm.comment_data.value + '&comment_code='+frm.comment_code.value;
		ajaxRequest('comments.php', {
			    parameters: params,
				method: 'post',
				onComplete: function(originalRequest) {
					var r = originalRequest.responseText;
					if( r == 'errCode' ) {
						alert('验证码有错！');
						frm.comment_code.value = '';
					}
					else {
						alert('多谢你的评论！');
						frm.reset();

						var comms = document.getElementById('commentList');
						comms.innerHTML = r + comms.innerHTML;
					}

					var commentCode = document.getElementById('commentCode');
					commentCode.src = '';
					commentCode.src = '/comments.php?type=commentCode&rnd=' + Math.random();
				}
		   });
	}
}
