// JavaScript Document
$(function(){	   
	$("#s").focus();
})

function keyw(){
	if ($("#s").val() == ''){
		$("#s").focus();
		return false;
	}
}

function checkEmail (str){
	isEmail1=/^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
	return (isEmail1.test(str));
}
function checkform(){
	if($.trim( $("#post_name").val() ) == ""){
		$("#mname").html("雁过留声，人过留名");
		$("#post_name").focus();
		return false;
	}else{
		$("#mname").html("");	
	}
	
	if($.trim( $("#post_name").val() ).length>12){
		$("#mname").html("你的网名太长了啊");
		$("#post_name").focus();
		return false;
	}else{
		$("#mname").html("");
	}
	
	if($.trim( $("#post_site").val() ).length >40){
		$("#msite").html("你的网址太长了");
		$("#post_site").focus();
		return false;
	}else{
		$("#msite").html("");
	}
	
	if($.trim( $("#post_mail").val() )!=""){
		if(!checkEmail( $("#post_mail").val() )){
			$("#mmail").html("邮件地址格式错误");
			$("#post_mail").focus();
			return false;
		}else{
			$("#mmail").html("");	
		}
	}
	
	if($.trim( $("#contents").val() ) == ""){
		$("#mcont").html("评论内容不能为空");
		$("#contents").focus();
		return false;
	}else{
		$("#mcont").html("");	
	}
	
	if($.trim( $("#contents").val() ).length >120){
		$("#mcont").html("评论内容太长");
		$("#contents").focus();
		return false;
	}else{
		$("#mcont").html("");	
	}
	return true;
}

//提交添加评论
function ajaxpostfrom(){
	if(checkform()){
		$.ajax({
			type:'POST',
			url:'/coderblog/Addcomment/utime/'+new Date().getTime(),
			data:$("form").serialize(),
			success:function(msg){
				ajaxgetcomment( $("#post_name").val() , $("#contents").val() );
				$("#post_name, #post_mail,#contents").val('');
				$("#post_site").val('http://');
			}
		});
	}
}


function ajaxgetcomment(post_name,comment){

		if($('#nocomment').html() != null ){
			$(".commentlist ul").html("");
		}

		var i = $(".commentlist ul li").length;
		$(".commentlist ul").append('<li class="comment"><div class="author"><div class="pic"><img src="http://www.coderbolg.com/qeestyle/img/user.bmp" class="avatar" heiht="32" width="32" /></div><div class="name"><span>'+post_name+'</span></div></div><div class="info"><div class="date">'+CurentTime()+'&nbsp;&nbsp;&nbsp;第'+(i+1)+'楼</div><div class="content">'+comment+'</div></div></li>');
}

function CurentTime(){
	 
    var now = new Date(); 
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日
   
    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
   
    var clock = year + "-";
   
    if(month < 10)
        clock += "0";
   
    clock += month + "-";
   
    if(day < 10)
        clock += "0";
       
    clock += day + " ";
   
    if(hh < 10)
        clock += "0";
       
    clock += hh + ":";
    if (mm < 10) clock += '0'; 
    clock += mm; 
    return(clock); 
} 
