﻿function ShowCustomErrors(names) {
    $.each(names.split("^"), function() {
        var item = $("[@name*=" + this + "]")
        if (item.attr("type") != "checkbox")
            $("[@name*=" + this + "]").addClass("inp-error")
    })
}

function ShowErrors(name, errors, fn) {
    var item = $("[@name*=" + name + "]")
    if (item.size() > 0) {
        item = item.eq(item.size() - 1)
    }
    //bad style solution, must be more universal and clean
    if (item.attr("type") == "checkbox") {
        if (item.parent().parent().is("dd")) {
            item.parent().parent().after("<dt></dt>")
        }
        if (item.parent().parent().is("li")) {
            item.parent().parent().after("<li style='padding:0px;'></li>")
        }
        var children = item.parent().parent().parent().children()
        if (children.size() > 0) {
            item = children.eq(children.size() - 1)
        }
        $.each(errors.split("^"), function() {
            fn(item).append("<div class=\"error-message\">" + this + "</div>")
        })
    }
    else {
        $("[@name*=" + name + "]").addClass("inp-error")
        $.each(errors.split("^"), function() {
            fn(item).after("<div class=\"error-message\">" + this + "</div>")
        })
    }
}

function ShowCommonErrors(name, errors) {
    ShowErrors(name, errors, function(item) { return $(item); });
}

function ShowCommonErrorsPhotoEditor(name, errors) {
    ShowErrors(name, errors, function(item) { return $(item).next("button"); });
}

function ShowCommonErrorsBecomeResident(name, errors) {
    ShowErrors(name, errors, function(item) {
        return $(item).next("div").length == 1
            ? $(item).next("div") : $(item);
    });
}

function DeletePreviousErrors() {
    $("div.error-message").remove()
    $(".inp-error").removeClass("inp-error")
}

function loadButtonsStyles() {
    $('button.button-style').mouseup(function() {
        $(this).children().removeClass('button-click')
    }).mousedown(function() {
        $(this).children().addClass('button-click')
    }).unbind("mouseenter mouseleave")
    .bind("mouseenter mouseleave", function(e) {
        $(this).children().toggleClass("button-hover")
    })
}

function ChangeFocusUsPhoneNumberWidget(text1Id, text2Id) {
    var text1 = document.getElementById(text1Id);
    var text2 = document.getElementById(text2Id);
    var count = text1.value.length;

    change = (count == text1.size) ? true : false;

    if (change) {
        text2.value = "";
        text2.focus();
    }
    else {
        text1.focus();
    }
}

function AddClassForFirstDD(name) {
    $("dd:has([@name*=" + name + "]):eq(0)").addClass("txt-check");
}

function AddAttrForFirstLI(name) {
    $("li:has([@name*=" + name + "]):eq(0)").attr("style", "width:55px;");
}

function scrollToErrors() {
    var errors = $("a[@name=customErrors]");
    if (errors.length > 0) {
        $.scrollTo(errors, 500);
    }
}