﻿

//防止嵌套
function MastTop() { if (top != self) { top.location.href = self.location.href } }; MastTop();
//导航菜单
function NavMore(){if($('NavLiMore')){Comm.AddEvent($('NavLiMore'),'mouseover',function(){$('NavLiMore').className="moreshow"});Comm.AddEvent($('NavLiMore'), 'mouseout', function() { $('NavLiMore').className = "more" });}};NavMore();
//对话框
function Alert(title) { alert(title); }

//只能输入数字
function NumOnly(a, b) { b = b || window.event; var c = b.keyCode || b.charCode; if ((c >= 46 && c <= 57) || (c == 8 || c == 116)) { return true; } return false; };

//用户
var User = {
    Login: function(btn) {
        if (!Comm.EmptyAlert('uName')) { return; }
        if (!Comm.EmptyAlert('uPass')) { return; }
        var uname = $F('uName');
        var upass = $F('uPass');
        var type = parseInt($F('uType'));
        btn.disabled = true;
        BaseAjax.UserLogin(type, uname, upass, function(Int) {
            if (Int.value != null) {
                switch (Int.value) {
                    case -1000: Alert('登录失败，请稍后重试，或与管理员联系'); break;
                    case -1001: Alert('用户名或密码错误'); break;
                    case -1002: Alert('用户名或密码错误，请认真核实用户名、密码、以及用户类型'); break;
                    default:
                        if (true) {
                            location.reload();
                        }
                        else {
                            OnlineUser = { uId: Int.value, uName: uname, uType: type };
                            User.ShowInfo(uname, type);
                        }
                        break;
                }
            } else { Alert('登录失败，请稍后重试，或与管理员联系'); }
            btn.disabled = false;
        });
    },
    Register: function(btn) { top.location.href = "http://user" + Tcsafea.domain + '/Register.html'; },
    ShowInfo: function(uname, type) {
        var login_box = $$("div[class*=login_box]");
        if (login_box.length == 0) { return; }
        if (type == -1) { $(login_box[0]).show(); return; }
        if (login_box.length > 0) { $(login_box[0]).hide(); }
        if (login_box.length > type) { $(login_box[type + 1]).show(); }
        var login_span = $$("span[class*=uname]");
        if (login_span.length == 0) { return; }
        if (login_span.length > type - 1) { $(login_span[type]).innerHTML = uname; }
    },
    LoginOut: function() {
        if (confirm("确定要退出吗？")) {
            BaseAjax.UserLoginOut(function(Bool) {
                if (Bool.value) {
                    alert("退出成功！"); top.location.reload();
                } else {
                    alert("退出失败！");
                }
            });
        }
    }
};

