//注册
var j = jQuery.noConflict();

//全选、取消、反选 
var SelectCheckBox = function(clas,index){
    var jq = clas==1 ? j(".Onediv :checkbox") : j(".Twodiv :checkbox");
    jq.each(function(){
        if(index==1)
            j(this).attr("checked",true);
        else if(index==-1)
            j(this).attr("checked",!this.checked);
        else
            j(this).attr("checked",false);
    });
} 


//扩展
j.extend({
    //ajax提交方法
    ajaxSubmit: function(obj,da,succeed,bool){
        j.ajax({
            url:"/Message/MsgAjax.ashx",
            type:"post",
            data:da,
            dataType:"html",
            timeout:10000,
            beforeSend:function(){
                var modile = j(".manager-content");
                if(modile.length > 0){
                    var tip = modile.offset();
                    modile.append("<div id='bgDiv1' style='position:absolute;left:"+tip.left+";background:#fff;top:"+tip.top+";z-index:1000;filter:alpha(opacity=40);width:"+modile.width()+";height:"+modile.height()+"'></div>")
                }
                obj.attr("disabled",true);
                if(obj.next("img").length==0)
                    obj.after("<img src='/images/loading.gif' />");
            },
            success:function(data,status){
                if(status){
                    if(j.isFunction(succeed))
                        succeed(data,obj);
                }
            },
            complete:function(status){
                j.remov(obj,bool);
            },
            error:function(err){
                j.remov(obj,bool);
                alert("数据提交失败或超时！");
            }
        });
    },
    remov:function(obj,bool){
        if(bool == true)
            obj.attr("disabled",false);
        obj.next("img").remove();
        j("#bgDiv1").remove();
    },
    //ajax删除列表集合方法
    ajaxDelList: function(obj,index,chkType){
        var chkChecked = j(":checkbox[name:CheckboxSelectHtml"+chkType+"]:checked");
        if(chkChecked.length==0){
            if(obj.next("span").length==0)
                DisplayMsg("请选择要删除的数据！");
        }else{
            if(confirm("您选择了"+chkChecked.length+"条记录，确定要删除？")==true){
                var idList='';
                chkChecked.each(function(){
                    var id = getID(this.id,'_');
                    idList += id + ',';
                });
                if(idList.length>0)
                    idList = idList.substr(0,idList.length-1);
                
                j.ajaxSubmit(obj,"index="+index+"&idList="+idList,ListDeleteSuccess);
            }
        }    
    },
    //表单提交验证
    Validator: function(){
        var b = true;
        j("[null='false']").each(function(){
            var jq = j(this);
            var reg = jq.attr("reg");
            if(jq.val().IsEmpty()){
                if(!jq.exist())
                    jq.err("此选项不能为空！");
                b = false;
            }
            else if(jq.is("textarea")){
                if(!/^[^…]{1,3000}$/.test(this.value)){
                    if(!jq.exist())
                        jq.err("请按格式要求填写内容！");
                    b = false;
                }
            }
            else if(reg != undefined || reg != null){
                var exp = new RegExp(reg);
                if(!exp.test(jq.val())){
                    if(!jq.exist())
                        jq.err("输入格式不正确！");
                    b = false;
                }
            }
        });
        return b;
    },
    //跳转
    Redirect: function(url){
        window.location.replace(url);
    },
    //获取参数值
    urlSearch: function(key){
        var returnValue=null;
        
        var search = window.location.search;
        var index = search.indexOf('?'+key+'=');
        if(index == -1){
            index  = search.indexOf('&'+key+'=');
            if(index==-1)
                return null;
        }
        var newSearch = search.substr(index+key.length+2,search.length);
        var index2 = newSearch.indexOf('&');
        if(index2 == -1)
            returnValue = newSearch;    
        else
            returnValue = newSearch.substr(0,index2);
        return returnValue;
    } 
});

//方法扩展
j.fn.extend({
    err:function(msg){
        if(this.parent().find("[name:err]").length==0)
            this.parent().append("<span name='err' style='color:Red'>"+msg+"</span>");
        else
            this.parent().find("[name:err]").text(msg);
    },
    move:function(){
        this.parent().find("[name:err]").remove();
    },
    exist:function(){
        return this.parent().find("[name:err]").length>0;
    }
});   

//删除执行成功方法
var ListDeleteSuccess = function(data,obj){
    if(data.toLowerCase() == "true"){
        window.location.reload();
    }
    else
        obj.attr("disabled",false);
}







//获取记录ID
var getID = function(id,split){
    var index = id.lastIndexOf(split)+1;
    return id.substr(index);
}

//验证只能输入非负数
var ValidNumber = function(obj)
{
    if(!(/^\d{1,}$/g).test(obj.value) || obj.value==0)
        obj.value = 1;
}

//刷新验证码
var FreshImgCode = function(){
    j("#imgCode").attr("src","/CommonCtrl/ValidateCodeImage.aspx?tmp="+Math.random());
}











//格式化日期
Date.prototype.Format = function(){ 
   if(this instanceof Date){ 
     var y = this.getFullYear(); 
     var m = this.getMonth() + 1; 
     var d = this.getDate(); 
     var h = this.getHours(); 
     var i = this.getMinutes(); 
     var s = this.getSeconds(); 
     if(h>0 || i>0 || s>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s; 
     return y + '-' + m + '-' + d; 
   } 
   return this; 
}

//THML特殊字符--编码
String.prototype.EncodeHTML = function()
{ 
    var i,e={'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',' ':'&nbsp;'},t=this;
    for(i in e) 
        t=t.replace(new RegExp(i,'g'),e[i]); 
    return t;
} 

//THML特殊字符--解码
String.prototype.DecodeHTML = function()
{ 
    var i,e={'&amp;':'&','&lt;':'<','&gt;':'>','&quot;':'"','&nbsp;':' ','\n':''},t=this;
    for(i in e) 
        t=t.replace(new RegExp(i,'g'),e[i]); 
    return t;
}

//字符串是否为空
String.prototype.IsEmpty = function()
{
    return j.trim(this).length==0;
}


function DisplayMsg(msg)
{
    var erdiv = document.getElementById("erDiv");
    erdiv.style.display='';
	erdiv.innerHTML=msg;
	setTimeout(function(){
	 erdiv.innerHTML='';
	 erdiv.style.display='none';
	  },3000);
}
