/* start========================================================================================= */
/**
 * 文 件 名: func.common.js
 * 创 建 人: simon(samlnye@hotmail.com)
 * 创建时间: 2007-12-12
 * 最后修改: 2007-12-12 by simon
 * 文件描述: JavaScript常用自定义函数
**/
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 初始化XMLHTTP对象
**/
function nbInitAjax()
{
    var xmlhttp = false;
    try
    {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch(e1)
    {
        try
        {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e2)
        {
            xmlhttp = false;
        }
    }
    if(!xmlhttp && (typeof XMLHttpRequest != 'undefined')) xmlhttp = new XMLHttpRequest();
    return xmlhttp;
}
var nbAjax = nbInitAjax();
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 简化document.getElementById方法
 * 调用示例: 
**/
function $()
{
    return document.getElementById(arguments[0]);
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 实现"设为首页"功能
 * 调用示例: <a href="#" onclick="nbSetHomePage(this, 'http://www.sina.com.cn');return false;">将新浪网设为首页</a>
**/
function nbSetHomePage()
{
    arguments[0].style.behavior = 'url(#default#homepage)';
    arguments[0].setHomePage(arguments[1]);
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 实现"加入收藏"功能
 * 调用示例: <a href="#" onclick="nbAddFavorite('新浪网', 'http://www.sina.com.cn');return false;">
             将新浪网加入到收藏夹</a>
**/
function nbAddFavorite()
{
    window.external.AddFavorite(arguments[0], arguments[1]);
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 打开指定宽度和高度新窗口
 * 调用示例: <script>
             nbOpenWin('http://www.163.com', 500, 500);
             nbOpenWin('http://www.163.com', 500, 500, 'center');
             </script>
**/
function nbOpenWin()
{
    var newwin = window.open(arguments[0], "newWin", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=0,left=0,width=" + arguments[1] + ",height=" + arguments[2]);
    newwin.focus();
    if(arguments[3] && arguments[3] == 'center')
    {
        var X = (screen.availWidth - w) / 2;
        X = parseInt(X, 10);
        var Y = (screen.availHeight - h) / 2;
        Y = parseInt(Y, 10);
        newwin.focus();
        newwin.moveTo(X, Y);
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 打开模式对话框
 * 调用示例: <script>
             nbOpenDialog('http://www.163.com', '这是要传递的参数', 500, 500);
             </script>
**/
function nbOpenDialog()
{
	window.showModalDialog(arguments[0], arguments[1], "dialogWidth:" + arguments[2] + "px;dialogHeight:" + arguments[3] + "px;Resizable:0;help:0;status:no;scroll:yes;center:yes;edge:Raised;");
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 根据给定的值选定select对象的项
 * 调用示例: <form id="form1" name="form1">
             <select id="sex" name="sex">
             <option value="0" selected>请选择性别</option>
             <option value="1">男</option>
             <option value="2">女</option>
             </select>
             <input type="button" value="测试" onclick="nbOptionSelected('sex', '2')">
             </form>
**/
function nbOptionSelected()
{
    for(var i = 0;i < $(arguments[0]).options.length;i++)
    {
        if($(arguments[0]).options[i].value == arguments[1])
        {
            $(arguments[0]).options[i].selected = true;
            break;
        }
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 根据给定的值选定radio对象的项
 * 调用示例: <form id="form1" name="form1">
             <input id="rdo_sex_1" name="sex" type="radio" value="1">男
             <input id="rdo_sex_2" name="sex" type="radio" value="2">女
             <input type="button" value="测试" onclick="nbRadioChecked('sex', '2')">
             </form>
**/
function nbRadioChecked()
{
    var arrElements = document.getElementsByTagName('input');
    for(var i = 0;i < arrElements.length;i++)
    {
        if(arrElements[i].type == 'radio' && arrElements[i].name == arguments[0])
        {
            if(arrElements[i].value = arguments[1]) arrElements[i].checked = true;
            else arrElements[i].checked = false;
        }
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 复选框全选/取消全选
 * 调用示例: <form id="form1" name="form1">
             <input id="isCheckAll" name="isCheckAll" type="checkbox" value="1" 
             onclick="nbCheckAll('country', this.id)" />全选
             <input id="cb_country_1" name="country[]" type="checkbox" value="1" 
             onclick="nbCheckAll('country', 'isCheckAll', true)" />中国
             <input id="cb_country_2" name="country[]" type="checkbox" value="2" 
             onclick="nbCheckAll('country', 'isCheckAll', true)" />美国
             <input id="cb_country_3" name="country[]" type="checkbox" value="3" 
             onclick="nbCheckAll('country', 'isCheckAll', true)" />英国
             </form>
**/
function nbCheckAll()
{
    var arrElements = document.getElementsByTagName('input');
    if(arguments[2] == true)
    {
        var isCheckAll = true;
        for(var i = 0;i < arrElements.length;i++)
        {
            if(arrElements[i].type == 'checkbox' && arrElements[i].id.indexOf(arguments[0]) != -1)
            {
                if(!arrElements[i].checked)
                {
                    isCheckAll = false;
                    break;
                }
            }
        }
        if($(arguments[1]).type == 'checkbox') $(arguments[1]).checked = isCheckAll;
        else $(arguments[1]).value = isCheckAll ? '1' : '0';
    }
    else
    {
        var isChecked = true;
        if($(arguments[1]).type == 'checkbox') isChecked = $(arguments[1]).checked;
        else isChecked = ($(arguments[1]).value == '0') ? true : false;
        for(var i = 0;i < arrElements.length;i++)
        {
            if(arrElements[i].type == 'checkbox' && arrElements[i].id.indexOf(arguments[0]) != -1)
            {
                arrElements[i].checked = isChecked;
            }
        }
        if($(arguments[1]).type != 'checkbox') $(arguments[1]).value = isChecked ? '1' : '0';
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 根据给定的值选定checkbox复选框，多个值用","分隔
 * 调用示例: <form id="form1" name="form1">
             <input id="cb_country_1" name="country[]" type="checkbox" value="1" />中国
             <input id="cb_country_2" name="country[]" type="checkbox" value="2" />美国
             <input id="cb_country_3" name="country[]" type="checkbox" value="3" />英国
             <input type="button" value="测试" onclick="nbCheckBoxChecked('country', '1,3')">
             </form>
**/
function nbCheckBoxChecked()
{
    var arr = arguments[1].split(',');
    var arrElements = document.getElementsByTagName('input');
    for(var i = 0;i < arrElements.length;i++)
    {
        if(arrElements[i].type == 'checkbox' && arrElements[i].id.indexOf(arguments[0]) != -1)
        {
            if(in_array(arrElements[i].value, arr)) arrElements[i].checked = true;
            else arrElements[i].checked = false;
        }
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 图片等比例缩放函数
 * 调用示例: <img src="images/123.gif" border="0" onload="nbResizeImage(this, 100, 100)" />
**/
function nbResizeImage()
{
    var imageObject;
    if(arguments[0] != null) imageObject = arguments[0];
    var state = imageObject.readyState;
    var oldImage = new Image();
    oldImage.src = imageObject.src;
    var dW = oldImage.width;
    var dH = oldImage.height;
    if(dW > arguments[1] || dH > arguments[2])
    {
        a = dW / arguments[1];
        b = dH / arguments[2];
        if(b > a) a = b;
        dW = dW / a;
        dH = dH / a;
    }
    if(dW > 0 && dH > 0) imageObject.width = dW;
    imageObject.Height = dH;
    if(state != 'complete' || imageObject.width > arguments[1] || imageObject.Height > arguments[2])
    {
        setTimeout("nbResizeImage(null, " + arguments[1] + ", " + arguments[2] + ")", 40);
    }
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 检查字符串是否为空
 * 调用示例: 无
**/
function empty()
{
    return (arguments[0] == '');
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 用JavaScript实现PHP的preg_match函数
 * 调用示例: <script>
             var str = '123';
             if(preg_match(/^\d+$/, str)) alert('字符串全部是由数字组成！');
             </script>
**/
function preg_match()
{
    var re = new RegExp(arguments[0]);
    return re.test(arguments[1]);
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 用JavaScript实现PHP的in_array函数
 * 调用示例: 参照PHP的相关函数
**/
function in_array(val, arr)
{
    for(var i = 0;i < arguments[1].length;i++)
    {
        if(arguments[0] == arguments[1][i])
        {
            return true;
        }
        continue;
    }
    return false;
}
/* ===========================================================================================end */


/* start========================================================================================= */
/**
 * 函数说明: 
 * 调用示例: 
**/
/* ===========================================================================================end */
