﻿
$(document).ready(function() {
    $(".loginTable").addClass("hidden");
    $("#saveLogin").hide();
    $(".lblPwdRec").hide();

    var fname = location.pathname.toLowerCase();
    if (fname.indexOf("top25.asp") > -1) {
        $("#rssIcon").attr({ title: "Top 25 RSS feed", alt: "Top 25 RSS feed" });
        $("#rssIcon").show();
        $("#rssIcon").click(function() {
            window.location = "http://www.1christian.net/rss/rwtc-top25.aspx"
        });
    };
    if (fname.indexOf("newtab.asp") > -1) {
        $("#rssIcon").attr({ title: "New Songs RSS feed", alt: "New Songs RSS feed" });
        $("#rssIcon").show();
        $("#rssIcon").click(function() {
            window.location = "http://www.1christian.net/rss/rwtc-newsongs.aspx?d=7"
        });
    };

    //Read cookies and set page accordingly
    var LoggedIn = $.cookie('LoggedIn');
    //determine if user is logged in and set page attributes accordingly
    if (LoggedIn != null) {
        $(".lblLogin").hide();
        $(".lblLogout").show();
        $(".loginTable").addClass("hidden");
        $(".lblMyAccount").show();
        $(".lblSignUp").hide();
    }
    else {
        $(".lblLogin").show();
        $(".lblLogout").hide();
        $(".lblMyAccount").hide();
        $(".lblSignUp").show();
    };

    //Tabs drop-down
    if ($.browser.msie) {
        $(".expand").hover(
           function() {
               $(".divexpand:hidden").show();
           },
           function() {
               $(".divexpand").hide();
           });
    } else {
        $(".expand").hover(
           function() {
               $(".divexpand:hidden").fadeIn(500);
           },
           function() {
               $(".divexpand").fadeOut(100);
           });
    };
    //Other Areas Drop-Down
    if ($.browser.msie) {
        $(".expandAreas").hover(
           function() {
               $(".divexpandAreas:hidden").show();
           },
           function() {
               $(".divexpandAreas").hide();
           });
    } else {
        $(".expandAreas").hover(
           function() {
               $(".divexpandAreas:hidden").fadeIn(500);
           },
           function() {
               $(".divexpandAreas").fadeOut(100);
           });
    };
    //Help Drop-Down
    if ($.browser.msie) {
        $(".expandHelp").hover(
           function() {
               $(".divexpandHelp:hidden").show();
           },
           function() {
               $(".divexpandHelp").hide();
           });
    } else {
        $(".expandHelp").hover(
           function() {
               $(".divexpandHelp:hidden").fadeIn(500);
           },
           function() {
               $(".divexpandHelp").fadeOut(100);
           });
    }
    //Categories Drop-Down
    if ($.browser.msie) {
        $(".expandCategories").hover(
        function() {
            $(".divexpandCategories:hidden").show();
        },
        function() {
            $(".divexpandCategories").hide();
        });
    } else {
        $(".expandCategories").hover(
        function() {
            $(".divexpandCategories:hidden").fadeIn(500);
        },
        function() {
            $(".divexpandCategories").fadeOut(100);
        });
    }

    //Login Form animation
    //if browser=Internet Explorer
    //    if ($.browser.msie) {
    //        $(".lblLogin").click(function() {
    //            window.location = "login.asp";
    //        });
    //    } else {
    $(".lblLogin").click(function() {
        if ($(".loginImage").is(":hidden")) {
            $(".loginImage").slideDown(500);
            $(".loginButton").slideDown(500);
            $(".loginTable").addClass("hidden").fadeTo(0, 0, function() {
                $(this).removeClass("hidden")
            }).fadeTo(0, 0).fadeTo(500, 0).fadeTo(0, 1);
            $("#saveLogin").fadeIn(500);
            $(".lblPwdRec").fadeIn(500);
            $(".txtUserName").focus();
        } else {
            $(".loginImage").slideUp(200);
            $(".loginButton").slideUp(200);
            $(".loginTable").toggleClass("hidden");
            $("#saveLogin").hide();
            $(".lblPwdRec").hide();
        }
    });
    //    }

    //bind click event to search button
    $(".searchIcon").bind("click", function(e) {
        window.location = "search.asp?searchMain=" + $("#searchMain").val();
    });

    //bind keypress "enter" to search form
    $("#searchMain").bind("keypress", function(e) {
        if (e.which == 13) {
            //window.location = "search.asp?searchMain=" + $("#searchMain").val();
            $("#searchTerms").val("");
            $("#aspnetForm").attr({
                action: "search.asp",
                method: "post"
            });
            $("#aspnetForm").submit();
        }
    });

    //search for default.aspx
    //bind click event to search button in Default.aspx
    $(".searchIconAdv").bind("click", function(e) {
        //get query to search.asp
        $.post("search.asp", { search: $(".searchType").val() + $('.searchBarAdv').val() }, //get query to search.asp
        function() {
            window.location = "search.asp?searchType=" + $("#searchType").val() + "&searchTerms=" + $(".searchBarAdv").val();
        });
    });

    //bind keypress "enter" to search form in Default.aspx
    $(".searchBarAdv").bind("keypress", function(e) {
        if (e.which == 13) {
            $("#searchMain").val("");
            $("#aspnetForm").attr({
                action: "search.asp",
                method: "post"
            });
            $("#aspnetForm").submit();
        }
    });

    //bind keypress "enter" to search form in Default.aspx
    $(".searchIconAdv").bind("keypress", function(e) {
        if (e.which == 13) {
            //get query to search.asp
            $.post("search.asp", { search: $('.searchBarAdv').val() }, //get query to search.asp
        function() {
            window.location = "search.asp?searchType=" + $("#searchType").val() + "&searchTerms=" + $(".searchBarAdv").val();
        });
        }
    });

    //bind click event to login button
    $("#loginButton").bind("click", function(e) {
        doLogin();
        return false; //not to post the  form physically
    });

    //bind keypress "enter" to login form
    $("#loginTable").bind("keypress", function(e) {
        if (e.which == 13) {
            doLogin()
            return false; //not to post the  form physically
        }        
    });
});                        //end brackets for document ready function

