﻿//load註冊事件或屬性
$(function () {
    setInterval("SlideShow()", 10000);

    $('#imgAVPic').mouseenter(
    function () {
        var data = $(this).attr('src');
        $(this).attr('src', $(this).attr('data'));
        $(this).attr('data', data);
    }).mouseleave(function () {
        var data = $(this).attr('src');
        $(this).attr('src', $(this).attr('data'));
        $(this).attr('data', data);
    });

    $('#AccDIV').mouseenter(
    function () {
        $('#AccWordDIV').hide();
    }).mouseleave(function () {
        if (!jQuery.data(document.body, "Acc")) {
            $('#AccWordDIV').show();
        }
    });

    $('#PwdDIV').mouseenter(
    function () {
        $('#PwdWordDIV').hide();
    }).mouseleave(function () {
        if (!jQuery.data(document.body, "pwd")) {
            $('#PwdWordDIV').show();
        }
    });

    $('#TxtAcc').focus(
    function () {
        $('#AccWordDIV').hide();
        jQuery.data(document.body, "Acc", true);
    }
    ).blur(
    function () {
        if ($(this).val().length == 0) {
            jQuery.data(document.body, "Acc", false);
            $('#AccWordDIV').show();
        }
    });

    $('#TxtPwd').focus(
    function () {
        $('#PwdWordDIV').hide();
        jQuery.data(document.body, "pwd", true);
    }
    ).blur(
    function () {
        if ($(this).val().length == 0) {
            jQuery.data(document.body, "pwd", false);
            $('#PwdWordDIV').show();
        }
    });

    $('#BtnLogin').click(function () {
        Loading();
        if ($('#TxtAcc').val().replace(/\ /g, '').length == 0) {
            AlertString("發生錯誤", "未輸入Email");
        }
        else if ($('#TxtPwd').val().replace(/\ /g, '').length == 0) {
            AlertString("發生錯誤", "未輸入密碼");
        }
        else if ($('#TxtCode').val().replace(/\ /g, '').length == 0) {
            AlertString("發生錯誤", "未輸入驗證碼");
        }
        else {
            $(this).attr("disabled", "disabled");
            $.ajax({
                type: "POST",
                url: "/jqueryget",
                data: "Method=Login&Acc=" + $('#TxtAcc').val() + "&Pwd=" + $('#TxtPwd').val() + "&Code=" + $('#TxtCode').val(),
                success: function (response) {
                    var responseList = response.split(',');
                    if (responseList[0] == "0") {
                        $('#CodeImg').attr("src", "imges/ValidateCode?" + Math.random());
                        $('#TxtPwd').val("");
                        $('#TxtCode').val("");
                        AlertString("發生錯誤", responseList[1], "#BtnLogin");
                    }
                    else {
                        AlertString("登入成功", responseList[1], "#BtnLogin", true);
                    }
                },
                error: function () { AlertString("發生錯誤", result.status + ' ' + result.statusText, "#BtnLogin"); }
            });
        }
    });

    $('#BtnCreateMember').click(function () {
        Loading();
        $(this).attr("disabled", "disabled");
        var restring = "";

        if ($('#TxtCreateAcc').val().replace(/\ /g, '').length == 0) {
            restring = "未輸入Email</br>";
        }

        if (!checkEmail($('#TxtCreateAcc').val())) {
            restring = "Email不正確</br>";
        }

        if ($('#TxtCreatePwd').val().replace(/\ /g, '').length == 0) {
            restring += "未輸入密碼</br>";
        }

        if ($('#TxtCreateNick').val().replace(/\ /g, '').length == 0) {
            restring += "未輸入暱稱</br>";
        }

        if ($('#TxtCreateCode').val().replace(/\ /g, '').length == 0) {
            restring += "未輸入驗證碼</br>";
        }

        if ($('#TxtCreatePwd').val() != $('#TxtConfirmPwd').val()) {
            restring += "密碼不一致";
        }

        if (restring.length > 0) {
            AlertString("發生錯誤", restring, "#BtnCreateMember");
        } else {
            $.ajax({
                type: "POST",
                url: "/jqueryget",
                data: "Method=NewMember&Acc=" + $('#TxtCreateAcc').val() + "&Pwd=" + $('#TxtCreatePwd').val() + "&Code=" + $('#TxtCreateCode').val() + "&Nick=" + encodeURIComponent($('#TxtCreateNick').val()),
                success: function (response) {
                    var responseList = response.split(',');
                    if (responseList[0] == "0") {
                        $('#ImgCreateCode').attr("src", "imges/ValidateCode?" + Math.random());
                        $('#TxtCreatePwd').val("");
                        $('#TxtConfirmPwd').val("");
                        $('#TxtCreateCode').val("");
                        AlertString("發生錯誤", responseList[1], "#BtnCreateMember");
                    }
                    else {
                        AlertString("加入會員成功", responseList[1], "#BtnCreateMember", "/");
                    }
                },
                error: function () { AlertString("發生錯誤", result.status + ' ' + result.statusText, "#BtnCreateMember"); }
            });
        }
    });

    $('#BtnUpdateMember').click(function () {
        Loading();
        $(this).attr("disabled", "disabled");
        var restring = "";
        if ($('#TxtChangePwd').val().replace(/\ /g, '').length == 0) {
            restring = "未輸入密碼";
        }
        else if ($('#TxtChangePwd').val().replace(/\ /g, '').length != 0 && $('#TxtChangePwd').val() != $('#TxtConfirmPwd').val()) {
            restring += "密碼不一致";
        }

        if ($('#TxtUpdateCode').val().replace(/\ /g, '').length == 0) {
            restring += "未輸入驗證碼</br>";
        }

        if (restring.length > 0) {
            AlertString("發生錯誤", restring, "#BtnUpdateMember");
        } else {
            $.ajax({
                type: "POST",
                url: "/jqueryget",
                data: "Method=UpdateMember&Pwd=" + $('#TxtChangePwd').val() + "&Code=" + $('#TxtUpdateCode').val(),
                success: function (response) {
                    var responseList = response.split(',');
                    //                    if (responseList[0] == "0") {
                    $('#ImgUpdateCode').attr("src", "imges/ValidateCode?" + Math.random());
                    $('#TxtChangePwd').val("");
                    $('#TxtConfirmPwd').val("");
                    $('#TxtUpdateCode').val("");
                    //                    }
                    AlertString((responseList[0] == "0" ? "發生錯誤" : "修改完成"), responseList[1], "#BtnUpdateMember");
                },
                error: function () { AlertString("發生錯誤", result.status + ' ' + result.statusText, "#BtnUpdateMember"); }
            });
        }
    });

    $('#BtnLogOut').click(function () {
        Loading();
        $.ajax({
            type: "POST",
            url: "/jqueryget",
            data: "Method=LogOut",
            success: function () {
                location.reload();
            },
            error: function () { AlertString("發生錯誤", result.status + ' ' + result.statusText); }
        });
    });

    $('#BtnCoop').click(function () {
        Loading();
        $(this).attr("disabled", "disabled");
        $('#ImgCooPCode').attr("src", "imges/ValidateCode?" + Math.random());
        $.ajax({
            type: "POST",
            url: "/jqueryget",
            data: "Method=Coop&Nick=" + $('#TxtCoopNick').val() + "&QQ=" + $('#TxtCoopQQ').val() + "&MSN=" + $('#TxtCoopMSN').val() + "&email=" + $('#TxtCoopEmail').val() + "&Type=" + $('#CoopSelect').val() + "&note=" + $('#TxtCoopNote').val() + "&Code=" + $('#TxtCoopCode').val(),
            success: function (response) {
                var responseList = response.split(',');
                $('#TxtCoopNick').val("");
                $('#TxtCoopQQ').val("");
                $('#TxtCoopMSN').val("");
                $('#TxtCoopEmail').val("");
                $('#TxtCoopNote').val("");
                $('#TxtCoopCode').val("");
                AlertString((responseList[0] == "0" ? "發生錯誤" : "修改完成"), responseList[1], "#BtnCoop");
            },
            error: function () {
                AlertString("發生錯誤", result.status + ' ' + result.statusText); 
            }
        });
    });

    $('#BtnFPwd').click(function () {
        Loading();
        if ($('#TxtAcc').val().replace(/\ /g, '').length == 0) {
            AlertString("發生錯誤", "未輸入Email");
        } else if (!checkEmail($('#TxtAcc').val())) {
            AlertString("發生錯誤", "Email不正確");
        }
        else if ($('#TxtCode').val().replace(/\ /g, '').length == 0) {
            AlertString("發生錯誤", "未輸入驗證碼");
        } else {
            $.ajax({
                type: "POST",
                url: "/jqueryget",
                data: "Method=FPwd&Acc=" + $('#TxtAcc').val() + "&Code=" + $('#TxtCode').val(),
                success: function (response) {
                    var responseList = response.split(',');
                    AlertString((responseList[0] == "0" ? "發生錯誤" : "訊息"), responseList[1], "#BtnFPwd", true);
                },
                error: function () { AlertString("發生錯誤", result.status + ' ' + result.statusText); }
            });
        }
    });

    $.ajax({
        type: "POST",
        url: "/jqueryget",
        data: "Method=GetUrlCount&referrer=" + document.referrer + "&location=" + window.location.href
    });

    $(function () {
        $("input").keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $('#BtnLogin').click();
                return false;
            } else {
                return true;
            }
        });
    });
});

