﻿//全局通用类
//*为必传参数

//正则表达试规则判断
var Expressions = {};

Expressions.Validate = function( strTargetID, arrValidateType )
{
    //正则表达式
    this.reg = 
    [
        /^$/,                                                               //是否为空      【1】
        /^-?[1-9]\d*$/,                                                     //是否为整数    【2】
        /^[1-9]\d*$/,                                                       //是否为正整数  【3】
        /((\d{11})|^((\d{7,8})|(\d{3})-(\d{3,4})-(\d{7,8})|(\d{3})-(\d{3,4})-(\d{7,8})-(\d{1,4})|(\d{3,4})-(\d{7,8})|(\d{3,4})-(\d{7,8})-(\d{1,4})|(\d{7,8})-(\d{1,4}))$)/,  
                                                                            //国内电话号码  【4】
        /[1-9][0-9]{4,}/,                                                   //QQ号码        【5】
        /[1-9]\d{5}(?!\d)/,                                                 //中国邮政编码  【6】 
        /\d{15}|\d{18}/,                                                    //15-18身份证   【7】
        /\d+\.\d+\.\d+\.\d+/,                                               //匹配ip地址    【8】 
        /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,                      //匹配Email地址的正则表达式 【9】
        /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/, 
                                                                            //URL，支持中文域名 【10】
        /^[a-zA-Z][a-zA-Z0-9_]{3,16}$/,                                     //账号：字母开头，允许4-16字节，允许字母数字下划线【11】
        /\d{11}|\d{15}/,                                                    //手机号:只验证长度为>=11位 【12】
        /^[a-zA-Z0-9_]{3,16}$/,                                             //密码：允许4-16字节【13】
        /^\d+(\.\d{1,2})?$/,                                                             //float【14】
        /\d{3,4}/,                                                        //15区号
    ];  
    var strTargetValue = document.getElementById(strTargetID).value;
    if (arrValidateType == 1 && strTargetValue.Trim().length == 0 )
        return false;
    else if (arrValidateType == 1)
        return true;
    return this.reg[arrValidateType-1].test( strTargetValue )
}
////光标移开输入判断

//*strTargetID: 需要验证的控件ID；
//*strShowErrorID: 显示错误信息的控件ID；
//*arrValidateType: 验证类型列表；
//arrShowErrorString：错误信息列表；
//strShowSuccessString: 成功信息列表
Expressions.BlurValidate = function( strTargetID, strShowErrorID, arrValidateType, arrShowErrorString, strShowSuccessString )
{
    //正则表达式
    this.reg = 
    [
        /^$/,                                                               //是否为空      【1】
        /^-?[1-9]\d*$/,                                                     //是否为整数    【2】
        /^[1-9]\d*$/,                                                       //是否为正整数  【3】
        /((\d{11})|^((\d{7,8})|(\d{3})-(\d{3,4})-(\d{7,8})|(\d{3})-(\d{3,4})-(\d{7,8})-(\d{1,4})|(\d{3,4})-(\d{7,8})|(\d{3,4})-(\d{7,8})-(\d{1,4})|(\d{7,8})-(\d{1,4}))$)/,  
                                                                            //国内电话号码  【4】
        /[1-9][0-9]{4,}/,                                                   //QQ号码        【5】
        /[1-9]\d{5}(?!\d)/,                                                 //中国邮政编码  【6】 
        /\d{15}|\d{18}/,                                                    //15-18身份证   【7】
        /\d+\.\d+\.\d+\.\d+/,                                               //匹配ip地址    【8】 
        /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,                      //匹配Email地址的正则表达式 【9】

        /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/, 
                                                                            //URL，支持中文域名 【10】           /^http(s)?:\/\/([\w\u4e00-\u9fa5-]+\.)+[\w\u4e00-\u9fa5-]+((:\d+)?)+(\/[\w\u4e00-\u9fa5- .\?%&=]+)*$/, 
        /^[a-zA-Z][a-zA-Z0-9_]{3,16}$/,                                     //账号：字母开头，允许4-16字节，允许字母数字下划线【11】
        /\d{11}|\d{15}/,                                                    //手机号:只验证长度为>=11位 【12】
        /^[a-zA-Z0-9_]{3,16}$/,                                             //密码：允许4-16字节【13】
        /^\d+(\.\d{1,2})?$/,                                                  //float【14】
        /\d{3,4}/,                                                        //15区号
    ];  
    
    //默认成功显示
    this.strSuccess = (strShowSuccessString === undefined)?"输入正确":strShowSuccessString;
    //默认失败显示
    this.strError   = (arrShowErrorString === undefined)?"输入错误":arrShowErrorString;
    
    //现实处理
    var domTargete   = document.getElementById(strTargetID);
    var domShowError = document.getElementById(strShowErrorID);
    var strTargetValue = domTargete.value;
    var bSuccessed = true;
    for( var i = 0; i<arrValidateType.length; i++ )
    {
        //为空判断
        if( arrValidateType[i] == 1 )
        {
            if( strTargetValue.Trim() == "" )
            { 
                bSuccessed = false;
                break;  
            }
            continue;
        }
        
        //其他正则验证
        if( this.reg[arrValidateType[i]-1].test( strTargetValue ) )
            continue;
        bSuccessed = false;
        break;    
    }
    
    //验证通过
    if( bSuccessed )
    {
        domShowError.innerHTML = this.strSuccess;
        domShowError.className = "correct_msg";
    }
    else
    {//验证失败
        
        if( this.strError != "输入错误" )
            domShowError.innerHTML = arrShowErrorString[i];
        else
            domShowError.innerHTML = this.strError;
        domShowError.className = "error_msg";
    }
    
    domShowError.style.display = "inline"; 
    return bSuccessed;             
}

