
var NO_USER_ERROR_MSG = "The email address provided is not recognized.";
/* Capatilize the first char */
String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

$(document).ready(function () {
    
    var user_email = "";

    $("img").unveil();
    $("img").trigger("unveil");
    
    // Reset all views
    var securityquesDiv = $("#id-div-forgot-pass-securityquestion");
    var forgotPasswordContent = $("#id-div-forgot-password-content");
    var forgotPasswordProgress = $("#id-div-forgot-password-progress");
    var forgotPassPane = $("#id-pane-forgot-pass");
    
    
    $(securityquesDiv).hide();
    $(forgotPasswordContent).show();
    $(forgotPasswordProgress).hide();
    $(forgotPassPane).show();    
    
    // redirect to register/login page
    $("#id-login-a-back-register").click(function(){
        var target = "register.php";
        window.location.href = target;    
    });   
   
    $("#id-btn-forgot-pass-submit").click(function(){
        var email = $("#id-forgot-pass-inp-email");
        var securityanswer = $("#id-forgot-pass-inp-securityanswer");
        var securityquesDiv = $("#id-div-forgot-pass-securityquestion");
        
        var username_val = $(email).val();
        var securityanswer_val = $(securityanswer).val();
        
        var eventDataUsername = {target : $(email)};
        var eventDataSecurityAnswer = {target : $(securityanswer)};
        showErrorSpan($(email),!validateEmail(eventDataUsername));
                 
        if((securityquesDiv.css("display") === "none") && validateEmail(eventDataUsername)){
            $("#id-btn-forgot-pass-submit").html("Please Wait..");
            $("#id-btn-forgot-pass-submit").addClass("disabled");
            $.post("API.php", { register: "RESET_PASSWORD_PREP", username: username_val }, function (data) {
                if(data.response){
                    $(email).parent().parent().find(".cl-lbl").html("");
                    $(securityquesDiv).show();
                    $("#id-span-forgot-pass-securityquestion").html(data.data.securityquestion_text);
                } else {
                    showErrorSpan(email,true);
                    $(email).parent().parent().find(".cl-lbl").html(NO_USER_ERROR_MSG).show();
                }
                //alert(data.response);
                
                $("#id-btn-forgot-pass-submit").html("Submit");
                $("#id-btn-forgot-pass-submit").removeClass("disabled");
            
            },"json");
        } 
        
        if((securityquesDiv.css("display") !== "none") && validateEmail(eventDataUsername) && validateSecurityAnswer(eventDataSecurityAnswer)){
            //showErrorSpan($(securityanswer),!validateSecurityAnswer(eventDataUsername));            
            //$("#id-div-forgot-password-content").hide();
            $("#id-div-forgot-password-progress").show();
            
            $("#id-div-forgot-password-progress-icon").addClass("fa-2x");
            $("#id-div-forgot-password-progress-icon").addClass("fa-spin");
            $("#id-div-forgot-password-progress-icon").addClass("fa-spinner");
            $("#id-div-forgot-password-progress-msg").html("We are working on initiating a password reset request. Please wait!");
            
            
            $.post("API.php", { register: "RESET_PASSWORD_INIT", username: username_val, securityanswer: securityanswer_val }, function (data) {
                $("#id-div-forgot-password-progress-icon").removeClass("fa-spinner");
                $("#id-div-forgot-password-progress-icon").removeClass("fa-spin");
                if(data.response){
                    $("#id-div-forgot-password-content").hide();
                    $("#id-div-forgot-password-progress-icon").addClass("fa-2x");
                    $("#id-div-forgot-password-progress-icon").addClass("fa-check");
                    $("#id-div-forgot-password-progress-msg").html("Reset Password successfully initiated. A reset link has been sent to your registered e-mail address. Kindly follow that link to reset your password.");
                    $("#login-btn").show();
                    $("#id-div-forgot-pass-submit").hide();                        
                } else {
                    $("#id-div-forgot-password-progress").hide();
                    // $("#id-btn-forgot-pass-submit").hide();
                    //$("#id-div-forgot-password-progress-icon").addClass("fa-2x");
                    //$("#id-div-forgot-password-progress-icon").addClass("fa-close");
                    // $("#id-div-forgot-password-progress-msg").html("Something went wrong! We couldn't initiate a reset password request. Error : "+data.message);
                    showErrorSpan($(securityanswer),true); 
                    text_box_error($(securityanswer));  
                    $("#id-forgot-pass-inp-error-lbl-securityanswer").show();
                }
                //alert(data.response);
            },"json");
        }
    });

});


function showErrorSpan(elem, show){
    var span = $(elem).parent().parent().find(".cl-lbl");
    if(show){
        $(span).removeClass("has-success");       
        $(span).addClass("has-danger");
        // $(span).show();
    } else {
        $(span).removeClass("has-danger");       
        $(span).addClass("has-success");
    }
}


function validateEmail(data){
    var elem = $("#id-register-inp-email");
    if(data){
        elem = data.target;
    }
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var checkEmpty = true || !(data && data.focus);
    if(((checkEmpty && val && val!='') || !checkEmpty) && filter.test(val)){
        $(parent).addClass("has-success");
        $(parent).removeClass("has-danger");
        $(span).addClass("has-success");
        $(span).removeClass("has-danger"); 
        $(elem).removeClass("form-control-danger");       
        $(elem).addClass("form-control-success");     
        return true;
    } else {
        $(parent).addClass("has-danger");
        $(parent).removeClass("has-success");
        $(span).addClass("has-danger");
        $(span).removeClass("has-success");        
        $(elem).addClass("form-control-danger");       
        $(elem).removeClass("form-control-success");   
        return false;
    }
}

function validateSecurityAnswer(data){
    var elem = $("#id-register-inp-securityanswer");
    if(data){
        elem = data.target;
    }
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^(?=.*[a-z])[A-Za-z\d$@$!%*?&\s]{3,}/;
    if(val && val!='' && filter.test(val)){
        $(parent).addClass("has-success");
        $(parent).removeClass("has-danger"); 
        $(elem).removeClass("form-control-danger");       
        $(elem).addClass("form-control-success");     
        return true;
    } else {
        $(parent).addClass("has-danger");
        $(parent).removeClass("has-success");        
        $(elem).addClass("form-control-danger");       
        $(elem).removeClass("form-control-success");      
        return false;
    }
}

function text_box_error(elem)
{
    var parent = $(elem).parent();
    $(parent).addClass("has-danger");
    $(elem).addClass("form-control-danger"); 
}
;