﻿//***************************************************************/
// 作者：冯岩
// 日期：2008.05.18 09:11
// 功能：Cmwin系统通用脚本封装
// 修改：修改日期,修改人加说明
//***************************************************************/

//通过ID获取元素
function $(id)
{
    var obj = document.getElementById(id);
    return obj;
}

/*
 * 函数说明：获取url参数
 * 参数：	url数值key
 * 返回值：	
 * 作者：冯岩
 * 时间：2008-06-18
 */
function GetQueryString(name)   
{   
    var reg   =   new   RegExp("(^|&)"+   name   +"=([^&]*)(&|$)");   
    var r   =   window.location.search.substr(1).match(reg);   
    if(r!=null)   
		return   unescape(r[2]);  
    return   "";   
} 

//验证文本框值为空
function inputIsNull(id)
{
    $(id).value = trim($(id).value);
    if($(id).value.length < 1)
    {
        $(id).focus();
        return true;
    }
    return false;
}
//去掉字串左边的空格
function lTrim(str) 
{ 
    if (str.charAt(0) == " ") 
    { 
        //如果字串左边第一个字符为空格 
        str = str.slice(1);//将空格从字串中去掉 
        //这一句也可改成 str = str.substring(1, str.length); 
        str = lTrim(str); //递归调用 
    } 
    return str; 
} 
	
//去掉字串右边的空格 
function rTrim(str) 
{ 
    var iLength; 
    iLength = str.length; 
    if (str.charAt(iLength - 1) == " ") 
    { 
        //如果字串右边第一个字符为空格 
        str = str.slice(0, iLength - 1);//将空格从字串中去掉 
        //这一句也可改成 str = str.substring(0, iLength - 1); 
        str = rTrim(str); //递归调用 
    } 
    return str; 
} 
//去掉字串两边的空格 
function trim(str) 
{ 
    return lTrim(rTrim(str)); 
} 

/*
 * 函数说明：自动检测全选
 * 参数：	参数1 全选按扭ID。参数2输入this
 * 返回值：	
 * 作者：冯岩
 * 时间：2008-06-18
 */
function checkAll(chkAllID,thisObj)
{
    var chkAll = document.getElementById(chkAllID);
    var chks = document.getElementsByTagName("input");
    var chkNo = 0;
    var selectNo = 0;
    for(var i =0; i < chks.length; i++)
    {
       if(chks[i].type == "checkbox")
       {
          //全选触发事件  
          if(chkAll == thisObj)
          {
             chks[i].checked = thisObj.checked; 
             if(chks[i].id!="chkAll")  //全选时改变行颜色
             {
                ChangeBg(chks[i]); 
             }
          }          
          else //非全选触发
          {
            if(chks[i].checked && chks[i].id != chkAllID)
            {
               selectNo++; 
            }
            if(chks[i].id!="chkAll")  //选中时改变行颜色
            {
               ChangeBg(chks[i]); 
            }
          }
          if(chks[i].id != chkAllID)
          {
            chkNo++;
          }
       }
    } 
    if(chkAll != thisObj)
    {
        chkAll.checked = chkNo==selectNo;
    }
}


/*
 * 函数说明：检测选择个数
 * 参数：	全选ID
 * 返回值：	页面checkbox的选择个除 除全选外的
 * 作者：冯岩
 * 时间：2008-06-18
 */ 
function checkSelectNo(chkAllID)
{
    var chks = document.getElementsByTagName("input");
    var selectNo =0;
    for(var i =0; i < chks.length; i++)
    {
       if(chks[i].type == "checkbox")
       {
            if(chks[i].id != chkAllID && chks[i].checked)
            {
                selectNo++;
            }
       }
    }  
    return selectNo;
}
/*
 * 函数说明：获取选择框的值
 * 参数：	全选的ID
 * 返回值：	返回选中的checkbox值，最先一个！
 * 作者：冯岩
 * 时间：2008-06-18
 */ 
function getChkSelectValue(chkAllID)
{
    var chks = document.getElementsByTagName("input");
    var selectValue =0;
    for(var i =0; i < chks.length; i++)
    {
       if(chks[i].type == "checkbox")
       {
            if(chks[i].id != chkAllID && chks[i].checked)
            {
               selectValue = chks[i].value;
            }
       }
    }  
    return selectValue;
}

//自动显示隐藏元素
function showElement(id)
{
    var obj = $(id);
    if(obj != undefined)
    {
        obj.style.dipsly = (obj.style.display == "inline"||obj.style.display == "")?"none":"inline";
    }
}

/*
 * 函数说明：防163删除确认对话框
 * 参数：	服务器端要触发事件的Button的ID，提示消息
 * 作者：zengxie
 * 时间：2008-11-03
 */ 
