﻿/***DataBase类目录**********************************************************************************************
    1、字符串处理部分
     1)、Str_Guid                 获取GUID
     2)、Str_Trim                 去空格
     3)、Str_delMarks             去引号
     4)、Int_StrLen               获取字符串实际长度
     5)、Str_SubRight             截取右边出现的字符
     6)、Str_SubLeft              截取左边出现的字符
     7)、Str_SubCenter            保留中间字符,截取左右出现的位置
     8)、Str_Match                字符在另外一个字符串中出现
     9)、Str_RightSpot            在制定位置截取字符并加入符号,默认加入...
     10)、Str_Substring           截取字符,从str中截取第一个由startmark和endmark包含的字符串内容
     11)、Str_StrListChange       对StrBody中的字符串进行替换，方法是依次查找StrSetA中的每一个元素，并将值替换为StrSetB中对应的值
     12)、Str_ToInteger           将指定的字符串转换为整数,如果无法转换则返回DefaultIntValue中指定的默认值
     13)、Str_ToDouble            将指定的字符串转换为双精度,如果无法转换则返回DefaultIntValue中指定的默认值
    2、页面处理部分
     1)、getById                  获取当页面ID
     2)、getByName                获取当页面Name
     3)、insertHtml               插入HTML方法
     4)、delHtml                  删除HTML
     5)、childNodes               获取子节点数量
     6)、operWin                  打开窗体
     7)、closeWin                 关闭窗体
     8)、redirectWin              跳转窗体
     9)、reloadHtml               刷新窗体
     10)、messageHtml             提示信息
     11)、backHtml                退回上一页
    3、控件处理部分
     1)、showPanel                打开窗体
     2)、closePanel               关闭窗体
     3)、addSelect                添加select
     4)、delSelect                删除select
     5)、clrSelect                清空select
     6)、cheCheckbox              反选Checkbox
     7)、sehText                  搜索Text
     8)、addText                  添加Text     
***Ctrl+F进行查找**************************************************************************************************/


//字符串处理部分-----------------------------------------------------------------------------------------------


//获取GUID
function Str_Guid()
{
    var guid = "{";
    for(var i=1;i<=32;i++)
    {
        var n = Math.floor(Math.random()*16.0).toString(16);
        guid+=n;
        if((i==8) || (i==12) || (i==16)||(i == 20))
        guid+="-";
    }
    guid+="}";
    return guid;
}