//图书
var Booker = {
    Cart: null,
    CartNum: null,
    InitCart: function() {
        var carCookie = Comm.ReadCookie('LiteWeb_Shoppingcart');
        var carnumCookie = Comm.ReadCookie('LiteWeb_Shoppingcart_Num');
        var pattern = "[^0-9|,]";
        var rgx = new RegExp(pattern);
        if (carCookie != null) {
            carCookie = carCookie.replace(rgx, '');
        }
        if (carnumCookie != null) {
            carnumCookie = carnumCookie.replace(rgx, '');
        }
        if (carCookie == null) {
            carCookie = new Array();
            carnumCookie = new Array();
        } else {
            carCookie = carCookie.split(',');
            if (carnumCookie == null) {
                carnumCookie = new Array();
                carCookie.each(function(bk) {
                    carnumCookie.push(1);
                });
            } else {
                carnumCookie = carnumCookie.split(',');
                if (carnumCookie.length != carCookie.length) {
                    carnumCookie = new Array();
                    carCookie.each(function(bk) {
                        carnumCookie.push(1);
                    });
                }
            }
        }
        Booker.Cart = carCookie;
        Booker.CartNum = carnumCookie;
    },
    AddBookNum: function(id) {
        var index = Booker.GetIndex(id);
        if (index != -1) {
            if (Booker.CartNum.length > index) {
                Booker.CartNum[index] = parseInt(Booker.CartNum[index]) + 1;
            }
        }
    },
    Buy: function(id) {
        Booker.InitCart();
        if (!Comm.IsInArray(Booker.Cart, id)) {
            Booker.Cart.push(id);
            Booker.CartNum.push(1);
        } else {
            Booker.AddBookNum(id);
        }
        Comm.WriteCookie('LiteWeb_Shoppingcart', Booker.Cart, null, Tcsafea.domain);
        Comm.WriteCookie('LiteWeb_Shoppingcart_Num', Booker.CartNum, null, Tcsafea.domain);
        window.open("http://user" + Tcsafea.domain + '/Shoppingcart.html?' + parseInt(Math.random() * 1000000), "LiteWeb_Shoppingcart");
    },
    CanNotBuy: function() { alert('该图书暂不提供网上购买，如有需要，请与相关部门联系'); },
    Remove: function(id) {
        Booker.InitCart();
        if (Comm.IsInArray(Booker.Cart, id)) {
            var index = Booker.GetIndex(id);
            if (index != -1) {
                if (!confirm("确定要删除该商品吗？")) { return; }
                Booker.Cart = Comm.ArrayRemoveAt(Booker.Cart, index);
                Booker.CartNum = Comm.ArrayRemoveAt(Booker.CartNum, index);
                Comm.WriteCookie('LiteWeb_Shoppingcart', Booker.Cart, null, Tcsafea.domain);
                Comm.WriteCookie('LiteWeb_Shoppingcart_Num', Booker.CartNum, null, Tcsafea.domain);
                window.open("http://user" + Tcsafea.domain + '/Shoppingcart.html?' + parseInt(Math.random() * 1000000), "LiteWeb_Shoppingcart");
            }
        }
    },
    ChangNum: function(id, oldnum, obj) {
        Booker.InitCart();
        if ($(obj) != null) {
            var num = parseInt($(obj).value);
            if (num == 0) { return; }
            if (num != oldnum) {
                var index = Booker.GetIndex(id);
                if (index != -1) {
                    Booker.CartNum[index] = num;
                    Comm.WriteCookie('LiteWeb_Shoppingcart_Num', Booker.CartNum, null, Tcsafea.domain);
                    window.open("http://user" + Tcsafea.domain + '/Shoppingcart.html?' + parseInt(Math.random() * 1000000), "LiteWeb_Shoppingcart");
                }
            }
        }
    },
    GetIndex: function(id) {
        var index = -1;
        for (var i = 0; i < Booker.Cart.length; i++) {
            if (Booker.Cart[i] == id) {
                index = i; break;
            }
        }
        return index;
    },
    Checkout: function(btn) {
        window.open("http://user" + Tcsafea.domain + '/Checkout.html?' + parseInt(Math.random() * 1000000), "LiteWeb_Shoppingcart");
    },
    Clear: function() {
        Comm.WriteCookie('LiteWeb_Shoppingcart', '', null, Tcsafea.domain);
        Comm.WriteCookie('LiteWeb_Shoppingcart_Num', '', null, Tcsafea.domain);
    }
};

//培训
var Courser = {
    Sign: function(btn, id) {
        if (OnlineUser.uId == 0) { alert("对不起，您还没有登录，请先登录"); if ($('uName')) { $("uName").focus(); } return; }
        if (OnlineUser.uType != 0) { alert("对不起，该活动只允许个人会员报名"); return; }
        if (!confirm("确定要报名参加该培训课程吗？")) { return; }
        btn.disabled = true;
        Ajax.CourseSign(id, OnlineUser.uId, function(Int) {
            if (Int.value != null) {
                switch (Int.value) {
                    case -1000: alert('报名失败，请刷新重试'); break;
                    case -1001:
                        if (confirm('您已经包过该培训课程，是否现在就去查看报名列表')) { window.location.href = 'http://user' + Tcsafea.domain + '/Mycoursesign' + Tcsafea.Ext; }
                        break;
                    case -1002: alert('报名已经结束'); break;
                    case -1003: alert('已过报名日期'); break;
                    case -1004: alert('报名人数已满'); break;
                    case -1005: alert('对不起，该培训课程只针对俱乐部会员开放'); break;
                    default: alert('报名成功'); window.location.href = 'http://user' + Tcsafea.domain + '/Mycoursesign/' + Int.value + Tcsafea.Ext; break;

                }
            } else {
                alert('报名失败，请刷新重试');
            }
        });
    },
    GoSign: function(btn, url) {
        btn.disabled = true;
        window.location.href = url;
    }
};