var flag=false;
 function confirmOperate(ServerControlId,msg)
 {
        hide();
        Container=document.createElement('div');
		Container.id='dvConformOperate';
		Container.style.display='inline';
		Container.style.zIndex='9999';	
		Container.style.position = 'absolute';
		Container.style.marginLeft ='-175px';
		Container.style.textAlign = 'left';
		Container.style.top = '100px';
		Container.style.left = '50%';
		Container.style.width= "350px";
		Container.style.backgroundColor = "#FFFFFF";
		html  = "<table width=\"100%\" border=\"0\" cellspacing=\"0\">";
        html += "        <tr>";
        html += "          <td align=\"center\" class=\"tsbg\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
        html += "            <tr>";
        html += "              <td width=\"44%\" height=\"25\" align=\"left\" class=\"color02\">提示信息</td>";
        html += "              <td width=\"56%\" align=\"right\" valign=\"top\"><img id=\"imgCloseMsg\" src=\"../images/close002.gif\" style=\"cursor:hand\" width=\"20\" height=\"23\" border=\"0\" onClick=\"IsConfirm(false,'"+ServerControlId+"')\" /></td>";
        html += "            </tr>";
        html += "          </table></td>";
        html += "        </tr>";
        html += "        <tr>";
        html += "          <td height=\"60\" align=\"center\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
        html += "            <tr>";
        html += "              <td width=\"14%\" align=\"left\"><img src=\"../images/ts.gif\" alt=\"提示信息\" width=\"36\" height=\"36\" /></td>";
        html += "              <td width=\"86%\" align=\"left\"><h3 id=\"tripMsg\">"+msg+"</h3>&nbsp;&nbsp;</td>";
        html += "            </tr>";
        html += "          </table></td>";
        html += "        </tr>";
        html += "        <tr id=\"msgBtns\">";
        html += "          <td align=\"center\" class=\"tdbg\"><table width=\"200\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
        html += "            <tr align=\"center\">";
        html += "              <td width=\"100\">";
        html += "                <img src=\"../images/btn-023.gif\" id=\"img_confirmMsg\" style=\"cursor:hand\" width=\"62\" height=\"21\" border=\"0\" onclick=\"IsConfirm(true,'"+ServerControlId+"')\" />";
        html += "                </td>";
        html += "              <td width=\"100\" height=\"28\" >";
        html += "                <img src=\"../images/btn-016.gif\" id=\"img_cancelMsg\" style=\"cursor:hand\" width=\"62\" height=\"21\" border=\"0\" onclick=\"IsConfirm(false,'"+ServerControlId+"')\" />";       
        html += "              </td>";
        html += "            </tr>";
        html += "          </table></td>";
        html += "        </tr>";
        html += "      </table>";
        Container.innerHTML = html;
        document.body.appendChild(Container);
        if(flag)
        {
        return true;
        }
        else
        {
        return false;
        }
 }
 
   /*
 * 函数说明：防163删除确认对话框调用方法
 * 参数：	
 * 作者：zengxie
 * 时间：2008-11-03
 */ 
 function IsConfirm(obj,ControlId)
 {
    if(obj==true)
    {
        cancel();
        document.body.removeChild($('dvConformOperate'));
        flag=true;
        $(ControlId).click();
    }
    else
    {
        cancel();
        flag=false;
        document.body.removeChild($('dvConformOperate'));
        return false;
    }
 } 
 
 
 
  /*
 * 函数说明：验证电子邮件
 * 参数：	
 * 作者：冯威
 * 时间：2008-09-17
 */ 
function isEmail(strEmail) 
{ 
    var stremail = strEmail;
	if(stremail.length == 0)
	{
		return false;
	}
	var regm = /^[0-9a-zA-Z]+([0-9a-zA-Z]|_|\.|-)+@(([0-9a-zA-Z]+\.)|([0-9a-zA-Z]+-))+[0-9a-zA-Z]+$/;//验证Mail的正则表达式,^[a-zA-Z0-9_-]:开头必须为字母,下划线,数字,
	var regm1= /^[0-9a-zA-Z]+@(([0-9a-zA-Z]+\.)|([0-9a-zA-Z]+-))+[0-9a-zA-Z]+$/;
	if (!stremail.match(regm) && !stremail.match(regm1) && stremail!="")
	{ 
		return false;
	} 
	else
	{
		return true;
	}			 
} 

/*
 * 函数说明：验证URL
 * 参数：	
 * 作者：冯威
 * 时间：2008-09-17
 */ 
function isUrl(strUrl)
{
    var regex= /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    if(regex.test(strUrl)) return true;
    return false;
}

/*
 * 函数说明：验证数字
 * 参数：	
 * 作者：冯威
 * 时间：2008-09-17
 */ 
function isNum(num)
{
    var regex=/^[0-9]{1,20}$/;
    if(regex.test(num)) return true;
    return false;
}

//控制DIV的隐藏与显示
function displayDiv(id,b)
{
    b = b?"inline":"none";
    $(id).style.display = b;
}

/*
 * 函数说明：验证手机号码
 * 参数：	
 * 作者：冯威
 * 时间：2008-11-17
 */ 
function isMobile(mobile)
{
    var regex = /^(((13[0-9]{1})|15[0|0|3|6|8|9])+\d{8})$/;
    if(regex.test(mobile))
    {
        return true;
    }
    return false;
}

/*
 * 函数说明：验证座机号码
 * 参数：	
 * 作者：冯威
 * 时间：2008-11-17
 */ 
function isPhone(phone)
{
    var regex= /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
    if(regex.test(phone))
    {
        return true;
    }
    return false;
}

/*
 * 函数说明：选择时改变此行的背景色
 * 参数：	
 * 作者：zengxie
 * 时间：2008-11-03
 */ 
 function ChangeBg(obj)
 {
    var parent=obj.parentElement.parentElement;
    if(parent.tagName=="TR")
    {
        if(obj.checked==true)
        {
            var nodes=parent.childNodes
            for(var i=0;i<nodes.length;i++)
            {
                if(nodes[i].tagName=="TD")
                {
                    //nodes[i].style.backgroundColor = '#FFFFCC';
                }
            }
        }
        else
        {
            var nodes=parent.childNodes
            for(var i=0;i<nodes.length;i++)
            {
                if(nodes[i].tagName=="TD")
                {
                    //nodes[i].style.backgroundColor = 'white';
                }
            }
        }
    }
 }