//去空格
function Str_Trim(str) 
{ 
   if (str==null || str=="" )   { return ""; }
   return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
function Str_LTrim(str) 
{ 
   if (str==null || str=="" )   { return ""; }
   return str.replace(/(^\s*)/g, "");
}
function Str_RTrim(str) 
{ 
   if (str==null || str=="" )   { return ""; }
   return str.replace(/(\s*$)/g, ""); 
} 

//去引号
function Str_delMarks(str) 
{ 
   if (str==null || str=="" )   { return ""; }
   return str.replace(/\"/g, ""); 
}

//获取字符串实际长度
function Int_StrLen(str)
{
   if (str==null || str=="" )   { return 0; }
   return str.replace(/[^\x00-\xff]/g,"xx").length;
}

//截取右边出现的字符
function Str_SubRight(str, strSign)
{
    if (str==null     || str=="" )     { return "";  }
    if (strSign==null || strSign=="" ) { return str; }
    try
    {
        str            = Str_Trim(str);
        var intPath    = str.indexOf(strSign) + strSign.length;
        str            = str.substring(intPath,str.length);
        return str;
    }catch (e) { return str; }        
}

//截取左边出现的字符
function Str_SubLeft(str, strSign)
{
    if (str==null     || str=="" )     { return "";  }
    if (strSign==null || strSign=="" ) { return str; }
    try
    {
        str         = Str_Trim(str);
        var intPath = str.indexOf(strSign);
        str         = str.substring(0, intPath);
        return str;
    }catch (e) { return str; }  
    
}

//保留中间字符,截取左右出现的位置
function Str_SubCenter(str, strSignA, strSignB)
{
    if (str==null      || str=="" )      { return "";   }
    if (strSignA==null || strSignA=="" ) { return str;  } 
    if (strSignB==null || strSignB=="" ) { return str;  }    
    try
    {       
        return Str_SubLeft(Str_SubRight(str, strSignA), strSignB);
    }catch (e) { return str; } 
}

//字符在另外一个字符串中出现
function Str_Match(str, chr)
{
   if (str==null || str=="" ) { return ""; }
   if (chr==null || chr=="" ) { return 0;  }
   try
   {
     var matches    = str.match(chr);
     return matches == null ? 0 : matches.length;
   }catch(e){return 0;}
}

//在制定位置截取字符并加入符号,默认加入...
function Str_RightSpot(str,spot,sign)
{
    if (str==null  || str=="" )  { return ""; }
    if (sign==null || sign=="" ) { sign = "..."; }
    if (spot==null || spot<0)    { spot = str.length; } 
    try
    {
        if (str.length > spot) { spot = spot; } else { spot = str.length; }
        strsub = str.substr(0, spot);
        if (str.length > spot) { return strsub + sign; } else { return strsub; }
    }catch(e){return str;}
}

//截取字符,从str中截取第一个由startmark和endmark包含的字符串内容
function Str_Substring(str,startmark,endmark)
{
    if (str==null       || str=="" )       { return "";   }
    if (startmark==null || startmark=="" ) { return str;  } 
    if (endmark==null   || endmark=="" )   { return str;  }  
    try
    {
        var startidx = -1;
        var endidx   = -1;
        str          = Str_Trim(str);
        if (startmark==null || startmark=="") { startidx = 0; }
        else { startidx = str.indexOf(startmark);}
        if (startidx >= 0) { startidx = startidx + Int_StrLen(startmark); }
        if (endmark==null || endmark=="") { endidx = Int_StrLen(str);}
        else { endidx = str.indexOf(endmark); }
        if ((startidx < 0) || (endidx < 0) || ((startidx >= 0) && (startidx >= endidx))) { return ""; }
        else { return Str_Trim(str.substring(startidx, endidx)); }
    }catch(e){return str;}
}

// 对StrBody中的字符串进行替换，方法是依次查找StrSetA中的每一个元素，并将值替换为StrSetB中对应的值
function Str_StrListChange(StrBody, StrSetA,StrSetB,Delimiter)
{      
    if (StrBody==null   || StrBody=="" )   { return "";   }
    if (StrSetA==null   || StrSetA=="" )   { return StrBody;  } 
    if (StrSetB==null   || StrSetB=="" )   { return StrBody;  }    
    if (Delimiter==null || Delimiter=="")  { Delimiter = ",";}        
    try
    {
        var StrSetValuesA = Str_Trim(StrSetA).split(Delimiter);
        var StrSetValuesB = Str_Trim(StrSetB).split(Delimiter);
        var MinSetCNT     = StrSetValuesA.length;
        if (StrSetValuesA.length>StrSetValuesB.length) { MinSetCNT=StrSetValuesB.length;}
        for (var idx = 0; idx < MinSetCNT; idx++)
        {
            if (StrSetValuesA[idx].toString() != "")
            {
                StrBody = StrBody.replace(StrSetValuesA[idx].toString(), StrSetValuesB[idx].toString());
            }
        }
    }catch(e){}
    return StrBody;    
}

//将指定的字符串转换为整数,如果无法转换则返回DefaultIntValue中指定的默认值
function Str_ToInteger(InStr, DefaultIntValue)
{     
    InStr           = Str_Trim(InStr);
    DefaultIntValue = Str_Trim(DefaultIntValue);    
    if (DefaultIntValue==null || DefaultIntValue=="" || isNaN(DefaultIntValue)) { DefaultIntValue = 0; }    
    if (InStr==null           || InStr==""           || isNaN(InStr))           { InStr = DefaultIntValue; }   
    try
    {
       return parseInt(InStr);
    }
    catch (e)
    {
       return parseInt(DefaultIntValue);
    }
}

//将指定的字符串转换为双精度,如果无法转换则返回DefaultIntValue中指定的默认值
function Str_ToDouble(InStr, DefaultIntValue)
{   
    InStr           = Str_Trim(InStr);
    DefaultIntValue = Str_Trim(DefaultIntValue);
    if (DefaultIntValue==null || DefaultIntValue=="" || isNaN(DefaultIntValue)) { DefaultIntValue = 0; }    
    if (InStr==null           || InStr==""           || isNaN(InStr))           { InStr = DefaultIntValue; }   
    try
    {
       return parseFloat(InStr);
    }
    catch (e)
    {
       
       return parseFloat(DefaultIntValue);
    }
}

/*
now=new Date();
hours = now.getHours(); //得到小时
minutes = now.getMinutes(); //得到分钟
seconds = now.getSeconds(); //得到秒

year=now.getYear(); //得到年份
month=now.getMonth()+1; //得到月份
date=now.getDate(); //得到日期
day=now.getDay(); //得到星期数

一个显示时间日期的函数：

function clock()
{
var timeStr, dateStr;
now = new Date();
// time
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
if(hours < 10)
   timeStr = "0"+hours;
else timeStr=hours;
if(minutes < 10)
   timeStr+=":0"+minutes;
else timeStr+=":"+minutes;
if(seconds < 10)
   timeStr+=":0"+seconds;
else timeStr+=":"+seconds;
document.clock.time.value=timeStr;
//date
year=now.getYear();
month=now.getMonth()+1;
date=now.getDate();
dateStr=year;
if(month<10)
   dateStr+="/0"+month;
else dateStr+="/"+month;
if(date<10)
   dateStr+="/0"+date;
else dateStr+="/"+date;
document.clock.date.value=dateStr;
Timer=setTimeout("clock()",1000);
}
*/

//页面处理部分-----------------------------------------------------------------------------------------------


//关闭刷新提示
/*window.onbeforeunload = function() {    
    var blnCheckUnload = true;
    if(blnCheckUnload) {
        return("您确定不保存退出吗?");
    }
}*/

//获取当页面ID
function getById(id)
{
     if (id==null || id=="") { return ;}
     try
     {
         if (document.getElementById(id)!=null)
         {
            return document.getElementById(id);
         }
         else if (parent.document.getElementById(id)!=null)
         {
            return parent.document.getElementById(id);
         }
         else if(window.opener.document.getElementById(id)!=null) 
         {
            return window.opener.document.getElementById(id);
         }
     }catch(e){}
}

//获取当页面Name
function getByName(name)
{   
     if (name==null || name=="") { return ;}
     try
     { 
         if (document.getElementsByName(name)!=null && document.getElementsByName(name).length>0)
         {
            return document.getElementsByName(name);
         }
         else if (parent.document.getElementsByName(name)!=null && parent.document.getElementsByName(name).length>0)
         {
            return parent.document.getElementsByName(name);
         }
         else if(window.opener.document.getElementsByName(name)!=null && window.opener.document.getElementsByName(name).length>0) 
         {
            return window.opener.document.getElementsByName(name);
         }     
     }catch(e){}
}

//插入HTML方法
function insertHtml(obj,type,html){
    if (obj==null)  { return ;}
    if (type==null  || type=="") { type="afterbegin"; }
    if (html==null) { html=""; }
    try
    {
        type  = type.toLowerCase();
        if(obj.insertAdjacentHTML){
            switch(type){
                case "beforebegin":   //在标签前一个对象插入
                    obj.insertAdjacentHTML('BeforeBegin', html);
                    return obj.previousSibling;
                case "afterbegin":    //在标签对象里插入,前
                    obj.insertAdjacentHTML('AfterBegin', html);
                    return obj.firstChild;
                case "beforeend":     //在标签对象里插入,后
                    obj.insertAdjacentHTML('BeforeEnd', html);
                    return obj.lastChild;
                case "afterend":      //在标签后一个对象插入
                     obj.insertAdjacentHTML('AfterEnd', html);
                     return obj.nextSibling;
            }
        }
        var range = obj.ownerDocument.createRange();
        var frag;
        switch(type){
            case "beforebegin":  
                range.setStartBefore(obj);
                frag = range.createContextualFragment(html);
                obj.parentNode.insertBefore(frag, obj);
                return obj.previousSibling;
            case "afterbegin":  
                if(obj.firstChild){
                    range.setStartBefore(obj.firstChild);
                    frag = range.createContextualFragment(html);
                    obj.insertBefore(frag, obj.firstChild);
                    return obj.firstChild;
                }else{
                    obj.innerHTML = html;
                    return obj.firstChild;
                }
            case "beforeend":
                if(obj.lastChild){
                    range.setStartAfter(obj.lastChild);
                    frag = range.createContextualFragment(html);
                    obj.appendChild(frag);
                    return obj.lastChild;
                }else{
                    obj.innerHTML = html;
                    return obj.lastChild;
                }
            case "afterend":
                range.setStartAfter(obj);
                frag = range.createContextualFragment(html);
                obj.parentNode.insertBefore(frag, obj.nextSibling);
                return obj.nextSibling;
        }
    }catch(e){}
}

//删除HTML
function delHtml(obj,type,html){
   if (obj==null)  { return ;}
   if (type==null  || type=="") { type="afterbegin"; }
   if (html==null) { html=""; }
   try
   {
     obj.innerHTML="";
     if (html!="") { insertHtml(obj,type,html);}
   }catch(e){}
}
function delNodesHtml(obj){
   if (obj==null) { return ;}
   try
   {
      obj.parentNode.removeChild(obj);
   }catch(e){}
}

//获取子节点数量
function childNodes(obj){
   if (obj==null) { return 0;}
   var k=0;
   for(var i=0;i<obj.childNodes.length;i++)   
   {   
      if (obj.childNodes[i].innerHTML!=null)
      {
         k++;
      }
   }
   return k;
}

//打开窗体
function operWin(url,width,height){
    if (url==null    || url=="")   { return ; }
    if (width==null  || width<=0)  { width  = 600; }
    if (height==null || height<=0) { height = 400; }
    var strLeft = parseInt((window.screen.width-Width))/2;
    var strTop  = parseInt((window.screen.height-Height))/2;
    window.open(url, 'poppage', 'toolbars=0, scrollbars=1, location=0, statusbars=0, menubars=0, resizable=0, width=' + width + ', height=' + height  + ',Top=' + strTop + ',Left='+ strLeft);
}

//关闭窗体
function closeWin(){
    return window.close();
}

//跳转窗体
function redirectWin(url){
    if (url==null || url=="") { return ; }
    window.location.href = url;    
}

//刷新窗体
function reloadHtml(){
    return window.top.location.reload();
}

//提示信息
function messageHtml(str){
    if (str==null) { str=""; }
    alert(str);
    window.location=window.location;
}

//退回上一页
function backHtml(){
    window.history.back();
}


//控件处理部分-----------------------------------------------------------------------------------------------


var bgObj,msgObj;
var w3c=(document.getElementById)? true: false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));

