﻿var ChangeBgColor = {
    obj: null,
    cover: "#EAEDD3",
    cout: "",
    cdef: "#FFF",
    b: function () {
        ChangeBgColor.obj = $$(ChangeBgColor.obj);
        for (var i = 0, len = ChangeBgColor.obj.length; i < len; i++) {
            ChangeBgColor.cout = ChangeBgColor.cout ? Element.getStyle(ChangeBgColor.obj[i], "backgroundColor") : ChangeBgColor.cdef;
            ChangeBgColor.obj[i].onmouseover = function () { this.style.backgroundColor = ChangeBgColor.cover; };
            ChangeBgColor.obj[i].onmouseout = function () { this.style.backgroundColor = ChangeBgColor.cout; };
        }
    },
    bind: function (obj, cover) {
        ChangeBgColor.obj = obj;
        ChangeBgColor.cover = cover || ChangeBgColor.cover;
        DomLoaded(ChangeBgColor.b);
    }
};
var ChangeClass = {
    bind: function (obj) {
        var m = $$(obj);
        for (var i = 0, len = m.length; i < len; i++) {
            m[i].onmouseover = function () {
                this.className = Element.readAttribute(this, "classover");
            };
            m[i].onmouseout = function () {
                this.className = Element.readAttribute(this, "classout");
            };
        }
    }
}
//GoogleAds.bind([{view: '', hide: ''}]);
var GoogleAds = {
    l: {},
    b: function () {
        GoogleAds.l.each(function (m) { if ($(m.view) && $(m.hide)) $(m.view).innerHTML = $(m.hide).innerHTML; });
    },
    bind: function (options) {
        GoogleAds.l = options || {};
        DomLoad(GoogleAds.b);
    },
    v: function () {
        GoogleAds.bind([
            { view: "topads", hide: "hidetopads" },
            { view: "contentads", hide: "hidecontentads" },
            { view: "bottomads", hide: "hidebottomads" }
        ]);
    }
};
var CheckString = {
    email: function (email) {
        return /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(email);
    },
    number: function (number) {
        return Object.isNumber(number);
    }
};
//onkeydown和onkeyup
function ViewInputNumber(textareaId, divId, minlength, maxlength) {
    var a = $(textareaId);
    var b = $(divId);
    var al = $F(a).length;
    var txt = "Required number of English letters: #{minlength} to #{maxlength}.<br /> Current input: <font color=\"#{color}\">#{stringlength}</font>";
    Element.show(b);
    if (al > maxlength) {
        a.value = $F(a).slice(0, maxlength);
        ViewInputNumber(textareaId, divId, minlength, maxlength);
    }
    else {
        b.innerHTML = "";
        var inner = txt.interpolate({ minlength: minlength, maxlength: maxlength, color: ((al < minlength) ? "red" : "green"), stringlength: al });
        b.appendChild(GetErrorMessage(inner));
    }
}
//Update Window
function UpdateTag() {
    var m = "<iframe src=\"http://www.chinalovematch.net/updateinfo.aspx\" style=\"background-color: #FFF9DB;\" id=\"iframeupdate\" name=\"iframeupdate\" height=\"320px\" width=\"100%\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" frameborder=\"0\" framespacing=\"0\" ></iframe>";
    var d = CreateElement("div", {
        id: "kkbac_updatetig",
        innerHTML: m
    });
    NewWindow.MiddleDiv(true, NewWindow.Hide);
    NewWindow.Bind(true, { title: " &nbsp; ", content: d, width: 800, top: 10 });
}
function LoginWindow(url) {
    var m = "<div class='loginwindow'>"
                + "<form method='post' id='formlogin' action='/login.aspx?returnurl=" + url + "'>"
                        + "<div class='divc'>To Submit a Topic or Reply you must be a member of CLM and/or ALM.</div>"
                        + "<table class='table'>"
                            + "<tbody>"
                                + "<tr>"
                                    + "<td class='l'>Username</td>"
                                    + "<td class='r'><input type='text' size='18' name='username' /></td>"
                                + "</tr>"
                                + "<tr>"
                                    + "<td class='l'>Password</td>"
                                    + "<td class='r'><input type='password' size='18' name='password' /></td>"
                                + "</tr>"
                                + "<tr>"
                                    + "<td class='l'></td>"
                                    + "<td class='r'><input type='submit' value=' Login ' /></td>"
                                + "</tr>"
                            + "</tbody>"
                        + "</table>"
                        + "<div class='divc'>Not a Member? <a href='http://www.chinalovematch.net/registrationstep1.aspx' target='blank'>Join Free Now</a></div>"
                + "</form>"
            + "</div>";
    m = '<div class="loginwindow">'
        + '    <iframe src="/loginwindow.aspx?returnurl=' + url + '" scrolling="no" frameborder="0" width="540" height="160"></iframe>'
        + '</div>';
    var d = CreateElement("div", {
        id: "kkbac_login",
        innerHTML: m
    });
    NewWindow.MiddleDiv(true, NewWindow.Hide);
    NewWindow.Bind(true, { title: " Login ", content: d, width: 560, top: 10 });
}