///鼠标聚焦显示提示信息

//*strShowInfoID: 显示提示信息的控件ID；
//*strShowInfoString：提示信息；
Expressions.FocusShowContent = function( strShowInfoID, strShowInfoString )
{
    var domShowInfo = document.getElementById(strShowInfoID);    
    
    //定义了提示信息
    domShowInfo.innerHTML = strShowInfoString;
    
    domShowInfo.className = "info_msg";  
    domShowInfo.style.display = "inline";      
}

////提交输入判断

//*输入参数：strTab ( 标签 )
//返回参数：成功返回true   失败返回false
Expressions.SubmitValidate = function( strTab )
{
    var strError = strTab+".error_msg"; var strInfo = strTab+".info_msg";
    
    var bSuccessed = true;

    //验证不通过    
    $(strError).each( //错误
        function(i)
        {
            this.style.display = "inline";
            if(i==0)
            {
                this.focus();
                var topoffset = $(this).offset().top > 120 ? $(this).offset().top - 120 : $(this).offset().top-30;
                $(document).scrollTop(topoffset)   
            }
            bSuccessed = false;
        }     
    );
    $(strInfo).each( //提示
        function(i)
        {
            this.style.display = "inline";
            if(i==0)
            {
                this.focus();
                var topoffset = $(this).offset().top > 120 ? $(this).offset().top - 120 : $(this).offset().top-30;
                $(document).scrollTop(topoffset)
            }
            bSuccessed = false;            
        }
    );
    return bSuccessed;           
}

//Date类重定义Date.parse()静态方法实现
var _saveDateParseFunction = Date.parse;
Date.parse = function( strText )
{
    if( /^[0-9]{2,4}-[0-9]{1,2}-[0-9]{1,2}( [0-9]{1,2}(:[0-9]{1,2}(:[0-9]{1,2}(.[0-9]{1,3})?)?)?)?$/.test( strText ) )
    {
        var find = strText.match( /[0-9]+/g );
        
        var nYear    = parseInt( find[0] );
        var nMonth   = parseInt( find[1] );
        var nDay     = parseInt( find[2] );
        var nHour    = parseInt( find[3] );
        var nMinute  = parseInt( find[4] );
        var nSecond  = parseInt( find[5] );
        var nMSecond = parseInt( find[6] );
        
        if( isNaN(nHour) )    nHour = 0;
        if( isNaN(nMinute) )  nMinute = 0;
        if( isNaN(nSecond) )  nSecond = 0;
        if( isNaN(nMSecond) ) nMSecond = 0;
        
        return new Date( nYear, nMonth-1, nDay, nHour, nMinute, nSecond, nMSecond );
    }
    else
    {
        return new Date( _saveDateParseFunction(strText) );
    }
}