//当前屏幕高度
function IeTrueBody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

//滚动条滚动高度
function GetScrollTop(){
return ie ? IeTrueBody().scrollTop : window.pageYOffset;
}

//打开窗体
function showPanel(url,iWidth,iHeight,state)
{    
    if (url==null     || url=="")    { return false; }
    if (iWidth==null  || iWidth<=0)  { iWidth  = 600; }
    if (iHeight==null || iHeight<=0) { iHeight = 400; }
    if (state==null)                 { state=true;}
    var bordercolor      = "#7F9DB9";   // 提示窗口的边框颜色
    var bgcolor          = "#FFFFFF";   // 提示内容的背景色    
    var x                = GetScrollTop() + document.documentElement.clientHeight;
    var y                = document.documentElement.clientHeight;
    var strHiehgt        = parseInt((x-y/2) - iHeight/2);
    bgObj                = document.createElement("div");
    cssText              = "position:absolute;left:0px;top:0px;width:" + document.documentElement.clientWidth + "px;height:" + Math.max(document.body.clientHeight, document.documentElement.clientHeight) + "px;filter:Alpha(Opacity=50);opacity:0.5;z-index:101;";
    if (state==true) {
        bgObj.style.cssText  = cssText + "background-color:#FFFFFF;";
        bgObj.onmousedown    = closePanel;
    } else {    
        bgObj.style.cssText  = cssText + "background-color:#000000;";
    }
    document.body.appendChild(bgObj);
    msgObj               = document.createElement("div");
    msgObj.style.cssText = "position:absolute;font:11px '宋体';top:" + strHiehgt + "px;left:" + (document.documentElement.clientWidth-iWidth) /2 + "px;width:" + iWidth + "px;height:" + iHeight + "px;text-align:center;border:1px solid " + bordercolor + ";background-color:" + bgcolor + ";padding:0px;line-height:22px;z-index:102;";
    var msg              = "<iframe src="+ url +" tabindex=-1 container width=100% height=100% frameborder=0 framespacing=0 marginheight=0 marginwidth=0></iframe>";
    msgObj.innerHTML     = msg;
    document.body.appendChild(msgObj);
    return false;
}