var PasswordStrength = {
    words_cn: ["弱", "中", "强"],
    words_en: ["Weak", "Medium", "Strong"],
    levelValue: [50, 40, 35, 30, 25, 20, 15, 10, 6, 0],
    regex: [/[0-9]/g, /[a-zA-Z]/g, /[^a-zA-Z0-9]/g],
    factor: [1, 2, 5],
    kindFactor: [0, 0, 5, 10],

    Value: function (pwd) {
        var strengthValue = 0;
        var ComposedKind = 0;
        for (var i = 0; i < PasswordStrength.regex.length; i++) {
            var chars = pwd.match(PasswordStrength.regex[i]);
            if (chars != null) {
                strengthValue += chars.length * PasswordStrength.factor[i];
                ComposedKind++;
            }
        }
        strengthValue += PasswordStrength.kindFactor[ComposedKind];
        return strengthValue;
    },
    Level: function (pwd) {
        var value = PasswordStrength.Value(pwd);
        for (var i = 0; i < PasswordStrength.levelValue.length; i++) {
            if (value > PasswordStrength.levelValue[i])
                return i;
        }
    },
    Get: function (pwd, width) {
        var w = width ? width : 100;
        var n;
        if (pwd.blank())
            n = PasswordStrength.levelValue.length;
        else
            n = PasswordStrength.Level(pwd);
        var m = (PasswordStrength.levelValue.length - n) * w / PasswordStrength.levelValue.length;
        m = parseInt(m);
        var c = m / w;
        return PasswordStrength.GetElement(w, m, c);
    },
    GetElement: function (w, m, c) {
        var ws = (pagelanguage.language == 1) ? PasswordStrength.words_cn : PasswordStrength.words_en;
        var wo = "";
        if (c < 0.4) {
            wo = ws[0];
        } else if (c < 0.7) {
            wo = ws[1];
        } else {
            wo = ws[2];
        }
        var t = CreateElement("div", { innerHTML: wo });
        Element.setStyle(t, { background: "url(/images/passwordstrength.jpg) repeat",
            backgroundColor: "green",
            width: m + "px",
            height: "18px",
            color: "#fff",
            fontSize: "12px",
            textAlign: "center"
        });
        var d = CreateElement("div", {}, t);
        Element.setStyle(d, { backgroundColor: "#ddd",
            border: "solid 1px #aaa",
            width: w + "px",
            height: "18px"
        });
        return d;
    }
};
function GetLoadingIcon() {
    return CreateElement("center", {}, CreateElement("img", { src: "/images/loadingdata.gif" }));
}
function FormSubmit(id) {
    $(id).submit();
}
function ResetImageSize(imgd, w) {
    var image = new Image();
    image.src = imgd.src;

    if (image.width > 0 && image.height > 0) {
        if (image.width >= w) {
            imgd.width = w;
            imgd.height = (image.height * w) / image.width;
        } else {
            imgd.width = image.width;
            imgd.height = image.height;
        }
    }
}
function SetBlogArticleTopImage(t) {
    var image = new Image();
    image.src = t.src;
    if (image.width > 500) {
        t.className = "";
        t.style.padding = "0 0 20px 0";
    }
}
function GetSelectedText() {
    if (window.getSelection) {
        return window.getSelection().toString();
    }
    else if (document.getSelection) {
        return document.getSelection().toString();
    }
    else if (document.selection) {
        return document.selection.createRange().text.toString();
    }
}
function SetSelectedText(t, text) {
    if (document.selection) {
        t.focus();
        t.selection.createRange().collapse;
        t.selection.createRange().text = text;
    }
    else if (t.selectionStart || t.selectionStart == '0') {
        var s = t.selectionStart;
        var e = t.selectionEnd;
        var val = t.value;
        var start = val.substring(0, s);
        var end = val.substring(e);
        t.value = start + text + end;
    }
}
