$(document).ready(function(){

    // Sign in
    $("#signInButton").click(function() {

        //console.log("signInButton clicked");
        $("#login2").submit();
        return false;
    }); //end signin

    $("#login2").submit(function() {

        var username = $('#userName').val();
        var password = $('#password').val();
        var previousPage = $('#previousUrl').val();
        var previousProtocol = $('#previousProtocol').val();
        if (previousProtocol == '') {
            previousProtocol = 'http';
        }
        var remember;
        if ($('#rememberCheck').is(':checked'))
                remember = 'true';
            else
                remember = 'false';
        //remove all the class add the messagebox classes and start fading
        //    $("#loginBox").text('Validating...').fadeIn(1000);
        //check the username exists or not from ajax
        var dataString = "type=ajax&username=" + username + "&password=" + password + "&remember=" + remember;


        //var dataString = "username=tylersavery&password=tyle1r";

        ////
        //$('#password').val(""); Redmine Ticket #75 Do not clear the password field when logging in
        $.ajax({
            type: "POST",
            url: "/account/login",
            //url: location.protocol + "//" + location.host + "/ajax_login1.php",
            data: dataString,
            cache: false,
            success: function(json){
                data = $.parseJSON( json );

                if( data.error )
                {
                    $("#signInMessage").fadeTo(200,0.1,function() //start fading the messagebox
                        {
                            //add message and change the class of the box and start fading
                            $(this).html( data.error.message ).fadeTo(900,1);
                            //$(this).html("Sorry<br>Username/Password is incorrect").fadeTo(900,1);
                        }
                    );
                    /*
                    $("#signInMessage").fadeTo(200,0.1,function() //start fading the messagebox
                                                {
                                                    //add message and change the class of the box and start fading
                                                    $(this).html("Sorry the user account could not be found.<br />Please Sign Up to upgrade your subscription to a full account.").fadeTo(900,1);
                                                });
                    $("#subscriberMessage").html("You do not have a user account, please take a moment to create one.");*/

                   //put back in when delay can be added.
                   /* if (data.error.code == 'ERROR_20302')
                    {
                        $("#accountEmail").val(username);
                        $("#joinTeamBuyButton").click();
                    }*/
                }
                else
                {
                    $("#signInMessage").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....').fadeTo(900,1,function()
                                {
                                    $("#signInBox").html('').fadeTo(900,1);
                                    //window.location.href = location.protocol + "//" + location.host + previousPage;
                                    //alert(previousProtocol + '://' + location.host + previousPage);
                                    window.location.href = previousProtocol + "://" + location.host + previousPage;
                                });
                        }
                    );
                }
            }

        });

        return false;//not to post the  form physically
    }); //endlogin2

    $("#forgotPassword").click(function(){
        $("#forgotMessage").fadeIn(100).html('Processing');

        $("#forgotPassword").fadeOut(100);

        if($("#userName").val() == null){

            $("#signInMessage").fadeIn(300).html('Please type in your username or email first');
            $("#forgotPassword").fadeIn(300);

        } else {

            var username = 	$("#forgotUserName").val();

            var dataString = "username=" + username;

            $.ajax({
                type: "POST",
                url: location.protocol + "//" + location.host + "/forgotPassword.php",
                data: dataString,
                cache: false,
                success: function(html){

                    if(html.indexOf("Sign Up") !== -1) {
                        $("#subscriberMessage").html("You do not have a user account, please take a moment to create one.");
                        $("#accountEmail").val(username);
                        $("#joinTeamBuyButton").click();
                        $("#forgotMessage").fadeIn(300).html(html);
                        $("#forgotPassword").fadeIn(300);
                    } else {
                        $("#forgotMessage").fadeIn(300).html(html);
                        $("#forgotPassword").fadeIn(300);
                    }
                }
            });

        } //end else



    }); //end the forgotPassword click



}); //end the document ready