var Examr = {
    GoSign: function(btn, url) {
        btn.disabled = true;
        window.location.href = url;
    }
};

//俱乐部活动活动
var Club_Activityr = {
    Sign: function(btn, url) { window.open(url); }
};

//加载整个页面input的动态样式
function inputstyle() {
    var normalbordercolor = "#ccc";
    var normalbackgroundcolor = "#fff";
    var focusbordercolor = "#812103";
    var focusbackgroundcolor = "#f6f6ff";
    $$('input[type="text"]', 'input[type="password"]').each(function(element) {
        if (element.className == "") {
            element.style.borderWidth = "1px";
            element.style.borderStyle = "solid";
            element.style.borderColor = normalbordercolor;
            element.onmouseover = function() { this.style.borderColor = focusbordercolor; this.style.backgroundColor = focusbackgroundcolor; };
            element.onmouseout = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; };
            element.onfocus = function() { this.style.borderColor = focusbordercolor; this.style.backgroundColor = focusbackgroundcolor; this.onmouseout = "" };
            if (element.onblur == null) {
                element.onblur = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; this.onmouseout = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; }; };
            }
        }
        element.autocomplete = "off";
    });
    $$('input[type="button"]', 'input[type="submit"]').each(function(element) { element.style.cursor = "pointer"; });
    $$('textarea').each(function(element) {
        element.style.borderWidth = "1px";
        element.style.borderStyle = "solid";
        element.style.borderColor = normalbordercolor;
        element.onmouseover = function() { this.style.borderColor = focusbordercolor; this.style.backgroundColor = focusbackgroundcolor; };
        element.onmouseout = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; };
        element.onfocus = function() { this.style.borderColor = focusbordercolor; this.style.backgroundColor = focusbackgroundcolor; this.onmouseout = "" };
        if (element.onblur == null) {
            element.onblur = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; this.onmouseout = function() { this.style.borderColor = normalbordercolor; this.style.backgroundColor = normalbackgroundcolor; }; };
        }
    });
}
inputstyle();

//修改列表的弹开窗口路径
function SetListTarget() {
    $$('ul[class*=list]', 'ul[id*=list]').each(function(element) {
        var s = new Selector("a");
        if (element.getElementsByTagName("a").length > 0) {
            s.findElements($(element)).each(function(link) {
                if (link.href != document.URL) { link.target = "_blank"; }
            });
        }
    });
}
SetListTarget();

//加载阴影样式
function SetMainPanel() {
    $$('div[class="mainpanel"]').each(function(element) {
        var iH = "<div class=\"md_t\"><div class=\"md_tl\"></div><div class=\"md_tr\"></div></div>\n<div class=\"md_c\"><div class=\"md_cl\"><div class=\"md_cr\"><div class=\"md_cc\">";
        iH += element.innerHTML;
        iH += "<div class=\"clear\"></div></div></div></div></div>\n<div class=\"md_f\"><div class=\"md_fl\"></div><div class=\"md_fr\"></div></div>";
        element.innerHTML = iH;
    });
}
SetMainPanel();

//搜索
var Sercher = {
    DefaultValue: '请输入标题关键字',
    Init: function() {
        if ($("SearchKeywords")) {
            Comm.AddEvent($("SearchKeywords"), 'click', function() {
                if (Comm.Trim($F('SearchKeywords')) == Sercher.DefaultValue) {
                    $("SearchKeywords").value = '';
                } else {
                    $("SearchKeywords").select();
                }
            });
            Comm.AddEvent($("SearchKeywords"), 'blur', function() {
                if (Comm.Trim($F('SearchKeywords')) == '') { $("SearchKeywords").value = Sercher.DefaultValue; }
            });
            Comm.AddEvent($("SearchKeywords"), 'keydown', function() {
                Sercher.OnKeyDown(event);
            });
        }
    },
    OnKeyDown: function(evt) {
        var b = evt || window.event; var c = b.keyCode || b.charCode;
        if (c == 13) {
            Sercher.DoSerch();
        }
    },
    DoSerch: function() {
        var key = Comm.Trim($F('SearchKeywords'));
        if (key != '' && key != Sercher.DefaultValue) {
            window.location.href = "/" + $("SearchKeywords").className + "/search.html?" + escape($F("SearchKeywords"));
        } else {
            alert(Sercher.DefaultValue);
        }
    }
};
Comm.Onload(function() { Sercher.Init(); });


