﻿String.prototype.sub = function(n) {
	var r = /[^\x00-\xff]/g;
	if (this.replace(r, "mm").length <= n)
		return this;
	var m = Math.floor(n / 2);
	for ( var i = m; i < this.length; i++) {
		if (this.substr(0, i).replace(r, "mm").length >= n) {
			return this.substr(0, i);
		}
	}
	return this;
};

String.prototype.len = function() {
	return this.replace(/[^\x00-\xff]/g, "rr").length;
}

cn189.common = {
	//批量删除通知
	batchDelNote : function() {
		var checks = $("input[name='delcheckbox']");
		var noteIds = "";
		for( i = 0 ; i < checks.length ; i++) {
			if(checks[i].checked) {
				if (noteIds != "") {
					noteIds += ",";
				}
				noteIds += checks[i].value;
			}
		}
		if(noteIds != "") {
			cn189.util.confirmDelete("delselected", function() {
				$.ajax({
					url : "/note/batchDelNote",
					type : "post",
					data : {
						"ids" : noteIds
					},
					success : function(){
						var checks = noteIds.split(",");
						for(i = 0 ; i < checks.length ; i++) {
							$("#note_" + checks[i]).slideUp("fast");
						}
					}
				});
			});
		} else {
			cn189.util.alertInfo("delselected", "请选择需要删除的通知!");
		}
	},
	/**
	 * 删除单个通知
	 */
	delNote : function(a_id, note_id) {
		cn189.util.confirmDelete(a_id, function() {
			$.ajax({
				url : "/note/delNote",
				type : "post",
				data : {
					"noteId" : note_id
				},
				success : function(){
					$("#note_"+ note_id).slideUp("normal", function() {
						$("#note_"+ note_id).remove();
					});
				}
			});
		});
	},
	/**
	 * 读取通知
	 */
	readNote : function(note_id) {
		if($("#note_" + note_id).attr("class") == 'active') {
			$.ajax({
				url : "/note/readNote",
				data : { "noteId" : note_id },
				success : function() {
					$("#note_" + note_id).removeClass("active");
				}
			});
		}
	},
	
	joinGroupAgree : function(a,b,c,d) {
		$.ajax({
		  url: '/group/manageapplyAjax?applyinfoid='+a+'&select='+b+'&userid='+c+'&groupid='+d,
		  cache: false,
		  success: function(html){			
			if(b==0){//同意
				$("#"+a).html('审核通过');
			}else{//拒绝
				$("#"+a).html('拒绝加入');				
			}			
		  },
		  error:function(){
			  $("#"+a).html('处理失败');	
		  }
		});
	},
    refreshContact: function(anoteid, tag, contactid){
        $.ajax({
            url: '/favor/contact/refreshContact?anoteid=' + anoteid + '&tag=' + tag + '&contactid=' + contactid,
            cache: false,
            success: function(html){
                var str = html.trim();
                if (str == "failed") {
                    alert('处理失败');
                }
                if (str == "success") {
                    $('#noteem_' + anoteid + '').html('已同步');
                }
                else {
                    if (str == "ignore") {
                        $('#noteem_' + anoteid + '').html('已忽略');
                    }
                }
            }
        });
    }
}
cn189.guestbook = {
	showguest : function() {
		var str="";
		var question=document.getElementById('question').value;
		if(document.getElementById('email').type=="text"){
			str=document.getElementById('email').value;
			strReg=/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
			r=str.search(strReg);
			if(str==null || str==""){
				$('#question_check_message').css('color','red').html('请输入有效的 Email');
				return false;
			}else if(r==-1){
				$('#question_check_message').css('color','red').html('您输入的邮件格式不正确');
				return false;
			}else{
				$('#question_check_message').css('color','').html('我们会将回复发到您的邮箱');
			}
		}
		var content=document.getElementById('question').value;
		if(content==null || content==""){
			$('#question_check_message').css('color','red').html('请填写留言');
			return false;
		}else{
	//		$('#question_check_message')[0].innerHTML ='';
			return true;
		}
	},
	submitadvice : function() {
		if (this.showguest()){
			
			$.ajax({
			  url: "/guestbook/guestbookAdd",
			  type: 'POST',		  
			  cache: false,
			  data:{
			    	 "guestbook.email" : $('#email')[0].value,
			    	 "guestbook.question" : $('#question')[0].value
			  },
			  success: function(html){			
				  $('#email')[0].value='@';
			 	  $('#question')[0].value='';
			 	  $('#sendsuccess')[0].style.display="";
			 	  window.setTimeout(function (){$('#sendsuccess')[0].style.display="none";}, 5000);

		//	 	  (function (){$('#sendsuccess')[0].style.display="none";}).delay(5000);
			 	  
			  },
			  error:function(){
				  $('#senderror')[0].style.display="";
				  window.setTimeout(function (){$('#senderror')[0].style.display="none";}, 5000);
			// 	  (function (){$('senderror').style.display="none";}).delay(5000);
			  }
			});
		}
	}
}