﻿var Globals = new Object();

/*
 *删除左右两端的空格
 */
Globals.Trim = function(str) {
    if (str == null) {
        return "";
    }

    return str.replace(/(^\s*)|(\s*$)/g, "");
}

/*
*删除左边的空格
*/
Globals.ltrim = function (str) {
    if (str == null) {
        return "";
    }

    return str.replace(/(^\s*)/g, "");
}

/*
*删除右边的空格
*/
Globals.rtrim = function (str) {
    if (str == null) {
        return "";
    }
    return str.replace(/(\s*$)/g, "");
}

/**
*取Cookie
*/
Globals.GetCookie = function (sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return decodeURIComponent(aCrumb[1]);
    }

    // a cookie with the requested name does not exist
    return null;
}

/**
*设置Cookie
*/
Globals.SetCookie = function (sName, sValue, sExpires) {
    var sCookie = sName + "=" + encodeURIComponent(sValue);
    if (sExpires != null) {
        sCookie += "; expires=" + sExpires;
    }

    document.cookie = sCookie;
}

/**
* 调整图像大小
*/

Globals.ResizeImage = function (img, width, height) {
    if (img == null || width == null || height == null || width < 1 || height < 1) {
        return;
    }

    if (img.src == null || img.src.length < 3) {
        return;
    }
    
    var img1 = new Image();
    img1.src = img.src;
    var w = img1.width;
    var h = img1.height;
    
    if (w == null || h == null || w < 1 || h < 1) {
        return;
    }
    
    var scale = w / h;
    
    if (w <= width && h <= height) {
        img.style.width = w + "px";
        img.style.height = h + "px";
        return;
    }
    if (w > width) {
        w = width;
        h = w / scale;
    }
    if (h > height) {
        h = height;
        w = h * scale;
    }

    img.style.width = w + "px";
    img.style.height = h + "px";
}
Globals.ResizeDivImage = function (img, width, height) {
    if (img == null || width == null || height == null || width < 1 || height < 1) {
        return;
    }

    if (img.src == null || img.src.length < 3) {
        return;
    }


    var img1 = new Image();
    img1.src = img.src;
    var w = img1.width;
    var h = img1.height;

    if (w == null || h == null || w < 1 || h < 1) {
        return;
    }

    var scale = w / h;

    if (w <= width && h <= height) {
        img.style.width = w + "px";
        img.style.height = h + "px";
        return;
    }
    if (w > width) {
        w = width;
        h = w / scale;
    }
    if (h > height) {
        h = height;
        w = h * scale;
    }

    img.style.width = w + "px";
    img.style.height = h + "px";
    img.marginLeft = parseInt((img1.width - w) / 2) + 'px';
    img.marginTop = parseInt((img1.height - w) / 2) + 'px';
}

/*
*HTML编码
*/
Globals.HtmlEncode = function (str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&amp;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    return s;
}

/*
*HTML解码
*/
Globals.HtmlDecode = function (str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
}

/*
 *邮箱验证规则
 */
Globals.RegularEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

/*
 *身份证正则
 */
Globals.ReglarIDCard = /^\d{17}[\d|x|X]$/;


/*
*手机正则
*/
Globals.RegularMobile = /^1(3|5|8)[0-9]{9}$/;




/**
*去掉下拉框的所有选项
*/
Globals.removeOpts = function (selobj) {
    if (selobj.options == null) {
        return;
    }

    while (selobj.options.length > 0) {
        selobj.options.remove(0);
    }
}

/**
* 添加下拉框选项
*/
Globals.createOptions = function (selobj, opts) {
    if (opts == null) {
        return;
    }
    for (var i = 0; i < opts.length; i++) {
        var objOption = document.createElement("OPTION");
        objOption.text = unescape(opts[i].text);
        objOption.value = opts[i].val;
        selobj.options.add(objOption);
    }
}