//发布评论
var uid = getCookie("dnt", "userid");
function comment(fid, tid, domainname) {
    if (uid == "") {
        alert("请先登录！");
        if ($('uName')) { $("uName").focus(); }
    }
    else {
        var content = $("commentcontent").value;
        if (content.replace(/(^\s*)|(\s*$)/g, "").length < 10) {
            alert("评论内容不能少于10个字！");
            $("commentcontent").value = content.replace(/(^\s*)|(\s*$)/g, "");
        }
        else {
            var newcontent = LiteWeb.Web.UC.comment.commentpostresult(fid, tid, content, domainname).value;
            if (newcontent != "") {
                $("commentlist").innerHTML = newcontent;
                $("commentcontent").value = "";
                location.href = "#commenttag";
            }
            else {
                alert("操作出错，请检查您的登录状态及发帖权限情况，然后刷新本页面重试！");
            }
        }
    }
}

function expertgpost(pid) {
    if (uid == "") {
        alert("请先登录！");
        if ($('uName')) { $("uName").focus(); }
    }
    else {
        var title = $("gtitle").value;
        if (title.replace(/(^\s*)|(\s*$)/g, "").length < 0) {
            alert("请输入提问标题！");
            $("gtitle").value = title.replace(/(^\s*)|(\s*$)/g, "");
            return;
        }
        var content = $("gcontent").value;
        if (content.replace(/(^\s*)|(\s*$)/g, "").length < 10) {
            alert("提问内容不能少于10个字！");
            $("gcontent").value = content.replace(/(^\s*)|(\s*$)/g, "");
            return;
        }
        if (LiteWeb.Page.ExpertPage.gpostresult(pid, title, content, OnlineUser.uId, OnlineUser.uName).value) {
            alert("提问发布成功，请等待专家进行解答！");
            location.reload();
        }
        else {
            alert("操作出错，请检查您的登录状态，然后刷新本页面重试！");
        }
    }
}

function gpost() {
    if (uid == "") {
        alert("请先登录！");
        if ($('uName')) { $("uName").focus(); }
    }
    else {
        var title = $("gtitle").value;
        if (title.replace(/(^\s*)|(\s*$)/g, "").length < 0) {
            alert("请输入留言标题！");
            $("gtitle").value = title.replace(/(^\s*)|(\s*$)/g, "");
            return;
        }
        var content = $("gcontent").value;
        if (content.replace(/(^\s*)|(\s*$)/g, "").length < 10) {
            alert("留言内容不能少于10个字！");
            $("gcontent").value = content.replace(/(^\s*)|(\s*$)/g, "");
            return;
        }
        if (LiteWeb.Web.Guestbook.gpostresult($("domid").value, title, content, OnlineUser.uId, OnlineUser.uName).value) {
            alert("提问发布成功，请等待我们的回复！");
            location.reload();
        }
        else {
            alert("操作出错，请检查您的登录状态，然后刷新本页面重试！");
        }
    }
}

//获取cookies
function getCookie(cookieName, paraName) {
    var search = cookieName + "=";
    paraName += "=";
    if (document.cookie.length > 0) {
        var offset = document.cookie.indexOf(search)
        if (offset != -1) {
            offset += search.length;
            var end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            var cookielist = unescape(document.cookie.substring(offset, end)).split("&");
            for (var i = 0; i < cookielist.length; i++) {
                if (cookielist[i].indexOf(paraName) != -1)
                    return cookielist[i].substring(paraName.length, cookielist[i].length);
            }
        }
    }
    return "";
}

var Check = {
    All: function(ck, cls) {
        $$('input[class="' + cls + '"]').each(function(el) {
            if (el != ck) {
                if (!el.disabled) {
                    el.checked = ck.checked;
                }
            }
        });
    },
    GetIds: function(cls) {
        var IDS = new Array();
        $$('input[class="' + cls + '"]').each(function(el) {
            if (!el.disabled && el.checked) {
                IDS.push(el.value);
            }
        });
        return IDS;
    }
};