/*
	jQuery Form Validators v0.3
	License: http://validator.codeplex.com/license
*/
function validate(B) {
    var A = true; 
    $("*[validate=" + B + "]").each(function(C, D) {
        if (check(D)) {
            $(D).highlight();
            if (A) { $(D).focus() }
            A = false
        }
        else {
            $(D).unhighlight()
        }
    });
    return A
}

function revalidate() {
    if (!check(this)) {
        $(this).unhighlight()
    }
    else {
        $(this).highlight()
    }
}

function check(C) {
    var A = $(C);
    var B = A.find("input:radio, input:checkbox").size();
    if (A.attr("disabled") || B > 0 && B == A.find("input:radio:disabled, input:checkbox:disabled").size())
        { return "" }

    if (A.val() == "" && A.find("input:radio:checked, input:checkbox:checked").size() == 0)
    { return A.attr("required") ? "required" : "" }

    if (A.attr("regular") && A.attr("validExpress") && !new RegExp(A.attr("validExpress"), "m").test(A.val()))
    { return "regular" }

    if (A.attr("regular") && A.attr("invalidExpress") && new RegExp(A.attr("invalidExpress"), "m").test(A.val()))
    { return "regular" }

    if (A.attr("compare") && $("#" + A.attr("compareTo")).val() != A.val())
    { return "compare" }

    if (A.attr("custom") && !new Function(A.attr("customFn")).call(C))
    { return "custom" }

    if (A.attr("invalid") && A.val() == A.attr("invalidVal"))
    { return "invalid" } 
}

function showAlert() {
    var A = $(this);
    var C = A.position().top + A.height() + 4;
    var B = A.position().left + Math.max(A.width() - 260, 0);
    
    A.parent().children(".alertbox").remove();
    A.parent().append("<div class='alertbox' style='top:" + C + "px; left:" + B + "px;' style='z-index:1500;white-space:nowrap;'><div>" + A.attr(check(this)) + "</div></div>")
}

function hideAlert() {
    $(this).parent().children(".alertbox").remove()
}

(function(A) {
    A.fn.highlight = function() { this.addClass("highlight").focus(showAlert).blur(hideAlert).change(revalidate) }
})

(jQuery);
(function(A) {
    A.fn.unhighlight = function() { this.removeClass("highlight").unbind("focus", showAlert).unbind("blur", hideAlert).parent().children(".alertbox").remove() }
})
(jQuery);