﻿String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function $(id) {
    return document.getElementById(id);
}

function Validate(str, el) {
    if (str == null || str == "" || el == null || el == "") { return false; }

    var expression = null;

    switch (el) {
        case "UserID":
            expression = /^[a-zA-Z0-9]([\.\-_]?[a-zA-Z0-9]+){2,19}$/;
            break;
        case "Passwd":
            expression = /^[a-zA-Z0-9]([\.\-_]?[a-zA-Z0-9]+){5,19}$/;
            break;
        case "Email":
            expression = /^[a-zA-Z0-9]([\.\-_]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z0-9]{2,4})$/;
            break;
        case "Tel":
            expression = /^\d{7,12}$/;
            break;
        case "Mobile":
            expression = /^\d{11,20}$/;
            break;
        case "Date":
            expression = /^(?:([0-9]{4}-(?:(?:0?[1,3-9]|1[0-2])-(?:29|30)|((?:0?[13578]|1[02])-31)))|([0-9]{4}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|1\d|2[0-8]))|(((?:(\d\d(?:0[48]|[2468][048]|[13579][26]))|(?:0[48]00|[2468][048]00|[13579][26]00))-0?2-29)))$/;
            break;
        case "vCode":
            expression = /^[a-zA-z0-9]{4}$/;
            break;
        case "Int":
            expression = /^(-)?\d+$/;
            break;
        case "Numeric":
            expression = /^(-)?\d+(\.\d+)?$/;
            break;
        case "Domain":
            expression = /^([a-zA-z0-9]+((\.)[a-zA-z0-9]+)+|localhost)$/;
            break;
        case "IPv4":
            expression = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}$/;
            break;
        case "Text":
            expression = /^([^\x00-\xff]|[\w\.\-])+$/;
            break;
        case "ID":
            expression = /^\d+((,|, )\d+)*$/;
            break;
        case "Array":
            expression = /^[a-zA-Z0-9\-\u4e00-\u9fa5]+((,|, )[a-zA-Z0-9\-\u4e00-\u9fa5]+)*$/;
            break;
        case "DBChar":
            expression = /[☆★○●◎◇◆□■△▲〓]/;
            break;
        default:
            return false;
    }
    return expression.test(str);
}


function openwin(file, name, para) {
    window.open(file, name, para);
    return (false);
}

function CheckAll(form) {
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name != "chkall") {
            e.checked = form.chkall.checked;
        }
    }
}

function Confirm(form, msg) {
    var result = false;
    for (var i = 0; i < form.elements.length; i++) {
        var e = form.elements[i];
        if (e.name == "ID" && e.checked) {
            result = true;
            break;
        }
    }
    if (!result) {
        alert("请选择至少一条记录！");
    } else {
        result = confirm(msg);
    }
    return result;
}


function jumpToPage(el) {
    if (!Validate(el.value, "Int")) {
        alert("页码必须为整数！");
        el.focus();
        el.select();
        return (false);
    }

    obj = $("QString");
    if (obj != null) {
        var target = obj.value + el.value;
        self.location = target;
    } else {
        alert("QString 参数取值失败！");
    }

    return (false);
}









function changeBack(el, color) {
    var tds = el.getElementsByTagName("td");
    for (var i = 0; i < tds.length; i++) {
        tds[i].style.backgroundColor = color;
    }
    //el.style.backgroundColor = color;
}

function resizeImg(ename) {
    var divContent = document.getElementById(ename);
    if (divContent) {
        var imgNodes = divContent.getElementsByTagName("img");
        for (var i = 0; i < imgNodes.length; i++) {
            var e = imgNodes[i];
            if (e.width > 600) {
                e.width = 600;
            }
        }
    }
}

function changeCSS(e, classid) {
    var div = document.getElementById("divLouPanPhoto"); var aryA = div.getElementsByTagName("a");
    for (var i = 0; i < aryA.length; i++) {
        aryA[i].className = null;
    }
    e.className = "chk";

    var ulPhoto = document.getElementById("ulPhoto");

    if (ulPhoto) {
        var aryLI = ulPhoto.getElementsByTagName("li");
        for (var i = 0; i < aryLI.length; i++) {
            if (parseInt(aryLI[i].classid) == classid) { aryLI[i].style.display = "block"; } else { aryLI[i].style.display = "none"; }
        }
    }
}

function loupanPhotoInit(classid) {
    var div = document.getElementById("divLouPanPhoto"); var aryA = div.getElementsByTagName("a");
    changeCSS(aryA[0], classid);
}

function setTab(e, css, ul, id) {
    var lis = document.getElementById(ul).getElementsByTagName("li");
    for (var i = 0; i < lis.length; i++) {
        lis[i].className = null;
        document.getElementById(ul + i).style.display = "none";
    }
    e.className = css; document.getElementById(ul + id).style.display = "block";
}

function resizePhoto(e, w) {
    if (e.width > w) {
        e.width = w;
    }
}

function CopyURL() {
    var myHerf = top.location.href;
    var title = document.title;
    if (window.clipboardData) {
        var tempCurLink = title + "\n" + myHerf;
        var ok = window.clipboardData.setData("Text", tempCurLink);
        if (ok) alert("複制地址成功，按Ctrl + V ,粘貼到MSN、QQ上發給你的好友們吧 ！");
    } else { alert("對不起，目前此功能只支持IE，請直接複制地址欄的地址！"); }
}

function addToFavorite() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie 8") != -1) {
        window.external.addToFavoritesBar(window.location.href, window.document.title);
    } else {
        try {
            window.external.addFavorite(window.location.href, window.document.title);
        } catch (e) {
            try {
                window.sidebar.addPanel(window.document.title, window.location.href, "");
            } catch (e) {
                alert("加入最愛失敗, 請手動添加至最愛!");
            }
        }
    }
}