//Date类重定义toString()方法实现
//showType   （Date.prototype.toString("N") 代表中文格式）
Date.prototype.toString = function( showType )
{    
    var nHour    = this.getHours();
    var nMinute  = this.getMinutes();
    var nSecond  = this.getSeconds();
    var nMSecond = this.getMilliseconds();
    
    //中文 格式
    if( showType == "N" )
    {
        if( nHour == 0 && nMinute == 0 && nSecond == 0 && nMSecond == 0 )
        {
            return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日";
        }
        
        if( nMSecond == 0 )
        {
            return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日";
                + " " + nHour + ":" + nMinute + ":" + nSecond;
        }
        else
        {
            return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日";
                + " " + nHour + ":" + nMinute + ":" + nSecond
                + "." + ( nMSecond + 1000 ).toString().substr( 1 );
        }    
        return;
    }
    else if (showType == "G")
    {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
    }
    else if (showType == "H")
    {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate()+" "+nHour+":"+nMinute;
    }
    if( nHour == 0 && nMinute == 0 && nSecond == 0 && nMSecond == 0 )
    {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
    }
    
    if( nMSecond == 0 )
    {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate()
            + " " + nHour + ":" + nMinute + ":" + nSecond;
    }
    else
    {
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate()
            + " " + nHour + ":" + nMinute + ":" + nSecond
            + "." + ( nMSecond + 1000 ).toString().substr( 1 );
    }
}

//返回截去左导空格后的字串
String.prototype.TrimLeft = function()
{
    return this.replace( /^\s*/, "" );
}

//返回截去右导空格后的字串
String.prototype.TrimRight = function()
{
    return this.replace( /\s*$/, "" );
}

//返回截去左导右导空格后的字串
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

//全局通用类
Global = {};
/**
 * 稍后调用
 * @param {Object} method 目标调用函数
 * @param {Object} ...    其它调用参数
 * @return 定时器句柄(可以使用clearTimeout清除)
 */
Global.afterCall = function( method )
{
    if( method == null )
        return null;

    var METHOD = method;
    var params = arguments;
    var callObject = { timer:null };
    
    callObject.timer = window.setTimeout(
        function()
        {
            //注意: 在IE中,定时器清除以后有时还会执行,这是IE的BUG
            if( callObject.timer == null ) return;

            METHOD( params[2], params[3], params[4], params[5], params[6] );   
        },
        0 );
        
    return callObject;
}

/**
 * 延长一定时间后调用
 * @param {Object} method  目标调用函数
 * @param {Int}    timeout 延时时长
 * @return 定时器句柄(可以使用clearTimeout清除)
 */
Global.timerCall = function( method, timeout )
{
    if( timeout == undefined )
        throw "Invalid timerCall timeout Param!";    
    if( method == null )
        return null;
        
    var METHOD = method;
    var params = arguments;
    var callObject = { timer:null };

    callObject.timer = window.setTimeout(
        function()
        {
            //注意: 在IE中,定时器清除以后有时还会执行,这是IE的BUG
            if( callObject.timer == null ) return;

            METHOD( params[2], params[3], params[4], params[5], params[6] );   
        },
        timeout );
        
    return callObject;
}

/**
 * 清除延时调用
 * @param {Object} callObject Global.afterCall或Global.timerCall函数返回的对象
 */
Global.clearCall = function( callObject )
{
    if( callObject != null && callObject.timer != null)
    {
        window.clearTimeout( callObject.timer );
        callObject.timer = null;
    }
}

// 定时器

