/*document.lmsg.con,.CodeStr*/
function chkMsgForm(form,maxLen,user){//user=0无须登陆
	if(top.chkLogin && user!=0){
		if(!top.chkLogin()){
			return false;
		}
	}
	var maxlength=maxLen;
	var tt=form.title,ta=form.con,code=form.captcha;
	if(tt){
		if (!tt.value.trim()){
			alert("请输入标题");
			tt.focus();
			return false;
		}
	}
	if (!ta.value.trim()){
		alert("请输入内容");
		ta.focus();
		return false;
	}
	if(code){
		if(!code.value.trim()){
			alert("请输入验证码");
			code.focus();
			return false;
		}
	}
	if(changebyte(ta.value,maxlength)){
		return true;
	}else{
		alert("输入字节数不要超过"+maxlength+"!");
		ta.style.border="1px solid #F00";
		ta.focus();
		return false;
	}
}
function changebyte(value,length){
    var l=strlength(value);
	var note=$$("#msgNote");
    if (l>length) {
        if(note){
			note.innerHTML="输入字节数不要超过"+length+"!";
		}else{
			alert("输入字节数不要超过"+length+"!");
		}
		return false;
    }else{
		if(note)note.innerHTML="";
	}
    return true;
}
function strlength(str){
    var l=str.trim().length;
    var n=l,cc;
    for (var i=0;i<l;i++){
		cc=str.charCodeAt(i);
	   if (cc>0 && cc<255) n++;
    }
    return n;
}

function AddText(textarea,NewCode) {
        if(document.all){
        	insertAtCaret(textarea, NewCode);
        	textarea.focus();
        }else{
        	textarea.value += NewCode;
        	textarea.focus();
        }
}

function insertAtCaret(textEl, text){
        if (textEl.createTextRange && textEl.caretPos){
                var caretPos = textEl.caretPos;
                caretPos.text += caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
        } else if(textEl) {
                textEl.value += text;
        } else {
        	textEl.value = text;
        }
}
function ajMSG(f,toURL,user){
	if(ajMSG.submiting) return;
	var fo=document.lmsg;
	if(!chkMsgForm(fo,700,user)){//user=0无须登陆
		return false;
	}
	ajMSG.submiting = true;
	enabledBtn(false,'lmsg');
	if(!ajMSG.oldMsg){
		ajMSG.oldMsg = '';
	}
	var con = fo.con.value;
	if(ajMSG.oldMsg == con.trim()){
		ajMSG.submiting = false;
		enabledBtn(true,'lmsg');
		styleTip("请不要连续提交相同的信息！","hide",1000,"",true);
		return;
	}
	var aj=new net.ContentLoader("cmt.php",
			"act=save&id="+fo.id.value+"&cid="+fo.cid.value+"&captcha="+fo.captcha.value+"&con="+encodeURIComponent(con),
			"POST",
			function(){
				var re=this.req.responseText;
				ajMSG.submiting = false;
				enabledBtn(true,'lmsg');
				if (re==0){
					styleTip("验证码输入错误","hide",700,"",true);
				}else if (re==1){
					styleTip("提交成功","hide",700,"",true);
					ajMSG.oldMsg = con.trim();
					try{
						p.goto(1);
					}catch(e){};
					if(toURL){
						location.href=toURL;
					}
				}else if (re == -1){
					alert("已禁止此IP发言！");
				}
				var img = $$('img',$$("#captcha_con0"))[0];
				img.src = img.src + new Date().valueOf();
		});
}
