﻿/*=============================================================================*/
//常用到的js方法
//author: 钟国勇
//date  : 2006-11-04

//去掉两端空格 
//用法:  var str = " abc ";
//       str = str.Trim()
String.prototype.Trim = function()
{
    return   this.replace(/(^\s*)|(\s*$)/g,"");
}
//去掉左端空格 
String.prototype.LTrim = function(){
    return   this.replace(/(^\s*)/g,"");
}  
//去掉右端空格  
String.prototype.RTrim = function(){
    return   this.replace(/(\s*$)/g,"");
}   


function chnStyle(obj,styleName){
    if(typeof(obj) == 'string')
    obj = $(obj);
    obj.className = styleName;
}

//过滤危险字符
function filtrate(content)
{
//    //过滤所有事件和脚本
//    str = str.replace(/<script(.|\n)*\/script>\s*/ig, "");
//    str = str.replace( /<[^>]+/ig, function($0){return $0.replace(/\s*on\w+=[^ ]+/ig, "")});
//    
//    //清楚所有html标签
//    str = str.replace((?<Style><style[\s\S]+?/style>)|(?<Script><script[\s\S]*?/script>)|(?<Explein><\!\-\-[\s\S]+?\-\->)|(?<Html></?\s*[^> ]+(\s*[^=>]+?=['"]?[^"']+?['"]?)*[^>]*>)|(?<Other>( )|(")|(&)|(·))|(?<Space>\s+),"");
 
    
    //过滤我用来分隔信息的特殊字符
    content = content.replace("|","｜");
    return content;    
}

//根据对象ID获取对象
function getObject(objId)
{
    if(typeof(objId)=='object') return objId;
    return document.getElementById(objId);
}

//使图片根据设定的高宽自适应大小
//用法: <img src="zzz.gif" style="zoom:0.1;" onload="SetSize(this,100,100)" alt="xxx" />
function SetSize(obj, width, height)
{
    myImage = new Image();
    myImage.src = obj.src;
    if (myImage.width>0 && myImage.height>0)
    {
        var rate = 1;
        if (myImage.width>width || myImage.height>height)
        {
            if (width/myImage.width<height/myImage.height)
            {
                rate = width/myImage.width;
            }
            else
            {
                rate = height/myImage.height;
            }
        }
        if (window.navigator.appName == "Microsoft Internet Explorer")
        {
          obj.style.zoom = rate;
        }
        else
        {
          obj.width = myImage.width*rate;
          obj.height = myImage.height*rate;
        }
    }
}

//清空指定对象的值
function clear(objId)   
{
    if(typeof(objId)=='object'){
        objId.value = "";
    }else{
        document.getElementById(objId).value = "";
    }
}

//指示指定对象是否可用,true为禁用,false可用
function changeDisable(objId,isDisable)
{
    if(isDisable)
    {
        getObject(objId).disabled = "disabled";
    }
    else
    {
        getObject(objId).disabled = "";
    }
}

//指示指定对象是否显示,true为显示,false隐藏
function changeDisplay(objId,isDisplay)
{
    if(isDisplay)
    {
        getObject(objId).style.display = "";
    }
    else
    {
        getObject(objId).style.display = "none";
    }
}

//显示提示信息
//param: 
//  objId 要显示信息的对象ID,建议使用div,然后在该div上套用自己的样式.  msg:提示信息.  time:多久后自动隐藏,0为不隐藏.
function ShowMsg(objId,msg,time)
{
    var obj = getObject(objId);
    if(obj == 'undefined') return;
        obj.innerHTML = msg; 
        obj.style.display = "";
    if(time != 0) window.setTimeout("HidMsg("+objId+")",time);
}

//隐藏提示信息
function HidMsg(objId)
{
    if(typeof(objId)=='object')
    {
        objId.style.display = "none";
    }
    else
    {
        var obj = getObject(objId);     
            obj.style.display = "none";
    }
}  

