﻿$(document).ready(function() {
    // Validator
    $(".validate_url").keyup(function() {
        var urlMsg = "<span>Skriv in en fungerande&nbsp;url.</span>";
        var positionId = $(this).attr("id");
        $("#" + positionId + "_validate").remove();

        if ($(this).val() != "") { // If there is anything in the url field to validate
            if (!validate_url($(this).val())) // If the regex says it's not a valid url
                $("#" + positionId).before("<div id='" + positionId + "_validate' class='error small validate_response_small' style='margin-left: " + ($("#" + positionId).width() + 20) + "px'>" + urlMsg + "</div>");
            else // If the url is valid
                $("#" + positionId).before("<div id='" + positionId + "_validate' class='success small validate_response_small' style='margin-left: " + ($("#" + positionId).width() + 20) + "px'>&nbsp;</div>").removeClass("error");
        }
    });

    $(".validate_email").keyup(function() {
        var urlMsg = "<span>Skriv in en fungerande&nbsp;e-postadress.</span>";
        //var filter = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,6})+$/);
        //var value = $(this).val();

        $("#" + $(this).attr("id") + "_validate").remove();

        if ($(this).val() != "") { // If there is anything in the email field to validate
            if (!validate_email($(this).val()))
                $(this).before("<div id='" + $(this).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + urlMsg + "</div>");
            else
                $(this).before("<div id='" + $(this).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>&nbsp;</div>").removeClass("error");
        }
    });

    $(".validate_phone").keyup(function() {

        var urlMsg = "Skriv in ett fungerande&nbsp;telefonnummer.";
        var filter = new RegExp(/[0-9+-]{6,15}/);
        var value = $(this).val();

        $("#" + $(this).attr("id") + "_validate").remove();

        if (!filter.test(value) && $(this).val() != "") { $(this).before("<div id='" + $(this).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + urlMsg + "</div>"); }
        if (filter.test(value)) { $(this).before("<div id='" + $(this).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>&nbsp;</div>"); }
    });

    $(".validate_fullname").keyup(function() {

        var fullnameMsg = "Skriv in för- och&nbsp;efternamn.";

        $("#" + $(this).attr("id") + "_validate").remove();
        if (validate_fullname($(this).attr("id"))) {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>&nbsp;</div>").removeClass("error");
        } else {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + fullnameMsg + "</div>");
        }
    });


    $(".validate_passwordstrength").keyup(function() {
        var weakMsg = "Lösenordet måste vara minst 5 tecken långt.";
        var mediumMsg = "Mediumstarkt lösenord.";
        var strongMsg = "Starkt lösenord.";

        $("#" + $(this).attr("id") + "_validate").remove();

        var strength = validate_passwordstrength($(this).attr("id"));

        if (strength == "weak") {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + weakMsg + "</div>");
        }
        if (strength == "medium") {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='notice small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + mediumMsg + "</div>").removeClass("error");
        }
        if (strength == "strong") {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + strongMsg + "</div>").removeClass("error");
        }
    });

    $(".required").keyup(function() {
        $("#" + $(this).attr("id") + "_validate_required").remove();
        $("#" + $(this).attr("id") + "_validate").remove();
        if ($(this).val() == "") {
            var requiredMsg = "<span>Detta är ett obligatoriskt&nbsp;fält.</span>";
            $(this).before("<div id='" + $(this).attr("id") + "_validate_required' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + requiredMsg + "</div>");
        } else {
            $(this).removeClass("error");
        }
    });
    
    $(".validate_repeatpassword").keyup(function() {
        var repeatpwd = $("#" + $(this).attr("repeatid")).val();
        var pwdMsg = "Lösenordet måste matcha.";

        $("#" + $(this).attr("id") + "_validate").remove();
        $("#" + $(this).attr("id") + "_validate_required").remove();

        if (repeatpwd != $(this).val()) {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>" + pwdMsg + "</div>");
        } else {
            $(this).before("<div id='" + $(this).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($(this).width() + 25) + "px'>&nbsp;</div>").removeClass("error");
        }
    });

    $(".validate_minlength").keyup(function() {
        //var success = validate_minlength($(this).attr("id"), $(this).attr("minlength"));
        var positionId = $(this).attr("id");
        var minlengthMsg = "Du måste skriva minst " + $(this).attr("minlength") + "&nbsp;tecken.";
        $("#" + positionId + "_validate").remove();
        
        if (validate_minlength($(this).attr("id"), $(this).attr("minlength")))
            $("#" + positionId).before("<div id='" + $("#" + positionId).attr("id") + "_validate' class='success small validate_response_small' style='margin-left: " + ($("#" + positionId).width() + 20) + "px'>&nbsp;</div>").removeClass("error");
        else
            $("#" + positionId).before("<div id='" + $("#" + positionId).attr("id") + "_validate' class='error small validate_response_small' style='margin-left: " + ($("#" + positionId).width() + 20) + "px'>" + minlengthMsg + "</div>");

        //if ($(this).data("validate_minlength_callback")) { <--- What's this???
        //    $(this).data("validate_minlength_callback")(success);
        //}
    });

});


function validate_minlength(positionId, minLength) {
    if ($("#" + positionId).val().length < minLength) {
        return false;
    } else {
        return true;
    }
}

function validate_url(url) {

    var filter = new RegExp(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/);
    if (url == "")
        return true;
    else
        return (filter.test(url));
}


function validate_email(email) {

    var filter = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,6})+$/);
    if (email == "")
        return true;
    else
        return (filter.test(email));
}



function validate_fullname(positionId) {
    var filter = new RegExp(/^([a-zA-Z\.-])+\s([a-zA-Z\.-]{2,90})/);
    var value = $("#" + positionId).val();
    return filter.test(value);
}

function validate_passwordstrength(positionId) {
    var value = $("#" + positionId).val();
    var weak = new RegExp(/^.{1,4}$/);
    var medium = new RegExp(/^.{5,7}$/);
    var strong = new RegExp(/^.{8,}/);
    if (weak.test(value)) {
        return "weak";
    }
    if (medium.test(value)) {
        return "medium";
    }
    if (strong.test(value)) {
        return "strong";
    }    
}