﻿function $(s) {
    return document.getElementById(s);
}

//获取网站参数

//alert(GetQueryString(url,"参数名1"));
function GetQueryString(url, paras) {

    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    }
     else {
        return returnValue;
    }
}


///创建XMLHttpRequeste
function createXMLHttpRequeste() {
    var xmlhttpe;
    try {
        xmlhttpe = new ActiveXObject('Msxml2.XMLHTTP');

    } catch(e) {
        try {
            xmlhttpe = new ActiveXObject('Microsoft.XMLHTTP');

        } catch(e) {
            try {
                xmlhttpe = new window.XMLHttpRequest();

            } catch(e) {
                alert("创建XMLHttpRequest对象失败！");
            }
        }
    }
    return xmlhttpe;
}


//Function trim a string
function trim(str, flag) {

    str = "" + str;
    var regularExp;
    if (flag == "l" || flag == "L")/*trim left side only*/
    {
        regularExp = /^\s+/gi;
        return str.replace(regularExp, "");
    }
    else if (flag == "r" || flag == "R")/*trim right side only*/
    {
        regularExp = /\s+$/gi;
        return str.replace(regularExp, "");
    }
    else/*defautly, trim both left and right side*/
    {
        regularExp = /^\s+|\s+$/gi;
        return str.replace(regularExp, "");
    }
}

///获取字符串长度
function Len(str) {
    var i, sum;
    sum = 0;
    for (i = 0; i < str.length; i++) {
        if ((str.charCodeAt(i) >= 0) && (str.charCodeAt(i) <= 255))
            sum = sum + 1;
        else
            sum = sum + 2;
    }
    return sum;
}

function commreg(expression, str) {
    //在JavaScript中，正则表达式只能使用"/"开头和结束，不能使用双引号
    //判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
    //下面的代码中应用了转义字符"\"输出一个字符"/"
    var objExp = new RegExp(expression);
    if (objExp.test(str) == true) {
        return true;
    } else {
        return false;
    }
}

//验证邮编 

function IsPostId(str) {

    var reg = /^\d{6}$/; 
    
    return commreg(reg,str);

} 
//判断url
function IsURL(strURL){
    var strRegex = "^(https|http|ftp|rtsp|mms)://"

        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@  
       + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
       + "|" // 允许IP和DOMAIN（域名） 
        + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.  
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名  
        + "[a-z]{2,6})" // first level domain- .com or .museum  
        + "(:[0-9]{1,4})?" // 端口- :80  
        + "((/?)|" // a slash isn't required if there is no file name  
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";  
        var re=new RegExp(strRegex);  
  //re.test() 
        if (re.test(strURL)){ 
            return (true);  
        }else{  
            return (false);  
        }
    }


//判断qq号码
    function isqq(qqstr) {
        return commreg( /^[1-9]\d{5,9}$/ , qqstr);
    }
    
//判断电话号码
    function isTelNo(telstr) {
        return commreg( /^[0-9][0-9,-]{5,23}[0-9]$/ , telstr);
    }
//判断邮件
    function isEmail(mailstr) {
        return commreg( /[\w-.]+@{1}[\w-]+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig , mailstr);
    }

//非负整数
    function isnumber(numberstr) {
        return commreg( /^\d+$/ , numberstr);
    }

    //非负整数（正整数 + 0）
    function isInteger(numberstr) {
        if (commreg(/^\d+$/, numberstr)) {
            if (numberstr > 0) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
    
///判断是否小数
    function isDecimal(numberstr) {
        return commreg( /^(-[0-9]|[0-9]|(0[.])|(-(0[.])))[0-9]{0,}(([.]*\d{1,2})|[0-9]{0,})$/ , numberstr);
    }
    ///判断正小数
    function isArefractional(numberstr) {
        if (commreg(/^[0-9]*(\.\d*)?$/, numberstr)) {

            if (numberstr > 0) {
                if (numberstr.length > 1) {
                    if (numberstr[0] == 0) {
                        if (numberstr[1] == ".") {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                    else {
                        return true;
                    }
                }
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    ///判断账号
    //只能包含汉字，英文，数字，下划线
    function isuid(uid, min, max) {
        var e = commreg( /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[_\a-zA-Z0-9]|[\s])*$/gi , uid);
        if (e) {
            if (Len(uid) < min || Len(uid) > max) {
                return false;
            }
        }
        return e;
    }

    ///去金额小数点  n是保留几位
    function Decimal_f(x, n) {
        var f_x = parseFloat(x);
        if (isNaN(f_x)) {
            alert('function:changeTwoDecimal->parameter error');
            return false;
        }
        f_x = Math.round(x * 100) / 100;
        var sX = f_x.toString();
        var posDecimal = sX.indexOf('.');
        if (posDecimal < 0) {
            posDecimal = sX.length;
            sX += '.';
        }
        while (sX.length <= posDecimal + n) {
            sX += '0';
        }
        return sX;
    }


    function SelectchkAll() {
        //将除头模板中的其它所有的CheckBox取反
        var form = document.forms[0];
        elem = form.elements;
        for (i = 0; i < elem.length; i++)   //这个FOR循环表示有多少个选框就循环多少次 
            if (elem[i].type == "checkbox") {
            if (elem[i].checked != true)
            { elem[i].click(); }
            else
            { elem[i].click(); }
        }
    }

    //加入收藏夹
    function AddFavorite(url, title) {
        if(confirm("网站名称："+title+"\n网址："+url+"\n确定添加收藏?")) {
            var ua=navigator.userAgent.toLowerCase();
            if(ua.indexOf("msie 8")>-1) {
                external.AddToFavoritesBar(url,title,''); //IE8
            } else {
                try {
                    window.external.addFavorite(url,title);
                } catch(e) {
                    try {
                        window.sidebar.addPanel(title,url,""); //firefox
                    } catch(e) {
                        alert("加入收藏失败，请使用Ctrl+D进行添加");
                    }
                }
            }
        }
        return false;
    }
