/// <reference path="../../typings/jquery/jquery.d.ts"/>

var redir_addr = "";
var ADMIN=1;
var SUPER_ADMIN=2;
var REVIEWER=5;

/* 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 newPasswordContent = $("#id-div-new-password-content");
    var newPasswordProgress = $("#id-div-new-password-progress");
    var registerContent = $("#id-form-register-2");
    var registerProgress = $("#id-div-register-progress");
    var reconfirmPasswordContent = $("#id-div-reconfirm-password-content");
    var reconfirmPasswordProgress = $("#id-div-reconfirm-password-progress");
    
    var registerPane = $("#id-pane-register");
    var newPassPane = $("#id-pane-new-pass");
    var reconfirmPassPane = $("#id-pane-reconfirm-pass");
    
    var loginDiv = $("#id-login-tbl");
    var registerDiv = $("#id-register-tbl");
    
    $(newPasswordContent).show();
    $(newPasswordProgress).hide();
    $(registerContent).show();
    $(registerProgress).hide();
    $(reconfirmPasswordContent).show();
    $(reconfirmPasswordProgress).hide();
    
    $(registerPane).show();
    $(newPassPane).hide();
    $(reconfirmPassPane).hide();
    
    
    
    var params={};window.location.search.replace(/[?&;#]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});
    var path = window.location.pathname;
    path = path.substring(path.lastIndexOf("/")+1);

    if(params.reset && params.rl){
        $(registerPane).hide();
        $(reconfirmPassPane).hide();
        $(newPassPane).show();

    } else if(path === "register.php" && params.edit){
        $(registerPane).hide();
        $(newPassPane).hide();
        $(reconfirmPassPane).show();
        
    } else {
        $(loginDiv).removeClass("col-md-12");
        $(loginDiv).addClass("col-md-4");
        $(registerDiv).removeClass("col-md-12");
        $(registerDiv).addClass("col-md-4");
    }

    if(params.redir){
        redir_addr = params.redir;
    }
    
    $("#id-register-inp-phone").intlTelInput({
        nationalMode: true,
        utilsScript: "JS/intlTelInputUtils.70e04798.js" // just for formatting/placeholders etc
    });
    
    $("#id-login-a-back-register").click(function(){
        $("#id-pane-register").show();
        $("#id-pane-forgot-pass").hide();
        grecaptcha.reset();    
        console.log("Register Account clicked")   
    });
    
    $("#id-btn-reconfirm-pass-submit").click(function(){
       var passwordElem = $("#id-reconfirm-pass-inp-password");
       var password = $(passwordElem).val();
       var eventDataPassword = {target : $("#id-reconfirm-pass-inp-password")};
       
       if(validatePassword(eventDataPassword)){
           $.post("API.php", { register: "RE_CONFIRM_PASS", password: password }, function (data) {
                if(data.response){
                    $(passwordElem).parent().parent().parent().parent().find(".cl-error-lbl").html("");
                    $("#id-div-banner-welcome").html("Welcome "+data.first_name+" "+data.last_name);
                    window.history.pushState("","Profile Change", "register.php?edit=true&profile="+data.link);

                    $(reconfirmPassPane).hide();
                    $(registerPane).show();
                    $(loginDiv).hide();
                    $("#id-form-register-2").addClass("edit");
                    $("#id-dropdown-item-editprofile").addClass("disabled");
                    $.post("API.php", { register: "EDIT_PROFILE_RETRIEVE" }, function (data) {
                        
                        if(!data.response){
                            $(registerContent).find(".cl-div-section-msg > h6").html("Edit profile : Something went wrong!");
                            $(registerContent).find(".cl-div-section-msg > p").html("Logout and Login back to edit profile.");
                        } else {
                            var data_package = data.data;
                            $(".cl-span-site-breadcrumb-title").html("Edit profile");
                            $(registerContent).find(".cl-div-section-msg").html("");
                            $("div.cl-div-sub-section-header .fa").show();  
                            $("#id-span-register-section-2-header").show();
                            
                            $("#id-register-inp-email").val(data_package['email_id']);
                            data_package['password'] = $("#id-register-inp-password").val();
                            
                            $("#id-register-inp-firstname").val(data_package['first_name']);
                            // $("#id-register-inp-middlename").val(data_package['middle_name']);
                            $("#id-register-inp-lastname").val(data_package['last_name']);
                            $("#id-register-inp-organization").val(data_package['organization_id']);
                            $("#id-register-inp-job-title").val(data_package['job_title']);
                            // data_package['securityquestion_id'] = $("#id-register-inp-securityquestion").val();
                            if(data_package['phone'] !== null){
                                $("#id-register-inp-phone").intlTelInput("setNumber",data_package['phone']);
                            }

                            $("#id-btn-register-submit").html("Save");
                            $("#id-btn-register-submit").attr("id","id-btn-profile-save");
                            
                            $("#id-register-inp-recaptcha").hide();
                            user_email = data_package['email_id'];

                        }
                        
                    },"json");
                    
                } else {
                    $(passwordElem).parent().parent().parent().parent().find(".cl-error-lbl").html(data.message).show();
                    $("#id-div-banner-welcome").html("");
                    alert("The password you entered doesn't match with our records. Logging you out for security");
                    $.post("API.php", { register: "LOGOUT" }, function (data) {
                        window.location.replace("index.php");  
                    });

                }
                //alert(data.response);
            },"json");
       } else {
            showErrorSpan($("#id-reconfirm-pass-inp-password"),!validatePassword(eventDataPassword));
       }
       
    });
    
    $("#id-btn-login-submit").click(function(){
       var usernameElem = $("#id-login-inp-email");
       var username = $("#id-login-inp-email").val();
       var password = $("#id-login-inp-password").val();
       var eventDataUsername = {target : $("#id-login-inp-email")};
       var eventDataPassword = {target : $("#id-login-inp-password")};
       if(validateEmail(eventDataUsername) && validate_login_password(eventDataPassword)){
           $.post("API.php", { register: "LOGIN", username: username, password: password }, function (data) {
                if(data.response){
                    localStorage.setItem("user_data", JSON.stringify(data));
                    $(usernameElem).parent().parent().parent().parent().find(".cl-error-lbl").html("");
                    $("#id-div-banner-welcome").html("Welcome "+data.first_name+" "+data.last_name);
                    
                    if(data.is_verified == 1){
                        // alert('verifed');
                        if(data.authtype_id == 1 || data.authtype_id == 2){
                            if(!redir_addr || redir_addr === ""){
                                // window.location.replace("index.php"); // force reload from server
                                window.location.replace("myProjects.php");
                            } else if(redir_addr ==="s"){
                                window.location.replace("myProjects.php?view=browse"); // force reload from server
                            }
                        }
                        else{
                            if(!redir_addr || redir_addr === ""){
                                // window.location.replace("index.php"); // force reload from server
                                window.location.replace("myProjects.php");
                            } else if(redir_addr ==="s"){
                                window.location.replace("myProjects.php?view=browse"); // force reload from server
                            }
                        }
                    } else {
                        // alert('going to pending');
                        window.location.replace("verify.php?verify=pending");
                    }
                    
                    
                } else {
                    data.message = "The email address and/or password you submitted are not valid. Please try again";
                    $(usernameElem).parent().parent().parent().parent().find(".cl-error-lbl").html(data.message).show();
                    $("#id-div-banner-welcome").html("");
                }
                //alert(data.response);
            },"json");
       } else {
            showErrorSpan($("#id-login-inp-email"),!validateEmail(eventDataUsername));
            showErrorSpan($("#id-login-inp-password"),!validate_login_password(eventDataPassword));
       }

       return false;
       
    });
    
    
    $("#id-btn-login-short-submit").click(function(){
       var usernameElem = $("#id-login-short-inp-email");
       var msgElem = $("#id-login-short-inp-msg");
       var username = $("#id-login-short-inp-email").val();
       var password = $("#id-login-short-inp-password").val();
       var eventDataUsername = {target : $("#id-login-short-inp-email")};
       var eventDataPassword = {target : $("#id-login-short-inp-password")};
       
       if(validateEmail(eventDataUsername) && validate_login_password(eventDataPassword)){
           $.post("API.php", { register: "LOGIN", username: username, password: password }, function (data) {
                if(data.response){
                    $(usernameElem).parent().parent().parent().parent().find(".cl-error-lbl").html("");
                    $("#id-div-banner-welcome").html("Welcome "+data.first_name+" "+data.last_name);
                    $(msgElem).html("").hide();
                    // window.location.reload(true); // force reload from server
                    window.location.replace("solutions.php");
                    
                } else {
                    // if(data.message === "NO USER"){
                    //     data.message = "Please check the E-mail Address."
                    // }
                    data.message = "The email address and/or password you submitted are not valid. Please try again";
                    $(usernameElem).parent().parent().parent().parent().find(".cl-error-lbl").html(data.message).show();
                    $("#id-div-banner-welcome").html("");
                    $(msgElem).html(data.message).show();
                }
                //alert(data.response);
            },"json");
       } else {
            showErrorSpan($("#id-login-short-inp-email"),!validateEmail(eventDataUsername));
            showErrorSpan($("#id-login-short-inp-password"),!validate_login_password(eventDataPassword));
       }

       return false;
       
    });
    
    $("#id-btn-register-submit").click(function (){
        var flag = true;

        if($(this).html() === "Create Account"){
            // flag = validateOrganization() && flag;
            flag = validateFirstName() && flag;
            flag = validateLastName() && flag;
            flag = validateEmail() && flag;
            flag = validatePassword() && flag;
            flag = validateConfirmPassword() && flag;
            flag = validateSecurityQuestion() && flag;
            flag = validateSecurityAnswer() && flag;
            flag = validatePhoneNumber() && flag;
            flag = validateRecaptcha() && flag;

            // console.log('before');
            // console.log(flag);
            if(flag){
                // console.log('after');
                // console.log(flag);

                var data_package = new Object;
                data_package['email_id'] = $("#id-register-inp-email").val();
                data_package['password'] = $("#id-register-inp-password").val();
                data_package['first_name'] = $("#id-register-inp-firstname").val();
                data_package['middle_name'] = $("#id-register-inp-middlename").val();
                data_package['last_name'] = $("#id-register-inp-lastname").val();
                data_package['organization_id'] = $("#id-register-inp-organization").val();
                data_package['job_title'] = $("#id-register-inp-job-title").val();
                data_package['authtype_id'] = 3;
                data_package['signuprole_id'] = $("#id-register-inp-signuprole").val();
                data_package['signupreason_id'] = $("#id-register-inp-signupreason").val();
                data_package['securityquestion_id'] = $("#id-register-inp-securityquestion").val();
                data_package['securityquestion_ans'] = $("#id-register-inp-securityanswer").val();
                data_package['recaptcha'] = $("#g-recaptcha-response").val();
                data_package['phone'] = $("#id-register-inp-phone").intlTelInput("getNumber");
                data_package['country_name'] = $("#id-register-inp-phone").intlTelInput("getSelectedCountryData").name;
                data_package['country_code'] = $("#id-register-inp-phone").intlTelInput("getSelectedCountryData").iso2;
                data_package['uses_sr'] = 0; //($("#id-register-inp-sr").is(":checked") ? 1 : 0);   

                var freeTrialExpDate = new Date();
                freeTrialExpDate.setMonth( freeTrialExpDate.getMonth() + 1 );
                freeTrialExpDate = $.datepicker.formatDate('yy-mm-dd', freeTrialExpDate);
                data_package['free_trial_expiry_date'] = freeTrialExpDate;
                // alert( dt );             

                // alert(JSON.stringify(data_package));

                showErrorSpan($("#id-register-inp-firstname"), false);
                showErrorSpan($("#id-register-inp-organization"), false);
                showErrorSpan($("#id-register-inp-job-title"), false);
                showErrorSpan($("#id-register-inp-password"), false);
                showErrorSpan($("#id-register-inp-email"), false);
                showErrorSpan($("#id-register-inp-securityanswer"), false);
                showErrorSpan($("#id-register-inp-phone").parent(), false);
                showErrorSpan($("#id-register-inp-recaptcha"), false);
                
                $("#id-form-register-2").hide();
                $("#id-div-register-progress").show();
                
                $("#id-div-register-progress-icon").addClass("fa-2x");
                $("#id-div-register-progress-icon").addClass("fa-spin");
                $("#id-div-register-progress-icon").addClass("fa-spinner");
                $("#id-div-register-progress-msg").html("We are working on registering you to our system. Please wait! If it takes too long please try again or contact isud team.");
                
                
                $.post("API.php", { register: "REGISTER", data: JSON.stringify(data_package) }, function (data) {
                    $("#id-div-register-progress-icon").removeClass("fa-spinner");
                    $("#id-div-register-progress-icon").removeClass("fa-spin");
                    if(data.response){
                        $("#id-div-register-progress-icon").addClass("fa-2x");
                        $("#id-div-register-progress-icon").addClass("fa-check");
                        $("#id-div-register-progress-msg").html("Registration Successful. Please wait!");

                        ///////////////VErify email code Starts////////////////////

                        $("#id-div-register-progress-icon").addClass("fa-2x");
                        $("#id-div-register-progress-icon").addClass("fa-spin");
                        $("#id-div-register-progress-icon").addClass("fa-spinner");
                        $("#id-div-register-progress-msg").html("Account Created! Sending verification email. Please wait!");
                        
                                $.post("API.php", { register: "VERIFY_EMAIL_SEND", username: data_package['email_id'] }, function (data) {
                                        
                                        if(data.response){
                                                window.location.replace("verify.php?verify=sent");              
                                        } else {
                                                window.location.replace("verify.php?verify=error");
                                        }
                                        //alert(data.response);
                                    },"json");

                        ///////////////VErify email code ends////////////////////

                        // SharpSpring integration code
                        // After registration is successfull on this site - we call the SharpSpring API to insert the user in their db
                        postUserSharpSpring(data_package);
                        // $("#id-div-register-progress-msg").html("You have successfully been registered. Please Login to use our systems.");
                    } else {
                        $("#id-div-register-progress-icon").addClass("fa-2x");
                        $("#id-div-register-progress-icon").addClass("fa-close");
                        $("#id-div-register-progress-msg").html("Something went wrong! We couldn't register you. Please retry in a while. Error : "+data.message);
                        
                        if(data.message === "Duplicate User"){
                            $("#id-div-register-progress-msg").html("This Email address is registered in our system. If you have forgotten your password, click the 'forgot your password?' link in the login section.");
                        }
                    }
                },"json");
            } else {
                showErrorSpan($("#id-register-inp-firstname"), (!validateFirstName()));
                showErrorSpan($("#id-register-inp-lastname"), (!validateLastName()));
                // showErrorSpan($("#id-register-inp-organization"), !validateOrganization());
                showErrorSpan($("#id-register-inp-password"), (!validatePassword() || !validateConfirmPassword()));
                showErrorSpan($("#id-register-inp-email"), !validateEmail());
                showErrorSpan($("#id-register-inp-phone").parent(), !validatePhoneNumber());
                showErrorSpan($("#id-register-inp-securityanswer"), (!validateSecurityQuestion() || !validateSecurityAnswer()));
                span = $("#id-div-register-submit").find('.cl-lbl');
                $(span).removeClass("has-success");       
                $(span).addClass("has-danger");
            }
        } else if ($(this).html() === "Save"){
            flag = validateOrganization() && flag;
            flag = validateFirstName() && flag;
            // flag = validateMiddleName() && flag;
            flag = validateLastName() && flag;
            flag = validateEmail() && flag;
            flag = validatePhoneNumber() && flag;
            // flag = validateRecaptcha() && flag;

            flag = ($("#id-register-inp-password").val() !=="" ? validatePassword() : true) && flag;
            flag = ($("#id-register-inp-password").val() !=="" ? validateConfirmPassword() : true) && flag;
            // flag = validateSecurityQuestion() && flag;
            flag = ($("#id-register-inp-securityanswer").val() !=="" ? validateSecurityAnswer() : true) && flag;

            if(user_email !== $("#id-register-inp-email").val()){
                alert("Modifying the E-mail address will mean you will have to login with the new E-mail from the next session.")
            }
            
            if(flag){
                var data_package = new Object;
                data_package['email_id'] = $("#id-register-inp-email").val();
                data_package['password'] = $("#id-register-inp-password").val();
                data_package['first_name'] = $("#id-register-inp-firstname").val();
                data_package['middle_name'] = $("#id-register-inp-middlename").val();
                data_package['last_name'] = $("#id-register-inp-lastname").val();
                data_package['organization_id'] = $("#id-register-inp-organization").val();
                data_package['job_title'] = $("#id-register-inp-job-title").val();
                data_package['authtype_id'] = 3;
                data_package['securityquestion_id'] = $("#id-register-inp-securityquestion").val();
                data_package['securityquestion_ans'] = $("#id-register-inp-securityanswer").val();
                data_package['recaptcha'] = $("#g-recaptcha-response").val();
                data_package['phone'] = $("#id-register-inp-phone").intlTelInput("getNumber");
                data_package['country_name'] = $("#id-register-inp-phone").intlTelInput("getSelectedCountryData").name;
                data_package['country_code'] = $("#id-register-inp-phone").intlTelInput("getSelectedCountryData").iso2;
                data_package['uses_sr'] = 0; //($("#id-register-inp-sr").is(":checked") ? 1 : 0); 


                showErrorSpan($("#id-register-inp-firstname"), false);
                showErrorSpan($("#id-register-inp-organization"), false);
                showErrorSpan($("#id-register-inp-job-title"), false);
                showErrorSpan($("#id-register-inp-password"), false);
                showErrorSpan($("#id-register-inp-email"), false);
                showErrorSpan($("#id-register-inp-securityanswer"), false);
                showErrorSpan($("#id-register-inp-phone").parent(), false);
                showErrorSpan($("#id-register-inp-recaptcha"), false);
                
                // $("#id-form-register-2").hide();
                $("#id-div-register-progress").show();
                
                $("#id-div-register-progress-icon").addClass("fa-2x");
                $("#id-div-register-progress-icon").addClass("fa-spin");
                $("#id-div-register-progress-icon").addClass("fa-spinner");
                $("#id-div-register-progress-msg").html("We are working on modifying your profile data. Please wait!");
                
                $.post("API.php", { register: "EDIT_PROFILE_SAVE", data: JSON.stringify(data_package) }, function (data) {
                    $("#id-div-register-progress-icon").removeClass("fa-spinner");
                    $("#id-div-register-progress-icon").removeClass("fa-spin");
                    if(data.response){
                        $("#id-div-register-progress-icon").addClass("fa-2x");
                        $("#id-div-register-progress-icon").addClass("fa-check");
                        $("#id-div-register-progress-msg").html("Profile Data has been successfully modified.");

                        // $("#id-div-register-progress").hide();
                    } else {
                        $("#id-div-register-progress-icon").addClass("fa-2x");
                        $("#id-div-register-progress-icon").addClass("fa-close");
                        $("#id-div-register-progress-msg").html("Something went wrong! We couldn't modify your profile data. Error : "+data.message);
                    }
                },"json");
            } else {
                showErrorSpan($("#id-register-inp-firstname"), (!validateFirstName() || !validateLastName()));
                showErrorSpan($("#id-register-inp-organization"), !validateOrganization());
                showErrorSpan($("#id-register-inp-email"), !validateEmail());
                showErrorSpan($("#id-register-inp-phone").parent(), !validatePhoneNumber());

                if($("#id-register-inp-password").val() !== "")
                    showErrorSpan($("#id-register-inp-password"), (!validatePassword() || !validateConfirmPassword()));
                if($("#id-register-inp-securityanswer").val() !== "")
                    showErrorSpan($("#id-register-inp-securityanswer"), (!validateSecurityQuestion() || !validateSecurityAnswer()));
            }
        } 
    });
    
    $("#id-btn-new-pass-submit").click(function(){
        var passwordElem = $("#id-new-pass-inp-password");
        var confirmpasswordElem = $("#id-new-pass-inp-confirmpassword");
        
        var password = $(passwordElem).val();
        var confirmpassword = $(confirmpasswordElem).val();
        
        var eventDataPassword = {target : $(passwordElem)};
        var eventDataConfirmPassword = {target : $(confirmpasswordElem)};
        showErrorSpan($(passwordElem),!validatePassword(eventDataPassword));
        showErrorSpan($(confirmpasswordElem),!validateConfirmPassword(eventDataConfirmPassword,eventDataPassword));
        
        if(validatePassword(eventDataPassword) && validateConfirmPassword(eventDataConfirmPassword,eventDataPassword)){
            showErrorSpan($(passwordElem),!validatePassword(eventDataPassword));
        showErrorSpan($(confirmpasswordElem),!validateConfirmPassword(eventDataConfirmPassword,eventDataPassword));            
            $("#id-div-new-password-content").hide();
            $("#id-div-new-password-progress").show();
            
            $("#id-div-new-password-progress-icon").addClass("fa-2x");
            $("#id-div-new-password-progress-icon").addClass("fa-spin");
            $("#id-div-new-password-progress-icon").addClass("fa-spinner");
            $("#id-div-new-password-progress-msg").html("We are working on reseting your password. Please wait!");
            
            
            $.post("API.php", { register: "RESET_PASSWORD_CONF", password: password, confirmpassword: confirmpassword, reset_link: params.rl }, function (data) {
                $("#id-div-new-password-progress-icon").removeClass("fa-spinner");
                $("#id-div-new-password-progress-icon").removeClass("fa-spin");
                if(data.response){
                    $("#id-div-new-password-progress-icon").addClass("fa-2x");
                    $("#id-div-new-password-progress-icon").addClass("fa-check");
                    $("#id-div-new-password-progress-msg").html("Password successfully reset. Please login to access your account.");
                    $("#login-btn").show();
                    $("#id-div-new-pass-submit").hide();                        
                } else {
                    $("#id-btn-new-pass-submit").hide();
                    $("#id-div-new-password-progress-icon").addClass("fa-2x");
                    $("#id-div-new-password-progress-icon").addClass("fa-close");
                    $("#id-div-new-password-progress-msg").html("Something went wrong! We couldn't complete the reset password request. Error : "+data.message);
                    
                    if(data.message === "NO SUCH LINK"){
                        $("#id-div-new-password-progress-msg").html("This reset link seems invalid. It might have be incorrectly copied or corrupted.");
                    }
                }
                //alert(data.response);
            },"json");
        }
    });
    
    var eventData = {focus : true};
    $("#id-register-inp-organization").focusout(eventData, validateOrganization);
    $("#id-register-inp-firstname").focusout(eventData, validateFirstName);
    $("#id-register-inp-middlename").focusout(eventData, validateMiddleName);
    $("#id-register-inp-lastname").focusout(eventData, validateLastName);
    $("#id-register-inp-email").focusout(eventData, validateEmail);
    $("#id-register-inp-password").focusout(eventData, validatePassword);
    $("#id-register-inp-confirmpassword").focusout(eventData, validateConfirmPassword);
    $("#id-register-inp-securityquestion").on('changed.bs.select', validateSecurityQuestion);
    $("#id-register-inp-securityanswer").focusout(eventData, validateSecurityAnswer);
    $("#id-register-inp-phone").focusout(eventData, validatePhoneNumber);
    
    var eventDataLoginUsername = {target : $("#id-login-inp-email"), focus : true};
    var eventDataLoginPassword = {target : $("#id-login-inp-password"), focus : true};
    $("#id-login-inp-email").focusout(eventDataLoginUsername, validateEmail);
    $("#id-login-inp-password").focusout(eventDataLoginPassword, validate_login_password);
    
    var eventDataLoginShortUsername = {target : $("#id-login-short-inp-email"), focus : true};
    var eventDataLoginShortPassword = {target : $("#id-login-short-inp-password"), focus : true};
    $("#id-login-short-inp-email").focusout(eventDataLoginShortUsername, validateEmail);
    $("#id-login-short-inp-password").focusout(eventDataLoginShortPassword, validate_login_password);
    // console.log('before api');
    $.post("API.php", { register: "QUESTIONS" }, function (data) {
        // console.log('inside api');
        var container = $("#id-register-inp-securityquestion");
        var generated_option = '';
        $(container).html('');
        
        $.each(data, function(index,elem){
            generated_option += '<option value="'+elem.sl_no+'">'+elem.question+'</option>';
        });
        container.html(generated_option);
        $(container).selectpicker('refresh');
    }, 'json');

    $.post("API.php", { register: "PROJECT_SIGNUP_ROLES" }, function (data) {
        // console.log('inside api');
        var container = $("#id-register-inp-signuprole");
        var generated_option = '';
        $(container).html('');
        // alert(JSON.stringify(data));
        $.each(data, function(index,elem){
            // alert(elem.name);
            generated_option += '<option value="'+elem.role_id+'">'+elem.name+'</option>';
        });
        container.html(generated_option);
        $(container).selectpicker('refresh');
    }, 'json');

    $.post("API.php", { register: "PROJECT_SIGNUP_REASON" }, function (data) {
        // console.log('inside api');
        var container = $("#id-register-inp-signupreason");
        var generated_option = '';
        $(container).html('');
        // alert(JSON.stringify(data));
        $.each(data, function(index,elem){
            // alert(elem.name);
            generated_option += '<option value="'+elem.reason_id+'">'+elem.name+'</option>';
        });
        container.html(generated_option);
        $(container).selectpicker('refresh');
    }, 'json');

});

/*
    Validate Register form fields  
*/

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 validateOrganization(data){
    var elem = $("#id-register-inp-organization");
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^([a-zA-Z0-9,.\-'\s\(\)]{1,})$/;
    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 validateJobTitle(data){
    var elem = $("#id-register-inp-job-title");
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^([a-zA-Z0-9,.\-'\s\(\)]{1,})$/;
    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 validateFirstName(data){
    var elem = $("#id-register-inp-firstname");
    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-zA-Z,.\-'\s\(\)]{1,})$/;
    var checkEmpty = !(data && data.focus);
    
    if(((checkEmpty && val && val!='') || !checkEmpty) && filter.test(val)){
        // console.log('success');
        $(parent).addClass("has-success");
        $(parent).removeClass("has-danger"); 
        $(elem).removeClass("form-control-danger");       
        $(elem).addClass("form-control-success");      
        return true;
    } else {
        console.log('fail');
        $(parent).addClass("has-danger");
        $(parent).removeClass("has-success");        
        $(elem).addClass("form-control-danger");       
        $(elem).removeClass("form-control-success");     
        return false;
    }
}

function validateMiddleName(data){
    var elem = $("#id-register-inp-middlename");
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^([a-zA-Z,.\-'\s\(\)]{0,})$/;
    if(val.length == 0 || 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 validateLastName(data){
    var elem = $("#id-register-inp-lastname");
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^([a-zA-Z,.\-'\s\(\)]{1,})$/;
    var checkEmpty = !(data && data.focus);
    if(((checkEmpty && val && val!='') || !checkEmpty) && 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 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 validatePassword(data){
    var elem = $("#id-register-inp-password");
    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])(?=.*[\d])[A-Za-z\d$@$!%*?&]{6,}/;
    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 validate_login_password(data){
    var elem = $("#id-register-inp-password");
    if(data){
        elem = data.target;
    }
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var checkEmpty = true || !(data && data.focus);
    if(((checkEmpty && val && val!='') || !checkEmpty)){
        $(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 validateConfirmPassword(data1, data2){
    var elem = $("#id-register-inp-confirmpassword");
    var elem2 = $("#id-register-inp-password");
    if(data1){
        elem = data1.target;
        if(data2){
            elem2 = data2.target;
        }
    }
    
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var val2 = $(elem2).val();
    var filter = /^(?=.*[a-z])(?=.*[\d])[A-Za-z\d$@$!%*?&]{6,}/;
    if(val && val!='' && filter.test(val) && val === val2){
        $(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 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 validateSecurityQuestion(data){
    var elem = $("#id-register-inp-securityquestion");
    var parent = $(elem).parents(".bootstrap-select").first();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var filter = /^(?=.*[0-9])[0-9]{0,}/;
    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 validatePhoneNumber(data){
    var elem = $("#id-register-inp-phone");
    var parent = $(elem).parent();
    var span = $(parent).parents(".form-group").find(".cl-lbl");
    var val = $(elem).val();
    var isValid = $(elem).intlTelInput("isValidNumber");
    if(isValid){
        $(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 validateRecaptcha(data){
    var elem = $("#g-recaptcha-response");
    var span = $("#id-register-inp-recaptcha").parent().find(".cl-lbl");
    var val = $(elem).val();
    if(val && val!=''){
        $(span).addClass("has-success");
        $(span).removeClass("has-danger");                   
        return true;
    } else {
        $(span).addClass("has-danger");
        $(span).removeClass("has-success");             
        return false;
    }
}

function postUserSharpSpring(data_package){
    
    // let PATH_URL = 'https://app-3QNLD3PVS8.marketingautomation.services/webforms/receivePostback/MzawMDG3tDQyAgA/';
    // let PATH_ENDPOINT = 'b48d6fb1-5146-4249-ba0e-2eba7498d7de/';

    let PATH_URL = 'https://app-3QNLD3PVS8.marketingautomation.services/webforms/receivePostback/MzawMDG3tDQyAgA/';
    let PATH_ENDPOINT = '35422d2e-b20b-46db-aa95-09916301e7fd/';
    let PATH_ENDPOINT_TYPE = 'jsonp/?'

    var roles = ["Project Architect/Design Team", "Consultant", "Real Estate Development", "Ownership/Management", "Human Resources", "Community Advocate", "Other"];
    var reasons = ["Just browsing / Interested in Universal Design",
    "Currently working on a Universal Design project",
    "Anticipating starting a Universal Design project in the next few months",
    "Anticipating starting a Universal Design project in the next few years",
    "Interested in how an existing building or past project would score"];

    let param_email = data_package['email_id'];
    let param_firstname = data_package['first_name'];
    let param_lastname = data_package['last_name'];
    let param_organization = data_package['organization_id'];
    let param_job_title = data_package['job_title'];
    let param_phone = data_package['phone'];
    let param_signup_reason = reasons[Number(data_package['signupreason_id'])-1];
    let param_signup_role = roles[Number(data_package['signuprole_id'])-1];

    let PATH_PARAMETERS = 'firstName=' + param_firstname + '&lastName=' + param_lastname + '&email=' + param_email + '&company=' + param_organization + '&phone=' + param_phone + '&jobtitle=' + param_job_title + '&signuprole=' + param_signup_role + '&signupreason=' + param_signup_reason;

    let PATH_FULL_PATH = PATH_URL + PATH_ENDPOINT + PATH_ENDPOINT_TYPE + PATH_PARAMETERS;
    // alert(PATH_PARAMETERS);
    $.post(PATH_FULL_PATH);
};