function AlertString(Title, Content, BtnName, reload) {
    $("#AlterContent").html(Content);
    $("#AlertDIV").dialog({
        modal: true,
        title: Title,
        closeOnEscape: false,
        open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); },
        buttons: {
            確定: function () {
                $(this).dialog("close");
                $(BtnName).removeAttr("disabled");
                if (reload != null) {
                    if (reload) {
                        location.reload();
                    } else {
                        location.href = reload;
                    }
                }
            }
        }
    });
}

function Loading() {
    $("#AlterContent").html("<table width=\"100%\"><tr><td align=\"center\"><img src=\"/images/loading.gif\" /></tr></td></table>");
    $("#AlertDIV").dialog({
        modal: true,
        title: "資料驗證中 - 請稍後",
        closeOnEscape: false,
        open: function (event, ui) { $(".ui-dialog-titlebar-close").hide(); }
    });
}

function checkEmail(email) {
    var at = "@";
    var dot = ".";
    var lat = email.indexOf(at);
    var lstr = email.length;
    var ldot = email.indexOf(dot);
    if (email.indexOf(at) == -1) {
        return false;
    }

    if (email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == lstr) {
        return false;
    }

    if (email.indexOf(dot) == -1 || email.indexOf(dot) == 0 || email.indexOf(dot) == lstr) {
        return false;
    }

    if (email.indexOf(at, (lat + 1)) != -1) {
        return false;
    }

    if (email.substring(lat - 1, lat) == dot || email.substring(lat + 1, lat + 2) == dot) {
        return false;
    }

    if (email.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }

    if (email.indexOf(" ") != -1) {
        return false;
    }

    return true
}

function SlideShow() {
    var $active = $('#SlideView a IMG.active');

    if ($active.length == 0) $active = $('#SlideView IMG:last');

    var $next = $active.next().length ? $active.next()
        : $('#SlideView a IMG:first');

    $active.addClass('last-active');


    $next.addClass('active')
    $active.removeClass('active last-active');

//    $next.css({ opacity: 0.0 })
//        .addClass('active')
//        .animate({ opacity: 1.0 }, 1000, function () {
//            //移除 active last-active
//            $active.removeClass('active last-active');
//        });
}

function addBookmark() {
    var p_title = document.title;
    var p_url = window.location.href;
    if (document.all && !window.opera) {
        window.external.AddFavorite(p_url, p_title);
    } else if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
        window.sidebar.addPanel(p_title, p_url, "");
    } else {
    AlertString("說明","您的瀏覽器不支援加入收藏，請自行手動加入");
    }
}
