﻿function isSame(str1, str2, ignoreCase) {
    if (ignoreCase == true) {
        return str1.toString().toLowerCase() == str2.toString().toLowerCase();
    }
    else {
        return str1 == str2;
    }
}

function isMail(str) {
    return str.match(/^[\W\.-_]+@[\w_-]+(\.[\w_-]+)+$/ig) != null;
}

function isMails(str) {
    return str.match(/^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)(;(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*))*$/ig) != null;
}

// false: not date format
function isDate(str) {
    //Matches 01/01/1990 | 12/12/9999 | 3/28/2001
    //Non-Matches 3-8-01 | 13/32/1001 | 03/32/1989 
    return str.match(/^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/ig) != null;
}

function isTime(str) {
    var reg = /^((0)?\d{1}|1\d{1}|2[0-3]):(([0-5])?\d{1}):(([0-5])?\d{1})$/;
    return reg.test(str);
}

// false: not decimal format
function isDecimal(str) {
    return str.match(/^[+-]?0|([1-9]{1}\d*)(\.\d+)*$/ig) != null;
}

// false: not int format
function isInteger(str) {
    //Matches 0,+12,12,-12
    return str.match(/^0$|^[+-]?[1-9][0-9]*$/ig) != null;
}
// false: not int format
function isPlusInteger(str) {
    //Matches 0,12, not +12, -12
    return str.match(/^0$|^[1-9][0-9]*$/ig) != null;
}

// arg1 * arg2
function Mul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
// arg1 + arg2
function Add(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}
// arg1 / arg2
function Divide(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
}
// arg1 - arg2
function Subtract(arg1, arg2) {
    var r1, r2, m, n;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

function toFixed(num, d) {
    var s = num + ""; if (!d) d = 0;
    if (s.indexOf(".") == -1) s += "."; s += new Array(d + 1).join("0");
    if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) {
        var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true;
        if (a == d + 2) {
            a = s.match(/\d/g); if (parseInt(a[a.length - 1]) > 4) {
                for (var i = a.length - 2; i >= 0; i--) {
                    a[i] = parseInt(a[i]) + 1;
                    if (a[i] == 10) { a[i] = 0; b = i != 1; } else break;
                }
            }
            s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2");
        } if (b) s = s.substr(1); return (pm + s).replace(/\.$/, "");
    } return num + "";
}

// get file's postfix
function GetFileType(str) {
    var fix = "";
    if (null != str && str.indexOf(".") > -1) {
        fix = str.substr(str.lastIndexOf(".") + 1);
    }
    return fix;
}

// pop up a new windows, isMore: false: only open one.
var OpenUrlWindow;
function OpenUrl(url, width, height, isMore) {
    try {
        if (!url) return;
        //        if (GetBrowse() == "MSIE")
        //            window.event.cancelBubble=true;
        var swd = screen.width;
        var shg = screen.height;
        if (!width) width = 650;
        if (!height) height = 500;
        var top = (shg - height) / 2;
        var left = (swd - width) / 2;
        if (OpenUrlWindow != null && !isMore) {
            OpenUrlWindow.close();
        }
        // OpenUrlWindow = window.open("Loading.htm?"+encodeURIComponent(url),"","height="+height+",width="+width+",top="+top+",left="+left+",resizable=1,scrollbars=1");
        OpenUrlWindow = window.open(url, "", "height=" + height + ",width=" + width + ",top=" + top + ",left=" + left + ",resizable=1,scrollbars=1");
        OpenUrlWindow.focus();
        return OpenUrlWindow;
    } catch (exception) { }
}
// for the jquery date control.
function BindDateBlur() {
    try {
        $('.date-pick').blur(function() {
            var value = $(this).val().length;
            if (value > 0) {
                if (!isDate($(this).val())) {
                    alert('Wrong Date.');
                    $(this).val("");
                    $(this).focus();
                }
            }
        });
    }
    catch (e) {

    }
}

function DownLoadFile(storeName, newName, directory) {
    var path = "";
    if (null != directory)
    {
        path = directory;
    }
    OpenUrl(path + "FileDownload.aspx?store=" + storeName + "&name=" + newName, 300, 100, false);
}

function FullScreen(win) {
    if (null != win) {
        try{
            win.moveTo(0, 0);
            win.resizeTo(screen.availWidth, screen.availHeight);
        }
        catch(e)
        {}
    }
    else {

    }
} 

function ReloadParent(isClose) {
    if (null != window.opener && window.opener.Refresh != null) {
        window.opener.Refresh();
    }
    if (isClose)
    {
        self.close();
    }
}