/**
 * 定时调用
 * @param {Object} method  目标调用函数
 * @param {Int}    timeout 延时时长
 * @return 定时器对象(可以使用Global.clearRecall清除)
 */
 
Global.timerRecall = function(  method, timeout )
{
    if( timeout == undefined )
        throw "Invalid timerCall timeout Param!";    
    if( method == null )
        return null;

    var METHOD = method;
    var params = arguments;
    var recallObject = { timer:null };

    recallObject.timer = window.setInterval(
        function()
        {
            //注意: 在IE中,定时器清除以后有时还会执行,这是IE的BUG
            if( recallObject.timer == null ) return;

            METHOD( params[2], params[3], params[4], params[5], params[6] );   
        },
        timeout
    );
    
    return recallObject;
}

/**
 * 清除定时调用
 * @param {Object} recallObject Global.timerRecall函数返回的对象
 */
Global.clearRecall = function( recallObject )
{
    if( recallObject != null && recallObject.timer != null )
    {
        window.clearInterval( recallObject.timer );
        recallObject.timer = null;
    }
}

//创建遮盖层
//使用前请应用脚本：jquery.js    样式文件：dialog.js
var OverMain = {};

//创建
OverMain._create = function( owner )
{
    this._owner = $(owner);
    this.overMain = $(_CrtEle("DIV"));
    this.overMain.attr("class","write_overlay").css("height",owner.offsetHeight).css("width",owner.offsetWidth).css("display", "none");
    var iframe = "<iframe   src= \"about:blank\"  frameborder=\"0\"  style= \"position:absolute;   visibility:inherit;   top:2px;   left:0px;   width:100%;     z-index:-1;   filter= 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"> </iframe>";
    this.overMain.html(iframe);
    this.loading = $(_CrtEle("DIV"));
    
    this.loading.css({ "top" : "50%", "left" : "50%", "z-index" : "1000","display" : "none" });
    var imgLoading = "<img src=\"/main/image/loading.gif\" alt=\"\">";
    this.loading.html(imgLoading).ajaxStop(function(){
        $(this).hide();
    });
    this._owner.append(this.loading);
    this._owner.append(this.overMain);
}

//显示
OverMain._show = function(loading)
{
    if(this.overMain)
        this.overMain.show();
    if(loading)
        this.loading.show();
}


//隐藏
OverMain._remove = function()
{
    if(this.overMain)
    {
        this.overMain.hide();
        this.loading.hide();
    }
}

function createAbsoluteDiv(id, zIndex)
{   
    var bodyBack = null;
    if(document.getElementById(id))
    {        
        bodyBack = document.getElementById(id); 
        bodyBack.style.display = "";      
    }
    else
    {
        bodyBack = document.createElement("div");
        document.body.appendChild(bodyBack);    
    }
        
    bodyBack.setAttribute("id",id)
    bodyBack.style.position = "absolute";
    bodyBack.style.width = "100%";
    bodyBack.style.height = (getPageSize()[1] + 'px');
    bodyBack.style.zIndex = zIndex;
    bodyBack.style.top = 0;
    bodyBack.style.left = 0;
    bodyBack.style.filter = "alpha(opacity=50)";
    bodyBack.style.opacity = 0.5;
    bodyBack.style.background = "#ffffff";

    var iframe = "<iframe   src= \"about:blank\"  frameborder=\"0\"  style= \"position:absolute;   visibility:inherit;   top:2px;   left:0px;   width:100%; height:"+getPageSize()[1]+"px;   z-index:-1;   filter= 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';\"> </iframe>";
   
    bodyBack.innerHTML = iframe;
    return bodyBack;
}

function PageOpen(url)
{
    var a = document.createElement("a");
    a.target="_blank";
    a.href = url;
    document.body.appendChild(a);
    a.click();
}

        function PageOpen2(url,id)
        {
            if (_$(id))
                _$(id).href = url;
            else
            {
                var a = document.createElement("a");
                a.id = id;
                a.target="_blank";
                a.href = url;
                a.innerHTML = "";
                document.body.appendChild(a);
            }
            _$(id).click();
        }