//
//一个表单输入值检测类
//author:钟国勇
//date  :2006-11-06
//e.g.
//  先定义样式
//  <script>
//      Checker.ErrStyle = 'Err';
//      Checker.RightStyle = 'Right';
//  </script>
//  <input id="realName" onblur="Checker.Check(this)" onfocus="Checker.Check(this)" check="Chinese" message="请输入中文" showobj="realNameMsg" />
//
//
function Checker(){
    this.Version = "v 1.0";
    this.Author = "钟国勇";
    this.LastModify = "2006-11-06";
}
    //必填项
    Checker.Require = function chkRequire(v){
        return regex(/\S+/,v);
    }
    //Email地址
    Checker.Email = function chkEmail(v){
        return regex(/^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/);
    }
    //电话
    Checker.Telphone = function chkTelphone(v){
        return regex(/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/,v);
    }
    //手机
    Checker.Mobile = function chkMobile(v){
        return regex(/^((\(0?13\d{1}\))|(0?13\d{1}))(\-?)(\d{8})$/,v);       
    }
    //小灵通和手机
    Checker.MobileTel = function chkMT(v){
        return regex(/^(((\(0?1\d{2}\))|(0?1\d{2}))(\-?)(\d{8}))$|^((0592){0,1}(\d{7,8}))$/,v);
    }
    //Url格式
    Checker.Url = function chkUrl(v){
        return regex(/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,v);
    }
    //身份证
    Checker.IDCard = function chkIDCard(v){
        return regex(/^\d{15}(\d{2}[A-Za-z0-9])?$/,v);
    }
    //货币
    Checker.Currency = function chkCurrency(v){
        return regex(/^\d+(\.\d+)?$/,v);
    }
    //邮政编码
    Checker.Zip = function chkZip(v){
        return regex(/^[1-9]\d{5}$/,v);
    }  
    //QQ
    Checker.QQ = function chkQQ(v){
        return regex(/^[1-9]\d{4,8}$/,v);
    }      
    //是否为中文
    Checker.Chinese =function chkChinese(v){
        return regex(/^[\u0391-\uFFE5]+$/,v);
    }
    //是否为英文
    Checker.English = function chkEnglish(v){
        return regex(/^[A-Za-z]+$/,v);
    }
    //是否为数字
    Checker.Number = function chkNumber(v){
        return regex(/^\d+$/,v);
    }
    //是否为整数
    Checker.Integer = function chkInteger(v){
        return regex(/^[-\+]?\d+$/,v);
    }
    //是否为实数
    Checker.Double = function chkDouble(v){
        return regex(/^[-\+]?\d+(\.\d+)?$/,v);
    }
    //是否为不安全密码,不安全为true
    Checker.UnSafe = function chkUnSafe(v){
        return regex(/^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,v);
    }
    //日期  
    //  op:检测的日期值, 
    //  formatString:检测的日期格式 (ymd:年-月-日,dmy:日-月-年)
    Checker.Date = function(op, formatString){
		formatString = formatString || "ymd";
		var m, year, month, day;
		switch(formatString){
			case "ymd" :
				m = op.match(new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$"));
				if(m == null ) return false;
				day = m[6];
				month = m[5]--;
				year =  (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
				break;
			case "dmy" :
				m = op.match(new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$"));
				if(m == null ) return false;
				day = m[1];
				month = m[3]--;
				year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
				break;
			default :
				break;
		}
		var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate());
		function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}
	}
	//是否超出给定的范围
	//  v:值
	//  min:最小长度
	//  max:最大长度
	//	
	Checker.Limit = function chkLimit(v,min,max){
	    var len = v.length;
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	}
	//设定用来显示提示信息的对象ID
	Checker.ObjMsg = "";
	//是否提示信息
	//  isDisplay:是否要显示,true为显示
	//  Msg:提示信息
	//  time:多少毫秒后隐藏,0为永久不隐藏
	Checker.Msg = function showMsg(isDisplay,Msg,time){
	    if(isDisplay){
	        ShowMsg(Checker.ObjMsg,Msg,time);
	    }
	    return isDisplay;
	}
	//出现错误的提示样式
	Checker.ErrStyle = "";
	//正确时的样式
	Checker.RightStyle = "";
	//是否改变对象的样式	
	Checker.ChangeStyle = function changeStyle(objId,isChange){
	    var obj = getObject(objId);
	    if(isChange){
	        obj.className = Checker.ErrStyle;
	    }else{
	        obj.className = Checker.RightStyle;
	    }	    
	    return isChange;
	}
	//放在ＨＴＭＬ控件里的检测方法	
    Checker.Check = function check(obj){
        var checkType = obj.getAttribute("check");
        if(typeof(checkType)=='object' || typeof(eval(Checker[checkType]))=='undefined'){
            return;
        }        
        var checked = eval(Checker[checkType](obj.value));
        if(!checked){
            var msg = obj.getAttribute("message");
            var showobj = obj.getAttribute("showobj");
            if(msg != null && showobj != null){
                ShowMsg(getObject(showobj),msg,0);
            }
        }
        return Checker.ChangeStyle(obj,!checked);
    }
    //清除样式
    Checker.ClearStyle = function clearStyle(obj){
        getObject(obj).className = "";
        var msg = obj.getAttribute("message");
        var showobj = obj.getAttribute("showobj");
        if(msg != null && showobj != null){
            getObject(showobj).innerHTML = "";
        }
    }
    
//根据正则表达式检测值
function regex(regStr,v){
    var reg = regStr;
    return reg.test(v);
}


function GetDX(v){
    switch(v){
        case 0: return '○'; break;
        case 1: return '一'; break;
        case 2: return '二'; break;
        case 3: return '三'; break;
        case 4: return '四'; break;
        case 5: return '五'; break;
        case 6: return '六'; break;
        case 7: return '七'; break;
        case 8: return '八'; break;
        case 9: return '九'; break;
    }
}