//关闭窗体
function closePanel()
{
    document.body.removeChild(bgObj);
    document.body.removeChild(msgObj);    
}

//添加select
function addSelect(id,key,value)
{
    if (id==null    || id=="")  { return ; }
    if (key==null   || key=="") { return ; }
    if (value==null)            { value = ""; }
    var objOption = document.createElement("option");
    objOption.appendChild(document.createTextNode(key));
    objOption.setAttribute("value", value);    
    getById(id).appendChild(objOption);
}

//删除select
function delSelect(id)
{   
    if (id==null || id=="") { return ; }
    var selTarget = getById(id);
    if(selTarget.selectedIndex > -1) 
    {
        for(var i=0;i<selTarget.options.length;i++)
        {
            if(selTarget.options[i].selected) { selTarget.remove(i); i = i - 1; }
        }
    }
}

//清空select
function clrSelect(id)
{   
    if (id==null || id=="") { return ; }
    getById(id).options.length=0;    
}

//反选Checkbox
function cheCheckbox(name)
{
    if (name==null || name=="") { return ; }
    var e = getByName(name);
    for (var i=0;i<e.length;i++)
    {
        if (e.item(i).checked)
        {
            getByName(name).item(i).checked=false;
        }
        else
        {
            getByName(name).item(i).checked=true;
        }
    }
}

//搜索Text
function sehText(id,value)
{ 
     if (id==null || id=="") { return ; }
     if (getById(id).value ==value)
     {
        getById(id).value = "";
        getById(id).style.color = "#000000";
     }     
}

//添加Text
function addText(id,value)
{ 
     if (id==null || id=="") { return ; }
     getById(id).value = value;
     getById(id).style.color = "#000000";
}