function doLogin() {
    //remove all the class add the messagebox classes and start fading
    $("#saveLogin").fadeOut();
    $(".lblPwdRec").fadeOut();
    var UserName = $(".txtUserName").val();
    var Password = $(".txtPassword").val();
    if (UserName == "") {
        $("#msgbox").removeClass().addClass("messagebox").text("A User Name Is Required").fadeIn(1000);
    }
    else if (Password == "") {
        $("#msgbox").removeClass().addClass("messagebox").text("A Password Is Required").fadeIn(1000);
    }
    else {
        $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
        var chkd = '';
        if ($('#chksaveLogin:checked').val() !== null)
            chkd = $('#chksaveLogin:checked').val();
     
            $.post("login_ajax.aspx", { txtUserName: $('.txtUserName').val(), txtPassword: $('.txtPassword').val(), chksaveLogin: chkd, rand: Math.random() },
            function(data) {
                if (data == '1') //if correct login detail
                {
                    $("#msgbox").fadeTo(200, 0.1, function()  //start fading the messagebox
                    {
                        //add message and change the class of the box and start fading
                        $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900, 1,
                      function() {
                          $(".lblLogin").hide();
                          $(".lblLogout").show();
                          $(".loginImage").slideUp("200");
                          $(".loginButton").slideUp("200");
                          $(".loginTable").addClass("hidden");
                          $("#msgbox").hide();
                          $(".lblMyAccount").show();
                          $(".lblSignUp").hide();
                      });
                    });
                }
                else if (data == '2') //if renew membership agreement
                {
                    $("#msgbox").fadeTo(200, 0.1, function()  //start fading the messagebox
                    {
                        //add message and change the class of the box and start fading
                        $(this).html('Please Renew Your Membership Agreement').addClass('messageboxok').fadeTo(900, 1,
                      function() {
                          window.location = "agreement_update.asp"; //redirect to membership agreement page
                      });
                    });
                }
                else if (data == '3') //if verify e-mail
                {
                    $("#msgbox").fadeTo(200, 0.1, function()  //start fading the messagebox
                    {
                        //add message and change the class of the box and start fading
                        $(this).html('Please Verify Email Information.').addClass('messageboxok').fadeTo(900, 1,
                      function() {
                          window.location = "email_update.asp"; //redirect to e-mail update page
                      });
                    });
                }
                else if (data == '4') { //if needs reactivate membership
                    $("#msgbox").fadeTo(200, 0.1, function() //start fading messagebox
                    {
                        $(this).html('Please Reactivate Your Membership').addClass('messageboxok').fadeTo(900, 1,
                       function() {
                           window.location = "profile.asp";
                       });
                    });
                }
                else {
                    $("#msgbox").fadeTo(200, 0.1, function() //start fading the messagebox
                    {
                        //add message and change the class of the box and start fading
                        $(this).html('Invalid UserName or Password').addClass('messageboxerror').fadeTo(900, 1).fadeTo(1000, 1).fadeTo(200, 0, function() {
                            $("#saveLogin").fadeIn();
                            $(".lblPwdRec").fadeIn();
                        });
                    });
                }
            });
    }
}

function logout() {
    var options = { path: '/', expires: 10 };
    var options2 = { path: '/guitar', expires: 10 };
    $.cookie('UserID', null, null, options);
    $.cookie('UserID', null, options2);
    $.cookie('userid', null, options);
    $.cookie('userid', null, options2);
    $.cookie('LoggedIn', null, options);
    $.cookie('LoggedIn', null, options2);
    $.cookie('loggedin', null, options);
    $.cookie('loggedin', null, options2);
    $.cookie('Access', null, options);
    $.cookie('Access', null, options2);
    $.cookie('access', null, options);
    $.cookie('access', null, options2);
    $(".lblLogin").show();
    $(".lblLogout").hide();
    $(".lblMyAccount").hide();
    $(".lblSignUp").show();
}
function rollover(obj) {obj.style.cursor = 'pointer'; obj.style.backgroundColor = '#433620';}
function rollout(obj) { obj.style.backgroundColor = '#332610'; }
function openTab(TabID) { location.href = "show.asp?ID=" + TabID; }