﻿var SelectCommon={
    load:function(objid,texts,values,selected){
        if(objid){
            var $obj=$("#"+objid);
             $obj.empty();
            for(var i in values){
                if(selected==values[i]){
                    $obj.append("<option value='"+values[i]+"' selected='selected'>"+texts[i]+"</option>");
                }else{
                    $obj.append("<option value='"+values[i]+"'>"+texts[i]+"</option>");
                }
            }
        }
    },
    selectedItem:function(objid,slitem){
        var obj=$("#"+objid).get(0);
        for(var i=0;i<obj.options.length;i++)
        {   
            if(obj.options[i].value==slitem)
            {
                 obj.options[i].selected=true;
                 break;
            }
        }
    }
}
String.prototype.checkContent=function(filter){
    var pattern={
        email:"(@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,})",
        num:"[0-9]{beginNum,endNum}",//连续的数字，位数自定义
        content:"",//自定义内容
        mobile:"^[1-9][0-9]{10}$"
    };
   var reg=new RegExp();
    if(filter.mobile)
    {
        reg.compile(pattern.mobile);
        if(!reg.test(this))
        {
            if(filter.mobile.errorTip){
                alert(filter.mobile.errorTip);
            }
            return false;
        }
    }
    if(filter.email)
    {
        reg.compile(pattern.email);
        if(!reg.test(this))
        {
            if(filter.email.errorTip){
                alert(filter.email.errorTip);
            }
            return false;
        }
    }
    if(filter.num)
    {
        pattern.num=pattern.num.replace('beginNum',filter.num.beginNum).replace('endNum',filter.num.endNum);
        reg.compile(pattern.num);
        if(!reg.test(this))
        {
            if(filter.num.errorTip){
                alert(filter.num.errorTip);
            }
            return false;
        }
    }
    if(filter.content)
    {
        for(var i in filter.content.content)
        {
            pattern.content=filter.content.content[i];
            if(typeof(pattern.content)=="string")
            {
                reg.compile(pattern.content);
                if(!reg.test(this))
                {
                    if(filter.content.errorTip){
                        alert(filter.content.errorTip);
                    }
                    return false;
                }
            }
        }
    }
    return true;
}
String.prototype.realLength=function(){    
    return this.replace(/[^\x00-\xff]/g,"**").length;   
}
String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
//1.
//    var str = this;
//    str = str.replace(/^\s+/,''); 
//    for (var i = str.length - 1; i >= 0; i--) { 
//        if (/\S/.test(str.charAt(i))) { 
//            str = str.substring(0, i + 1); 
//            break; 
//        } 
//    } 
//    return str; 

//2.
//    var str = this;
//    str = str.replace(/^\s\s*/, '');
//    var ws = /\s/;
//    var i = str.length; 
//    while (ws.test(str.charAt(--i))); 
//    return str.slice(0, i + 1); 
}
//验证码的验证
codeblur=function(codeid,submitid,msgid){
    if(codeid==""||codeid==null){
        codeid="vcode";
    }
    if(submitid==""||submitid==null){
        submitid="btnOK";
    }
    if(msgid==""||msgid==null){
        msgid="lblmsg";
    }
    if($("#"+codeid).val()!=""){
        $("#"+submitid).attr("disabled","disabled");
        $("#"+msgid).html("&nbsp;<img src=\"/manager/images/spinner.gif\" />&nbsp;正在检查，请稍等...&nbsp;");
        $.ajax({
            type:"post",
            url:"/Handler/CheckObject.ashx",
            data:{action:"checkcode",params:$("#"+codeid).val()},
            dataType:"json",
            success:function(data){
                if(data.success){
                    $("#"+submitid).removeAttr("disabled");
                    $("#"+msgid).html("");
                }else{
                    $("#"+msgid).html(data.info);
                    $("#"+submitid).attr("disabled","disabled");
                }
            },
            error:function(data){
                alert("服务器发生错误！");
                $("#"+submitid).attr("disabled","disabled");
            }
        });
    }else{
        $("#"+msgid).html("");
    }
}
//判断某个元素是否存在
IsExits_element=function(id){
    if($("#"+id).length>0){
        return true;
    }else{
        return false;
    }
}
//加入收藏
function addFavorites(t_url,t_title)
{
    if(t_url==""){
        t_url=window.location.href;
    }
    if(t_title==""){
        t_title=document.title;
    }
    if (document.all){
       window.external.addFavorite(t_url,t_title);
    }
    else if (window.sidebar){
       window.sidebar.addPanel(t_title,t_url);
    }
    return false;
}
//设为首页
function setHomepages(obj)
{
    var t_url=window.location.href;
    if (document.all){
        obj.style.behavior='url(#default#homepage)';
        obj.setHomePage(t_url);
    }else if (window.sidebar){
       if(window.netscape){
             try{
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
             }  
             catch (e){  
                alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );  
             }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage',t_url);
    }
    return false;
}
