﻿
//模拟继承
Object.extend=function(instance,source){
    if(instance && source){
        for(var property in source){
            instance[property]=source[property];
        }
        return instance;
    }
};

Object.extend(String.prototype,{
    //去字符串头部空格
    ltrim:function(){
        return this.replace(/^\s+/g,"");
    },
    //去字符串尾部空格
    rtrim:function(){
        return this.replace(/\s+$/g,"");
    },
    //去字符串两头空格
    trim:function(){
        return this.replace(/^\s+|\s+$/g,"");
    },
    //检测字符串是否以指定的字符开头
    startWith:function(str){
        if(str){
            return this.includeWith(str)==0;
        }
        return false;
    },
    //检测字符串是否以指定的字符结尾
    endWith:function(str){
        if(str){
            return this.lastIndexOf(str)==0;
        }
        return false;
    },
    //检测字符串中是否包含指定的字符
    contains:function(str){
        if(str){
            return this.indexOf(str)!=-1;
        }
    },
    isEmpty:function(){
        return this.trim()=="";
    },
    isInteger:function(){
        var exp=this.toString();
        if(!exp.length) return true;
        return /^[0-9]+$/g.test(this);
    },
    isDate:function(){
        var exp=this.toString().replace(/-/,"/");
        return Date.parse(exp).isNumber();
    },
    isNumber:function(){
        var exp=this.toString();
        if(!exp.length) return true;
        return /^[0-9]+(\.\d+)?$/g.test(exp);
    }
});

Object.extend(Number.prototype,{
    isNumber:function(){
        return /^[0-9]+$/g.test(this.toString());
    },
    isDecimal:function(){
        return /^[0-9]+(\.\d+)?$/g.test(this.toString());
    }
});

var $=function(id){
    if(id && typeof id=="string"){
        var ele= document.getElementById(id);
        if(!ele) return null;
        ele.isNull=function(){
            return ele.value.trim().length==0;
        }
        ele.isRangeOut=function(iRange){
            if(!iRange.isNumber()) return;
            return ele.value.trim().length>iRange;
        }
        ele.isInteger=function(){
            return ele.value.isInteger();
        }
        ele.isNumber=function(){
            return ele.value.isNumber();
        }
        ele.isDate=function(){
            return ele.value.isDate();
        }
        return ele;
    }
    return null;
};
var $V=function(id){
    var target=$(id);
    if(target && target.value){
        return target.value.trim();
    }
    return "";
}
var Sys={
    //用户界面层
    UI:{
        Element:{
            $:function(id){
                if(id && typeof id=="string"){
                    var ele= document.getElementById(id);
                    if(!ele) return null;
                    ele.isNull=function(){
                        return ele.value.trim().length==0;
                    }
                    ele.isRangeOut=function(iRange){
                        if(!iRange.isNumber()) return;
                        return ele.value.trim().length>iRange;
                    }
                    ele.isInteger=function(){
                        return ele.value.isInteger();
                    }
                    ele.isNumber=function(){
                        return ele.value.isNumber();
                    }
                    ele.isDate=function(){
                        return ele.value.isDate();
                    }
                    return ele;
                }
                return null;
            },
            $V:function(id){
                var target=this.$(id);
                if(target && target.value){
                    return target.value.trim();
                }
                return "";
            },
            $html:function(id,html){
                this.$(id).innerHTML=html;
            },
            $DDLTEXT:function(id){
                if(id && typeof id=="string"){
                    var target=this.$(id);
                    if(target) return target.options[target.selectedIndex].text;
                }
            },
            $A:function(source){
                if(source){
                    var instance=[];
                    for(var property in source){
                        instance.push(source[property]);
                    }
                    return instance;
                }
                return [];
            },
            $Tag:function(parent,tag){
                if(tag && typeof tag=="string"){
                    if(this.isDomObject(parent)) return parent.getElementsByTagName(tag);
                }
                return null;
            },
            visibles:function(id,visible){
                try
                {
                    this.$(id).style.display=(visible?"block":"none");
                }
                catch(e){}
            },
            bindDDL:function(ddl,xml,node){
                if(ddl && xml && node){
                    var doc=new ActiveXObject("MICROSOFT.XMLDOM");
                    doc.async=false;
                    doc.load(xml);
                    var root=doc.documentElement;
                    if(root){
                        var items=root.selectSingleNode("//config/"+node+"");
                        if(items.childNodes.length){
                            ddl.options.add(new Option("--请选择--",""));
                            for(var iLen=items.childNodes.length,i=0;i<iLen;i++){
                                var text=" "+items.childNodes[i].text;
                                var value=items.childNodes[i].attributes(0).value;
                                var option=new Option(text,value);
                                ddl.options.add(option);
                            }
                        }
                    }
                }
            }
        },
        Page:{
            refresh:function(){
                window.location.href=window.location.href;
            }
        },
        URL:{
            getKey:function(key){
                var QUERY_STRING=window.location.search.substring(1);
                var params=QUERY_STRING.split("&");
                if(params.length){
                    for(var i=0,iLen=params.length;i<iLen;i++){
                        var _key=params[i].substring(0,params[i].indexOf("="));
                        if(_key==key) return true;
                    }
                }
                return false;
            },
            getParameter:function(key){
                var QUERY_STRING=window.location.search.substring(1);
                var params=QUERY_STRING.split("&");
                if(params.length){
                    for(var i=0,iLen=params.length;i<iLen;i++){
                        var _key=params[i].substring(0,params[i].indexOf("="));
                        var _value=params[i].substring(params[i].indexOf("=")+1);
                        if(_key==key) return _value;
                    }
                }
                return "";
            },
            transfer:function(url){
                window.location.replace(url);
            }
        },
        MsgBox:{
            show:function(msg){
                alert(msg);
            },
            confirm:function(msg){
                return confirm(msg);
            }
        },
        Event:{
            Mouse:{
                overColor:"#E8E8E8",
                over:function(target){
                    if(target && !target.key) target.style.backgroundColor=this.overColor;            
                },
                out:function(target){
                    if(target && !target.key) target.style.backgroundColor="";
                }
            }
        }
    }
};
var DateTime={
    now:{
        date:null,
        time:null
    },
    load:function(){
        var weekDays=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        var today = new Date();
        var weekDay=today.getDay();
        weekDay=weekDays[weekDay];
        var date = (today.getFullYear()) + "年" + (today.getMonth() + 1 ) + "月" + today.getDate() + "日 " + weekDay +"";
        this.now.date=date;
    }
};
